github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_valid_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 "testing" 19 20 "github.com/stretchr/testify/require" 21 "gopkg.in/src-d/go-errors.v1" 22 23 "github.com/dolthub/go-mysql-server/sql" 24 ) 25 26 func TestValid(t *testing.T) { 27 _, err := NewJSONValid() 28 require.True(t, errors.Is(err, sql.ErrInvalidArgumentNumber)) 29 30 f1 := buildGetFieldExpressions(t, NewJSONValid, 1) 31 32 testCases := []struct { 33 f sql.Expression 34 row sql.Row 35 expected interface{} 36 }{ 37 {f1, sql.Row{`null`}, true}, 38 {f1, sql.Row{`1`}, true}, 39 {f1, sql.Row{`[1]`}, true}, 40 {f1, sql.Row{`"fjsadflkd"`}, true}, 41 {f1, sql.Row{`[1, false]`}, true}, 42 {f1, sql.Row{`[1, {"a": 1}]`}, true}, 43 {f1, sql.Row{`{"a": 1}`}, true}, 44 {f1, sql.Row{`{"a": [1, false]}`}, true}, 45 {f1, sql.Row{`{"a": [1, {"a": 1}]}`}, true}, 46 {f1, sql.Row{`{"a": 1, "b": [2, 3], "c": {"d": "foo"}}`}, true}, 47 {f1, sql.Row{nil}, nil}, 48 {f1, sql.Row{1}, false}, 49 {f1, sql.Row{true}, false}, 50 {f1, sql.Row{`incorrect`}, false}, 51 {f1, sql.Row{`{"a": 1"}`}, false}, 52 {f1, sql.Row{`{1}`}, false}, 53 {f1, sql.Row{`[1, "a": 1]`}, false}, 54 } 55 56 for _, tt := range testCases { 57 t.Run(tt.f.String(), func(t *testing.T) { 58 require := require.New(t) 59 // any error case will result in output of 'false' value 60 result, _ := tt.f.Eval(sql.NewEmptyContext(), tt.row) 61 require.Equal(tt.expected, result) 62 }) 63 } 64 }