1
0
Fork 0
gobelt/dbc/rows_test.go

81 lines
2.0 KiB
Go
Raw Normal View History

2018-07-07 12:11:23 +00:00
package dbc
2018-06-23 21:46:35 +00:00
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/localhots/gobelt/context2"
2018-06-23 21:46:35 +00:00
)
func TestLoadSingleValue(t *testing.T) {
requireConn(t)
ctx := context2.TestContext(t)
2018-06-23 21:46:35 +00:00
exp := int(1)
var out int
2018-07-07 12:11:23 +00:00
mustQuery(t, conn.Query(ctx, "SELECT 1").Load(&out))
2018-06-23 21:46:35 +00:00
if exp != out {
t.Errorf("Value doesn't match: expected %d, got %d", exp, out)
}
}
func TestLoadSlice(t *testing.T) {
requireConn(t)
ctx := context2.TestContext(t)
2018-06-23 21:46:35 +00:00
exp := []int{1, 2}
var out []int
2018-07-07 12:11:23 +00:00
mustQuery(t, conn.Query(ctx, "SELECT id FROM sqldb_test").Load(&out))
2018-06-23 21:46:35 +00:00
if !cmp.Equal(exp, out) {
t.Errorf("Values dont't match: %s", cmp.Diff(exp, out))
}
}
func TestLoadMap(t *testing.T) {
requireConn(t)
ctx := context2.TestContext(t)
2018-06-23 21:46:35 +00:00
exp := map[string]interface{}{"id": int64(1), "name": "Alice"}
var out map[string]interface{}
2018-07-07 12:11:23 +00:00
mustQuery(t, conn.Query(ctx, "SELECT * FROM sqldb_test WHERE id = 1").Load(&out))
2018-06-23 21:46:35 +00:00
if !cmp.Equal(exp, out) {
t.Errorf("Record doesn't match: %s", cmp.Diff(exp, out))
}
}
func TestLoadSliceOfMaps(t *testing.T) {
requireConn(t)
ctx := context2.TestContext(t)
2018-06-23 21:46:35 +00:00
exp := []map[string]interface{}{
{"id": int64(1), "name": "Alice"},
{"id": int64(2), "name": "Bob"},
}
var out []map[string]interface{}
2018-07-07 12:11:23 +00:00
mustQuery(t, conn.Query(ctx, "SELECT * FROM sqldb_test ORDER BY id ASC").Load(&out))
2018-06-23 21:46:35 +00:00
if !cmp.Equal(exp, out) {
t.Errorf("Records don't match: %s", cmp.Diff(exp, out))
}
}
func TestLoadStruct(t *testing.T) {
requireConn(t)
ctx := context2.TestContext(t)
2018-06-23 21:46:35 +00:00
exp := record{ID: 1, Name: "Alice"}
var out record
2018-07-07 12:11:23 +00:00
mustQuery(t, conn.Query(ctx, "SELECT * FROM sqldb_test WHERE id = 1").Load(&out))
2018-06-23 21:46:35 +00:00
if !cmp.Equal(exp, out) {
t.Errorf("Record doesn't match: %s", cmp.Diff(exp, out))
}
}
func TestLoadSliceOfStructs(t *testing.T) {
requireConn(t)
ctx := context2.TestContext(t)
2018-06-23 21:46:35 +00:00
exp := []record{
{ID: 1, Name: "Alice"},
{ID: 2, Name: "Bob"},
}
var out []record
2018-07-07 12:11:23 +00:00
mustQuery(t, conn.Query(ctx, "SELECT * FROM sqldb_test").Load(&out))
2018-06-23 21:46:35 +00:00
if !cmp.Equal(exp, out) {
t.Errorf("Records don't match: %s", cmp.Diff(exp, out))
}
}