vitess.io/vitess@v0.16.2/go/vt/vtgate/vindexes/binary_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  	"bytes"
    21  	"context"
    22  	"encoding/hex"
    23  	"fmt"
    24  	"reflect"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/stretchr/testify/require"
    29  
    30  	"vitess.io/vitess/go/sqltypes"
    31  	"vitess.io/vitess/go/vt/key"
    32  )
    33  
    34  var binOnlyVindex SingleColumn
    35  
    36  func init() {
    37  	vindex, _ := CreateVindex("binary", "binary_varchar", nil)
    38  	binOnlyVindex = vindex.(SingleColumn)
    39  }
    40  
    41  func TestBinaryInfo(t *testing.T) {
    42  	assert.Equal(t, 0, binOnlyVindex.Cost())
    43  	assert.Equal(t, "binary_varchar", binOnlyVindex.String())
    44  	assert.True(t, binOnlyVindex.IsUnique())
    45  	assert.False(t, binOnlyVindex.NeedsVCursor())
    46  }
    47  
    48  func TestBinaryMap(t *testing.T) {
    49  	tcases := []struct {
    50  		in  sqltypes.Value
    51  		out []byte
    52  	}{{
    53  		in:  sqltypes.NewVarChar("test1"),
    54  		out: []byte("test1"),
    55  	}, {
    56  		in:  sqltypes.NULL,
    57  		out: []byte(nil),
    58  	}, {
    59  		in:  sqltypes.NewVarChar("test2"),
    60  		out: []byte("test2"),
    61  	}}
    62  	for _, tcase := range tcases {
    63  		got, err := binOnlyVindex.Map(context.Background(), nil, []sqltypes.Value{tcase.in})
    64  		if err != nil {
    65  			t.Error(err)
    66  		}
    67  		out := []byte(got[0].(key.DestinationKeyspaceID))
    68  		if !bytes.Equal(tcase.out, out) {
    69  			t.Errorf("Map(%#v): %#v, want %#v", tcase.in, out, tcase.out)
    70  		}
    71  	}
    72  }
    73  
    74  func TestBinaryVerify(t *testing.T) {
    75  	hexValStr := "8a1e"
    76  	hexValStrSQL := fmt.Sprintf("x'%s'", hexValStr)
    77  	hexNumStrSQL := fmt.Sprintf("0x%s", hexValStr)
    78  	hexBytes, _ := hex.DecodeString(hexValStr)
    79  	ids := []sqltypes.Value{sqltypes.NewVarBinary("1"), sqltypes.NewVarBinary("2"), sqltypes.NewHexVal([]byte(hexValStrSQL)), sqltypes.NewHexNum([]byte(hexNumStrSQL))}
    80  	ksids := [][]byte{[]byte("1"), []byte("1"), hexBytes, hexBytes}
    81  	got, err := binOnlyVindex.Verify(context.Background(), nil, ids, ksids)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	want := []bool{true, false, true, true}
    86  	if !reflect.DeepEqual(got, want) {
    87  		t.Errorf("binary.Verify: %v, want %v", got, want)
    88  	}
    89  }
    90  
    91  func TestBinaryReverseMap(t *testing.T) {
    92  	got, err := binOnlyVindex.(Reversible).ReverseMap(nil, [][]byte{[]byte("\x00\x00\x00\x00\x00\x00\x00\x01")})
    93  	require.NoError(t, err)
    94  	want := []sqltypes.Value{sqltypes.NewVarBinary("\x00\x00\x00\x00\x00\x00\x00\x01")}
    95  	if !reflect.DeepEqual(got, want) {
    96  		t.Errorf("ReverseMap(): %+v, want %+v", got, want)
    97  	}
    98  
    99  	// Negative Test
   100  	_, err = binOnlyVindex.(Reversible).ReverseMap(nil, [][]byte{[]byte(nil)})
   101  	wantErr := "Binary.ReverseMap: keyspaceId is nil"
   102  	if err == nil || err.Error() != wantErr {
   103  		t.Errorf("ReverseMap(): %v, want %s", err, wantErr)
   104  	}
   105  }