github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_depth_test.go (about) 1 // Copyright 2023 Dolthub, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package json 16 17 import ( 18 "fmt" 19 "strings" 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 "gopkg.in/src-d/go-errors.v1" 24 25 "github.com/dolthub/go-mysql-server/sql" 26 ) 27 28 func TestJSONDepth(t *testing.T) { 29 _, err := NewJSONDepth() 30 require.True(t, errors.Is(err, sql.ErrInvalidArgumentNumber)) 31 32 f1 := buildGetFieldExpressions(t, NewJSONDepth, 1) 33 testCases := []struct { 34 f sql.Expression 35 row sql.Row 36 exp interface{} 37 err bool 38 }{ 39 { 40 f: f1, 41 row: sql.Row{``}, 42 err: true, 43 }, 44 { 45 f: f1, 46 row: sql.Row{`badjson`}, 47 err: true, 48 }, 49 { 50 f: f1, 51 row: sql.Row{true}, 52 err: true, 53 }, 54 { 55 f: f1, 56 row: sql.Row{1}, 57 err: true, 58 }, 59 60 { 61 f: f1, 62 row: sql.Row{nil}, 63 exp: nil, 64 }, 65 66 { 67 f: f1, 68 row: sql.Row{`null`}, 69 exp: 1, 70 }, 71 { 72 f: f1, 73 row: sql.Row{`1`}, 74 exp: 1, 75 }, 76 { 77 f: f1, 78 row: sql.Row{`true`}, 79 exp: 1, 80 }, 81 { 82 f: f1, 83 row: sql.Row{`123.456`}, 84 exp: 1, 85 }, 86 { 87 f: f1, 88 row: sql.Row{`"abcdef"`}, 89 exp: 1, 90 }, 91 92 { 93 f: f1, 94 row: sql.Row{`[]`}, 95 exp: 1, 96 }, 97 { 98 f: f1, 99 row: sql.Row{`{}`}, 100 exp: 1, 101 }, 102 103 { 104 f: f1, 105 row: sql.Row{`[null]`}, 106 exp: 2, 107 }, 108 { 109 f: f1, 110 row: sql.Row{`{"a": null}`}, 111 exp: 2, 112 }, 113 { 114 f: f1, 115 row: sql.Row{`[1]`}, 116 exp: 2, 117 }, 118 { 119 f: f1, 120 row: sql.Row{`{"a": 1}`}, 121 exp: 2, 122 }, 123 { 124 f: f1, 125 row: sql.Row{`[1, 2, 3]`}, 126 exp: 2, 127 }, 128 { 129 f: f1, 130 row: sql.Row{`{"aa": 1, "bb": 2, "c": 3}`}, 131 exp: 2, 132 }, 133 134 { 135 f: f1, 136 row: sql.Row{`{"a": 1, "b": [1, 2, 3]}`}, 137 exp: 3, 138 }, 139 { 140 f: f1, 141 row: sql.Row{`[0, {"a": 1, "b": 2}]`}, 142 exp: 3, 143 }, 144 145 { 146 f: f1, 147 row: sql.Row{`{"a": 1, "b": {"aa": 1, "bb": {"aaa": 1, "bbb": {"aaaa": 1}}}}`}, 148 exp: 5, 149 }, 150 } 151 152 for _, tt := range testCases { 153 var args []string 154 for _, a := range tt.row { 155 args = append(args, fmt.Sprintf("%v", a)) 156 } 157 t.Run(strings.Join(args, ", "), func(t *testing.T) { 158 require := require.New(t) 159 result, err := tt.f.Eval(sql.NewEmptyContext(), tt.row) 160 if tt.err { 161 require.Error(err) 162 } else { 163 require.NoError(err) 164 } 165 require.Equal(tt.exp, result) 166 }) 167 } 168 }