storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/s3select/sql/stringfuncs_test.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2019 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package sql
    18  
    19  import (
    20  	"testing"
    21  )
    22  
    23  func TestEvalSQLLike(t *testing.T) {
    24  	dropCases := []struct {
    25  		input, resultExpected string
    26  		matchExpected         bool
    27  	}{
    28  		{"", "", false},
    29  		{"a", "", true},
    30  		{"ab", "b", true},
    31  		{"தமிழ்", "மிழ்", true},
    32  	}
    33  
    34  	for i, tc := range dropCases {
    35  		res, ok := dropRune(tc.input)
    36  		if res != tc.resultExpected || ok != tc.matchExpected {
    37  			t.Errorf("DropRune Case %d failed", i)
    38  		}
    39  	}
    40  
    41  	matcherCases := []struct {
    42  		iText, iPat        string
    43  		iHasLeadingPercent bool
    44  		resultExpected     string
    45  		matchExpected      bool
    46  	}{
    47  		{"abcd", "bcd", false, "", false},
    48  		{"abcd", "bcd", true, "", true},
    49  		{"abcd", "abcd", false, "", true},
    50  		{"abcd", "abcd", true, "", true},
    51  		{"abcd", "ab", false, "cd", true},
    52  		{"abcd", "ab", true, "cd", true},
    53  		{"abcd", "bc", false, "", false},
    54  		{"abcd", "bc", true, "d", true},
    55  	}
    56  
    57  	for i, tc := range matcherCases {
    58  		res, ok := matcher(tc.iText, tc.iPat, tc.iHasLeadingPercent)
    59  		if res != tc.resultExpected || ok != tc.matchExpected {
    60  			t.Errorf("Matcher Case %d failed", i)
    61  		}
    62  	}
    63  
    64  	evalCases := []struct {
    65  		iText, iPat   string
    66  		iEsc          rune
    67  		matchExpected bool
    68  		errExpected   error
    69  	}{
    70  		{"abcd", "abc", runeZero, false, nil},
    71  		{"abcd", "abcd", runeZero, true, nil},
    72  		{"abcd", "abc_", runeZero, true, nil},
    73  		{"abcd", "_bdd", runeZero, false, nil},
    74  		{"abcd", "_b_d", runeZero, true, nil},
    75  
    76  		{"abcd", "____", runeZero, true, nil},
    77  		{"abcd", "____%", runeZero, true, nil},
    78  		{"abcd", "%____", runeZero, true, nil},
    79  		{"abcd", "%__", runeZero, true, nil},
    80  		{"abcd", "____", runeZero, true, nil},
    81  
    82  		{"", "_", runeZero, false, nil},
    83  		{"", "%", runeZero, true, nil},
    84  		{"abcd", "%%%%%", runeZero, true, nil},
    85  		{"abcd", "_____", runeZero, false, nil},
    86  		{"abcd", "%%%%%", runeZero, true, nil},
    87  
    88  		{"a%%d", `a\%\%d`, '\\', true, nil},
    89  		{"a%%d", `a\%d`, '\\', false, nil},
    90  		{`a%%\d`, `a\%\%\\d`, '\\', true, nil},
    91  		{`a%%\`, `a\%\%\\`, '\\', true, nil},
    92  		{`a%__%\`, `a\%\_\_\%\\`, '\\', true, nil},
    93  
    94  		{`a%__%\`, `a\%\_\_\%_`, '\\', true, nil},
    95  		{`a%__%\`, `a\%\_\__`, '\\', false, nil},
    96  		{`a%__%\`, `a\%\_\_%`, '\\', true, nil},
    97  		{`a%__%\`, `a?%?_?_?%\`, '?', true, nil},
    98  	}
    99  
   100  	for i, tc := range evalCases {
   101  		// fmt.Println("Case:", i)
   102  		res, err := evalSQLLike(tc.iText, tc.iPat, tc.iEsc)
   103  		if res != tc.matchExpected || err != tc.errExpected {
   104  			t.Errorf("Eval Case %d failed: %v %v", i, res, err)
   105  		}
   106  	}
   107  }
   108  
   109  func TestEvalSQLSubstring(t *testing.T) {
   110  	evalCases := []struct {
   111  		s           string
   112  		startIdx    int
   113  		length      int
   114  		resExpected string
   115  		errExpected error
   116  	}{
   117  		{"abcd", 1, 1, "a", nil},
   118  		{"abcd", -1, 1, "a", nil},
   119  		{"abcd", 999, 999, "", nil},
   120  		{"", 999, 999, "", nil},
   121  		{"测试abc", 1, 1, "测", nil},
   122  		{"测试abc", 5, 5, "c", nil},
   123  	}
   124  
   125  	for i, tc := range evalCases {
   126  		res, err := evalSQLSubstring(tc.s, tc.startIdx, tc.length)
   127  		if res != tc.resExpected || err != tc.errExpected {
   128  			t.Errorf("Eval Case %d failed: %v %v", i, res, err)
   129  		}
   130  	}
   131  }