github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/reverse_repeat_replace_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 TestReverse(t *testing.T) { 28 f := NewReverse(expression.NewGetField(0, types.LongText, "", false)) 29 testCases := []struct { 30 name string 31 row sql.Row 32 expected interface{} 33 err bool 34 }{ 35 {"null input", sql.NewRow(nil), nil, false}, 36 {"empty string", sql.NewRow(""), "", false}, 37 {"handles numbers as strings", sql.NewRow(123), "321", false}, 38 {"valid string", sql.NewRow("foobar"), "raboof", false}, 39 } 40 for _, tt := range testCases { 41 t.Run(tt.name, func(t *testing.T) { 42 t.Helper() 43 require := require.New(t) 44 ctx := sql.NewEmptyContext() 45 46 v, err := f.Eval(ctx, tt.row) 47 if tt.err { 48 require.Error(err) 49 } else { 50 require.NoError(err) 51 require.Equal(tt.expected, v) 52 } 53 }) 54 } 55 } 56 57 func TestRepeat(t *testing.T) { 58 f := NewRepeat( 59 expression.NewGetField(0, types.LongText, "", false), 60 expression.NewGetField(1, types.Int32, "", false), 61 ) 62 63 testCases := []struct { 64 name string 65 row sql.Row 66 expected interface{} 67 err bool 68 }{ 69 {"null input", sql.NewRow(nil), nil, false}, 70 {"empty string", sql.NewRow("", 2), "", false}, 71 {"count is zero", sql.NewRow("foo", 0), "", false}, 72 {"count is negative", sql.NewRow("foo", -2), "foo", true}, 73 {"valid string", sql.NewRow("foobar", 2), "foobarfoobar", false}, 74 } 75 for _, tt := range testCases { 76 t.Run(tt.name, func(t *testing.T) { 77 t.Helper() 78 require := require.New(t) 79 ctx := sql.NewEmptyContext() 80 81 v, err := f.Eval(ctx, tt.row) 82 if tt.err { 83 require.Error(err) 84 } else { 85 require.NoError(err) 86 require.Equal(tt.expected, v) 87 } 88 }) 89 } 90 } 91 92 func TestReplace(t *testing.T) { 93 f := NewReplace( 94 expression.NewGetField(0, types.LongText, "", false), 95 expression.NewGetField(1, types.LongText, "", false), 96 expression.NewGetField(2, types.LongText, "", false), 97 ) 98 99 testCases := []struct { 100 name string 101 row sql.Row 102 expected interface{} 103 err bool 104 }{ 105 {"null inputs", sql.NewRow(nil), nil, false}, 106 {"empty str", sql.NewRow("", "foo", "bar"), "", false}, 107 {"empty fromStr", sql.NewRow("foobarfoobar", "", "car"), "foobarfoobar", false}, 108 {"empty toStr", sql.NewRow("foobarfoobar", "bar", ""), "foofoo", false}, 109 {"valid strings", sql.NewRow("foobarfoobar", "bar", "car"), "foocarfoocar", false}, 110 } 111 for _, tt := range testCases { 112 t.Run(tt.name, func(t *testing.T) { 113 t.Helper() 114 require := require.New(t) 115 ctx := sql.NewEmptyContext() 116 117 v, err := f.Eval(ctx, tt.row) 118 if tt.err { 119 require.Error(err) 120 } else { 121 require.NoError(err) 122 require.Equal(tt.expected, v) 123 } 124 }) 125 } 126 }