-
Notifications
You must be signed in to change notification settings - Fork 72
/
batcher_test.go
146 lines (125 loc) · 3.21 KB
/
batcher_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
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
package kallax
import (
"fmt"
"testing"
"github.com/Masterminds/squirrel"
"github.com/stretchr/testify/require"
)
func TestOneToManyWithFilterNotWritable(t *testing.T) {
r := require.New(t)
db, err := openTestDB()
r.NoError(err)
setupTables(t, db)
defer db.Close()
defer teardownTables(t, db)
store := NewStore(db)
m := newModel("foo", "bar", 1)
r.NoError(store.Insert(ModelSchema, m))
for i := 0; i < 4; i++ {
r.NoError(store.Insert(RelSchema, newRel(m.GetID(), fmt.Sprint(i))))
}
q := NewBaseQuery(ModelSchema)
r.NoError(q.AddRelation(RelSchema, "rels", OneToMany, Eq(f("foo"), "1")))
runner := newBatchQueryRunner(ModelSchema, squirrel.NewStmtCacher(db), q)
record, err := runner.next()
r.NoError(err)
r.False(record.IsWritable())
}
func TestBatcherLimit(t *testing.T) {
r := require.New(t)
db, err := openTestDB()
r.NoError(err)
setupTables(t, db)
defer db.Close()
defer teardownTables(t, db)
store := NewStore(db)
for i := 0; i < 10; i++ {
m := newModel("foo", "bar", 1)
r.NoError(store.Insert(ModelSchema, m))
for i := 0; i < 4; i++ {
r.NoError(store.Insert(RelSchema, newRel(m.GetID(), fmt.Sprint(i))))
}
}
q := NewBaseQuery(ModelSchema)
q.BatchSize(2)
q.Limit(5)
r.NoError(q.AddRelation(RelSchema, "rels", OneToMany, Eq(f("foo"), "1")))
runner := newBatchQueryRunner(ModelSchema, store.runner, q)
rs := NewBatchingResultSet(runner)
var count int
for rs.Next() {
_, err := rs.Get(nil)
r.NoError(err)
count++
}
r.NoError(err)
r.Equal(5, count)
}
func TestBatcherNoExtraQueryIfLessThanLimit(t *testing.T) {
r := require.New(t)
db, err := openTestDB()
r.NoError(err)
setupTables(t, db)
defer db.Close()
defer teardownTables(t, db)
store := NewStore(db)
for i := 0; i < 4; i++ {
m := newModel("foo", "bar", 1)
r.NoError(store.Insert(ModelSchema, m))
for i := 0; i < 4; i++ {
r.NoError(store.Insert(RelSchema, newRel(m.GetID(), fmt.Sprint(i))))
}
}
q := NewBaseQuery(ModelSchema)
q.Limit(6)
r.NoError(q.AddRelation(RelSchema, "rels", OneToMany, Eq(f("foo"), "1")))
var queries int
proxy := store.DebugWith(func(_ string, _ ...interface{}) {
queries++
}).runner
runner := newBatchQueryRunner(ModelSchema, proxy, q)
rs := NewBatchingResultSet(runner)
var count int
for rs.Next() {
_, err := rs.Get(nil)
r.NoError(err)
count++
}
r.NoError(err)
r.Equal(4, count)
r.Equal(2, queries)
}
func TestBatcherNoExtraQueryIfLessThanBatchSize(t *testing.T) {
r := require.New(t)
db, err := openTestDB()
r.NoError(err)
setupTables(t, db)
defer db.Close()
defer teardownTables(t, db)
store := NewStore(db)
for i := 0; i < 4; i++ {
m := newModel("foo", "bar", 1)
r.NoError(store.Insert(ModelSchema, m))
for i := 0; i < 4; i++ {
r.NoError(store.Insert(RelSchema, newRel(m.GetID(), fmt.Sprint(i))))
}
}
q := NewBaseQuery(ModelSchema)
q.BatchSize(3)
r.NoError(q.AddRelation(RelSchema, "rels", OneToMany, Eq(f("foo"), "1")))
var queries int
proxy := store.DebugWith(func(_ string, _ ...interface{}) {
queries++
}).runner
runner := newBatchQueryRunner(ModelSchema, proxy, q)
rs := NewBatchingResultSet(runner)
var count int
for rs.Next() {
_, err := rs.Get(nil)
r.NoError(err)
count++
}
r.NoError(err)
r.Equal(4, count)
r.Equal(4, queries)
}