github.com/ecodeclub/eorm@v0.0.2-0.20231001112437-dae71da914d0/internal/integration/sharding_select_test.go (about) 1 // Copyright 2021 ecodeclub 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 //go:build e2e 16 17 package integration 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/ecodeclub/eorm" 24 "github.com/ecodeclub/eorm/internal/datasource/masterslave" 25 "github.com/ecodeclub/eorm/internal/model" 26 "github.com/ecodeclub/eorm/internal/test" 27 "github.com/stretchr/testify/assert" 28 "github.com/stretchr/testify/require" 29 "github.com/stretchr/testify/suite" 30 ) 31 32 type ShardingSelectTestSuite struct { 33 ShardingSelectUpdateInsertSuite 34 } 35 36 func (s *ShardingSelectTestSuite) TestSardingSelectorGet() { 37 t := s.T() 38 r := model.NewMetaRegistry() 39 _, err := r.Register(&test.OrderDetail{}, 40 model.WithTableShardingAlgorithm(s.algorithm)) 41 require.NoError(t, err) 42 eorm.DBWithMetaRegistry(r)(s.shardingDB) 43 44 testCases := []struct { 45 name string 46 s *eorm.ShardingSelector[test.OrderDetail] 47 wantErr error 48 wantRes *test.OrderDetail 49 }{ 50 { 51 name: "found tab 1", 52 s: func() *eorm.ShardingSelector[test.OrderDetail] { 53 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 54 Where(eorm.C("OrderId").EQ(123)) 55 return builder 56 }(), 57 wantRes: &test.OrderDetail{OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 58 }, 59 { 60 name: "found tab 2", 61 s: func() *eorm.ShardingSelector[test.OrderDetail] { 62 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 63 Where(eorm.C("OrderId").EQ(234)) 64 return builder 65 }(), 66 wantRes: &test.OrderDetail{OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 67 }, 68 { 69 name: "found tab and", 70 s: func() *eorm.ShardingSelector[test.OrderDetail] { 71 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 72 Where(eorm.C("OrderId").EQ(234).And(eorm.C("ItemId").EQ(12))) 73 return builder 74 }(), 75 wantRes: &test.OrderDetail{OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 76 }, 77 } 78 79 for _, tc := range testCases { 80 s.T().Run(tc.name, func(t *testing.T) { 81 // TODO 从库测试目前有查不到数据的bug 82 ctx := masterslave.UseMaster(context.Background()) 83 res, err := tc.s.Get(ctx) 84 assert.Equal(t, tc.wantErr, err) 85 if err != nil { 86 return 87 } 88 assert.Equal(t, tc.wantRes, res) 89 }) 90 } 91 } 92 93 func (s *ShardingSelectTestSuite) TestSardingSelectorGetMulti() { 94 t := s.T() 95 r := model.NewMetaRegistry() 96 _, err := r.Register(&test.OrderDetail{}, 97 model.WithTableShardingAlgorithm(s.algorithm)) 98 require.NoError(t, err) 99 eorm.DBWithMetaRegistry(r)(s.shardingDB) 100 101 testCases := []struct { 102 name string 103 s *eorm.ShardingSelector[test.OrderDetail] 104 wantErr error 105 wantRes []*test.OrderDetail 106 }{ 107 { 108 name: "found tab eq not data", 109 s: func() *eorm.ShardingSelector[test.OrderDetail] { 110 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 111 Where(eorm.C("OrderId").EQ(500)) 112 return builder 113 }(), 114 wantRes: []*test.OrderDetail{}, 115 }, 116 { 117 name: "found tab eq", 118 s: func() *eorm.ShardingSelector[test.OrderDetail] { 119 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 120 Where(eorm.C("OrderId").EQ(123)) 121 return builder 122 }(), 123 wantRes: []*test.OrderDetail{ 124 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 125 }, 126 }, 127 { 128 name: "found tab or", 129 s: func() *eorm.ShardingSelector[test.OrderDetail] { 130 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 131 Where(eorm.C("OrderId").EQ(123).Or(eorm.C("OrderId").EQ(234))) 132 return builder 133 }(), 134 wantRes: []*test.OrderDetail{ 135 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 136 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 137 }, 138 }, 139 { 140 name: "found tab or broadcast", 141 s: func() *eorm.ShardingSelector[test.OrderDetail] { 142 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 143 Where(eorm.C("OrderId").EQ(123).Or(eorm.C("ItemId").EQ(12))) 144 return builder 145 }(), 146 wantRes: []*test.OrderDetail{ 147 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 148 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 149 }, 150 }, 151 { 152 name: "found tab or-and", 153 s: func() *eorm.ShardingSelector[test.OrderDetail] { 154 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 155 Where(eorm.C("OrderId").EQ(234). 156 Or(eorm.C("ItemId").EQ(10).And(eorm.C("OrderId").EQ(123)))) 157 return builder 158 }(), 159 wantRes: []*test.OrderDetail{ 160 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 161 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 162 }, 163 }, 164 { 165 name: "found tab and-or", 166 s: func() *eorm.ShardingSelector[test.OrderDetail] { 167 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 168 Where(eorm.C("OrderId").EQ(234). 169 And(eorm.C("ItemId").EQ(12).Or(eorm.C("OrderId").EQ(123)))) 170 return builder 171 }(), 172 wantRes: []*test.OrderDetail{ 173 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 174 }, 175 }, 176 { 177 name: "where gt", 178 s: func() *eorm.ShardingSelector[test.OrderDetail] { 179 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 180 Where(eorm.C("OrderId").GT(1)) 181 return builder 182 }(), 183 wantRes: []*test.OrderDetail{ 184 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 185 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 186 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 187 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 188 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 189 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 190 }, 191 }, 192 { 193 name: "where lt", 194 s: func() *eorm.ShardingSelector[test.OrderDetail] { 195 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 196 Where(eorm.C("OrderId").LT(150)) 197 return builder 198 }(), 199 wantRes: []*test.OrderDetail{ 200 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 201 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 202 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 203 }, 204 }, 205 { 206 name: "where gt eq", 207 s: func() *eorm.ShardingSelector[test.OrderDetail] { 208 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 209 Where(eorm.C("OrderId").GTEQ(123)) 210 return builder 211 }(), 212 wantRes: []*test.OrderDetail{ 213 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 214 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 215 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 216 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 217 }, 218 }, 219 { 220 name: "where lt eq", 221 s: func() *eorm.ShardingSelector[test.OrderDetail] { 222 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 223 Where(eorm.C("OrderId").LTEQ(123)) 224 return builder 225 }(), 226 wantRes: []*test.OrderDetail{ 227 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 228 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 229 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 230 }, 231 }, 232 { 233 name: "where in", 234 s: func() *eorm.ShardingSelector[test.OrderDetail] { 235 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 236 Where(eorm.C("OrderId").In(8, 11, 123, 234)) 237 return builder 238 }(), 239 wantRes: []*test.OrderDetail{ 240 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 241 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 242 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 243 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 244 }, 245 }, 246 { 247 name: "where eq or gt", 248 s: func() *eorm.ShardingSelector[test.OrderDetail] { 249 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 250 Where(eorm.C("OrderId").EQ(8). 251 Or(eorm.C("OrderId").GT(240))) 252 return builder 253 }(), 254 wantRes: []*test.OrderDetail{ 255 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 256 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 257 }, 258 }, 259 { 260 name: "where in or eq", 261 s: func() *eorm.ShardingSelector[test.OrderDetail] { 262 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 263 Where(eorm.C("OrderId").In(8, 11, 123). 264 Or(eorm.C("OrderId").EQ(181))) 265 return builder 266 }(), 267 wantRes: []*test.OrderDetail{ 268 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 269 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 270 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 271 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 272 }, 273 }, 274 { 275 name: "where in or gt", 276 s: func() *eorm.ShardingSelector[test.OrderDetail] { 277 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 278 Where(eorm.C("OrderId").In(8, 11). 279 Or(eorm.C("OrderId").GT(200))) 280 return builder 281 }(), 282 wantRes: []*test.OrderDetail{ 283 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 284 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 285 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 286 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 287 }, 288 }, 289 { 290 name: "where between", 291 s: func() *eorm.ShardingSelector[test.OrderDetail] { 292 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 293 Where(eorm.C("OrderId").GTEQ(123).And(eorm.C("OrderId").LTEQ(253))) 294 return builder 295 }(), 296 wantRes: []*test.OrderDetail{ 297 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 298 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 299 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 300 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 301 }, 302 }, 303 { 304 name: "where eq and lt or gt", 305 s: func() *eorm.ShardingSelector[test.OrderDetail] { 306 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 307 Where(eorm.C("OrderId").EQ(11).And(eorm.C("OrderId").LT(123)). 308 Or(eorm.C("OrderId").GT(234))) 309 return builder 310 }(), 311 wantRes: []*test.OrderDetail{ 312 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 313 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 314 }, 315 }, 316 { 317 name: "where not in", 318 s: func() *eorm.ShardingSelector[test.OrderDetail] { 319 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 320 Where(eorm.C("OrderId").NotIn(8, 123, 253)) 321 return builder 322 }(), 323 wantRes: []*test.OrderDetail{ 324 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 325 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 326 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 327 }, 328 }, 329 { 330 name: "where not in and eq", 331 s: func() *eorm.ShardingSelector[test.OrderDetail] { 332 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 333 Where(eorm.C("OrderId").NotIn(8, 123, 253). 334 And(eorm.C("OrderId").EQ(11))) 335 return builder 336 }(), 337 wantRes: []*test.OrderDetail{ 338 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 339 }, 340 }, 341 { 342 name: "where not in or eq", 343 s: func() *eorm.ShardingSelector[test.OrderDetail] { 344 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 345 Where(eorm.C("OrderId").NotIn(8, 123, 253). 346 Or(eorm.C("OrderId").EQ(11))) 347 348 return builder 349 }(), 350 wantRes: []*test.OrderDetail{ 351 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 352 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 353 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 354 }, 355 }, 356 { 357 name: "where not in or gt", 358 s: func() *eorm.ShardingSelector[test.OrderDetail] { 359 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 360 Where(eorm.C("OrderId").NotIn(8, 123, 253). 361 Or(eorm.C("OrderId").GT(11))) 362 return builder 363 }(), 364 wantRes: []*test.OrderDetail{ 365 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 366 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 367 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 368 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 369 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 370 }, 371 }, 372 { 373 name: "where not gt", 374 s: func() *eorm.ShardingSelector[test.OrderDetail] { 375 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 376 Where(eorm.Not(eorm.C("OrderId").GT(181))) 377 return builder 378 }(), 379 wantRes: []*test.OrderDetail{ 380 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 381 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 382 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 383 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 384 }, 385 }, 386 { 387 name: "where not lt", 388 s: func() *eorm.ShardingSelector[test.OrderDetail] { 389 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 390 Where(eorm.Not(eorm.C("OrderId").LT(181))) 391 return builder 392 }(), 393 wantRes: []*test.OrderDetail{ 394 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 395 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 396 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 397 }, 398 }, 399 { 400 name: "where not gt eq", 401 s: func() *eorm.ShardingSelector[test.OrderDetail] { 402 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 403 Where(eorm.Not(eorm.C("OrderId").GTEQ(181))) 404 return builder 405 }(), 406 wantRes: []*test.OrderDetail{ 407 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 408 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 409 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 410 }, 411 }, 412 { 413 name: "where not lt eq", 414 s: func() *eorm.ShardingSelector[test.OrderDetail] { 415 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 416 Where(eorm.Not(eorm.C("OrderId").LTEQ(181))) 417 return builder 418 }(), 419 wantRes: []*test.OrderDetail{ 420 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 421 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 422 }, 423 }, 424 { 425 name: "where not eq", 426 s: func() *eorm.ShardingSelector[test.OrderDetail] { 427 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 428 Where(eorm.Not(eorm.C("OrderId").EQ(181))) 429 return builder 430 }(), 431 wantRes: []*test.OrderDetail{ 432 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 433 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 434 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 435 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 436 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 437 }, 438 }, 439 { 440 name: "where not neq", 441 s: func() *eorm.ShardingSelector[test.OrderDetail] { 442 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 443 Where(eorm.Not(eorm.C("OrderId").NEQ(181))) 444 return builder 445 }(), 446 wantRes: []*test.OrderDetail{ 447 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 448 }, 449 }, 450 { 451 name: "where not (gt and lt)", 452 s: func() *eorm.ShardingSelector[test.OrderDetail] { 453 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 454 Where(eorm.Not(eorm.C("OrderId").GT(11). 455 And(eorm.C("OrderId").LT(230)))) 456 return builder 457 }(), 458 wantRes: []*test.OrderDetail{ 459 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 460 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 461 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 462 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 463 }, 464 }, 465 { 466 name: "where not (gt eq and lt eq)", 467 s: func() *eorm.ShardingSelector[test.OrderDetail] { 468 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 469 Where(eorm.Not(eorm.C("OrderId").GTEQ(11). 470 And(eorm.C("OrderId").LTEQ(240)))) 471 return builder 472 }(), 473 wantRes: []*test.OrderDetail{ 474 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 475 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 476 }, 477 }, 478 { 479 name: "where not (in or gt)", 480 s: func() *eorm.ShardingSelector[test.OrderDetail] { 481 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 482 Where(eorm.Not(eorm.C("OrderId").In(8, 123, 253). 483 Or(eorm.C("OrderId").GT(200)))) 484 return builder 485 }(), 486 wantRes: []*test.OrderDetail{ 487 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 488 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 489 }, 490 }, 491 { 492 name: "where not (in or eq)", 493 s: func() *eorm.ShardingSelector[test.OrderDetail] { 494 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 495 Where(eorm.Not(eorm.C("OrderId").In(8, 123). 496 Or(eorm.C("OrderId").EQ(253)))) 497 return builder 498 }(), 499 wantRes: []*test.OrderDetail{ 500 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 501 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 502 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 503 }, 504 }, 505 { 506 name: "where not (eq and eq)", 507 s: func() *eorm.ShardingSelector[test.OrderDetail] { 508 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 509 Where(eorm.Not(eorm.C("OrderId").EQ(11). 510 And(eorm.C("OrderId").EQ(234)))) 511 return builder 512 }(), 513 wantRes: []*test.OrderDetail{ 514 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 515 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 516 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 517 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 518 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 519 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 520 }, 521 }, 522 { 523 name: "where not (eq and eq not sharding key)", 524 s: func() *eorm.ShardingSelector[test.OrderDetail] { 525 builder := eorm.NewShardingSelector[test.OrderDetail](s.shardingDB). 526 Where(eorm.Not(eorm.C("OrderId").EQ(11). 527 And(eorm.C("ItemId").EQ(12)))) 528 return builder 529 }(), 530 wantRes: []*test.OrderDetail{ 531 {OrderId: 8, ItemId: 6, UsingCol1: "Kobe", UsingCol2: "Bryant"}, 532 {OrderId: 11, ItemId: 8, UsingCol1: "James", UsingCol2: "Harden"}, 533 {OrderId: 123, ItemId: 10, UsingCol1: "LeBron", UsingCol2: "James"}, 534 {OrderId: 234, ItemId: 12, UsingCol1: "Kevin", UsingCol2: "Durant"}, 535 {OrderId: 253, ItemId: 8, UsingCol1: "Stephen", UsingCol2: "Curry"}, 536 {OrderId: 181, ItemId: 11, UsingCol1: "Kawhi", UsingCol2: "Leonard"}, 537 }, 538 }, 539 } 540 541 for _, tc := range testCases { 542 s.T().Run(tc.name, func(t *testing.T) { 543 // TODO 从库测试目前有查不到数据的bug 544 ctx := masterslave.UseMaster(context.Background()) 545 res, err := tc.s.GetMulti(ctx) 546 assert.Equal(t, tc.wantErr, err) 547 if err != nil { 548 return 549 } 550 assert.ElementsMatch(t, tc.wantRes, res) 551 }) 552 } 553 } 554 555 func TestMySQL8ShardingSelect(t *testing.T) { 556 suite.Run(t, &ShardingSelectTestSuite{ 557 ShardingSelectUpdateInsertSuite: newShardingSelectUpdateInsertSuite(), 558 }) 559 }