github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/field_test.go (about) 1 // Copyright 2024 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 function 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 TestField(t *testing.T) { 28 tests := []struct { 29 name string 30 args []sql.Expression 31 exp interface{} 32 err bool 33 skip bool 34 }{ 35 { 36 name: "null argument", 37 args: []sql.Expression{ 38 nil, 39 nil, 40 }, 41 exp: int64(0), 42 }, 43 { 44 name: "not found", 45 args: []sql.Expression{ 46 expression.NewLiteral("abc", types.Text), 47 expression.NewLiteral("def", types.Text), 48 }, 49 exp: int64(0), 50 }, 51 { 52 name: "simple case", 53 args: []sql.Expression{ 54 expression.NewLiteral("abc", types.Int32), 55 expression.NewLiteral("abc", types.Int32), 56 expression.NewLiteral("def", types.Int32), 57 expression.NewLiteral("xyz", types.Int32), 58 }, 59 exp: int64(1), 60 }, 61 { 62 name: "simple case again", 63 args: []sql.Expression{ 64 expression.NewLiteral("def", types.Int32), 65 expression.NewLiteral("abc", types.Int32), 66 expression.NewLiteral("def", types.Int32), 67 expression.NewLiteral("xyz", types.Int32), 68 }, 69 exp: int64(2), 70 }, 71 { 72 name: "index is int", 73 args: []sql.Expression{ 74 expression.NewLiteral(10, types.Int32), 75 expression.NewLiteral("8", types.Text), 76 expression.NewLiteral("8", types.Text), 77 expression.NewLiteral("10", types.Text), 78 }, 79 exp: int64(3), 80 }, 81 { 82 name: "index is float", 83 args: []sql.Expression{ 84 expression.NewLiteral(2.9, types.Float64), 85 expression.NewLiteral("1", types.Text), 86 expression.NewLiteral("2.9", types.Text), 87 expression.NewLiteral("3", types.Text), 88 }, 89 exp: int64(2), 90 }, 91 { 92 name: "scientific string is truncated", 93 args: []sql.Expression{ 94 expression.NewLiteral(1e2, types.Int32), 95 expression.NewLiteral("100", types.Text), 96 }, 97 exp: int64(1), 98 }, 99 } 100 101 for _, tt := range tests { 102 t.Run(tt.name, func(t *testing.T) { 103 if tt.skip { 104 t.Skip() 105 } 106 107 ctx := sql.NewEmptyContext() 108 f, err := NewField(tt.args...) 109 require.NoError(t, err) 110 111 res, err := f.Eval(ctx, nil) 112 if tt.err { 113 require.Error(t, err) 114 return 115 } 116 117 require.NoError(t, err) 118 require.Equal(t, tt.exp, res) 119 }) 120 } 121 }