@@ -345,6 +345,73 @@ func TestGetListFilter(t *testing.T) {
345
345
}
346
346
347
347
func TestGetListArray (t * testing.T ) {
348
+ sharedInit := func () * requestTestVars {
349
+ s := mem .NewHandler ()
350
+ s .Insert (context .TODO (), []* resource.Item {
351
+ {ID : "1" , Payload : map [string ]interface {}{"id" : 1 ,
352
+ "foo" : []interface {}{"bar" , "baz" }},
353
+ },
354
+ {ID : "2" , Payload : map [string ]interface {}{"id" : 2 ,
355
+ "foo" : []interface {}{"bar" }},
356
+ },
357
+ {ID : "3" , Payload : map [string ]interface {}{"id" : 3 ,
358
+ "foo" : []interface {}{"baz" }},
359
+ },
360
+ })
361
+
362
+ idx := resource .NewIndex ()
363
+ idx .Bind ("foo" , schema.Schema {
364
+ Fields : schema.Fields {
365
+ "foo" : {
366
+ Filterable : true ,
367
+ Validator : & schema.Array {
368
+ Values : schema.Field {
369
+ Validator : & schema.String {},
370
+ },
371
+ },
372
+ },
373
+ },
374
+ }, s , resource .DefaultConf )
375
+
376
+ return & requestTestVars {
377
+ Index : idx ,
378
+ Storers : map [string ]resource.Storer {"foo" : s },
379
+ }
380
+ }
381
+
382
+ tests := map [string ]requestTest {
383
+ `filter/array:foo:not-found` : {
384
+ Init : sharedInit ,
385
+ NewRequest : func () (* http.Request , error ) {
386
+ return http .NewRequest ("GET" , `/foo?filter={foo:"mar"}` , nil )
387
+ },
388
+ ResponseCode : http .StatusOK ,
389
+ ResponseBody : `[]` ,
390
+ },
391
+ `filter/array:foo:try-object` : {
392
+ Init : sharedInit ,
393
+ NewRequest : func () (* http.Request , error ) {
394
+ return http .NewRequest ("GET" , `/foo?filter={foo:{bar:"mar"}}` , nil )
395
+ },
396
+ ResponseCode : 422 ,
397
+ ResponseBody : `{"code":422,"issues":{"filter":["foo: invalid query expression: invalid value at #1: not a string"]},"message":"URL parameters contain error(s)"}` ,
398
+ },
399
+ `filter/array:foo:find` : {
400
+ Init : sharedInit ,
401
+ NewRequest : func () (* http.Request , error ) {
402
+ return http .NewRequest ("GET" , `/foo?filter={foo:"bar"}` , nil )
403
+ },
404
+ ResponseCode : http .StatusOK ,
405
+ ResponseBody : `[{"foo":["bar","baz"],"id":1},{"foo":["bar"],"id":2}]` ,
406
+ },
407
+ }
408
+ for n , tc := range tests {
409
+ tc := tc // capture range variable
410
+ t .Run (n , tc .Test )
411
+ }
412
+ }
413
+
414
+ func TestGetListArrayOfObjects (t * testing.T ) {
348
415
sharedInit := func () * requestTestVars {
349
416
s := mem .NewHandler ()
350
417
s .Insert (context .TODO (), []* resource.Item {
@@ -573,14 +640,6 @@ func TestGetListArray(t *testing.T) {
573
640
{"id":2,"foo":[{"a":"bar","b":20}]}
574
641
]` ,
575
642
},
576
- `filter/array:foo:not-an-array` : {
577
- Init : sharedInit ,
578
- NewRequest : func () (* http.Request , error ) {
579
- return http .NewRequest ("GET" , `/foo?filter={foo:"mar"}` , nil )
580
- },
581
- ResponseCode : http .StatusUnprocessableEntity ,
582
- ResponseBody : `{"code":422,"issues":{"filter":["foo: invalid query expression: not an array"]},"message":"URL parameters contain error(s)"}` ,
583
- },
584
643
`filter/array:foo:invalid-elemMatch` : {
585
644
Init : sharedInit ,
586
645
NewRequest : func () (* http.Request , error ) {
0 commit comments