-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_test.go
68 lines (57 loc) · 1.4 KB
/
query_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package queries
import (
"reflect"
"testing"
"go-simpler.org/queries/internal/assert"
. "go-simpler.org/queries/internal/assert/EF"
)
func Test_scan(t *testing.T) {
t.Run("non-struct T", func(t *testing.T) {
fn := func() { _, _ = scan[int](new(mockRows)) }
assert.Panics[E](t, fn, "queries: T must be a struct")
})
t.Run("empty tag", func(t *testing.T) {
type row struct {
Foo int `sql:""`
}
fn := func() { _, _ = scan[row](new(mockRows)) }
assert.Panics[E](t, fn, "queries: field Foo has an empty `sql` tag")
})
t.Run("missing field", func(t *testing.T) {
rows := mockRows{
columns: []string{"foo", "bar"},
}
type row struct {
Foo int `sql:"foo"`
Bar string
}
fn := func() { _, _ = scan[row](&rows) }
assert.Panics[E](t, fn, `queries: no field for column "bar"`)
})
t.Run("ok", func(t *testing.T) {
rows := mockRows{
columns: []string{"foo", "bar"},
values: []any{1, "A"},
}
type row struct {
Foo int `sql:"foo"`
Bar string `sql:"bar"`
}
r, err := scan[row](&rows)
assert.NoErr[F](t, err)
assert.Equal[E](t, r.Foo, 1)
assert.Equal[E](t, r.Bar, "A")
})
}
type mockRows struct {
columns []string
values []any
}
func (r *mockRows) Columns() ([]string, error) { return r.columns, nil }
func (r *mockRows) Scan(dst ...any) error {
for i := range dst {
v := reflect.ValueOf(r.values[i])
reflect.ValueOf(dst[i]).Elem().Set(v)
}
return nil
}