github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/json/json_array_test.go (about) 1 // Copyright 2022 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 22 "github.com/dolthub/go-mysql-server/sql" 23 "github.com/dolthub/go-mysql-server/sql/expression" 24 "github.com/dolthub/go-mysql-server/sql/types" 25 ) 26 27 func TestJSONArray(t *testing.T) { 28 f0, err := NewJSONArray() 29 require.NoError(t, err) 30 31 f1, err := NewJSONArray( 32 expression.NewGetField(0, types.LongText, "arg1", false), 33 ) 34 require.NoError(t, err) 35 36 f2, err := NewJSONArray( 37 expression.NewGetField(0, types.LongText, "arg1", false), 38 expression.NewGetField(1, types.LongText, "arg2", false), 39 ) 40 require.NoError(t, err) 41 42 f3, err := NewJSONArray( 43 expression.NewGetField(0, types.LongText, "arg1", false), 44 expression.NewGetField(1, types.LongText, "arg2", false), 45 expression.NewGetField(2, types.LongText, "arg3", false), 46 ) 47 require.NoError(t, err) 48 49 f4, err := NewJSONArray( 50 expression.NewGetField(0, types.LongText, "arg1", false), 51 expression.NewGetField(1, types.LongText, "arg2", false), 52 expression.NewGetField(2, types.LongText, "arg3", false), 53 expression.NewGetField(3, types.LongText, "arg4", false), 54 ) 55 require.NoError(t, err) 56 57 testCases := []struct { 58 f sql.Expression 59 row sql.Row 60 expected interface{} 61 err error 62 }{ 63 {f0, sql.Row{}, types.JSONDocument{Val: []interface{}{}}, nil}, 64 {f1, sql.Row{[]interface{}{1, 2}}, types.JSONDocument{Val: []interface{}{[]interface{}{1, 2}}}, nil}, 65 {f2, sql.Row{[]interface{}{1, 2}, "second item"}, types.JSONDocument{Val: []interface{}{[]interface{}{1, 2}, "second item"}}, nil}, 66 {f2, sql.Row{[]interface{}{1, 2}, map[string]interface{}{"name": "x"}}, types.JSONDocument{Val: []interface{}{[]interface{}{1, 2}, map[string]interface{}{"name": "x"}}}, nil}, 67 {f2, sql.Row{map[string]interface{}{"name": "x"}, map[string]interface{}{"id": 47}}, types.JSONDocument{Val: []interface{}{map[string]interface{}{"name": "x"}, map[string]interface{}{"id": 47}}}, nil}, 68 {f3, sql.Row{"foo", -44, "b"}, types.JSONDocument{Val: []interface{}{"foo", -44, "b"}}, nil}, 69 {f4, sql.Row{100, true, nil, "four"}, types.JSONDocument{Val: []interface{}{100, true, nil, "four"}}, nil}, 70 {f4, sql.Row{100.44, `{"name":null,"id":{"number":998,"type":"A"}}`, nil, `four`}, 71 types.JSONDocument{Val: []interface{}{100.44, "{\"name\":null,\"id\":{\"number\":998,\"type\":\"A\"}}", nil, "four"}}, nil}, 72 } 73 74 for _, tt := range testCases { 75 t.Run(tt.f.String(), func(t *testing.T) { 76 require := require.New(t) 77 result, err := tt.f.Eval(sql.NewEmptyContext(), tt.row) 78 if tt.err == nil { 79 require.NoError(err) 80 } else { 81 require.Equal(err.Error(), tt.err.Error()) 82 } 83 84 require.Equal(tt.expected, result) 85 }) 86 } 87 }