github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/find_in_set_test.go (about)

     1  // Copyright 2023 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 TestFindInSet(t *testing.T) {
    28  	testCases := []struct {
    29  		name     string
    30  		left     string
    31  		right    string
    32  		expected int
    33  		skip     bool
    34  	}{
    35  		{
    36  			name:     "string exists",
    37  			left:     "b",
    38  			right:    "a,b,c",
    39  			expected: 2,
    40  		},
    41  		{
    42  			name:     "string does not exist",
    43  			left:     "abc",
    44  			right:    "a,b,c",
    45  			expected: 0,
    46  		},
    47  		{
    48  			name:     "whitespace not removed",
    49  			left:     "  b   ",
    50  			right:    "a,b,c",
    51  			expected: 0,
    52  		},
    53  		{
    54  			name:     "whitespace not removed 2",
    55  			left:     "b",
    56  			right:    "  a  ,  b ,  c  ",
    57  			expected: 0,
    58  		},
    59  		{
    60  			name:     "whitespace not removed 3",
    61  			left:     " a b ",
    62  			right:    "a, a b ,c",
    63  			expected: 2,
    64  		},
    65  		{
    66  			name:     "comma bad",
    67  			left:     "b,",
    68  			right:    "a,b,c",
    69  			expected: 0,
    70  		},
    71  		{
    72  			name:     "special characters ok",
    73  			left:     "test@example.com",
    74  			right:    "nottest@example.com,hello@example.com,test@example.com",
    75  			expected: 3,
    76  		},
    77  		{
    78  			name:     "look for empty string",
    79  			left:     "",
    80  			right:    "a,",
    81  			expected: 2,
    82  		},
    83  		{
    84  			name:     "look in empty string",
    85  			left:     "a",
    86  			right:    "",
    87  			expected: 0,
    88  		},
    89  	}
    90  
    91  	for _, tt := range testCases {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			if tt.skip {
    94  				t.Skip()
    95  			}
    96  			require := require.New(t)
    97  			f := NewFindInSet(expression.NewLiteral(tt.left, types.LongText), expression.NewLiteral(tt.right, types.LongText))
    98  			v, err := f.Eval(sql.NewEmptyContext(), nil)
    99  			require.NoError(err)
   100  			require.Equal(tt.expected, v)
   101  		})
   102  	}
   103  
   104  	t.Run("test find in null set", func(t *testing.T) {
   105  		require := require.New(t)
   106  		f := NewFindInSet(expression.NewLiteral("a", types.LongText), expression.NewLiteral(nil, types.Null))
   107  		v, err := f.Eval(sql.NewEmptyContext(), nil)
   108  		require.NoError(err)
   109  		require.Equal(nil, v)
   110  	})
   111  
   112  	t.Run("find null in set", func(t *testing.T) {
   113  		require := require.New(t)
   114  		f := NewFindInSet(expression.NewLiteral("a", types.LongText), expression.NewLiteral(nil, types.Null))
   115  		v, err := f.Eval(sql.NewEmptyContext(), nil)
   116  		require.NoError(err)
   117  		require.Equal(nil, v)
   118  	})
   119  
   120  	t.Run("find number", func(t *testing.T) {
   121  		require := require.New(t)
   122  		f := NewFindInSet(expression.NewLiteral(500, types.Int64), expression.NewLiteral("1,2,3,500", types.Null))
   123  		v, err := f.Eval(sql.NewEmptyContext(), nil)
   124  		require.NoError(err)
   125  		require.Equal(4, v)
   126  	})
   127  }