vitess.io/vitess@v0.16.2/go/vt/vtgate/vindexes/hash_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 "github.com/stretchr/testify/require" 26 27 "vitess.io/vitess/go/sqltypes" 28 "vitess.io/vitess/go/vt/key" 29 ) 30 31 var hash SingleColumn 32 33 func init() { 34 hv, err := CreateVindex("hash", "nn", map[string]string{"Table": "t", "Column": "c"}) 35 if err != nil { 36 panic(err) 37 } 38 hash = hv.(SingleColumn) 39 } 40 41 func TestHashInfo(t *testing.T) { 42 assert.Equal(t, 1, hash.Cost()) 43 assert.Equal(t, "nn", hash.String()) 44 assert.True(t, hash.IsUnique()) 45 assert.False(t, hash.NeedsVCursor()) 46 } 47 48 func TestHashMap(t *testing.T) { 49 got, err := hash.Map(context.Background(), nil, []sqltypes.Value{ 50 sqltypes.NewInt64(1), 51 sqltypes.NewInt64(2), 52 sqltypes.NewInt64(3), 53 sqltypes.NULL, 54 sqltypes.NewInt64(4), 55 sqltypes.NewInt64(5), 56 sqltypes.NewInt64(6), 57 sqltypes.NewInt64(0), 58 sqltypes.NewInt64(-1), 59 sqltypes.NewUint64(18446744073709551615), // 2^64 - 1 60 sqltypes.NewInt64(9223372036854775807), // 2^63 - 1 61 sqltypes.NewUint64(9223372036854775807), // 2^63 - 1 62 sqltypes.NewInt64(-9223372036854775808), // - 2^63 63 }) 64 require.NoError(t, err) 65 want := []key.Destination{ 66 key.DestinationKeyspaceID([]byte("\x16k@\xb4J\xbaK\xd6")), 67 key.DestinationKeyspaceID([]byte("\x06\xe7\xea\"Βp\x8f")), 68 key.DestinationKeyspaceID([]byte("N\xb1\x90ɢ\xfa\x16\x9c")), 69 key.DestinationNone{}, 70 key.DestinationKeyspaceID([]byte("\xd2\xfd\x88g\xd5\r-\xfe")), 71 key.DestinationKeyspaceID([]byte("p\xbb\x02<\x81\f\xa8z")), 72 key.DestinationKeyspaceID([]byte("\xf0\x98H\n\xc4ľq")), 73 key.DestinationKeyspaceID([]byte("\x8c\xa6M\xe9\xc1\xb1#\xa7")), 74 key.DestinationKeyspaceID([]byte("5UP\xb2\x15\x0e$Q")), 75 key.DestinationKeyspaceID([]byte("5UP\xb2\x15\x0e$Q")), 76 key.DestinationKeyspaceID([]byte("\xf7}H\xaaݡ\xf1\xbb")), 77 key.DestinationKeyspaceID([]byte("\xf7}H\xaaݡ\xf1\xbb")), 78 key.DestinationKeyspaceID([]byte("\x95\xf8\xa5\xe5\xdd1\xd9\x00")), 79 } 80 if !reflect.DeepEqual(got, want) { 81 for i, v := range got { 82 if v.String() != want[i].String() { 83 t.Errorf("Map() %d: %#v, want %#v", i, v, want[i]) 84 } 85 } 86 } 87 } 88 89 func TestHashVerify(t *testing.T) { 90 ids := []sqltypes.Value{sqltypes.NewInt64(1), sqltypes.NewInt64(2)} 91 ksids := [][]byte{[]byte("\x16k@\xb4J\xbaK\xd6"), []byte("\x16k@\xb4J\xbaK\xd6")} 92 got, err := hash.Verify(context.Background(), nil, ids, ksids) 93 if err != nil { 94 t.Fatal(err) 95 } 96 want := []bool{true, false} 97 if !reflect.DeepEqual(got, want) { 98 t.Errorf("binaryMD5.Verify: %v, want %v", got, want) 99 } 100 101 // Failure test 102 _, err = hash.Verify(context.Background(), nil, []sqltypes.Value{sqltypes.NewVarBinary("aa")}, [][]byte{nil}) 103 require.EqualError(t, err, "could not parse value: 'aa'") 104 } 105 106 func TestHashReverseMap(t *testing.T) { 107 got, err := hash.(Reversible).ReverseMap(nil, [][]byte{ 108 []byte("\x16k@\xb4J\xbaK\xd6"), 109 []byte("\x06\xe7\xea\"Βp\x8f"), 110 []byte("N\xb1\x90ɢ\xfa\x16\x9c"), 111 []byte("\xd2\xfd\x88g\xd5\r-\xfe"), 112 []byte("p\xbb\x02<\x81\f\xa8z"), 113 []byte("\xf0\x98H\n\xc4ľq"), 114 []byte("\x8c\xa6M\xe9\xc1\xb1#\xa7"), 115 []byte("5UP\xb2\x15\x0e$Q"), 116 []byte("5UP\xb2\x15\x0e$Q"), 117 []byte("\xf7}H\xaaݡ\xf1\xbb"), 118 []byte("\xf7}H\xaaݡ\xf1\xbb"), 119 []byte("\x95\xf8\xa5\xe5\xdd1\xd9\x00"), 120 }) 121 require.NoError(t, err) 122 neg1 := int64(-1) 123 negmax := int64(-9223372036854775808) 124 want := []sqltypes.Value{ 125 sqltypes.NewUint64(uint64(1)), 126 sqltypes.NewUint64(2), 127 sqltypes.NewUint64(3), 128 sqltypes.NewUint64(4), 129 sqltypes.NewUint64(5), 130 sqltypes.NewUint64(6), 131 sqltypes.NewUint64(0), 132 sqltypes.NewUint64(uint64(neg1)), 133 sqltypes.NewUint64(18446744073709551615), // 2^64 - 1 134 sqltypes.NewUint64(9223372036854775807), // 2^63 - 1 135 sqltypes.NewUint64(9223372036854775807), // 2^63 - 1 136 sqltypes.NewUint64(uint64(negmax)), // - 2^63 137 } 138 if !reflect.DeepEqual(got, want) { 139 t.Errorf("ReverseMap(): %v, want %v", got, want) 140 } 141 } 142 143 func TestHashReverseMapNeg(t *testing.T) { 144 _, err := hash.(Reversible).ReverseMap(nil, [][]byte{[]byte("\x16k@\xb4J\xbaK\xd6\x16k@\xb4J\xbaK\xd6")}) 145 want := "invalid keyspace id: 166b40b44aba4bd6166b40b44aba4bd6" 146 if err.Error() != want { 147 t.Error(err) 148 } 149 }