vitess.io/vitess@v0.16.2/go/vt/vtgate/endtoend/deletetest/delete_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 endtoend 18 19 import ( 20 "context" 21 "flag" 22 "fmt" 23 "os" 24 "testing" 25 26 "vitess.io/vitess/go/vt/log" 27 28 "vitess.io/vitess/go/mysql" 29 "vitess.io/vitess/go/sqltypes" 30 vschemapb "vitess.io/vitess/go/vt/proto/vschema" 31 vttestpb "vitess.io/vitess/go/vt/proto/vttest" 32 "vitess.io/vitess/go/vt/vttest" 33 ) 34 35 var ( 36 cluster *vttest.LocalCluster 37 vtParams mysql.ConnParams 38 mysqlParams mysql.ConnParams 39 grpcAddress string 40 41 schema = ` 42 create table t1( 43 id1 bigint, 44 id2 bigint, 45 primary key(id1) 46 ) Engine=InnoDB; 47 48 create table t1_id2_idx( 49 id2 bigint, 50 keyspace_id varbinary(10), 51 primary key(id2) 52 ) Engine=InnoDB; 53 54 create table del_test_a( 55 id bigint, 56 val1 varbinary(16), 57 val2 bigint, 58 primary key(id) 59 ) Engine=InnoDB; 60 61 create table del_test_b( 62 id bigint, 63 val1 varbinary(16), 64 val2 bigint, 65 primary key(id) 66 ) Engine=InnoDB; 67 ` 68 69 vschema = &vschemapb.Keyspace{ 70 Sharded: false, 71 Vindexes: map[string]*vschemapb.Vindex{ 72 "hash": { 73 Type: "hash", 74 }, 75 "t1_id2_vdx": { 76 Type: "consistent_lookup_unique", 77 Params: map[string]string{ 78 "table": "t1_id2_idx", 79 "from": "id2", 80 "to": "keyspace_id", 81 }, 82 Owner: "t1", 83 }, 84 }, 85 Tables: map[string]*vschemapb.Table{ 86 "t1": { 87 ColumnVindexes: []*vschemapb.ColumnVindex{{ 88 Column: "id1", 89 Name: "hash", 90 }, { 91 Column: "id2", 92 Name: "t1_id2_vdx", 93 }}, 94 }, 95 "t1_id2_idx": { 96 ColumnVindexes: []*vschemapb.ColumnVindex{{ 97 Column: "id2", 98 Name: "hash", 99 }}, 100 }, 101 "del_test_a": { 102 ColumnVindexes: []*vschemapb.ColumnVindex{{ 103 Column: "id", 104 Name: "hash", 105 }}, 106 }, 107 "del_test_b": { 108 ColumnVindexes: []*vschemapb.ColumnVindex{{ 109 Column: "id", 110 Name: "hash", 111 }}, 112 }, 113 }, 114 } 115 ) 116 117 func TestMain(m *testing.M) { 118 flag.Parse() 119 120 exitCode := func() int { 121 var cfg vttest.Config 122 cfg.Topology = &vttestpb.VTTestTopology{ 123 Keyspaces: []*vttestpb.Keyspace{{ 124 Name: "ks", 125 Shards: []*vttestpb.Shard{{ 126 Name: "80", 127 }}, 128 }}, 129 } 130 if err := cfg.InitSchemas("ks", schema, vschema); err != nil { 131 fmt.Fprintf(os.Stderr, "%v\n", err) 132 os.RemoveAll(cfg.SchemaDir) 133 return 1 134 } 135 defer os.RemoveAll(cfg.SchemaDir) 136 137 cluster = &vttest.LocalCluster{ 138 Config: cfg, 139 } 140 if err := cluster.Setup(); err != nil { 141 fmt.Fprintf(os.Stderr, "%v\n", err) 142 //log error 143 if err := cluster.TearDown(); err != nil { 144 log.Errorf("cluster.TearDown() did not work: ", err) 145 } 146 return 1 147 } 148 defer cluster.TearDown() 149 150 vtParams = mysql.ConnParams{ 151 Host: "localhost", 152 Port: cluster.Env.PortForProtocol("vtcombo_mysql_port", ""), 153 } 154 mysqlParams = cluster.MySQLConnParams() 155 grpcAddress = fmt.Sprintf("localhost:%d", cluster.Env.PortForProtocol("vtcombo", "grpc")) 156 157 return m.Run() 158 }() 159 os.Exit(exitCode) 160 } 161 162 func TestDelete(t *testing.T) { 163 ctx := context.Background() 164 conn, err := mysql.Connect(ctx, &vtParams) 165 if err != nil { 166 t.Fatal(err) 167 } 168 defer conn.Close() 169 170 exec(t, conn, "insert into del_test_a(id, val1, val2) values(1,'a',1), (2,'a',1), (3,'b',1), (4,'c',3), (5,'c',4)") 171 exec(t, conn, "insert into del_test_b(id, val1, val2) values(1,'a',1), (2,'a',1), (3,'a',1), (4,'d',3), (5,'f',4)") 172 173 qr := exec(t, conn, "delete a.*, b.* from del_test_a a, del_test_b b where a.id = b.id and b.val1 = 'a' and a.val1 = 'a'") 174 if got, want := fmt.Sprintf("%v", qr.Rows), `[]`; got != want { 175 t.Errorf("select:\n%v want\n%v", got, want) 176 } 177 178 qr = exec(t, conn, "select * from del_test_a") 179 if got, want := fmt.Sprintf("%v", qr.Rows), `[[INT64(3) VARBINARY("b") INT64(1)] [INT64(4) VARBINARY("c") INT64(3)] [INT64(5) VARBINARY("c") INT64(4)]]`; got != want { 180 t.Errorf("select:\n%v want\n%v", got, want) 181 } 182 } 183 184 func exec(t *testing.T, conn *mysql.Conn, query string) *sqltypes.Result { 185 t.Helper() 186 qr, err := conn.ExecuteFetch(query, 1000, true) 187 if err != nil { 188 t.Fatal(err) 189 } 190 return qr 191 }