(t *testing.T)
| 989 | } |
| 990 | |
| 991 | func TestJSONArrayInsert(t *testing.T) { |
| 992 | ctx := createContext(t) |
| 993 | fc := funcs[ast.JSONArrayInsert] |
| 994 | tbl := []struct { |
| 995 | input []any |
| 996 | expected any |
| 997 | success bool |
| 998 | err *terror.Error |
| 999 | }{ |
| 1000 | // Success |
| 1001 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, `$.b[1]`, `z`}, `{"a": 1, "b": [2, "z", 3], "c": 4}`, true, nil}, |
| 1002 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, `$.a[1]`, `z`}, `{"a": 1, "b": [2, 3], "c": 4}`, true, nil}, |
| 1003 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, `$.d[1]`, `z`}, `{"a": 1, "b": [2, 3], "c": 4}`, true, nil}, |
| 1004 | {[]any{`[{"a": 1, "b": [2, 3], "c": 4}]`, `$[1]`, `w`}, `[{"a": 1, "b": [2, 3], "c": 4}, "w"]`, true, nil}, |
| 1005 | {[]any{`[{"a": 1, "b": [2, 3], "c": 4}]`, `$[0]`, nil}, `[null, {"a": 1, "b": [2, 3], "c": 4}]`, true, nil}, |
| 1006 | {[]any{`[1, 2, 3]`, `$[100]`, `{"b": 2}`}, `[1, 2, 3, "{\"b\": 2}"]`, true, nil}, |
| 1007 | // About null |
| 1008 | {[]any{nil, `$`, nil}, nil, true, nil}, |
| 1009 | {[]any{nil, `$`, `a`}, nil, true, nil}, |
| 1010 | {[]any{`[]`, `$[0]`, nil}, `[null]`, true, nil}, |
| 1011 | {[]any{`{}`, `$[0]`, nil}, `{}`, true, nil}, |
| 1012 | // Bad arguments |
| 1013 | {[]any{`asdf`, `$`, nil}, nil, false, types.ErrInvalidJSONText}, |
| 1014 | {[]any{``, `$`, nil}, nil, false, types.ErrInvalidJSONText}, |
| 1015 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, `$.d`}, nil, false, ErrIncorrectParameterCount}, |
| 1016 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, `$.c`, `y`, `$.b`}, nil, false, ErrIncorrectParameterCount}, |
| 1017 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, nil, nil}, nil, true, nil}, |
| 1018 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, `asdf`, nil}, nil, false, types.ErrInvalidJSONPath}, |
| 1019 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, 42, nil}, nil, false, types.ErrInvalidJSONPath}, |
| 1020 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, `$.*`, nil}, nil, false, types.ErrInvalidJSONPathMultipleSelection}, |
| 1021 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, `$.b[0]`, nil, `$.a`, nil}, nil, false, types.ErrInvalidJSONPathArrayCell}, |
| 1022 | {[]any{`{"a": 1, "b": [2, 3], "c": 4}`, `$.a`, nil}, nil, false, types.ErrInvalidJSONPathArrayCell}, |
| 1023 | // Following tests come from MySQL doc. |
| 1024 | {[]any{`["a", {"b": [1, 2]}, [3, 4]]`, `$[1]`, `x`}, `["a", "x", {"b": [1, 2]}, [3, 4]]`, true, nil}, |
| 1025 | {[]any{`["a", {"b": [1, 2]}, [3, 4]]`, `$[100]`, `x`}, `["a", {"b": [1, 2]}, [3, 4], "x"]`, true, nil}, |
| 1026 | {[]any{`["a", {"b": [1, 2]}, [3, 4]]`, `$[1].b[0]`, `x`}, `["a", {"b": ["x", 1, 2]}, [3, 4]]`, true, nil}, |
| 1027 | {[]any{`["a", {"b": [1, 2]}, [3, 4]]`, `$[2][1]`, `y`}, `["a", {"b": [1, 2]}, [3, "y", 4]]`, true, nil}, |
| 1028 | {[]any{`["a", {"b": [1, 2]}, [3, 4]]`, `$[0]`, `x`, `$[2][1]`, `y`}, `["x", "a", {"b": [1, 2]}, [3, 4]]`, true, nil}, |
| 1029 | // More test cases |
| 1030 | {[]any{`["a", {"b": [1, 2]}, [3, 4]]`, `$[0]`, `x`, `$[0]`, `y`}, `["y", "x", "a", {"b": [1, 2]}, [3, 4]]`, true, nil}, |
| 1031 | } |
| 1032 | for _, tt := range tbl { |
| 1033 | args := types.MakeDatums(tt.input...) |
| 1034 | f, err := fc.getFunction(ctx, datumsToConstants(args)) |
| 1035 | // Parameter count error |
| 1036 | if err != nil { |
| 1037 | require.Error(t, tt.err) |
| 1038 | require.True(t, tt.err.Equal(err)) |
| 1039 | continue |
| 1040 | } |
| 1041 | |
| 1042 | d, err := evalBuiltinFunc(f, ctx, chunk.Row{}) |
| 1043 | |
| 1044 | if tt.success { |
| 1045 | require.NoError(t, err) |
| 1046 | switch x := tt.expected.(type) { |
| 1047 | case string: |
| 1048 | var j1, j2 types.BinaryJSON |
nothing calls this directly
no test coverage detected