github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/isbinary_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 TestIsBinary(t *testing.T) { 28 f := NewIsBinary(expression.NewGetField(0, types.Blob, "blob", true)) 29 30 testCases := []struct { 31 name string 32 row sql.Row 33 expected bool 34 }{ 35 {"binary", sql.NewRow([]byte{0, 1, 2}), true}, 36 {"not binary", sql.NewRow([]byte{1, 2, 3}), false}, 37 {"null", sql.NewRow(nil), false}, 38 } 39 40 for _, tt := range testCases { 41 t.Run(tt.name, func(t *testing.T) { 42 require.Equal(t, tt.expected, eval(t, f, tt.row)) 43 }) 44 } 45 } 46 47 func TestSubstringArity(t *testing.T) { 48 expr := expression.NewGetField(0, types.Int64, "foo", false) 49 testCases := []struct { 50 name string 51 args []sql.Expression 52 ok bool 53 }{ 54 {"0 args", nil, false}, 55 {"1 args", []sql.Expression{expr}, false}, 56 {"2 args", []sql.Expression{expr, expr}, true}, 57 {"3 args", []sql.Expression{expr, expr, expr}, true}, 58 {"4 args", []sql.Expression{expr, expr, expr, expr}, false}, 59 } 60 61 for _, tt := range testCases { 62 t.Run(tt.name, func(t *testing.T) { 63 require := require.New(t) 64 f, err := NewSubstring(tt.args...) 65 if tt.ok { 66 require.NotNil(f) 67 require.NoError(err) 68 } else { 69 require.Error(err) 70 } 71 }) 72 } 73 }