vitess.io/vitess@v0.16.2/go/vt/vtgate/vindexes/numeric_static_map_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 // createVindex creates the "numeric_static_map" vindex object which is used by 32 // each test. 33 func createVindex() (SingleColumn, error) { 34 m := make(map[string]string) 35 m["json_path"] = "testdata/numeric_static_map_test.json" 36 vindex, err := CreateVindex("numeric_static_map", "numericStaticMap", m) 37 if err != nil { 38 panic(err) 39 } 40 return vindex.(SingleColumn), nil 41 } 42 43 func TestNumericStaticMapInfo(t *testing.T) { 44 numericStaticMap, err := createVindex() 45 require.NoError(t, err) 46 assert.Equal(t, 1, numericStaticMap.Cost()) 47 assert.Equal(t, "numericStaticMap", numericStaticMap.String()) 48 assert.True(t, numericStaticMap.IsUnique()) 49 assert.False(t, numericStaticMap.NeedsVCursor()) 50 } 51 52 func TestNumericStaticMapMap(t *testing.T) { 53 numericStaticMap, err := createVindex() 54 if err != nil { 55 t.Fatalf("failed to create vindex: %v", err) 56 } 57 got, err := numericStaticMap.Map(context.Background(), nil, []sqltypes.Value{ 58 sqltypes.NewInt64(1), 59 sqltypes.NewInt64(2), 60 sqltypes.NewInt64(3), 61 sqltypes.NewFloat64(1.1), 62 sqltypes.NewInt64(4), 63 sqltypes.NewInt64(5), 64 sqltypes.NewInt64(6), 65 sqltypes.NewInt64(7), 66 sqltypes.NewInt64(8), 67 sqltypes.NULL, 68 }) 69 require.NoError(t, err) 70 71 // in the third slice, we expect 2 instead of 3 as numeric_static_map_test.json 72 // has 3 mapped to 2 73 want := []key.Destination{ 74 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x01")), 75 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x02")), 76 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x02")), 77 key.DestinationNone{}, 78 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x04")), 79 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x05")), 80 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x06")), 81 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x07")), 82 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x08")), 83 key.DestinationNone{}, 84 } 85 if !reflect.DeepEqual(got, want) { 86 t.Errorf("Map(): %+v, want %+v", got, want) 87 } 88 } 89 90 func TestNumericStaticMapVerify(t *testing.T) { 91 numericStaticMap, err := createVindex() 92 if err != nil { 93 t.Fatalf("failed to create vindex: %v", err) 94 } 95 got, err := numericStaticMap.Verify(context.Background(), nil, []sqltypes.Value{sqltypes.NewInt64(1), sqltypes.NewInt64(2)}, [][]byte{[]byte("\x00\x00\x00\x00\x00\x00\x00\x01"), []byte("\x00\x00\x00\x00\x00\x00\x00\x01")}) 96 require.NoError(t, err) 97 want := []bool{true, false} 98 if !reflect.DeepEqual(got, want) { 99 t.Errorf("lhu.Verify(match): %v, want %v", got, want) 100 } 101 102 // Failure test 103 _, err = numericStaticMap.Verify(context.Background(), nil, []sqltypes.Value{sqltypes.NewVarBinary("aa")}, [][]byte{nil}) 104 require.EqualError(t, err, "could not parse value: 'aa'") 105 }