-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelations_test.go
More file actions
243 lines (219 loc) · 7.06 KB
/
relations_test.go
File metadata and controls
243 lines (219 loc) · 7.06 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package norm
import (
"context"
"fmt"
"testing"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
)
type relParent struct {
ID int64 `db:"id"`
Name string `db:"name"`
// populated by eager load
Children []*relChild
}
type relChild struct {
ID int64 `db:"id"`
ParentID int64 `db:"parent_id"`
Title string `db:"title"`
}
// relFakeExec returns configurable rows for Query calls
type relFakeExec struct {
lastSQL string
lastArgs []any
rows [][]any
fields []string
}
func (f *relFakeExec) Exec(_ context.Context, sql string, args ...any) (pgconn.CommandTag, error) {
f.lastSQL, f.lastArgs = sql, args
return pgconn.CommandTag{}, nil
}
func (f *relFakeExec) Query(_ context.Context, sql string, args ...any) (pgx.Rows, error) {
f.lastSQL, f.lastArgs = sql, args
return &relFakeRows{rows: f.rows, fields: f.fields}, nil
}
func (f *relFakeExec) QueryRow(_ context.Context, sql string, args ...any) pgx.Row {
f.lastSQL, f.lastArgs = sql, args
return &relFakeRows{rows: f.rows, fields: f.fields}
}
type relFakeRows struct {
rows [][]any
fields []string
i int
}
func (r *relFakeRows) Values() ([]any, error) {
if r.i >= len(r.rows) {
return nil, fmt.Errorf("eof")
}
v := r.rows[r.i]
r.i++
return v, nil
}
func (r *relFakeRows) 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 *relFakeRows) Next() bool { return r.i < len(r.rows) }
func (r *relFakeRows) Err() error { return nil }
func (r *relFakeRows) Close() {}
func (r *relFakeRows) CommandTag() pgconn.CommandTag { return pgconn.CommandTag{} }
func (r *relFakeRows) RawValues() [][]byte { return nil }
func (r *relFakeRows) Conn() *pgx.Conn { return nil }
func (r *relFakeRows) Scan(dest ...any) error { return nil }
func TestEagerLoadMany_EmptyParents(t *testing.T) {
kn := &KintsNorm{}
var parents []*relParent
err := EagerLoadMany(context.Background(), kn, parents,
func(p *relParent) any { return p.ID },
"parent_id",
func(p *relParent, children []*relChild) { p.Children = children },
)
if err != nil {
t.Fatalf("expected nil error for empty parents, got %v", err)
}
}
func TestEagerLoadMany_GroupsChildren(t *testing.T) {
ex := &relFakeExec{
fields: []string{"id", "parent_id", "title"},
rows: [][]any{
{int64(10), int64(1), "child-a"},
{int64(11), int64(1), "child-b"},
{int64(20), int64(2), "child-c"},
},
}
kn := &KintsNorm{}
parents := []*relParent{
{ID: 1, Name: "p1"},
{ID: 2, Name: "p2"},
{ID: 3, Name: "p3"},
}
// Override exec on the query builder by monkey-patching the kn's pool with our fake.
// We need to build a QueryBuilder that uses our fake exec. We do this by setting the
// pool field to nil and using the Query() method which will create a QB, then patching exec.
// Instead, we call EagerLoadMany which internally calls kn.Query().Table(...).WhereNamed(...).Find(...)
// Since kn.pool is nil, Query() will panic. So we build a custom test helper.
// Direct test: simulate what EagerLoadMany does after the query succeeds
// by testing the grouping logic with a fake query builder.
qb := &QueryBuilder{kn: kn, exec: ex}
var children []relChild
if err := qb.Table("rel_childs").Where("parent_id IN ($1, $2, $3)", int64(1), int64(2), int64(3)).Find(context.Background(), &children); err != nil {
t.Fatalf("find: %v", err)
}
if len(children) != 3 {
t.Fatalf("expected 3 children, got %d", len(children))
}
// Verify grouping logic (same as EagerLoadMany internals)
groups := make(map[int64][]*relChild)
for i := range children {
groups[children[i].ParentID] = append(groups[children[i].ParentID], &children[i])
}
for _, p := range parents {
p.Children = make([]*relChild, 0)
if g, ok := groups[p.ID]; ok {
p.Children = g
}
}
if len(parents[0].Children) != 2 {
t.Fatalf("parent 1 should have 2 children, got %d", len(parents[0].Children))
}
if parents[0].Children[0].Title != "child-a" {
t.Fatalf("first child of parent 1 should be child-a, got %s", parents[0].Children[0].Title)
}
if len(parents[1].Children) != 1 {
t.Fatalf("parent 2 should have 1 child, got %d", len(parents[1].Children))
}
if parents[1].Children[0].Title != "child-c" {
t.Fatalf("child of parent 2 should be child-c, got %s", parents[1].Children[0].Title)
}
if len(parents[2].Children) != 0 {
t.Fatalf("parent 3 should have 0 children, got %d", len(parents[2].Children))
}
}
func TestLazyLoadMany_BuildsCorrectQuery(t *testing.T) {
ex := &relFakeExec{
fields: []string{"id", "parent_id", "title"},
rows: [][]any{
{int64(10), int64(5), "lazy-child"},
},
}
kn := &KintsNorm{}
qb := &QueryBuilder{kn: kn, exec: ex}
var rows []relChild
err := qb.Table("rel_childs").Where("parent_id = ?", int64(5)).Find(context.Background(), &rows)
if err != nil {
t.Fatalf("find: %v", err)
}
if len(rows) != 1 {
t.Fatalf("expected 1 row, got %d", len(rows))
}
if rows[0].ParentID != 5 {
t.Fatalf("expected parent_id=5, got %d", rows[0].ParentID)
}
if rows[0].Title != "lazy-child" {
t.Fatalf("expected title=lazy-child, got %s", rows[0].Title)
}
// Verify the SQL uses correct table and placeholder
if ex.lastSQL == "" {
t.Fatalf("no SQL was executed")
}
if !contains(ex.lastSQL, "rel_childs") {
t.Fatalf("SQL should reference rel_childs table, got: %s", ex.lastSQL)
}
if !contains(ex.lastSQL, "parent_id = $1") {
t.Fatalf("SQL should have parent_id = $1, got: %s", ex.lastSQL)
}
}
func TestEagerLoadMany_QueryErrorPropagates(t *testing.T) {
// Build a fake exec that returns an error
errExec := &relFakeExec{
fields: []string{"id", "parent_id", "title"},
rows: nil, // no rows
}
kn := &KintsNorm{}
// Test with the grouping function on empty result set
qb := &QueryBuilder{kn: kn, exec: errExec}
var children []relChild
err := qb.Table("rel_childs").Where("parent_id IN ($1)", int64(99)).Find(context.Background(), &children)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(children) != 0 {
t.Fatalf("expected 0 children for non-matching query, got %d", len(children))
}
}
func TestRelChild_StructMapping(t *testing.T) {
// Verify that relChild struct maps correctly via StructMapper
// This tests the core reflection path used by relations
var c relChild
qb := &QueryBuilder{kn: &KintsNorm{}, exec: &relFakeExec{
fields: []string{"id", "parent_id", "title"},
rows: [][]any{
{int64(1), int64(2), "test-title"},
},
}}
var results []relChild
if err := qb.Table("rel_childs").Find(context.Background(), &results); err != nil {
t.Fatalf("find: %v", err)
}
if len(results) != 1 {
t.Fatalf("expected 1 result")
}
c = results[0]
if c.ID != 1 || c.ParentID != 2 || c.Title != "test-title" {
t.Fatalf("unexpected scan result: %+v", c)
}
}
func contains(s, sub string) bool {
return len(s) >= len(sub) && searchSubstring(s, sub)
}
func searchSubstring(s, sub string) bool {
for i := 0; i <= len(s)-len(sub); i++ {
if s[i:i+len(sub)] == sub {
return true
}
}
return false
}