vitess.io/vitess@v0.16.2/go/vt/vtgate/engine/comparer_test.go (about)

     1  /*
     2  Copyright 2021 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 engine
    18  
    19  import (
    20  	"strconv"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  
    25  	"vitess.io/vitess/go/sqltypes"
    26  )
    27  
    28  func TestComparer(t *testing.T) {
    29  	tests := []struct {
    30  		comparer comparer
    31  		row1     []sqltypes.Value
    32  		row2     []sqltypes.Value
    33  		output   int
    34  	}{
    35  		{
    36  			comparer: comparer{
    37  				orderBy:      0,
    38  				weightString: -1,
    39  				desc:         true,
    40  			},
    41  			row1: []sqltypes.Value{
    42  				sqltypes.NewInt64(23),
    43  			},
    44  			row2: []sqltypes.Value{
    45  				sqltypes.NewInt64(34),
    46  			},
    47  			output: 1,
    48  		}, {
    49  			comparer: comparer{
    50  				orderBy:      0,
    51  				weightString: -1,
    52  				desc:         false,
    53  			},
    54  			row1: []sqltypes.Value{
    55  				sqltypes.NewInt64(23),
    56  			},
    57  			row2: []sqltypes.Value{
    58  				sqltypes.NewInt64(23),
    59  			},
    60  			output: 0,
    61  		}, {
    62  			comparer: comparer{
    63  				orderBy:      0,
    64  				weightString: -1,
    65  				desc:         false,
    66  			},
    67  			row1: []sqltypes.Value{
    68  				sqltypes.NewInt64(23),
    69  			},
    70  			row2: []sqltypes.Value{
    71  				sqltypes.NewInt64(12),
    72  			},
    73  			output: 1,
    74  		}, {
    75  			comparer: comparer{
    76  				orderBy:      1,
    77  				weightString: 0,
    78  				desc:         false,
    79  			},
    80  			row1: []sqltypes.Value{
    81  				sqltypes.NewInt64(23),
    82  				sqltypes.NewVarChar("b"),
    83  			},
    84  			row2: []sqltypes.Value{
    85  				sqltypes.NewInt64(34),
    86  				sqltypes.NewVarChar("a"),
    87  			},
    88  			output: -1,
    89  		}, {
    90  			comparer: comparer{
    91  				orderBy:      1,
    92  				weightString: 0,
    93  				desc:         true,
    94  			},
    95  			row1: []sqltypes.Value{
    96  				sqltypes.NewInt64(23),
    97  				sqltypes.NewVarChar("A"),
    98  			},
    99  			row2: []sqltypes.Value{
   100  				sqltypes.NewInt64(23),
   101  				sqltypes.NewVarChar("a"),
   102  			},
   103  			output: 0,
   104  		},
   105  	}
   106  
   107  	for i, test := range tests {
   108  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   109  			got, err := test.comparer.compare(test.row1, test.row2)
   110  			require.NoError(t, err)
   111  			require.Equal(t, test.output, got)
   112  		})
   113  	}
   114  }