| 508 | } |
| 509 | |
| 510 | func TestNot(t *testing.T) { |
| 511 | dryDB := DB.Session(&gorm.Session{DryRun: true}) |
| 512 | |
| 513 | result := dryDB.Not(map[string]interface{}{"name": "jinzhu"}).Find(&User{}) |
| 514 | if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* <> .+").MatchString(result.Statement.SQL.String()) { |
| 515 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 516 | } |
| 517 | |
| 518 | result = dryDB.Where("name = ?", "jinzhu1").Not("name = ?", "jinzhu2").Find(&User{}) |
| 519 | if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* = .+ AND NOT.*name.* = .+").MatchString(result.Statement.SQL.String()) { |
| 520 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 521 | } |
| 522 | |
| 523 | result = dryDB.Where(map[string]interface{}{"name": []string{"jinzhu", "jinzhu 2"}}).Find(&User{}) |
| 524 | if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* IN \\(.+,.+\\)").MatchString(result.Statement.SQL.String()) { |
| 525 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 526 | } |
| 527 | |
| 528 | result = dryDB.Not("name = ?", "jinzhu").Find(&User{}) |
| 529 | if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE NOT.*name.* = .+").MatchString(result.Statement.SQL.String()) { |
| 530 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 531 | } |
| 532 | |
| 533 | result = dryDB.Not(map[string]interface{}{"name": []string{}}).Find(&User{}) |
| 534 | if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* IS NOT NULL").MatchString(result.Statement.SQL.String()) { |
| 535 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 536 | } |
| 537 | |
| 538 | result = dryDB.Not(map[string]interface{}{"name": []string{"jinzhu", "jinzhu 2"}}).Find(&User{}) |
| 539 | if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* NOT IN \\(.+,.+\\)").MatchString(result.Statement.SQL.String()) { |
| 540 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 541 | } |
| 542 | |
| 543 | result = dryDB.Not([]int64{1, 2}).First(&User{}) |
| 544 | if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*id.* NOT IN \\(.+,.+\\)").MatchString(result.Statement.SQL.String()) { |
| 545 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 546 | } |
| 547 | |
| 548 | result = dryDB.Not([]int64{}).First(&User{}) |
| 549 | if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .users.\\..deleted_at. IS NULL ORDER BY").MatchString(result.Statement.SQL.String()) { |
| 550 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 551 | } |
| 552 | |
| 553 | result = dryDB.Not(User{Name: "jinzhu", Age: 18}).First(&User{}) |
| 554 | if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*users.*..*name.* <> .+ AND .*users.*..*age.* <> .+").MatchString(result.Statement.SQL.String()) { |
| 555 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 556 | } |
| 557 | |
| 558 | result = dryDB.Not(DB.Where("manager IS NULL").Where("age >= ?", 20)).Find(&User{}) |
| 559 | if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE NOT \\(manager IS NULL AND age >= .+\\) AND .users.\\..deleted_at. IS NULL").MatchString(result.Statement.SQL.String()) { |
| 560 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 561 | } |
| 562 | |
| 563 | result = dryDB.Not(DB.Where("manager IS NULL").Or("age >= ?", 20)).Find(&User{}) |
| 564 | if !regexp.MustCompile(`SELECT \* FROM .*users.* WHERE NOT \(manager IS NULL OR age >= .+\) AND .users.\..deleted_at. IS NULL`).MatchString(result.Statement.SQL.String()) { |
| 565 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 566 | } |
| 567 | } |