vitess.io/vitess@v0.16.2/go/vt/vtexplain/vtexplain_vttablet_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 vtexplain
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"github.com/stretchr/testify/require"
    25  
    26  	"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
    27  
    28  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    29  )
    30  
    31  func TestRun(t *testing.T) {
    32  	testVSchema := `
    33  {
    34  	"test_keyspace": {
    35  		"sharded": false,
    36  		"tables": {
    37  			"t1": {
    38  				"columns": [
    39  					{ "name": "id", "type": "INT64" }
    40  				],
    41  				"column_list_authoritative": true
    42  			},
    43  			"t2": {
    44  				"columns": [
    45  					{ "name": "id", "type": "INT32" }
    46  				],
    47  				"column_list_authoritative": true
    48  			}
    49  		}
    50  	}
    51  }
    52  `
    53  
    54  	testSchema := `
    55  create table t1 (
    56  	id bigint unsigned not null
    57  );
    58  
    59  create table t2 (
    60  	id int unsigned not null
    61  );
    62  `
    63  
    64  	opts := &Options{
    65  		ExecutionMode:   "multi",
    66  		ReplicationMode: "ROW",
    67  		NumShards:       2,
    68  	}
    69  
    70  	vte, err := Init(testVSchema, testSchema, "", opts)
    71  	require.NoError(t, err)
    72  	defer vte.Stop()
    73  
    74  	sql := "SELECT * FROM t1 INNER JOIN t2 ON t1.id = t2.id"
    75  
    76  	_, err = vte.Run(sql)
    77  	require.NoError(t, err)
    78  }
    79  
    80  func TestParseSchema(t *testing.T) {
    81  	testSchema := `
    82  create table t1 (
    83  	id bigint(20) unsigned not null default 123,
    84  	val varchar default "default",
    85  	primary key (id)
    86  );
    87  
    88  create table t2 (
    89  	val text default "default2"
    90  );
    91  
    92  create table t3 (
    93      b bit(1) default B'0'
    94  );
    95  
    96  create table t4 like t3;
    97  
    98  create table t5 (like t2);
    99  
   100  create table t1_seq(
   101    id int,
   102    next_id bigint,
   103    cache bigint,
   104    primary key(id)
   105  ) comment 'vitess_sequence';
   106  
   107  create table test_partitioned (
   108  	id bigint,
   109  	date_create int,
   110  	primary key(id)
   111  ) Engine=InnoDB	/*!50100 PARTITION BY RANGE (date_create)
   112  	(PARTITION p2018_06_14 VALUES LESS THAN (1528959600) ENGINE = InnoDB,
   113  	PARTITION p2018_06_15 VALUES LESS THAN (1529046000) ENGINE = InnoDB,
   114  	PARTITION p2018_06_16 VALUES LESS THAN (1529132400) ENGINE = InnoDB,
   115  	PARTITION p2018_06_17 VALUES LESS THAN (1529218800) ENGINE = InnoDB)*/;
   116  `
   117  
   118  	ddls, err := parseSchema(testSchema, &Options{StrictDDL: false})
   119  	if err != nil {
   120  		t.Fatalf("parseSchema: %v", err)
   121  	}
   122  	vte := initTest(ModeMulti, defaultTestOpts(), &testopts{}, t)
   123  
   124  	tabletEnv, _ := newTabletEnvironment(ddls, defaultTestOpts())
   125  	vte.setGlobalTabletEnv(tabletEnv)
   126  
   127  	tablet := vte.newTablet(defaultTestOpts(), &topodatapb.Tablet{
   128  		Keyspace: "test_keyspace",
   129  		Shard:    "-80",
   130  		Alias:    &topodatapb.TabletAlias{},
   131  	})
   132  	se := tablet.tsv.SchemaEngine()
   133  	tables := se.GetSchema()
   134  
   135  	t1 := tables["t1"]
   136  	require.NotNil(t, t1, "table t1 wasn't parsed properly")
   137  
   138  	wantCols := `[{"name":"id","type":778},{"name":"val","type":6165}]`
   139  	got, _ := json.Marshal(t1.Fields)
   140  	assert.Equal(t, wantCols, string(got))
   141  
   142  	if !t1.HasPrimary() || len(t1.PKColumns) != 1 || t1.PKColumns[0] != 0 {
   143  		t.Errorf("expected HasPrimary && t1.PKColumns == [0] got %v", t1.PKColumns)
   144  	}
   145  	pkCol := t1.GetPKColumn(0)
   146  	if pkCol == nil || pkCol.String() != `name:"id" type:UINT64` {
   147  		t.Errorf("expected pkCol[0] == id, got %v", pkCol)
   148  	}
   149  
   150  	t2 := tables["t2"]
   151  	require.NotNil(t, t2, "table t2 wasn't parsed properly")
   152  
   153  	wantCols = `[{"name":"val","type":6163}]`
   154  	got, _ = json.Marshal(t2.Fields)
   155  	assert.Equal(t, wantCols, string(got))
   156  
   157  	if t2.HasPrimary() || len(t2.PKColumns) != 0 {
   158  		t.Errorf("expected !HasPrimary && t2.PKColumns == [] got %v", t2.PKColumns)
   159  	}
   160  
   161  	t5 := tables["t5"]
   162  	require.NotNil(t, t5, "table t5 wasn't parsed properly")
   163  	got, _ = json.Marshal(t5.Fields)
   164  	assert.Equal(t, wantCols, string(got))
   165  
   166  	if t5.HasPrimary() || len(t5.PKColumns) != 0 {
   167  		t.Errorf("expected !HasPrimary && t5.PKColumns == [] got %v", t5.PKColumns)
   168  	}
   169  
   170  	seq := tables["t1_seq"]
   171  	require.NotNil(t, seq)
   172  	assert.Equal(t, schema.Sequence, seq.Type)
   173  }
   174  
   175  func TestErrParseSchema(t *testing.T) {
   176  	testSchema := `create table t1 like t2`
   177  	ddl, err := parseSchema(testSchema, &Options{StrictDDL: true})
   178  	require.NoError(t, err)
   179  
   180  	_, err = newTabletEnvironment(ddl, defaultTestOpts())
   181  	require.Error(t, err, "check your schema, table[t2] doesn't exist")
   182  }