github.com/XiaoMi/Gaea@v1.2.5/proxy/plan/plan_delete_test.go (about) 1 // Copyright 2019 The Gaea Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package plan 16 17 import "testing" 18 19 func TestMycatShardSimpleDelete(t *testing.T) { 20 ns, err := preparePlanInfo() 21 if err != nil { 22 t.Fatalf("prepare namespace error: %v", err) 23 } 24 25 tests := []SQLTestcase{ 26 { 27 db: "db_mycat", 28 sql: "delete from tbl_mycat", 29 sqls: map[string]map[string][]string{ 30 "slice-0": { 31 "db_mycat_0": {"DELETE FROM `tbl_mycat`"}, 32 "db_mycat_1": {"DELETE FROM `tbl_mycat`"}, 33 }, 34 "slice-1": { 35 "db_mycat_2": {"DELETE FROM `tbl_mycat`"}, 36 "db_mycat_3": {"DELETE FROM `tbl_mycat`"}, 37 }, 38 }, 39 }, 40 } 41 for _, test := range tests { 42 t.Run(test.sql, getTestFunc(ns, test)) 43 } 44 } 45 46 func TestMycatShardDeleteWithWhere(t *testing.T) { 47 ns, err := preparePlanInfo() 48 if err != nil { 49 t.Fatalf("prepare namespace error: %v", err) 50 } 51 52 tests := []SQLTestcase{ 53 { 54 db: "db_mycat", 55 sql: "delete from tbl_mycat where id = 0", 56 sqls: map[string]map[string][]string{ 57 "slice-0": { 58 "db_mycat_0": {"DELETE FROM `tbl_mycat` WHERE `id`=0"}, 59 }, 60 }, 61 }, 62 { // column should be case insensitive 63 db: "db_mycat", 64 sql: "delete from tbl_mycat where ID = 0", 65 sqls: map[string]map[string][]string{ 66 "slice-0": { 67 "db_mycat_0": {"DELETE FROM `tbl_mycat` WHERE `ID`=0"}, 68 }, 69 }, 70 }, 71 { 72 db: "db_mycat", 73 sql: "delete from tbl_mycat where id = 1", 74 sqls: map[string]map[string][]string{ 75 "slice-0": { 76 "db_mycat_1": {"DELETE FROM `tbl_mycat` WHERE `id`=1"}, 77 }, 78 }, 79 }, 80 { 81 db: "db_mycat", 82 sql: "delete from tbl_mycat where id = 2", 83 sqls: map[string]map[string][]string{ 84 "slice-1": { 85 "db_mycat_2": {"DELETE FROM `tbl_mycat` WHERE `id`=2"}, 86 }, 87 }, 88 }, 89 { 90 db: "db_mycat", 91 sql: "delete from tbl_mycat where id = 3", 92 sqls: map[string]map[string][]string{ 93 "slice-1": { 94 "db_mycat_3": {"DELETE FROM `tbl_mycat` WHERE `id`=3"}, 95 }, 96 }, 97 }, 98 { 99 db: "db_mycat", 100 sql: "delete from tbl_mycat where id = 4", 101 sqls: map[string]map[string][]string{ 102 "slice-0": { 103 "db_mycat_0": {"DELETE FROM `tbl_mycat` WHERE `id`=4"}, 104 }, 105 }, 106 }, 107 { 108 db: "db_mycat", 109 sql: "delete from tbl_mycat where id = 6", 110 sqls: map[string]map[string][]string{ 111 "slice-1": { 112 "db_mycat_2": {"DELETE FROM `tbl_mycat` WHERE `id`=6"}, 113 }, 114 }, 115 }, 116 { 117 db: "db_mycat", 118 sql: "delete from tbl_mycat where tbl_mycat.id = 6", 119 sqls: map[string]map[string][]string{ 120 "slice-1": { 121 "db_mycat_2": {"DELETE FROM `tbl_mycat` WHERE `tbl_mycat`.`id`=6"}, // table name in assignment is removed 122 }, 123 }, 124 }, 125 { 126 db: "db_mycat", 127 sql: "delete from tbl_mycat where db_mycat.tbl_mycat.id = 6", 128 sqls: map[string]map[string][]string{ 129 "slice-1": { 130 "db_mycat_2": {"DELETE FROM `tbl_mycat` WHERE `db_mycat_2`.`tbl_mycat`.`id`=6"}, // db name in assignment is removed 131 }, 132 }, 133 }, 134 { 135 db: "db_mycat", 136 sql: "delete from tbl_mycat where id in (1,3,5)", 137 sqls: map[string]map[string][]string{ 138 "slice-0": { 139 "db_mycat_1": {"DELETE FROM `tbl_mycat` WHERE `id` IN (1,5)"}, 140 }, 141 "slice-1": { 142 "db_mycat_3": {"DELETE FROM `tbl_mycat` WHERE `id` IN (3)"}, 143 }, 144 }, 145 }, 146 } 147 for _, test := range tests { 148 t.Run(test.sql, getTestFunc(ns, test)) 149 } 150 } 151 152 func TestMycatShardDeleteWithOrderBy(t *testing.T) { 153 ns, err := preparePlanInfo() 154 if err != nil { 155 t.Fatalf("prepare namespace error: %v", err) 156 } 157 158 tests := []SQLTestcase{ 159 { 160 db: "db_mycat", 161 sql: "delete from tbl_mycat order by id", 162 sqls: map[string]map[string][]string{ 163 "slice-0": { 164 "db_mycat_0": {"DELETE FROM `tbl_mycat` ORDER BY `id`"}, 165 "db_mycat_1": {"DELETE FROM `tbl_mycat` ORDER BY `id`"}, 166 }, 167 "slice-1": { 168 "db_mycat_2": {"DELETE FROM `tbl_mycat` ORDER BY `id`"}, 169 "db_mycat_3": {"DELETE FROM `tbl_mycat` ORDER BY `id`"}, 170 }, 171 }, 172 }, 173 { 174 db: "db_mycat", 175 sql: "delete from tbl_mycat where id = 0 order by id", 176 sqls: map[string]map[string][]string{ 177 "slice-0": { 178 "db_mycat_0": {"DELETE FROM `tbl_mycat` WHERE `id`=0 ORDER BY `id`"}, 179 }, 180 }, 181 }, 182 { 183 db: "db_mycat", 184 sql: "delete from tbl_mycat where id = 0 order by db_mycat.tbl_mycat.id", 185 sqls: map[string]map[string][]string{ 186 "slice-0": { 187 "db_mycat_0": {"DELETE FROM `tbl_mycat` WHERE `id`=0 ORDER BY `db_mycat_0`.`tbl_mycat`.`id`"}, 188 }, 189 }, 190 }, 191 { 192 db: "db_mycat", 193 sql: "delete from tbl_mycat where id = 0 order by a", 194 sqls: map[string]map[string][]string{ 195 "slice-0": { 196 "db_mycat_0": {"DELETE FROM `tbl_mycat` WHERE `id`=0 ORDER BY `a`"}, 197 }, 198 }, 199 }, 200 { 201 db: "db_mycat", 202 sql: "delete from tbl_mycat where id = 0 order by id desc", 203 sqls: map[string]map[string][]string{ 204 "slice-0": { 205 "db_mycat_0": {"DELETE FROM `tbl_mycat` WHERE `id`=0 ORDER BY `id` DESC"}, 206 }, 207 }, 208 }, 209 } 210 for _, test := range tests { 211 t.Run(test.sql, getTestFunc(ns, test)) 212 } 213 } 214 215 func TestMycatShardDeleteWithLimit(t *testing.T) { 216 ns, err := preparePlanInfo() 217 if err != nil { 218 t.Fatalf("prepare namespace error: %v", err) 219 } 220 221 tests := []SQLTestcase{ 222 { 223 db: "db_mycat", 224 sql: "delete from tbl_mycat limit 10", 225 sqls: map[string]map[string][]string{ 226 "slice-0": { 227 "db_mycat_0": {"DELETE FROM `tbl_mycat` LIMIT 10"}, 228 "db_mycat_1": {"DELETE FROM `tbl_mycat` LIMIT 10"}, 229 }, 230 "slice-1": { 231 "db_mycat_2": {"DELETE FROM `tbl_mycat` LIMIT 10"}, 232 "db_mycat_3": {"DELETE FROM `tbl_mycat` LIMIT 10"}, 233 }, 234 }, 235 }, 236 { 237 db: "db_mycat", 238 sql: "delete from tbl_mycat limit 0, 10", 239 hasErr: true, // parse sql error 240 }, 241 { 242 db: "db_mycat", 243 sql: "delete from tbl_mycat limit 10 offset 20", 244 hasErr: true, // parse sql error 245 }, 246 } 247 for _, test := range tests { 248 t.Run(test.sql, getTestFunc(ns, test)) 249 } 250 }