56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
|
package sql
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"reflect"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestDecoder(t *testing.T) {
|
||
|
db, err := sql.Open("sqlite3", ":memory:")
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
defer db.Close()
|
||
|
|
||
|
if _, err := db.Exec(`create table foo ( id integer primary key, bar blob );`); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
if _, err := db.Exec(`insert into foo ( id, bar ) values (1, ?);`, []byte(`["a", "b"]`)); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
var got []string
|
||
|
if err := db.QueryRow(`select bar from foo where id = 1;`).Scan(decoder(&got)); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
want := []string{"a", "b"}
|
||
|
if !reflect.DeepEqual(got, want) {
|
||
|
t.Errorf("wanted %q got %q", want, got)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func TestEncoder(t *testing.T) {
|
||
|
db, err := sql.Open("sqlite3", ":memory:")
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
defer db.Close()
|
||
|
|
||
|
if _, err := db.Exec(`create table foo ( id integer primary key, bar blob );`); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
put := []string{"a", "b"}
|
||
|
if _, err := db.Exec(`insert into foo ( id, bar ) values (1, ?)`, encoder(put)); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
|
||
|
var got []byte
|
||
|
if err := db.QueryRow(`select bar from foo where id = 1;`).Scan(&got); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
want := []byte(`["a","b"]`)
|
||
|
if !reflect.DeepEqual(got, want) {
|
||
|
t.Errorf("wanted %q got %q", want, got)
|
||
|
}
|
||
|
}
|