@@ -259,7 +259,7 @@ func (s *testEvaluatorSuite) TestJSONObject(c *C) {
259
259
}
260
260
}
261
261
262
- func (s * testEvaluatorSuite ) TestJSONORemove (c * C ) {
262
+ func (s * testEvaluatorSuite ) TestJSONRemove (c * C ) {
263
263
defer testleak .AfterTest (c )()
264
264
fc := funcs [ast .JSONRemove ]
265
265
tbl := []struct {
@@ -308,3 +308,66 @@ func (s *testEvaluatorSuite) TestJSONORemove(c *C) {
308
308
}
309
309
}
310
310
}
311
+
312
+ func (s * testEvaluatorSuite ) TestJSONContains (c * C ) {
313
+ defer testleak .AfterTest (c )()
314
+ fc := funcs [ast .JSONContains ]
315
+ tbl := []struct {
316
+ input []interface {}
317
+ expected interface {}
318
+ success bool
319
+ }{
320
+ // Tests nil arguments
321
+ {[]interface {}{nil , `1` , "$.c" }, nil , true },
322
+ {[]interface {}{`{"a": [1, 2, {"aa": "xx"}]}` , nil , "$.a[3]" }, nil , true },
323
+ {[]interface {}{`{"a": [1, 2, {"aa": "xx"}]}` , `1` , nil }, nil , true },
324
+ // Tests with path expression
325
+ {[]interface {}{`[1,2,[1,[5,[3]]]]` , `[1,3]` , "$[2]" }, 1 , true },
326
+ {[]interface {}{`[1,2,[1,[5,{"a":[2,3]}]]]` , `[1,{"a":[3]}]` , "$[2]" }, 1 , true },
327
+ {[]interface {}{`[{"a":1}]` , `{"a":1}` , "$" }, 1 , true },
328
+ {[]interface {}{`[{"a":1,"b":2}]` , `{"a":1,"b":2}` , "$" }, 1 , true },
329
+ {[]interface {}{`[{"a":{"a":1},"b":2}]` , `{"a":1}` , "$.a" }, 0 , true },
330
+ // Tests without path expression
331
+ {[]interface {}{`{}` , `{}` }, 1 , true },
332
+ {[]interface {}{`{"a":1}` , `{}` }, 1 , true },
333
+ {[]interface {}{`{"a":1}` , `1` }, 0 , true },
334
+ {[]interface {}{`{"a":[1]}` , `[1]` }, 0 , true },
335
+ {[]interface {}{`{"b":2, "c":3}` , `{"c":3}` }, 1 , true },
336
+ {[]interface {}{`1` , `1` }, 1 , true },
337
+ {[]interface {}{`[1]` , `1` }, 1 , true },
338
+ {[]interface {}{`[1,2]` , `[1]` }, 1 , true },
339
+ {[]interface {}{`[1,2]` , `[1,3]` }, 0 , true },
340
+ {[]interface {}{`[1,2]` , `["1"]` }, 0 , true },
341
+ {[]interface {}{`[1,2,[1,3]]` , `[1,3]` }, 1 , true },
342
+ {[]interface {}{`[1,2,[1,[5,[3]]]]` , `[1,3]` }, 1 , true },
343
+ {[]interface {}{`[1,2,[1,[5,{"a":[2,3]}]]]` , `[1,{"a":[3]}]` }, 1 , true },
344
+ {[]interface {}{`[{"a":1}]` , `{"a":1}` }, 1 , true },
345
+ {[]interface {}{`[{"a":1,"b":2}]` , `{"a":1}` }, 1 , true },
346
+ {[]interface {}{`[{"a":{"a":1},"b":2}]` , `{"a":1}` }, 0 , true },
347
+ // Tests path expression contains any asterisk
348
+ {[]interface {}{`{"a": [1, 2, {"aa": "xx"}]}` , `1` , "$.*" }, nil , false },
349
+ {[]interface {}{`{"a": [1, 2, {"aa": "xx"}]}` , `1` , "$[*]" }, nil , false },
350
+ {[]interface {}{`{"a": [1, 2, {"aa": "xx"}]}` , `1` , "$**.a" }, nil , false },
351
+ // Tests path expression does not identify a section of the target document
352
+ {[]interface {}{`{"a": [1, 2, {"aa": "xx"}]}` , `1` , "$.c" }, nil , true },
353
+ {[]interface {}{`{"a": [1, 2, {"aa": "xx"}]}` , `1` , "$.a[3]" }, nil , true },
354
+ {[]interface {}{`{"a": [1, 2, {"aa": "xx"}]}` , `1` , "$.a[2].b" }, nil , true },
355
+ }
356
+ for _ , t := range tbl {
357
+ args := types .MakeDatums (t .input ... )
358
+ f , err := fc .getFunction (s .ctx , s .datumsToConstants (args ))
359
+ c .Assert (err , IsNil )
360
+ d , err := evalBuiltinFunc (f , chunk.Row {})
361
+
362
+ if t .success {
363
+ c .Assert (err , IsNil )
364
+ if t .expected == nil {
365
+ c .Assert (d .IsNull (), IsTrue )
366
+ } else {
367
+ c .Assert (d .GetInt64 (), Equals , int64 (t .expected .(int )))
368
+ }
369
+ } else {
370
+ c .Assert (err , NotNil )
371
+ }
372
+ }
373
+ }
0 commit comments