vitess.io/vitess@v0.16.2/go/vt/sqlparser/utils_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     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 sqlparser
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  )
    24  
    25  func TestNormalizeAlphabetically(t *testing.T) {
    26  	testcases := []struct {
    27  		in  string
    28  		out string
    29  	}{{
    30  		in:  "select * from tbl",
    31  		out: "select * from tbl",
    32  	}, {
    33  		in:  "select * from tbl where a=3",
    34  		out: "select * from tbl where a = 3",
    35  	}, {
    36  		in:  "select * from tbl where a=3 and b=4",
    37  		out: "select * from tbl where a = 3 and b = 4",
    38  	}, {
    39  		in:  "select * from tbl where b=4 and a=3",
    40  		out: "select * from tbl where a = 3 and b = 4",
    41  	}, {
    42  		in:  "select * from tbl where b=4 and c>5 and a=3",
    43  		out: "select * from tbl where a = 3 and b = 4 and c > 5",
    44  	}, {
    45  		in:  "select * from tbl where b=4 or a=3",
    46  		out: "select * from tbl where b = 4 or a = 3",
    47  	}}
    48  
    49  	for _, tc := range testcases {
    50  		normalized, err := NormalizeAlphabetically(tc.in)
    51  		assert.NoError(t, err)
    52  		assert.Equal(t, tc.out, normalized)
    53  	}
    54  }
    55  
    56  func TestQueryMatchesTemplates(t *testing.T) {
    57  	testcases := []struct {
    58  		name string
    59  		q    string
    60  		tmpl []string
    61  		out  bool
    62  	}{
    63  		{
    64  			name: "trivial, identical",
    65  			q:    "select id from tbl",
    66  			tmpl: []string{
    67  				"select id from tbl",
    68  			},
    69  			out: true,
    70  		}, {
    71  			name: "trivial, canonical",
    72  			q:    "select `id` from tbl",
    73  			tmpl: []string{
    74  				"select id FROM `tbl`",
    75  			},
    76  			out: true,
    77  		}, {
    78  			name: "trivial, identical from list",
    79  			q:    "select id from tbl",
    80  			tmpl: []string{
    81  				"select name from tbl",
    82  				"select id from tbl",
    83  			},
    84  			out: true,
    85  		}, {
    86  			name: "trivial no match",
    87  			q:    "select id from tbl where a=3",
    88  			tmpl: []string{
    89  				"select id from tbl",
    90  			},
    91  			out: false,
    92  		}, {
    93  			name: "int value",
    94  			q:    "select id from tbl where a=3",
    95  			tmpl: []string{
    96  				"select name from tbl where a=17",
    97  				"select id from tbl where a=5",
    98  			},
    99  			out: true,
   100  		}, {
   101  			name: "string value",
   102  			q:    "select id from tbl where a='abc'",
   103  			tmpl: []string{
   104  				"select name from tbl where a='x'",
   105  				"select id from tbl where a='y'",
   106  			},
   107  			out: true,
   108  		}, {
   109  			name: "two params",
   110  			q:    "select id from tbl where a='abc' and b='def'",
   111  			tmpl: []string{
   112  				"select name from tbl where a='x' and b = 'y'",
   113  				"select id from tbl where a='x' and b = 'y'",
   114  			},
   115  			out: true,
   116  		}, {
   117  			name: "no match",
   118  			q:    "select id from tbl where a='abc' and b='def'",
   119  			tmpl: []string{
   120  				"select name from tbl where a='x' and b = 'y'",
   121  				"select id from tbl where a='x' and c = 'y'",
   122  			},
   123  			out: false,
   124  		}, {
   125  			name: "reorder AND params",
   126  			q:    "select id from tbl where a='abc' and b='def'",
   127  			tmpl: []string{
   128  				"select id from tbl where b='x' and a = 'y'",
   129  			},
   130  			out: true,
   131  		}, {
   132  			name: "no reorder OR params",
   133  			q:    "select id from tbl where a='abc' or b='def'",
   134  			tmpl: []string{
   135  				"select id from tbl where b='x' or a = 'y'",
   136  			},
   137  			out: false,
   138  		}, {
   139  			name: "strict reorder OR params",
   140  			q:    "select id from tbl where a='abc' or b='def'",
   141  			tmpl: []string{
   142  				"select id from tbl where a='x' or b = 'y'",
   143  			},
   144  			out: true,
   145  		}, {
   146  			name: "identical 'x' annotation in template, identical query values",
   147  			q:    "select id from tbl where a='abc' or b='abc'",
   148  			tmpl: []string{
   149  				"select id from tbl where a='x' or b = 'x'",
   150  			},
   151  			out: true,
   152  		}, {
   153  			name: "identical 'x' annotation in template, different query values",
   154  			q:    "select id from tbl where a='abc' or b='def'",
   155  			tmpl: []string{
   156  				"select id from tbl where a='x' or b = 'x'",
   157  			},
   158  			out: false,
   159  		}, {
   160  			name: "reorder AND params, range test",
   161  			q:    "select id from tbl where a >'abc' and b<3",
   162  			tmpl: []string{
   163  				"select id from tbl where b<17 and a > 'y'",
   164  			},
   165  			out: true,
   166  		}, {
   167  			name: "canonical, case",
   168  			q:    "SHOW BINARY LOGS",
   169  			tmpl: []string{
   170  				"show binary logs",
   171  			},
   172  			out: true,
   173  		},
   174  	}
   175  	for _, tc := range testcases {
   176  		t.Run(tc.name, func(t *testing.T) {
   177  			match, err := QueryMatchesTemplates(tc.q, tc.tmpl)
   178  			assert.NoError(t, err)
   179  			assert.Equal(t, tc.out, match)
   180  		})
   181  	}
   182  }