github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/length_test.go (about) 1 // Copyright 2020-2021 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 TestLength(t *testing.T) { 28 testCases := []struct { 29 name string 30 input interface{} 31 inputType sql.Type 32 fn func(sql.Expression) sql.Expression 33 expected interface{} 34 }{ 35 { 36 "length string", 37 "fóo", 38 types.Text, 39 NewLength, 40 int32(4), 41 }, 42 { 43 "length binary", 44 []byte("fóo"), 45 types.Blob, 46 NewLength, 47 int32(4), 48 }, 49 { 50 "length empty", 51 "", 52 types.Blob, 53 NewLength, 54 int32(0), 55 }, 56 { 57 "length empty binary", 58 []byte{}, 59 types.Blob, 60 NewLength, 61 int32(0), 62 }, 63 { 64 "length nil", 65 nil, 66 types.Blob, 67 NewLength, 68 nil, 69 }, 70 { 71 "char_length string", 72 "fóo", 73 types.LongText, 74 NewCharLength, 75 int32(3), 76 }, 77 { 78 "char_length binary", 79 []byte("fóo"), 80 types.Blob, 81 NewCharLength, 82 int32(4), 83 }, 84 { 85 "char_length empty", 86 "", 87 types.Blob, 88 NewCharLength, 89 int32(0), 90 }, 91 { 92 "char_length empty binary", 93 []byte{}, 94 types.Blob, 95 NewCharLength, 96 int32(0), 97 }, 98 { 99 "char_length nil", 100 nil, 101 types.Blob, 102 NewCharLength, 103 nil, 104 }, 105 } 106 107 for _, tt := range testCases { 108 t.Run(tt.name, func(t *testing.T) { 109 require := require.New(t) 110 111 result, err := tt.fn(expression.NewGetField(0, tt.inputType, "foo", false)).Eval( 112 sql.NewEmptyContext(), 113 sql.Row{tt.input}, 114 ) 115 116 require.NoError(err) 117 require.Equal(tt.expected, result) 118 }) 119 } 120 }