This repository has been archived on 2022-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
dex/storage/sql/crud_test.go
A Gardner 19d7edd530 Don't try to build sqlite when cgo isn't enabled
Signed-off-by: A Gardner <3100188+actgardner@users.noreply.github.com>
2020-11-17 17:48:40 -05:00

57 lines
1.3 KiB
Go

// +build cgo
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)
}
}