| 567 | } |
| 568 | |
| 569 | func TestNotWithAllFields(t *testing.T) { |
| 570 | dryDB := DB.Session(&gorm.Session{DryRun: true, QueryFields: true}) |
| 571 | userQuery := "SELECT .*users.*id.*users.*created_at.*users.*updated_at.*users.*deleted_at.*users.*name" + |
| 572 | ".*users.*age.*users.*birthday.*users.*company_id.*users.*manager_id.*users.*active.* FROM .*users.* " |
| 573 | |
| 574 | result := dryDB.Not(map[string]interface{}{"users.name": "jinzhu"}).Find(&User{}) |
| 575 | |
| 576 | if !regexp.MustCompile(userQuery + "WHERE .*users.*name.* <> .+").MatchString(result.Statement.SQL.String()) { |
| 577 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 578 | } |
| 579 | |
| 580 | result = dryDB.Where("users.name = ?", "jinzhu1").Not("users.name = ?", "jinzhu2").Find(&User{}) |
| 581 | if !regexp.MustCompile(userQuery + "WHERE .*users.*name.* = .+ AND NOT .*users.*name.* = .+").MatchString(result.Statement.SQL.String()) { |
| 582 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 583 | } |
| 584 | |
| 585 | result = dryDB.Where(map[string]interface{}{"users.name": []string{"jinzhu", "jinzhu 2"}}).Find(&User{}) |
| 586 | if !regexp.MustCompile(userQuery + "WHERE .*users.*name.* IN \\(.+,.+\\)").MatchString(result.Statement.SQL.String()) { |
| 587 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 588 | } |
| 589 | |
| 590 | result = dryDB.Not("users.name = ?", "jinzhu").Find(&User{}) |
| 591 | if !regexp.MustCompile(userQuery + "WHERE NOT .*users.*name.* = .+").MatchString(result.Statement.SQL.String()) { |
| 592 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 593 | } |
| 594 | |
| 595 | result = dryDB.Not(map[string]interface{}{"users.name": []string{"jinzhu", "jinzhu 2"}}).Find(&User{}) |
| 596 | if !regexp.MustCompile(userQuery + "WHERE .*users.*name.* NOT IN \\(.+,.+\\)").MatchString(result.Statement.SQL.String()) { |
| 597 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 598 | } |
| 599 | |
| 600 | result = dryDB.Not([]int64{1, 2}).First(&User{}) |
| 601 | if !regexp.MustCompile(userQuery + "WHERE .*users.*id.* NOT IN \\(.+,.+\\)").MatchString(result.Statement.SQL.String()) { |
| 602 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 603 | } |
| 604 | |
| 605 | result = dryDB.Not([]int64{}).First(&User{}) |
| 606 | if !regexp.MustCompile(userQuery + "WHERE .users.\\..deleted_at. IS NULL ORDER BY").MatchString(result.Statement.SQL.String()) { |
| 607 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 608 | } |
| 609 | |
| 610 | result = dryDB.Not(User{Name: "jinzhu", Age: 18}).First(&User{}) |
| 611 | if !regexp.MustCompile(userQuery + "WHERE .*users.*..*name.* <> .+ AND .*users.*..*age.* <> .+").MatchString(result.Statement.SQL.String()) { |
| 612 | t.Fatalf("Build NOT condition, but got %v", result.Statement.SQL.String()) |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | func TestOr(t *testing.T) { |
| 617 | dryDB := DB.Session(&gorm.Session{DryRun: true}) |