1515package json
1616
1717import (
18- "fmt"
1918 "strings"
2019 "testing"
2120
@@ -41,25 +40,25 @@ func TestInsert(t *testing.T) {
4140 expected interface {}
4241 err error
4342 }{
44- {f1 , sql.Row {json , "$.a" , 10.1 }, json , nil }, // insert existing does nothing
45- {f1 , sql.Row {json , "$.e" , "new" }, `{"a": 1, "b": [2, 3], "c": {"d": "foo"},"e":"new"}` , nil }, // insert new
46- {f1 , sql.Row {json , "$.c.d" , "test" }, json , nil }, // insert existing nested does nothing
47- {f2 , sql.Row {json , "$.a" , 10.1 , "$.e" , "new" }, `{"a": 1, "b": [2, 3], "c": {"d": "foo"},"e":"new"}` , nil }, // insert multiple, one change.
48- {f1 , sql.Row {json , "$.a.e" , "test" }, json , nil }, // insert nested does nothing
49- {f1 , sql.Row {json , "$.c.e" , "test" }, `{"a": 1, "b": [2, 3], "c": {"d": "foo","e":"test"}}` , nil }, // insert nested in existing struct
50- {f1 , sql.Row {json , "$.c[5]" , 4.1 }, `{"a": 1, "b": [2, 3], "c": [{"d": "foo"}, 4.1]}` , nil }, // insert struct with indexing out of range
51- {f1 , sql.Row {json , "$.b[0]" , 4.1 }, json , nil }, // insert element in array does nothing
52- {f1 , sql.Row {json , "$.b[5]" , 4.1 }, `{"a": 1, "b": [2, 3, 4.1], "c": {"d": "foo"}}` , nil }, // insert element in array out of range
53- {f1 , sql.Row {json , "$.b.c" , 4 }, json , nil }, // insert nested in array does nothing
54- {f1 , sql.Row {json , "$.a[0]" , 4.1 }, json , nil }, // struct as array does nothing
55- {f1 , sql.Row {json , "$[0]" , 4.1 }, json , nil }, // struct does nothing.
56- {f1 , sql.Row {json , "$.[0]" , 4.1 }, nil , fmt . Errorf ( "Invalid JSON path expression" )}, // improper struct indexing
57- {f1 , sql.Row {json , "foo" , "test" }, nil , fmt . Errorf ( "Invalid JSON path expression" )}, // invalid path
58- {f1 , sql.Row {json , "$.c.*" , "test" }, nil , fmt . Errorf ( "Path expressions may not contain the * and ** tokens" )}, // path contains * wildcard
59- {f1 , sql.Row {json , "$.c.**" , "test" }, nil , fmt . Errorf ( "Path expressions may not contain the * and ** tokens" )}, // path contains ** wildcard
60- {f1 , sql.Row {json , "$" , 10.1 }, json , nil }, // whole document no opt
61- {f1 , sql.Row {nil , "$" , 42.7 }, nil , nil }, // null document returns null
62- {f1 , sql.Row {json , nil , 10 }, nil , nil }, // if any path is null, return null
43+ {f1 , sql.Row {json , "$.a" , 10.1 }, json , nil }, // insert existing does nothing
44+ {f1 , sql.Row {json , "$.e" , "new" }, `{"a": 1, "b": [2, 3], "c": {"d": "foo"},"e":"new"}` , nil }, // insert new
45+ {f1 , sql.Row {json , "$.c.d" , "test" }, json , nil }, // insert existing nested does nothing
46+ {f2 , sql.Row {json , "$.a" , 10.1 , "$.e" , "new" }, `{"a": 1, "b": [2, 3], "c": {"d": "foo"},"e":"new"}` , nil }, // insert multiple, one change.
47+ {f1 , sql.Row {json , "$.a.e" , "test" }, json , nil }, // insert nested does nothing
48+ {f1 , sql.Row {json , "$.c.e" , "test" }, `{"a": 1, "b": [2, 3], "c": {"d": "foo","e":"test"}}` , nil }, // insert nested in existing struct
49+ {f1 , sql.Row {json , "$.c[5]" , 4.1 }, `{"a": 1, "b": [2, 3], "c": [{"d": "foo"}, 4.1]}` , nil }, // insert struct with indexing out of range
50+ {f1 , sql.Row {json , "$.b[0]" , 4.1 }, json , nil }, // insert element in array does nothing
51+ {f1 , sql.Row {json , "$.b[5]" , 4.1 }, `{"a": 1, "b": [2, 3, 4.1], "c": {"d": "foo"}}` , nil }, // insert element in array out of range
52+ {f1 , sql.Row {json , "$.b.c" , 4 }, json , nil }, // insert nested in array does nothing
53+ {f1 , sql.Row {json , "$.a[0]" , 4.1 }, json , nil }, // struct as array does nothing
54+ {f1 , sql.Row {json , "$[0]" , 4.1 }, json , nil }, // struct does nothing.
55+ {f1 , sql.Row {json , "$.[0]" , 4.1 }, nil , ErrInvalidPath }, // improper struct indexing
56+ {f1 , sql.Row {json , "foo" , "test" }, nil , ErrInvalidPath }, // invalid path
57+ {f1 , sql.Row {json , "$.c.*" , "test" }, nil , ErrPathWildcard }, // path contains * wildcard
58+ {f1 , sql.Row {json , "$.c.**" , "test" }, nil , ErrPathWildcard }, // path contains ** wildcard
59+ {f1 , sql.Row {json , "$" , 10.1 }, json , nil }, // whole document no opt
60+ {f1 , sql.Row {nil , "$" , 42.7 }, nil , nil }, // null document returns null
61+ {f1 , sql.Row {json , nil , 10 }, nil , nil }, // if any path is null, return null
6362
6463 // mysql> select JSON_INSERT(JSON_ARRAY(), "$[2]", 1 , "$[2]", 2 ,"$[2]", 3 ,"$[2]", 4);
6564 // +------------------------------------------------------------------------+
0 commit comments