github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/s3select/sql/stringfuncs_test.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package sql
    19  
    20  import (
    21  	"testing"
    22  )
    23  
    24  func TestEvalSQLLike(t *testing.T) {
    25  	dropCases := []struct {
    26  		input, resultExpected string
    27  		matchExpected         bool
    28  	}{
    29  		{"", "", false},
    30  		{"a", "", true},
    31  		{"ab", "b", true},
    32  		{"தமிழ்", "மிழ்", true},
    33  	}
    34  
    35  	for i, tc := range dropCases {
    36  		res, ok := dropRune(tc.input)
    37  		if res != tc.resultExpected || ok != tc.matchExpected {
    38  			t.Errorf("DropRune Case %d failed", i)
    39  		}
    40  	}
    41  
    42  	matcherCases := []struct {
    43  		iText, iPat        string
    44  		iHasLeadingPercent bool
    45  		resultExpected     string
    46  		matchExpected      bool
    47  	}{
    48  		{"abcd", "bcd", false, "", false},
    49  		{"abcd", "bcd", true, "", true},
    50  		{"abcd", "abcd", false, "", true},
    51  		{"abcd", "abcd", true, "", true},
    52  		{"abcd", "ab", false, "cd", true},
    53  		{"abcd", "ab", true, "cd", true},
    54  		{"abcd", "bc", false, "", false},
    55  		{"abcd", "bc", true, "d", true},
    56  	}
    57  
    58  	for i, tc := range matcherCases {
    59  		res, ok := matcher(tc.iText, tc.iPat, tc.iHasLeadingPercent)
    60  		if res != tc.resultExpected || ok != tc.matchExpected {
    61  			t.Errorf("Matcher Case %d failed", i)
    62  		}
    63  	}
    64  
    65  	evalCases := []struct {
    66  		iText, iPat   string
    67  		iEsc          rune
    68  		matchExpected bool
    69  		errExpected   error
    70  	}{
    71  		{"abcd", "abc", runeZero, false, nil},
    72  		{"abcd", "abcd", runeZero, true, nil},
    73  		{"abcd", "abc_", runeZero, true, nil},
    74  		{"abcd", "_bdd", runeZero, false, nil},
    75  		{"abcd", "_b_d", runeZero, true, nil},
    76  
    77  		{"abcd", "____", runeZero, true, nil},
    78  		{"abcd", "____%", runeZero, true, nil},
    79  		{"abcd", "%____", runeZero, true, nil},
    80  		{"abcd", "%__", runeZero, true, nil},
    81  		{"abcd", "____", runeZero, true, nil},
    82  
    83  		{"", "_", runeZero, false, nil},
    84  		{"", "%", runeZero, true, nil},
    85  		{"abcd", "%%%%%", runeZero, true, nil},
    86  		{"abcd", "_____", runeZero, false, nil},
    87  		{"abcd", "%%%%%", runeZero, true, nil},
    88  
    89  		{"a%%d", `a\%\%d`, '\\', true, nil},
    90  		{"a%%d", `a\%d`, '\\', false, nil},
    91  		{`a%%\d`, `a\%\%\\d`, '\\', true, nil},
    92  		{`a%%\`, `a\%\%\\`, '\\', true, nil},
    93  		{`a%__%\`, `a\%\_\_\%\\`, '\\', true, nil},
    94  
    95  		{`a%__%\`, `a\%\_\_\%_`, '\\', true, nil},
    96  		{`a%__%\`, `a\%\_\__`, '\\', false, nil},
    97  		{`a%__%\`, `a\%\_\_%`, '\\', true, nil},
    98  		{`a%__%\`, `a?%?_?_?%\`, '?', true, nil},
    99  	}
   100  
   101  	for i, tc := range evalCases {
   102  		// fmt.Println("Case:", i)
   103  		res, err := evalSQLLike(tc.iText, tc.iPat, tc.iEsc)
   104  		if res != tc.matchExpected || err != tc.errExpected {
   105  			t.Errorf("Eval Case %d failed: %v %v", i, res, err)
   106  		}
   107  	}
   108  }