github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/rpad_lpad_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 TestLPad(t *testing.T) { 28 f, err := NewPad( 29 lPadType, 30 expression.NewGetField(0, types.LongText, "str", false), 31 expression.NewGetField(1, types.Int64, "len", false), 32 expression.NewGetField(2, types.LongText, "padStr", false), 33 ) 34 require.NoError(t, err) 35 testCases := []struct { 36 name string 37 row sql.Row 38 expected interface{} 39 err bool 40 }{ 41 {"null string", sql.NewRow(nil, 1, "bar"), nil, false}, 42 {"null len", sql.NewRow("foo", nil, "bar"), nil, false}, 43 {"null padStr", sql.NewRow("foo", 1, nil), nil, false}, 44 45 {"negative length", sql.NewRow("foo", -1, "bar"), "", false}, 46 {"length 0", sql.NewRow("foo", 0, "bar"), "", false}, 47 {"invalid length", sql.NewRow("foo", "a", "bar"), "", true}, 48 49 {"empty padStr and len < len(str)", sql.NewRow("foo", 1, ""), "f", false}, 50 {"empty padStr and len > len(str)", sql.NewRow("foo", 4, ""), "", false}, 51 {"empty padStr and len == len(str)", sql.NewRow("foo", 3, ""), "foo", false}, 52 53 {"non empty padStr and len < len(str)", sql.NewRow("foo", 1, "abcd"), "f", false}, 54 {"non empty padStr and len == len(str)", sql.NewRow("foo", 3, "abcd"), "foo", false}, 55 56 {"padStr repeats exactly once", sql.NewRow("foo", 6, "abc"), "abcfoo", false}, 57 {"padStr does not repeat once", sql.NewRow("foo", 5, "abc"), "abfoo", false}, 58 {"padStr repeats many times", sql.NewRow("foo", 10, "abc"), "abcabcafoo", false}, 59 } 60 for _, tt := range testCases { 61 t.Run(tt.name, func(t *testing.T) { 62 t.Helper() 63 require := require.New(t) 64 ctx := sql.NewEmptyContext() 65 66 v, err := f.Eval(ctx, tt.row) 67 if tt.err { 68 require.Error(err) 69 } else { 70 require.NoError(err) 71 require.Equal(tt.expected, v) 72 } 73 }) 74 } 75 } 76 77 func TestRPad(t *testing.T) { 78 f, err := NewPad( 79 rPadType, 80 expression.NewGetField(0, types.LongText, "str", false), 81 expression.NewGetField(1, types.Int64, "len", false), 82 expression.NewGetField(2, types.LongText, "padStr", false), 83 ) 84 require.NoError(t, err) 85 testCases := []struct { 86 name string 87 row sql.Row 88 expected interface{} 89 err bool 90 }{ 91 {"null string", sql.NewRow(nil, 1, "bar"), nil, false}, 92 {"null len", sql.NewRow("foo", nil, "bar"), nil, false}, 93 {"null padStr", sql.NewRow("foo", 1, nil), nil, false}, 94 95 {"negative length", sql.NewRow("foo", -1, "bar"), "", false}, 96 {"length 0", sql.NewRow("foo", 0, "bar"), "", false}, 97 {"invalid length", sql.NewRow("foo", "a", "bar"), "", true}, 98 99 {"empty padStr and len < len(str)", sql.NewRow("foo", 1, ""), "f", false}, 100 {"empty padStr and len > len(str)", sql.NewRow("foo", 4, ""), "", false}, 101 {"empty padStr and len == len(str)", sql.NewRow("foo", 3, ""), "foo", false}, 102 103 {"non empty padStr and len < len(str)", sql.NewRow("foo", 1, "abcd"), "f", false}, 104 {"non empty padStr and len == len(str)", sql.NewRow("foo", 3, "abcd"), "foo", false}, 105 106 {"padStr repeats exactly once", sql.NewRow("foo", 6, "abc"), "fooabc", false}, 107 {"padStr does not repeat once", sql.NewRow("foo", 5, "abc"), "fooab", false}, 108 {"padStr repeats many times", sql.NewRow("foo", 10, "abc"), "fooabcabca", false}, 109 } 110 for _, tt := range testCases { 111 t.Run(tt.name, func(t *testing.T) { 112 t.Helper() 113 require := require.New(t) 114 ctx := sql.NewEmptyContext() 115 116 v, err := f.Eval(ctx, tt.row) 117 if tt.err { 118 require.Error(err) 119 } else { 120 require.NoError(err) 121 require.Equal(tt.expected, v) 122 } 123 }) 124 } 125 }