vitess.io/vitess@v0.16.2/go/vt/vtgate/vindexes/region_experimental_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  	"strconv"
    22  	"testing"
    23  
    24  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"vitess.io/vitess/go/sqltypes"
    30  	"vitess.io/vitess/go/vt/key"
    31  )
    32  
    33  func TestRegionExperimentalMisc(t *testing.T) {
    34  	ge, err := createRegionVindex(t, "region_experimental", "f1,f2", 1)
    35  	require.NoError(t, err)
    36  	assert.Equal(t, 1, ge.Cost())
    37  	assert.Equal(t, "region_experimental", ge.String())
    38  	assert.True(t, ge.IsUnique())
    39  	assert.False(t, ge.NeedsVCursor())
    40  }
    41  
    42  func TestRegionExperimentalMap(t *testing.T) {
    43  	vindex, err := createRegionVindex(t, "region_experimental", "f1,f2", 1)
    44  	assert.NoError(t, err)
    45  	ge := vindex.(MultiColumn)
    46  	got, err := ge.Map(context.Background(), nil, [][]sqltypes.Value{{
    47  		sqltypes.NewInt64(1), sqltypes.NewInt64(1),
    48  	}, {
    49  		sqltypes.NewInt64(255), sqltypes.NewInt64(1),
    50  	}, {
    51  		sqltypes.NewInt64(256), sqltypes.NewInt64(1),
    52  	}, {
    53  		// only region id provided, partial column for key range mapping.
    54  		sqltypes.NewInt64(1),
    55  	}, {
    56  		// Invalid region.
    57  		sqltypes.NewVarBinary("abcd"), sqltypes.NewInt64(256),
    58  	}, {
    59  		// Invalid id.
    60  		sqltypes.NewInt64(1), sqltypes.NewVarBinary("abcd"),
    61  	}})
    62  	assert.NoError(t, err)
    63  
    64  	want := []key.Destination{
    65  		key.DestinationKeyspaceID([]byte("\x01\x16k@\xb4J\xbaK\xd6")),
    66  		key.DestinationKeyspaceID([]byte("\xff\x16k@\xb4J\xbaK\xd6")),
    67  		key.DestinationKeyspaceID([]byte("\x00\x16k@\xb4J\xbaK\xd6")),
    68  		key.DestinationKeyRange{KeyRange: &topodatapb.KeyRange{Start: []byte("\x01"), End: []byte("\x02")}},
    69  		key.DestinationNone{},
    70  		key.DestinationNone{},
    71  	}
    72  	assert.Equal(t, want, got)
    73  }
    74  
    75  func TestRegionExperimentalMapMulti2(t *testing.T) {
    76  	vindex, err := createRegionVindex(t, "region_experimental", "f1,f2", 2)
    77  	assert.NoError(t, err)
    78  	ge := vindex.(MultiColumn)
    79  	got, err := ge.Map(context.Background(), nil, [][]sqltypes.Value{{
    80  		sqltypes.NewInt64(1), sqltypes.NewInt64(1),
    81  	}, {
    82  		sqltypes.NewInt64(255), sqltypes.NewInt64(1),
    83  	}, {
    84  		sqltypes.NewInt64(256), sqltypes.NewInt64(1),
    85  	}, {
    86  		sqltypes.NewInt64(0x10000), sqltypes.NewInt64(1),
    87  	}})
    88  	assert.NoError(t, err)
    89  
    90  	want := []key.Destination{
    91  		key.DestinationKeyspaceID([]byte("\x00\x01\x16k@\xb4J\xbaK\xd6")),
    92  		key.DestinationKeyspaceID([]byte("\x00\xff\x16k@\xb4J\xbaK\xd6")),
    93  		key.DestinationKeyspaceID([]byte("\x01\x00\x16k@\xb4J\xbaK\xd6")),
    94  		key.DestinationKeyspaceID([]byte("\x00\x00\x16k@\xb4J\xbaK\xd6")),
    95  	}
    96  	assert.Equal(t, want, got)
    97  }
    98  
    99  func TestRegionExperimentalVerifyMulti(t *testing.T) {
   100  	vindex, err := createRegionVindex(t, "region_experimental", "f1,f2", 1)
   101  	assert.NoError(t, err)
   102  	ge := vindex.(MultiColumn)
   103  	vals := [][]sqltypes.Value{{
   104  		// One for match
   105  		sqltypes.NewInt64(1), sqltypes.NewInt64(1),
   106  	}, {
   107  		// One for mismatch
   108  		sqltypes.NewInt64(1), sqltypes.NewInt64(1),
   109  	}, {
   110  		// One invalid value
   111  		sqltypes.NewInt64(1),
   112  	}}
   113  	ksids := [][]byte{
   114  		[]byte("\x01\x16k@\xb4J\xbaK\xd6"),
   115  		[]byte("no match"),
   116  		[]byte(""),
   117  	}
   118  
   119  	want := []bool{true, false, false}
   120  	got, err := ge.Verify(context.Background(), nil, vals, ksids)
   121  	assert.NoError(t, err)
   122  	assert.Equal(t, want, got)
   123  }
   124  
   125  func TestRegionExperimentalCreateErrors(t *testing.T) {
   126  	_, err := createRegionVindex(t, "region_experimental", "f1,f2", 3)
   127  	assert.EqualError(t, err, "region_bits must be 1 or 2: 3")
   128  	_, err = CreateVindex("region_experimental", "region_experimental", nil)
   129  	assert.EqualError(t, err, "region_experimental missing region_bytes param")
   130  }
   131  
   132  func createRegionVindex(t *testing.T, name, from string, rb int) (Vindex, error) {
   133  	return CreateVindex(name, name, map[string]string{
   134  		"region_bytes": strconv.Itoa(rb),
   135  		"table":        "t",
   136  		"from":         from,
   137  		"to":           "toc",
   138  	})
   139  }