-
Notifications
You must be signed in to change notification settings - Fork 5
/
query_index_match_test.go
308 lines (247 loc) · 8.82 KB
/
query_index_match_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
package tormenta_test
import (
"errors"
"testing"
"time"
"github.com/jpincas/gouuidv6"
"github.com/jpincas/tormenta"
"github.com/jpincas/tormenta/testtypes"
)
// Simple test of bool indexing
func Test_IndexQuery_Match_Bool(t *testing.T) {
ttFalse := testtypes.FullStruct{}
ttTrue := testtypes.FullStruct{BoolField: true}
ttTrue2 := testtypes.FullStruct{BoolField: true}
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
db.Save(&ttFalse, &ttTrue, &ttTrue2)
results := []testtypes.FullStruct{}
// Test true
n, err := db.Find(&results).Match("BoolField", true).Run()
if err != nil {
t.Error("Testing basic querying - got error")
}
if n != 2 {
t.Errorf("Testing bool index. Expected 2 results, got %v", n)
}
// Test false + count
c, err := db.Find(&results).Match("BoolField", false).Count()
if err != nil {
t.Error("Testing basic querying - got error")
}
if c != 1 {
t.Errorf("Testing bool index. Expected 1 result, got %v", c)
}
}
// Test exact matching on strings
func Test_IndexQuery_Match_String(t *testing.T) {
customers := []string{"jon", "jonathan", "pablo"}
var fullStructs []tormenta.Record
for i := 0; i < 100; i++ {
fullStructs = append(fullStructs, &testtypes.FullStruct{
StringField: customers[i%len(customers)],
})
}
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
db.Save(fullStructs...)
testCases := []struct {
testName string
match interface{}
expected int
expectedError error
}{
{"blank string", nil, 0, errors.New(tormenta.ErrNilInputMatchIndexQuery)},
{"blank string", "", 0, nil},
{"should not match any", "nocustomerwiththisname", 0, nil},
{"matches 1 exactly with no interference", "pablo", 33, nil},
{"matches 1 exactly and 1 prefix", "jon", 34, nil},
{"matches 1 exactly and has same prefix as other", "jonathan", 33, nil},
{"uppercase - should make no difference", "JON", 34, nil},
{"mixed-case - should make no difference", "Jon", 34, nil},
}
for _, testCase := range testCases {
results := []testtypes.FullStruct{}
// Forwards
q := db.Find(&results).Match("StringField", testCase.match)
n, err := q.Run()
if testCase.expectedError != nil && err == nil {
t.Errorf("Testing %s. Expected error [%v] but got none", testCase.testName, testCase.expectedError)
}
if testCase.expectedError == nil && err != nil {
t.Errorf("Testing %s. Didn't expect error [%v]", testCase.testName, err)
}
if n != testCase.expected {
t.Errorf("Testing %s. Expecting %v, got %v", testCase.testName, testCase.expected, n)
}
// Reverse
q = db.Find(&results).Match("StringField", testCase.match).Reverse()
rn, err := q.Run()
if testCase.expectedError != nil && err == nil {
t.Errorf("Testing %s. Expected error [%v] but got none", testCase.testName, testCase.expectedError)
}
if testCase.expectedError == nil && err != nil {
t.Errorf("Testing %s. Didn't expect error [%v]", testCase.testName, err)
}
if n != testCase.expected {
t.Errorf("Testing %s. Expecting %v, got %v", testCase.testName, testCase.expected, rn)
}
}
}
func Test_IndexQuery_Match_Int(t *testing.T) {
var fullStructs []tormenta.Record
for i := 0; i < 100; i++ {
fullStructs = append(fullStructs, &testtypes.FullStruct{
IntField: i % 10,
})
}
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
db.Save(fullStructs...)
testCases := []struct {
testName string
match interface{}
expected int
expectedError error
}{
{"nothing", nil, 0, errors.New(tormenta.ErrNilInputMatchIndexQuery)},
{"1", 1, 10, nil},
{"11", 11, 0, nil},
}
for _, testCase := range testCases {
results := []testtypes.FullStruct{}
// Forwards
q := db.Find(&results).Match("IntField", testCase.match)
n, err := q.Run()
if testCase.expectedError != nil && err == nil {
t.Errorf("Testing %s. Expected error [%v] but got none", testCase.testName, testCase.expectedError)
}
if testCase.expectedError == nil && err != nil {
t.Errorf("Testing %s. Didn't expect error [%v]", testCase.testName, err)
}
if n != testCase.expected {
t.Errorf("Testing %s. Expecting %v, got %v", testCase.testName, testCase.expected, n)
}
// Reverse
q = db.Find(&results).Match("IntField", testCase.match).Reverse()
rn, err := q.Run()
if testCase.expectedError != nil && err == nil {
t.Errorf("Testing %s. Expected error [%v] but got none", testCase.testName, testCase.expectedError)
}
if testCase.expectedError == nil && err != nil {
t.Errorf("Testing %s. Didn't expect error [%v]", testCase.testName, err)
}
if n != testCase.expected {
t.Errorf("Testing %s. Expecting %v, got %v", testCase.testName, testCase.expected, rn)
}
}
}
func Test_IndexQuery_Match_Float(t *testing.T) {
var fullStructs []tormenta.Record
for i := 1; i <= 100; i++ {
fullStructs = append(fullStructs, &testtypes.FullStruct{
FloatField: float64(i) / float64(10),
})
}
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
db.Save(fullStructs...)
testCases := []struct {
testName string
match interface{}
expected int
expectedError error
}{
{"nothing", nil, 0, errors.New(tormenta.ErrNilInputMatchIndexQuery)},
{"0.1", 0.1, 1, nil},
{"0.1", 0.10, 1, nil},
{"0.11", 0.1, 1, nil},
{"0.20", 0.200, 1, nil},
}
for _, testCase := range testCases {
results := []testtypes.FullStruct{}
// Forwards
q := db.Find(&results).Match("FloatField", testCase.match)
n, err := q.Run()
if testCase.expectedError != nil && err == nil {
t.Errorf("Testing %s. Expected error [%v] but got none", testCase.testName, testCase.expectedError)
}
if testCase.expectedError == nil && err != nil {
t.Errorf("Testing %s. Didn't expect error [%v]", testCase.testName, err)
}
if n != testCase.expected {
t.Errorf("Testing %s. Expecting %v, got %v", testCase.testName, testCase.expected, n)
}
// Reverse
q = db.Find(&results).Match("FloatField", testCase.match).Reverse()
rn, err := q.Run()
if testCase.expectedError != nil && err == nil {
t.Errorf("Testing %s. Expected error [%v] but got none", testCase.testName, testCase.expectedError)
}
if testCase.expectedError == nil && err != nil {
t.Errorf("Testing %s. Didn't expect error [%v]", testCase.testName, err)
}
if n != testCase.expected {
t.Errorf("Testing %s. Expecting %v, got %v", testCase.testName, testCase.expected, rn)
}
}
}
func Test_IndexQuery_Match_DateRange(t *testing.T) {
var fullStructs []tormenta.Record
for i := 1; i <= 30; i++ {
fullStruct := &testtypes.FullStruct{
Model: tormenta.Model{
ID: gouuidv6.NewFromTime(time.Date(2009, time.November, i, 23, 0, 0, 0, time.UTC)),
},
IntField: getDept(i),
}
fullStructs = append(fullStructs, fullStruct)
}
tormenta.RandomiseRecords(fullStructs)
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
db.Save(fullStructs...)
testCases := []struct {
testName string
indexRangeStart interface{}
addFrom, addTo bool
from, to time.Time
expected int
indexRangeEnd interface{}
}{
// Exact match tests (indexRangeEnd is nil)
{"match department 1 - no date restriction", 1, false, false, time.Time{}, time.Time{}, 10, nil},
{"match department 1 - from beginning of time", 1, true, false, time.Time{}, time.Now(), 10, nil},
{"match department 1 - from beginning of time to now", 1, true, true, time.Time{}, time.Now(), 10, nil},
{"match department 1 - from now (no to)", 1, true, false, time.Now(), time.Time{}, 0, nil},
{"match department 1 - from 1st Nov (no to)", 1, true, false, time.Date(2009, time.November, 1, 23, 0, 0, 0, time.UTC), time.Time{}, 10, nil},
{"match department 1 - from 5th Nov", 1, true, false, time.Date(2009, time.November, 5, 23, 0, 0, 0, time.UTC), time.Time{}, 6, nil},
{"match department 1 - from 1st-5th Nov", 1, true, true, time.Date(2009, time.November, 1, 23, 0, 0, 0, time.UTC), time.Date(2009, time.November, 5, 23, 0, 0, 0, time.UTC), 5, nil},
}
for _, testCase := range testCases {
rangequeryResults := []testtypes.FullStruct{}
query := db.Find(&rangequeryResults).Match("IntField", testCase.indexRangeStart)
if testCase.addFrom {
query = query.From(testCase.from)
}
if testCase.addTo {
query = query.To(testCase.to)
}
// Forwards
n, err := query.Run()
if err != nil {
t.Error("Testing basic querying - got error")
}
if n != testCase.expected {
t.Errorf("Testing %s (number fullStructs retrieved). Expected %v - got %v", testCase.testName, testCase.expected, n)
}
// Backwards
rn, err := query.Reverse().Run()
if err != nil {
t.Error("Testing basic querying - got error")
}
if rn != testCase.expected {
t.Errorf("Testing %s (number fullStructs retrieved). Expected %v - got %v", testCase.testName, testCase.expected, rn)
}
}
}