@@ -17,6 +17,7 @@ type Task struct {
17
17
Done bool `json:"done"`
18
18
Likes int64 `json:"likes"`
19
19
Todos []Todo `json:"todos"`
20
+ Tags []string `json:"tags"`
20
21
Created time.Time `json:"created"`
21
22
}
22
23
@@ -57,6 +58,7 @@ func newTask(title string, done bool) map[string]interface{} {
57
58
Title : title ,
58
59
Done : done ,
59
60
Todos : []Todo {Todo {Title : "sub" , Done : done }, Todo {Title : "sub2" , Done : done }},
61
+ Tags : []string {title , "unittest" , "tag" },
60
62
Created : time .Now (),
61
63
})
62
64
}
@@ -370,3 +372,66 @@ func TestQueryDocumentsWithNonExistingDB(t *testing.T) {
370
372
t .Fatalf ("expected empty result but got %v" , result )
371
373
}
372
374
}
375
+
376
+ func TestQueryWithInOperator (t * testing.T ) {
377
+ redTask := newTask ("red" , false )
378
+ redTask , err := datastore .CreateDocument (adminAuth , confDBName , colName , redTask )
379
+ if err != nil {
380
+ t .Fatal (err )
381
+ }
382
+
383
+ blueTask := newTask ("blue" , false )
384
+ blueTask , err = datastore .CreateDocument (adminAuth , confDBName , colName , blueTask )
385
+ if err != nil {
386
+ t .Fatal (err )
387
+ }
388
+
389
+ // test the "in" operator
390
+ var clauses [][]interface {}
391
+ clauses = append (clauses , []interface {}{"tags" , "in" , "red" })
392
+ clauses = append (clauses , []interface {}{"done" , "=" , false })
393
+
394
+ lp := model.ListParams {Page : 1 , Size : 5 }
395
+
396
+ filters , err := datastore .ParseQuery (clauses )
397
+ if err != nil {
398
+ t .Fatal (err )
399
+ }
400
+
401
+ result , err := datastore .QueryDocuments (adminAuth , confDBName , colName , filters , lp )
402
+ if err != nil {
403
+ t .Fatal (err )
404
+ } else if result .Total != 1 {
405
+ t .Fatalf ("expected total to be 1 got %d" , result .Total )
406
+ } else if id := result .Results [0 ]["ID" ]; id != redTask ["ID" ] {
407
+ t .Fatalf ("expected result item id to be %s got %s" , redTask ["ID" ], id )
408
+ }
409
+
410
+ clauses = nil
411
+ clauses = append (clauses , []interface {}{"tags" , "!in" , "red" })
412
+ clauses = append (clauses , []interface {}{"done" , "=" , false })
413
+
414
+ filters , err = datastore .ParseQuery (clauses )
415
+ if err != nil {
416
+ t .Fatal (err )
417
+ }
418
+
419
+ result , err = datastore .QueryDocuments (adminAuth , confDBName , colName , filters , lp )
420
+ if err != nil {
421
+ t .Fatal (err )
422
+ } else if result .Total <= 1 {
423
+ t .Fatalf ("expected total to be > 1 got %d" , result .Total )
424
+ }
425
+
426
+ found := false
427
+ for _ , task := range result .Results {
428
+ if task ["ID" ] == blueTask ["ID" ] {
429
+ found = true
430
+ break
431
+ }
432
+ }
433
+
434
+ if ! found {
435
+ t .Errorf ("expected to find blueTask ID in result set" )
436
+ }
437
+ }
0 commit comments