github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/elt_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 TestElt(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: nil, 42 }, 43 { 44 name: "zero returns null", 45 args: []sql.Expression{ 46 expression.NewLiteral(0, types.Int32), 47 expression.NewLiteral("foo", types.Text), 48 }, 49 exp: nil, 50 }, 51 { 52 name: "negative returns null", 53 args: []sql.Expression{ 54 expression.NewLiteral(-10, types.Int32), 55 expression.NewLiteral("foo", types.Text), 56 }, 57 exp: nil, 58 }, 59 { 60 name: "too large returns null", 61 args: []sql.Expression{ 62 expression.NewLiteral(100, types.Int32), 63 expression.NewLiteral("foo", types.Text), 64 }, 65 exp: nil, 66 }, 67 { 68 name: "simple case", 69 args: []sql.Expression{ 70 expression.NewLiteral(1, types.Int32), 71 expression.NewLiteral("foo", types.Text), 72 }, 73 exp: "foo", 74 }, 75 { 76 name: "simple case again", 77 args: []sql.Expression{ 78 expression.NewLiteral(3, types.Int32), 79 expression.NewLiteral("foo1", types.Text), 80 expression.NewLiteral("foo2", types.Text), 81 expression.NewLiteral("foo3", types.Text), 82 }, 83 exp: "foo3", 84 }, 85 { 86 name: "index is float", 87 args: []sql.Expression{ 88 expression.NewLiteral(2.9, types.Float64), 89 expression.NewLiteral("foo1", types.Text), 90 expression.NewLiteral("foo2", types.Text), 91 expression.NewLiteral("foo3", types.Text), 92 }, 93 exp: "foo3", 94 }, 95 { 96 name: "index is valid string", 97 args: []sql.Expression{ 98 expression.NewLiteral("2", types.Text), 99 expression.NewLiteral("foo1", types.Text), 100 expression.NewLiteral("foo2", types.Text), 101 expression.NewLiteral("foo3", types.Text), 102 }, 103 exp: "foo2", 104 }, 105 { 106 name: "args are cast to string", 107 args: []sql.Expression{ 108 expression.NewLiteral("3", types.Text), 109 expression.NewLiteral("foo1", types.Text), 110 expression.NewLiteral("foo2", types.Text), 111 expression.NewLiteral(123, types.Int32), 112 }, 113 exp: "123", 114 }, 115 { 116 // we don't do truncation yet 117 // https://github.com/dolthub/dolt/issues/7302 118 name: "scientific string is truncated", 119 args: []sql.Expression{ 120 expression.NewLiteral("1e1", types.Text), 121 expression.NewLiteral("foo1", types.Text), 122 expression.NewLiteral("foo2", types.Text), 123 expression.NewLiteral("foo3", types.Int32), 124 }, 125 exp: "foo3", 126 skip: true, 127 }, 128 } 129 130 for _, tt := range tests { 131 t.Run(tt.name, func(t *testing.T) { 132 if tt.skip { 133 t.Skip() 134 } 135 136 ctx := sql.NewEmptyContext() 137 f, err := NewElt(tt.args...) 138 require.NoError(t, err) 139 140 res, err := f.Eval(ctx, nil) 141 if tt.err { 142 require.Error(t, err) 143 return 144 } 145 146 require.NoError(t, err) 147 require.Equal(t, tt.exp, res) 148 }) 149 } 150 }