vitess.io/vitess@v0.16.2/go/vt/vtgate/vindexes/unicodeloosemd5_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 vindexes
    18  
    19  import (
    20  	"context"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  
    26  	"vitess.io/vitess/go/sqltypes"
    27  	"vitess.io/vitess/go/vt/key"
    28  )
    29  
    30  var charVindexMD5 SingleColumn
    31  
    32  func init() {
    33  	vindex, _ := CreateVindex("unicode_loose_md5", "utf8ch", nil)
    34  	charVindexMD5 = vindex.(SingleColumn)
    35  }
    36  
    37  func TestUnicodeLooseMD5Info(t *testing.T) {
    38  	assert.Equal(t, 1, charVindexMD5.Cost())
    39  	assert.Equal(t, "utf8ch", charVindexMD5.String())
    40  	assert.True(t, charVindexMD5.IsUnique())
    41  	assert.False(t, charVindexMD5.NeedsVCursor())
    42  }
    43  
    44  func TestUnicodeLooseMD5Map(t *testing.T) {
    45  	tcases := []struct {
    46  		in  sqltypes.Value
    47  		out string
    48  	}{{
    49  		in:  sqltypes.NewVarBinary("Test"),
    50  		out: "\v^۴\x01\xfdu$96\x90I\x1dd\xf1\xf5",
    51  	}, {
    52  		in:  sqltypes.NewVarBinary("TEST"),
    53  		out: "\v^۴\x01\xfdu$96\x90I\x1dd\xf1\xf5",
    54  	}, {
    55  		in:  sqltypes.NewVarBinary("Te\u0301st"),
    56  		out: "\v^۴\x01\xfdu$96\x90I\x1dd\xf1\xf5",
    57  	}, {
    58  		in:  sqltypes.NewVarBinary("Tést"),
    59  		out: "\v^۴\x01\xfdu$96\x90I\x1dd\xf1\xf5",
    60  	}, {
    61  		in:  sqltypes.NewVarBinary("Bést"),
    62  		out: "²3.Os\xd0\aA\x02bIpo/\xb6",
    63  	}, {
    64  		in:  sqltypes.NewVarBinary("Test "),
    65  		out: "\v^۴\x01\xfdu$96\x90I\x1dd\xf1\xf5",
    66  	}, {
    67  		in:  sqltypes.NewVarBinary(" Test"),
    68  		out: "\xa2\xe3Q\\~\x8d\xf1\xff\xd2\xcc\xfc\x11Ʊ\x9d\xd1",
    69  	}, {
    70  		in:  sqltypes.NewVarBinary("Test\t"),
    71  		out: "\x82Em\xd8z\x9cz\x02\xb1\xc2\x05kZ\xba\xa2r",
    72  	}, {
    73  		in:  sqltypes.NewVarBinary("TéstLooong"),
    74  		out: "\x96\x83\xe1+\x80C\f\xd4S\xf5\xdfߺ\x81ɥ",
    75  	}, {
    76  		in:  sqltypes.NewVarBinary("T"),
    77  		out: "\xac\x0f\x91y\xf5\x1d\xb8\u007f\xe8\xec\xc0\xcf@ʹz",
    78  	}, {
    79  		in:  sqltypes.NULL,
    80  		out: "\xd4\x1d\x8cُ\x00\xb2\x04\xe9\x80\t\x98\xec\xf8B~",
    81  	}}
    82  	for _, tcase := range tcases {
    83  		got, err := charVindexMD5.Map(context.Background(), nil, []sqltypes.Value{tcase.in})
    84  		if err != nil {
    85  			t.Error(err)
    86  		}
    87  		out := string(got[0].(key.DestinationKeyspaceID))
    88  		if out != tcase.out {
    89  			t.Errorf("Map(%#v): %#v, want %#v", tcase.in, out, tcase.out)
    90  		}
    91  	}
    92  }
    93  
    94  func TestUnicodeLooseMD5Verify(t *testing.T) {
    95  	ids := []sqltypes.Value{sqltypes.NewVarBinary("Test"), sqltypes.NewVarBinary("TEst"), sqltypes.NewVarBinary("different")}
    96  	ksids := [][]byte{[]byte("\v^۴\x01\xfdu$96\x90I\x1dd\xf1\xf5"), []byte("\v^۴\x01\xfdu$96\x90I\x1dd\xf1\xf5"), []byte("\v^۴\x01\xfdu$96\x90I\x1dd\xf1\xf5")}
    97  	got, err := charVindexMD5.Verify(context.Background(), nil, ids, ksids)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  	want := []bool{true, true, false}
   102  	if !reflect.DeepEqual(got, want) {
   103  		t.Errorf("UnicodeLooseMD5.Verify: %v, want %v", got, want)
   104  	}
   105  }