MCPcopy
hub / github.com/go-gorm/gorm / TestOr

Function TestOr

tests/query_test.go:616–664  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

614}
615
616func TestOr(t *testing.T) {
617 dryDB := DB.Session(&gorm.Session{DryRun: true})
618
619 var count int64
620 result := dryDB.Model(&User{}).Or("role = ?", "admin").Count(&count)
621 if !regexp.MustCompile("SELECT count\\(\\*\\) FROM .*users.* WHERE role = .+ AND .*users.*\\..*deleted_at.* IS NULL").MatchString(result.Statement.SQL.String()) {
622 t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
623 }
624
625 result = dryDB.Where("role = ?", "admin").Where(DB.Or("role = ?", "super_admin")).Find(&User{})
626 if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ AND .*role.* = .+").MatchString(result.Statement.SQL.String()) {
627 t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
628 }
629
630 result = dryDB.Where("role = ?", "admin").Where(DB.Or("role = ?", "super_admin").Or("role = ?", "admin")).Find(&User{})
631 if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ AND (.*role.* = .+ OR .*role.* = .+)").MatchString(result.Statement.SQL.String()) {
632 t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
633 }
634
635 sub := dryDB.Clauses(clause.Where{
636 Exprs: []clause.Expression{
637 clause.OrConditions{
638 Exprs: []clause.Expression{
639 clause.Expr{SQL: "role = ?", Vars: []interface{}{"super_admin"}},
640 clause.Expr{SQL: "role = ?", Vars: []interface{}{"admin"}},
641 },
642 },
643 },
644 })
645 result = dryDB.Where(sub).Find(&User{})
646 if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ OR .*role.* = .+").MatchString(result.Statement.SQL.String()) {
647 t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
648 }
649
650 result = dryDB.Where("role = ?", "admin").Or("role = ?", "super_admin").Find(&User{})
651 if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*role.* = .+ OR .*role.* = .+").MatchString(result.Statement.SQL.String()) {
652 t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
653 }
654
655 result = dryDB.Where("name = ?", "jinzhu").Or(User{Name: "jinzhu 2", Age: 18}).Find(&User{})
656 if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* = .+ OR \\(.*name.* AND .*age.*\\)").MatchString(result.Statement.SQL.String()) {
657 t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
658 }
659
660 result = dryDB.Where("name = ?", "jinzhu").Or(map[string]interface{}{"name": "jinzhu 2", "age": 18}).Find(&User{})
661 if !regexp.MustCompile("SELECT \\* FROM .*users.* WHERE .*name.* = .+ OR \\(.*age.* AND .*name.*\\)").MatchString(result.Statement.SQL.String()) {
662 t.Fatalf("Build OR condition, but got %v", result.Statement.SQL.String())
663 }
664}
665
666func TestOrWithAllFields(t *testing.T) {
667 dryDB := DB.Session(&gorm.Session{DryRun: true, QueryFields: true})

Callers

nothing calls this directly

Calls 8

SessionMethod · 0.80
ModelMethod · 0.80
ClausesMethod · 0.80
CountMethod · 0.65
OrMethod · 0.65
FindMethod · 0.65
WhereMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected