vitess.io/vitess@v0.16.2/go/vt/vtgate/vindexes/numeric_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 numeric SingleColumn 32 33 func init() { 34 vindex, _ := CreateVindex("numeric", "num", nil) 35 numeric = vindex.(SingleColumn) 36 } 37 38 func TestNumericInfo(t *testing.T) { 39 assert.Equal(t, 0, numeric.Cost()) 40 assert.Equal(t, "num", numeric.String()) 41 assert.True(t, numeric.IsUnique()) 42 assert.False(t, numeric.NeedsVCursor()) 43 } 44 45 func TestNumericMap(t *testing.T) { 46 got, err := numeric.Map(context.Background(), nil, []sqltypes.Value{ 47 sqltypes.NewInt64(1), 48 sqltypes.NewInt64(2), 49 sqltypes.NewInt64(3), 50 sqltypes.NewFloat64(1.1), 51 sqltypes.NewInt64(4), 52 sqltypes.NewInt64(5), 53 sqltypes.NewInt64(6), 54 sqltypes.NewInt64(7), 55 sqltypes.NewInt64(8), 56 sqltypes.NewInt32(8), 57 sqltypes.NULL, 58 }) 59 require.NoError(t, err) 60 want := []key.Destination{ 61 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x01")), 62 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x02")), 63 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x03")), 64 key.DestinationNone{}, 65 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x04")), 66 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x05")), 67 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x06")), 68 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x07")), 69 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x08")), 70 key.DestinationKeyspaceID([]byte("\x00\x00\x00\x00\x00\x00\x00\x08")), 71 key.DestinationNone{}, 72 } 73 if !reflect.DeepEqual(got, want) { 74 t.Errorf("Map(): %+v, want %+v", got, want) 75 } 76 } 77 78 func TestNumericVerify(t *testing.T) { 79 got, err := numeric.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")}) 80 require.NoError(t, err) 81 want := []bool{true, false} 82 if !reflect.DeepEqual(got, want) { 83 t.Errorf("lhu.Verify(match): %v, want %v", got, want) 84 } 85 86 // Failure test 87 _, err = numeric.Verify(context.Background(), nil, []sqltypes.Value{sqltypes.NewVarBinary("aa")}, [][]byte{nil}) 88 require.EqualError(t, err, "could not parse value: 'aa'") 89 } 90 91 func TestNumericReverseMap(t *testing.T) { 92 got, err := numeric.(Reversible).ReverseMap(nil, [][]byte{[]byte("\x00\x00\x00\x00\x00\x00\x00\x01")}) 93 require.NoError(t, err) 94 want := []sqltypes.Value{sqltypes.NewUint64(1)} 95 if !reflect.DeepEqual(got, want) { 96 t.Errorf("ReverseMap(): %v, want %v", got, want) 97 } 98 } 99 100 func TestNumericReverseMapBadData(t *testing.T) { 101 _, err := numeric.(Reversible).ReverseMap(nil, [][]byte{[]byte("aa")}) 102 want := `Numeric.ReverseMap: length of keyspaceId is not 8: 2` 103 if err == nil || err.Error() != want { 104 t.Errorf("numeric.Map: %v, want %v", err, want) 105 } 106 }