20
20
package leia
21
21
22
22
import (
23
+ "context"
23
24
"errors"
24
25
"testing"
26
+ "time"
25
27
26
28
"github.com/stretchr/testify/assert"
27
29
"go.etcd.io/bbolt"
@@ -143,7 +145,6 @@ func TestCollection_Delete(t *testing.T) {
143
145
}
144
146
145
147
assertIndexSize (t , db , i , 0 )
146
- // the index sub-bucket counts as 1
147
148
assertSize (t , db , documentCollection , 0 )
148
149
})
149
150
@@ -170,7 +171,7 @@ func TestCollection_Find(t *testing.T) {
170
171
c .Add ([]Document {exampleDoc })
171
172
q := New (Eq ("key" , "value" ))
172
173
173
- docs , err := c .Find (q )
174
+ docs , err := c .Find (context . TODO (), q )
174
175
175
176
if ! assert .NoError (t , err ) {
176
177
return
@@ -185,7 +186,7 @@ func TestCollection_Find(t *testing.T) {
185
186
c .Add ([]Document {exampleDoc })
186
187
q := New (Eq ("key" , "value" )).And (Eq ("non_indexed" , "value" ))
187
188
188
- docs , err := c .Find (q )
189
+ docs , err := c .Find (context . TODO (), q )
189
190
190
191
if ! assert .NoError (t , err ) {
191
192
return
@@ -200,7 +201,7 @@ func TestCollection_Find(t *testing.T) {
200
201
c .Add ([]Document {exampleDoc })
201
202
q := New (Eq ("non_indexed" , "value" ))
202
203
203
- docs , err := c .Find (q )
204
+ docs , err := c .Find (context . TODO (), q )
204
205
205
206
if ! assert .NoError (t , err ) {
206
207
return
@@ -215,7 +216,7 @@ func TestCollection_Find(t *testing.T) {
215
216
c .Add ([]Document {exampleDoc })
216
217
q := New (Eq ("key" , "value" )).And (Range ("non_indexed" , "v" , "value1" ))
217
218
218
- docs , err := c .Find (q )
219
+ docs , err := c .Find (context . TODO (), q )
219
220
220
221
if ! assert .NoError (t , err ) {
221
222
return
@@ -230,7 +231,7 @@ func TestCollection_Find(t *testing.T) {
230
231
c .Add ([]Document {exampleDoc })
231
232
q := New (Eq ("key" , "value" )).And (Range ("non_indexed" , "value1" , "value2" ))
232
233
233
- docs , err := c .Find (q )
234
+ docs , err := c .Find (context . TODO (), q )
234
235
235
236
if ! assert .NoError (t , err ) {
236
237
return
@@ -245,7 +246,7 @@ func TestCollection_Find(t *testing.T) {
245
246
c .AddIndex (i )
246
247
q := New (Eq ("key" , "value" ))
247
248
248
- docs , err := c .Find (q )
249
+ docs , err := c .Find (context . TODO (), q )
249
250
250
251
if ! assert .NoError (t , err ) {
251
252
return
@@ -260,7 +261,7 @@ func TestCollection_Find(t *testing.T) {
260
261
c .Add ([]Document {exampleDoc })
261
262
q := New (Eq ("key" , struct {}{}))
262
263
263
- _ , err := c .Find (q )
264
+ _ , err := c .Find (context . TODO (), q )
264
265
265
266
assert .Error (t , err )
266
267
})
@@ -270,10 +271,43 @@ func TestCollection_Find(t *testing.T) {
270
271
c .AddIndex (i )
271
272
c .Add ([]Document {exampleDoc })
272
273
273
- _ , err := c .Find (nil )
274
+ _ , err := c .Find (context . TODO (), nil )
274
275
275
276
assert .Error (t , err )
276
277
})
278
+
279
+ t .Run ("error - ctx cancelled" , func (t * testing.T ) {
280
+ c := createCollection (db )
281
+ c .AddIndex (i )
282
+ c .Add ([]Document {exampleDoc })
283
+ q := New (Eq ("key" , "value" ))
284
+ ctx , cancelFn := context .WithCancel (context .Background ())
285
+
286
+ cancelFn ()
287
+ _ , err := c .Find (ctx , q )
288
+
289
+ if ! assert .Error (t , err ) {
290
+ return
291
+ }
292
+
293
+ assert .Equal (t , context .Canceled , err )
294
+ })
295
+
296
+ t .Run ("error - deadline exceeded" , func (t * testing.T ) {
297
+ c := createCollection (db )
298
+ c .AddIndex (i )
299
+ c .Add ([]Document {exampleDoc })
300
+ q := New (Eq ("key" , "value" ))
301
+ ctx , _ := context .WithTimeout (context .Background (), time .Nanosecond )
302
+
303
+ _ , err := c .Find (ctx , q )
304
+
305
+ if ! assert .Error (t , err ) {
306
+ return
307
+ }
308
+
309
+ assert .Equal (t , context .DeadlineExceeded , err )
310
+ })
277
311
}
278
312
279
313
func TestCollection_Iterate (t * testing.T ) {
@@ -287,24 +321,42 @@ func TestCollection_Iterate(t *testing.T) {
287
321
t .Run ("ok - count fn" , func (t * testing.T ) {
288
322
count := 0
289
323
290
- err := db .View (func (tx * bbolt.Tx ) error {
291
- b := testBucket (t , tx )
292
- return i .Iterate (b , q , func (key Reference , value []byte ) error {
293
- count ++
294
- return nil
295
- })
324
+ err := c .Iterate (q , func (key Reference , value []byte ) error {
325
+ count ++
326
+ return nil
296
327
})
297
328
298
329
assert .NoError (t , err )
299
330
assert .Equal (t , 1 , count )
300
331
})
301
332
333
+ t .Run ("ok - document indexed multiple times, query should un double" , func (t * testing.T ) {
334
+ doc := DocumentFromString (jsonExample )
335
+ doc2 := DocumentFromString (jsonExample2 )
336
+ db := testDB (t )
337
+ count := 0
338
+
339
+ i := NewIndex (t .Name (),
340
+ NewFieldIndexer ("path.part" , AliasOption ("key" )),
341
+ NewFieldIndexer ("path.more.#.parts" , AliasOption ("key3" )),
342
+ )
343
+
344
+ c := createCollection (db )
345
+ c .AddIndex (i )
346
+ c .Add ([]Document {doc , doc2 })
347
+
348
+ err := c .Iterate (q , func (key Reference , value []byte ) error {
349
+ count ++
350
+ return nil
351
+ })
352
+
353
+ assert .NoError (t , err )
354
+ assert .Equal (t , 2 , count )
355
+ })
356
+
302
357
t .Run ("error" , func (t * testing.T ) {
303
- err := db .View (func (tx * bbolt.Tx ) error {
304
- b := testBucket (t , tx )
305
- return i .Iterate (b , q , func (key Reference , value []byte ) error {
306
- return errors .New ("b00m!" )
307
- })
358
+ err := c .Iterate (q , func (key Reference , value []byte ) error {
359
+ return errors .New ("b00m!" )
308
360
})
309
361
310
362
assert .Error (t , err )
0 commit comments