/
query_test.go
327 lines (300 loc) · 7.91 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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package db
import (
"fmt"
"math"
"strconv"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestQueryWithValue(t *testing.T) {
db := newTestDB(t)
defer db.Close()
col, err := db.NewCollection("people", &testModel{})
require.NoError(t, err)
ageIndex := col.AddIndex("age", func(m Model) []byte {
return []byte(fmt.Sprint(m.(*testModel).Age))
})
// expected is a set of testModels with Age = 42
expected := []*testModel{}
for i := 0; i < 5; i++ {
model := &testModel{
Name: "ExpectedPerson_" + strconv.Itoa(i),
Age: 42,
}
require.NoError(t, col.Insert(model))
expected = append(expected, model)
}
// We also insert some other models with a different age.
for i := 0; i < 5; i++ {
model := &testModel{
Name: "OtherPerson_" + strconv.Itoa(i),
Age: i,
}
require.NoError(t, col.Insert(model))
}
// Save one more model with an Age that is a prefix of the target age.
model := &testModel{
Name: "PersonWithPrefixAge",
Age: 420,
}
require.NoError(t, col.Insert(model))
filter := ageIndex.ValueFilter([]byte("42"))
testQueryWithFilter(t, col, filter, expected)
}
func TestQueryWithRange(t *testing.T) {
db := newTestDB(t)
defer db.Close()
col, err := db.NewCollection("people", &testModel{})
require.NoError(t, err)
ageIndex := col.AddIndex("age", func(m Model) []byte {
return []byte(fmt.Sprint(m.(*testModel).Age))
})
all := []*testModel{}
for i := 0; i < 5; i++ {
model := &testModel{
Name: "Person_" + strconv.Itoa(i),
Age: i,
}
require.NoError(t, col.Insert(model))
all = append(all, model)
}
// expected is the set of people with 1 <= age < 4
expected := all[1:4]
filter := ageIndex.RangeFilter([]byte("1"), []byte("4"))
testQueryWithFilter(t, col, filter, expected)
}
func TestQueryWithPrefix(t *testing.T) {
db := newTestDB(t)
defer db.Close()
col, err := db.NewCollection("people", &testModel{})
require.NoError(t, err)
ageIndex := col.AddIndex("age", func(m Model) []byte {
return []byte(fmt.Sprint(m.(*testModel).Age))
})
// expected is a set of testModels with an age that starts with "2"
expected := []*testModel{
{
Name: "ExpectedPerson_0",
Age: 2021,
},
{
Name: "ExpectedPerson_1",
Age: 22,
},
{
Name: "ExpectedPerson_2",
Age: 250,
},
}
for _, model := range expected {
require.NoError(t, col.Insert(model))
}
// We also insert some other models with different ages.
excluded := []*testModel{
{
Name: "ExcludedPerson_0",
Age: 40,
},
{
Name: "ExcludedPerson_1",
Age: 41,
},
{
Name: "ExcludedPerson_2",
Age: 42,
},
}
for _, model := range excluded {
require.NoError(t, col.Insert(model))
}
{
filter := ageIndex.PrefixFilter([]byte("2"))
testQueryWithFilter(t, col, filter, expected)
}
{
// An empty prefix should return all models.
all := append(expected, excluded...)
filter := ageIndex.PrefixFilter([]byte{})
testQueryWithFilter(t, col, filter, all)
}
}
func TestFindWithValueWithMultiIndex(t *testing.T) {
db := newTestDB(t)
defer db.Close()
col, err := db.NewCollection("people", &testModel{})
require.NoError(t, err)
nicknameIndex := col.AddMultiIndex("nicknames", func(m Model) [][]byte {
person := m.(*testModel)
indexValues := make([][]byte, len(person.Nicknames))
for i, nickname := range person.Nicknames {
indexValues[i] = []byte(nickname)
}
return indexValues
})
// expected is a set of testModels that include the nickname "Bob"
expected := []*testModel{
{
Name: "ExpectedPerson_0",
Age: 42,
Nicknames: []string{"Bob", "Jim", "John"},
},
{
Name: "ExpectedPerson_1",
Age: 43,
Nicknames: []string{"Alice", "Bob", "Emily"},
},
{
Name: "ExpectedPerson_2",
Age: 44,
Nicknames: []string{"Bob", "No one"},
},
}
for _, model := range expected {
require.NoError(t, col.Insert(model))
}
// We also insert some other models with different nicknames.
excluded := []*testModel{
{
Name: "ExcludedPerson_0",
Age: 42,
Nicknames: []string{"Bill", "Jim", "John"},
},
{
Name: "ExcludedPerson_1",
Age: 43,
Nicknames: []string{"Alice", "Jane", "Emily"},
},
{
Name: "ExcludedPerson_2",
Age: 44,
Nicknames: []string{"Nemo", "No one"},
},
}
for _, model := range excluded {
require.NoError(t, col.Insert(model))
}
filter := nicknameIndex.ValueFilter([]byte("Bob"))
testQueryWithFilter(t, col, filter, expected)
}
func TestFindWithRangeWithMultiIndex(t *testing.T) {
db := newTestDB(t)
defer db.Close()
col, err := db.NewCollection("people", &testModel{})
require.NoError(t, err)
nicknameIndex := col.AddMultiIndex("nicknames", func(m Model) [][]byte {
person := m.(*testModel)
indexValues := make([][]byte, len(person.Nicknames))
for i, nickname := range person.Nicknames {
indexValues[i] = []byte(nickname)
}
return indexValues
})
// expected is a set of testModels that include at least one nickname that
// satisfies "B" <= nickname < "E"
expected := []*testModel{
{
Name: "ExpectedPerson_0",
Age: 42,
Nicknames: []string{"Alice", "Beth", "Emily"},
},
{
Name: "ExpectedPerson_1",
Age: 43,
Nicknames: []string{"Bob", "Charles", "Dan"},
},
{
Name: "ExpectedPerson_2",
Age: 44,
Nicknames: []string{"James", "Darell"},
},
}
for _, model := range expected {
require.NoError(t, col.Insert(model))
}
// We also insert some other models with different nicknames.
excluded := []*testModel{
{
Name: "ExcludedPerson_0",
Age: 42,
Nicknames: []string{"Allen", "Jim", "John"},
},
{
Name: "ExcludedPerson_1",
Age: 43,
Nicknames: []string{"Sophia", "Jane", "Emily"},
},
{
Name: "ExcludedPerson_2",
Age: 44,
Nicknames: []string{"Nemo", "No one"},
},
}
for _, model := range excluded {
require.NoError(t, col.Insert(model))
}
filter := nicknameIndex.RangeFilter([]byte("B"), []byte("E"))
testQueryWithFilter(t, col, filter, expected)
}
// testQueryWithFilter runs a comprehensive set of queries based on the given
// filter and checks that the results are always what we expect.
func testQueryWithFilter(t *testing.T, col *Collection, filter *Filter, expected []*testModel) {
reverseExpected := reverseSlice(expected)
// safeMax is min(2, len(expected)) to account for the fact that expected may
// have length shorter than 2.
var safeMax = int(math.Min(2, float64(len(expected))))
// Each test case covers slight variations of the same query.
testCases := []struct {
query *Query
expected []*testModel
}{
{
query: col.NewQuery(filter),
expected: expected,
},
{
query: col.NewQuery(filter).Reverse(),
expected: reverseExpected,
},
{
query: col.NewQuery(filter).Max(safeMax),
expected: expected[:safeMax],
},
{
query: col.NewQuery(filter).Reverse().Max(safeMax),
expected: reverseExpected[:safeMax],
},
{
query: col.NewQuery(filter).Offset(1),
expected: expected[1:],
},
{
query: col.NewQuery(filter).Offset(1).Max(safeMax - 1),
expected: expected[1:safeMax],
},
{
query: col.NewQuery(filter).Offset(1).Reverse(),
expected: reverseExpected[1:],
},
{
query: col.NewQuery(filter).Offset(1).Max(safeMax - 1).Reverse(),
expected: reverseExpected[1:safeMax],
},
}
for i, tc := range testCases {
var actual []*testModel
require.NoError(t, tc.query.Run(&actual), "test case %d", i)
assert.Equal(t, tc.expected, actual, "test case %d", i)
actualCount, err := tc.query.Count()
require.NoError(t, err, "test case %d", i)
assert.Equal(t, len(tc.expected), actualCount, "test case %d", i)
}
}
func reverseSlice(s []*testModel) []*testModel {
reversed := make([]*testModel, len(s))
copy(reversed, s)
for left, right := 0, len(reversed)-1; left < right; left, right = left+1, right-1 {
reversed[left], reversed[right] = reversed[right], reversed[left]
}
return reversed
}