github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/locate_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 TestLocate(t *testing.T) { 28 intPtr := func(i int) *int { 29 return &i 30 } 31 32 testCases := []struct { 33 Name string 34 Substr string 35 Str string 36 Start *int 37 Expected int32 38 }{ 39 { 40 Name: "locate", 41 Substr: "o", 42 Str: "locate", 43 Expected: 2, 44 }, 45 { 46 Name: "locate not found", 47 Substr: "notinlocate", 48 Str: "locate", 49 Expected: 0, 50 }, 51 { 52 Name: "locate with start before", 53 Substr: "ocate", 54 Str: "locate", 55 Start: intPtr(1), 56 Expected: 2, 57 }, 58 { 59 Name: "locate with start after", 60 Substr: "o", 61 Str: "locate", 62 Start: intPtr(3), 63 Expected: 0, 64 }, 65 { 66 Name: "locate with start after 1st iteration", 67 Substr: "o", 68 Str: "locate the second o", 69 Start: intPtr(5), 70 Expected: 15, 71 }, 72 { 73 Name: "locate is case insensitive", 74 Substr: "c", 75 Str: "LOCATE", 76 Expected: 3, 77 }, 78 { 79 Name: "locate with start 0", 80 Substr: "o", 81 Str: "locate", 82 Start: intPtr(0), 83 Expected: 0, 84 }, 85 { 86 Name: "locate empty substring", 87 Substr: "", 88 Str: "locate", 89 Expected: 1, 90 }, 91 { 92 Name: "locate empty substring", 93 Substr: "", 94 Str: "locate", 95 Expected: 1, 96 }, 97 { 98 Name: "locate all empty", 99 Substr: "", 100 Str: "", 101 Expected: 1, 102 }, 103 { 104 Name: "locate all empty with start > 1", 105 Substr: "", 106 Str: "", 107 Start: intPtr(2), 108 Expected: 0, 109 }, 110 } 111 112 for _, tt := range testCases { 113 t.Run(tt.Name, func(t *testing.T) { 114 require := require.New(t) 115 116 exprs := []sql.Expression{ 117 expression.NewGetField(0, types.Text, "substr", false), 118 expression.NewGetField(1, types.LongText, "str", false), 119 } 120 row := sql.Row{tt.Substr, tt.Str} 121 122 if tt.Start != nil { 123 exprs = append(exprs, expression.NewGetField(2, types.Int32, "start", false)) 124 row = append(row, *tt.Start) 125 } 126 127 f, err := NewLocate(exprs...) 128 require.NoError(err) 129 130 result, err := f.Eval(sql.NewEmptyContext(), row) 131 require.NoError(err) 132 require.Equal(tt.Expected, result) 133 }) 134 } 135 }