-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_builder_struct_scan_test.go
More file actions
72 lines (64 loc) · 2 KB
/
query_builder_struct_scan_test.go
File metadata and controls
72 lines (64 loc) · 2 KB
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
69
70
71
72
package norm
import (
"context"
"errors"
"testing"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type rowsStruct struct {
rows [][]any
fields []string
i int
}
func (r *rowsStruct) Values() ([]any, error) {
if r.i >= len(r.rows) {
return nil, errors.New("eof")
}
v := r.rows[r.i]
r.i++
return v, nil
}
func (r *rowsStruct) FieldDescriptions() []pgconn.FieldDescription {
out := make([]pgconn.FieldDescription, len(r.fields))
for i, n := range r.fields {
out[i] = pgconn.FieldDescription{Name: n}
}
return out
}
func (r *rowsStruct) Next() bool { return r.i < len(r.rows) }
func (r *rowsStruct) Err() error { return nil }
func (r *rowsStruct) Close() {}
func (r *rowsStruct) CommandTag() pgconn.CommandTag { return pgconn.CommandTag{} }
func (r *rowsStruct) RawValues() [][]byte { return nil }
func (r *rowsStruct) Conn() *pgx.Conn { return nil }
func (r *rowsStruct) Scan(dest ...any) error { return nil }
type execStruct struct {
rows [][]any
fields []string
}
func (e *execStruct) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error) {
return pgconn.CommandTag{}, nil
}
func (e *execStruct) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) {
return &rowsStruct{rows: e.rows, fields: e.fields}, nil
}
func (e *execStruct) QueryRow(ctx context.Context, sql string, args ...any) pgx.Row {
return &rowsStruct{rows: e.rows, fields: e.fields}
}
type sUser struct {
ID int64 `db:"id"`
Name string `db:"name"`
}
func TestFind_StructScan(t *testing.T) {
kn := &KintsNorm{}
ex := &execStruct{rows: [][]any{{int64(1), "a"}, {int64(2), "b"}}, fields: []string{"id", "name"}}
qb := (&QueryBuilder{kn: kn, exec: ex}).Table("users").Select("id", "name")
var out []sUser
if err := qb.Find(context.Background(), &out); err != nil {
t.Fatalf("find: %v", err)
}
if len(out) != 2 || out[0].Name != "a" || out[1].ID != 2 {
t.Fatalf("out=%v", out)
}
}