vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/vstreamer/local_vschema_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 vstreamer
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	vschemapb "vitess.io/vitess/go/vt/proto/vschema"
    26  	"vitess.io/vitess/go/vt/vtgate/vindexes"
    27  )
    28  
    29  func TestFindColVindex(t *testing.T) {
    30  	testSrvVSchema := &vschemapb.SrvVSchema{
    31  		Keyspaces: map[string]*vschemapb.Keyspace{
    32  			"ks1": {
    33  				Sharded: true,
    34  				Vindexes: map[string]*vschemapb.Vindex{
    35  					"hash": {
    36  						Type: "hash",
    37  					},
    38  					"lookup_unique": {
    39  						Type: "lookup_unique",
    40  						Params: map[string]string{
    41  							"table": "t",
    42  							"from":  "fromc",
    43  							"to":    "toc",
    44  						},
    45  					},
    46  					"lookup": {
    47  						Type: "lookup",
    48  						Params: map[string]string{
    49  							"table": "t",
    50  							"from":  "fromc",
    51  							"to":    "toc",
    52  						},
    53  					},
    54  					"numeric": {
    55  						Type: "numeric",
    56  					},
    57  				},
    58  				Tables: map[string]*vschemapb.Table{
    59  					"t1": {
    60  						ColumnVindexes: []*vschemapb.ColumnVindex{{
    61  							Name:    "hash",
    62  							Columns: []string{"id"},
    63  						}},
    64  					},
    65  					"nogoodvindex": {
    66  						ColumnVindexes: []*vschemapb.ColumnVindex{{
    67  							Name:    "lookup_unique",
    68  							Columns: []string{"id"},
    69  						}},
    70  					},
    71  					"cheapest": {
    72  						ColumnVindexes: []*vschemapb.ColumnVindex{{
    73  							Name:    "hash",
    74  							Columns: []string{"id"},
    75  						}, {
    76  							Name:    "numeric",
    77  							Columns: []string{"id"},
    78  						}},
    79  					},
    80  				},
    81  			},
    82  			"unsharded": {
    83  				Tables: map[string]*vschemapb.Table{
    84  					"t1": {},
    85  				},
    86  			},
    87  		},
    88  	}
    89  	vschema := vindexes.BuildVSchema(testSrvVSchema)
    90  
    91  	testcases := []struct {
    92  		keyspace   string
    93  		tablename  string
    94  		vindexname string
    95  		err        string
    96  	}{{
    97  		keyspace:   "ks1",
    98  		tablename:  "t1",
    99  		vindexname: "hash",
   100  	}, {
   101  		keyspace:  "ks1",
   102  		tablename: "nogoodvindex",
   103  		err:       "could not find a vindex to compute keyspace id for table nogoodvindex",
   104  	}, {
   105  		keyspace:   "ks1",
   106  		tablename:  "cheapest",
   107  		vindexname: "numeric",
   108  	}, {
   109  		keyspace:  "unsharded",
   110  		tablename: "t1",
   111  		err:       "table t1 has no vindex",
   112  	}}
   113  	for _, tcase := range testcases {
   114  		lvs := &localVSchema{
   115  			keyspace: tcase.keyspace,
   116  			vschema:  vschema,
   117  		}
   118  		cv, err := lvs.FindColVindex(tcase.tablename)
   119  		if err != nil {
   120  			assert.EqualError(t, err, tcase.err, tcase.tablename)
   121  			continue
   122  		}
   123  		assert.NoError(t, err, tcase.tablename)
   124  		assert.Equal(t, cv.Name, tcase.vindexname, tcase.tablename)
   125  	}
   126  }
   127  
   128  func TestFindOrCreateVindex(t *testing.T) {
   129  	testSrvVSchema := &vschemapb.SrvVSchema{
   130  		Keyspaces: map[string]*vschemapb.Keyspace{
   131  			"ks1": {
   132  				Sharded: true,
   133  				Vindexes: map[string]*vschemapb.Vindex{
   134  					"duphash": {
   135  						Type: "hash",
   136  					},
   137  				},
   138  			},
   139  			"ks2": {
   140  				Sharded: true,
   141  				Vindexes: map[string]*vschemapb.Vindex{
   142  					"duphash": {
   143  						Type: "hash",
   144  					},
   145  					"otherhash": {
   146  						Type: "hash",
   147  					},
   148  				},
   149  			},
   150  		},
   151  	}
   152  	vschema := vindexes.BuildVSchema(testSrvVSchema)
   153  
   154  	lvs := &localVSchema{
   155  		keyspace: "ks1",
   156  		vschema:  vschema,
   157  	}
   158  
   159  	testcases := []struct {
   160  		name string
   161  		err  string
   162  	}{{
   163  		name: "otherhash",
   164  		err:  "",
   165  	}, {
   166  		name: "ks1.duphash",
   167  		err:  "",
   168  	}, {
   169  		name: "hash",
   170  		err:  "",
   171  	}, {
   172  		name: "a.b.c",
   173  		err:  "invalid vindex name: a.b.c",
   174  	}, {
   175  		name: "duphash",
   176  		err:  "ambiguous vindex reference: duphash",
   177  	}, {
   178  		name: "ks1.hash",
   179  		err:  "vindex ks1.hash not found",
   180  	}, {
   181  		name: "none",
   182  		err:  `vindexType "none" not found`,
   183  	}}
   184  	for _, tcase := range testcases {
   185  		vindex, err := lvs.FindOrCreateVindex(tcase.name)
   186  		if err != nil {
   187  			assert.EqualError(t, err, tcase.err, tcase.name)
   188  			continue
   189  		}
   190  		assert.NoError(t, err, tcase.name)
   191  		splits := strings.Split(tcase.name, ".")
   192  		want := splits[len(splits)-1]
   193  		assert.Equal(t, vindex.String(), want, tcase.name)
   194  	}
   195  }
   196  
   197  func TestFindTable(t *testing.T) {
   198  	testSrvVSchema := &vschemapb.SrvVSchema{
   199  		Keyspaces: map[string]*vschemapb.Keyspace{
   200  			"ks1": {
   201  				Tables: map[string]*vschemapb.Table{
   202  					"t1": {},
   203  				},
   204  			},
   205  		},
   206  	}
   207  	vschema := vindexes.BuildVSchema(testSrvVSchema)
   208  
   209  	testcases := []struct {
   210  		keyspace  string
   211  		tablename string
   212  		err       string
   213  	}{{
   214  		keyspace:  "ks1",
   215  		tablename: "t1",
   216  		err:       "",
   217  	}, {
   218  		keyspace:  "ks1",
   219  		tablename: "t2",
   220  		err:       "table t2 not found",
   221  	}, {
   222  		keyspace:  "noks",
   223  		tablename: "t2",
   224  		err:       "keyspace noks not found in vschema",
   225  	}}
   226  	for _, tcase := range testcases {
   227  		lvs := &localVSchema{
   228  			keyspace: tcase.keyspace,
   229  			vschema:  vschema,
   230  		}
   231  		table, err := lvs.findTable(tcase.tablename)
   232  		if err != nil {
   233  			assert.EqualError(t, err, tcase.err, tcase.keyspace, tcase.tablename)
   234  			continue
   235  		}
   236  		assert.NoError(t, err, tcase.keyspace, tcase.tablename)
   237  		assert.Equal(t, table.Name.String(), tcase.tablename, tcase.keyspace, tcase.tablename)
   238  	}
   239  }