@@ -358,7 +358,59 @@ func (s *testEvaluatorSuite) TestJSONContains(c *C) {
358
358
f , err := fc .getFunction (s .ctx , s .datumsToConstants (args ))
359
359
c .Assert (err , IsNil )
360
360
d , err := evalBuiltinFunc (f , chunk.Row {})
361
+ if t .success {
362
+ c .Assert (err , IsNil )
363
+ if t .expected == nil {
364
+ c .Assert (d .IsNull (), IsTrue )
365
+ } else {
366
+ c .Assert (d .GetInt64 (), Equals , int64 (t .expected .(int )))
367
+ }
368
+ } else {
369
+ c .Assert (err , NotNil )
370
+ }
371
+ }
372
+ }
361
373
374
+ func (s * testEvaluatorSuite ) TestJSONContainsPath (c * C ) {
375
+ defer testleak .AfterTest (c )()
376
+ fc := funcs [ast .JSONContainsPath ]
377
+ jsonString := `{"a": 1, "b": 2, "c": {"d": 4}}`
378
+ invalidJSON := `{"a": 1`
379
+ tbl := []struct {
380
+ input []interface {}
381
+ expected interface {}
382
+ success bool
383
+ }{
384
+ // Tests nil arguments
385
+ {[]interface {}{nil , json .ContainsPathOne , "$.c" }, nil , true },
386
+ {[]interface {}{nil , json .ContainsPathAll , "$.c" }, nil , true },
387
+ {[]interface {}{jsonString , nil , "$.a[3]" }, nil , true },
388
+ {[]interface {}{jsonString , json .ContainsPathOne , nil }, nil , true },
389
+ {[]interface {}{jsonString , json .ContainsPathAll , nil }, nil , true },
390
+ // Tests with one path expression
391
+ {[]interface {}{jsonString , json .ContainsPathOne , "$.c.d" }, 1 , true },
392
+ {[]interface {}{jsonString , json .ContainsPathOne , "$.a.d" }, 0 , true },
393
+ {[]interface {}{jsonString , json .ContainsPathAll , "$.c.d" }, 1 , true },
394
+ {[]interface {}{jsonString , json .ContainsPathAll , "$.a.d" }, 0 , true },
395
+ // Tests with multiple path expression
396
+ {[]interface {}{jsonString , json .ContainsPathOne , "$.a" , "$.e" }, 1 , true },
397
+ {[]interface {}{jsonString , json .ContainsPathOne , "$.a" , "$.c" }, 1 , true },
398
+ {[]interface {}{jsonString , json .ContainsPathAll , "$.a" , "$.e" }, 0 , true },
399
+ {[]interface {}{jsonString , json .ContainsPathAll , "$.a" , "$.c" }, 1 , true },
400
+ // Tests path expression contains any asterisk
401
+ {[]interface {}{jsonString , json .ContainsPathOne , "$.*" }, 1 , true },
402
+ {[]interface {}{jsonString , json .ContainsPathOne , "$[*]" }, 0 , true },
403
+ {[]interface {}{jsonString , json .ContainsPathAll , "$.*" }, 1 , true },
404
+ {[]interface {}{jsonString , json .ContainsPathAll , "$[*]" }, 0 , true },
405
+ // Tests invalid json document
406
+ {[]interface {}{invalidJSON , json .ContainsPathOne , "$.a" }, nil , false },
407
+ {[]interface {}{invalidJSON , json .ContainsPathAll , "$.a" }, nil , false },
408
+ }
409
+ for _ , t := range tbl {
410
+ args := types .MakeDatums (t .input ... )
411
+ f , err := fc .getFunction (s .ctx , s .datumsToConstants (args ))
412
+ c .Assert (err , IsNil )
413
+ d , err := evalBuiltinFunc (f , chunk.Row {})
362
414
if t .success {
363
415
c .Assert (err , IsNil )
364
416
if t .expected == nil {
0 commit comments