| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"gorm.io/gorm" |
| 8 |
. "gorm.io/gorm/utils/tests" |
| 9 |
) |
| 10 |
|
| 11 |
func NameIn1And2(d *gorm.DB) *gorm.DB { |
| 12 |
return d.Where("name in (?)", []string{"ScopeUser1", "ScopeUser2"}) |
| 13 |
} |
| 14 |
|
| 15 |
func NameIn2And3(d *gorm.DB) *gorm.DB { |
| 16 |
return d.Where("name in (?)", []string{"ScopeUser2", "ScopeUser3"}) |
| 17 |
} |
| 18 |
|
| 19 |
func NameIn(names []string) func(d *gorm.DB) *gorm.DB { |
| 20 |
return func(d *gorm.DB) *gorm.DB { |
| 21 |
return d.Where("name in (?)", names) |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
func TestScopes(t *testing.T) { |
| 26 |
users := []*User{ |
| 27 |
GetUser("ScopeUser1", Config{}), |
| 28 |
GetUser("ScopeUser2", Config{}), |
| 29 |
GetUser("ScopeUser3", Config{}), |
| 30 |
} |
| 31 |
|
| 32 |
DB.Create(&users) |
| 33 |
|
| 34 |
var users1, users2, users3 []User |
| 35 |
DB.Scopes(NameIn1And2).Find(&users1) |
| 36 |
if len(users1) != 2 { |
| 37 |
t.Errorf("Should found two users's name in 1, 2, but got %v", len(users1)) |
| 38 |
} |
| 39 |
|
| 40 |
DB.Scopes(NameIn1And2, NameIn2And3).Find(&users2) |
| 41 |
if len(users2) != 1 { |
| 42 |
t.Errorf("Should found one user's name is 2, but got %v", len(users2)) |
| 43 |
} |
| 44 |
|
| 45 |
DB.Scopes(NameIn([]string{users[0].Name, users[2].Name})).Find(&users3) |
| 46 |
if len(users3) != 2 { |
| 47 |
t.Errorf("Should found two users's name in 1, 3, but got %v", len(users3)) |
| 48 |
} |
| 49 |
|
| 50 |
db := DB.Scopes(func(tx *gorm.DB) *gorm.DB { |
| 51 |
return tx.Table("custom_table") |
| 52 |
}).Session(&gorm.Session{}) |
| 53 |
|
| 54 |
db.AutoMigrate(&User{}) |
| 55 |
if db.Find(&User{}).Statement.Table != "custom_table" { |
| 56 |
t.Errorf("failed to call Scopes") |
| 57 |
} |
| 58 |
|
| 59 |
result := DB.Scopes(NameIn1And2, func(tx *gorm.DB) *gorm.DB { |
| 60 |
return tx.Session(&gorm.Session{}) |
| 61 |
}).Find(&users1) |
| 62 |
|
| 63 |
if result.RowsAffected != 2 { |
| 64 |
t.Errorf("Should found two users's name in 1, 2, but got %v", result.RowsAffected) |
| 65 |
} |
| 66 |
|
| 67 |
var maxId int64 |
| 68 |
userTable := func(db *gorm.DB) *gorm.DB { |
| 69 |
return db.WithContext(context.Background()).Table("users") |
| 70 |
} |
| 71 |
if err := DB.Scopes(userTable).Select("max(id)").Scan(&maxId).Error; err != nil { |
| 72 |
t.Errorf("select max(id)") |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestComplexScopes(t *testing.T) { |
| 77 |
tests := []struct { |
| 78 |
name string |
| 79 |
queryFn func(tx *gorm.DB) *gorm.DB |
| 80 |
expected string |
| 81 |
}{ |
| 82 |
{ |
| 83 |
name: "depth_1", |
| 84 |
queryFn: func(tx *gorm.DB) *gorm.DB { |
| 85 |
return tx.Scopes( |
| 86 |
func(d *gorm.DB) *gorm.DB { return d.Where("a = 1") }, |
| 87 |
func(d *gorm.DB) *gorm.DB { |
| 88 |
return d.Where(DB.Or("b = 2").Or("c = 3")) |
| 89 |
}, |
| 90 |
).Find(&Language{}) |
| 91 |
}, |
| 92 |
expected: `SELECT * FROM "languages" WHERE a = 1 AND (b = 2 OR c = 3)`, |
| 93 |
}, { |
| 94 |
name: "depth_1_pre_cond", |
| 95 |
queryFn: func(tx *gorm.DB) *gorm.DB { |
| 96 |
return tx.Where("z = 0").Scopes( |
| 97 |
func(d *gorm.DB) *gorm.DB { return d.Where("a = 1") }, |
| 98 |
func(d *gorm.DB) *gorm.DB { |
| 99 |
return d.Or(DB.Where("b = 2").Or("c = 3")) |
| 100 |
}, |
| 101 |
).Find(&Language{}) |
| 102 |
}, |
| 103 |
expected: `SELECT * FROM "languages" WHERE z = 0 AND a = 1 OR (b = 2 OR c = 3)`, |
| 104 |
}, { |
| 105 |
name: "depth_2", |
| 106 |
queryFn: func(tx *gorm.DB) *gorm.DB { |
| 107 |
return tx.Scopes( |
| 108 |
func(d *gorm.DB) *gorm.DB { return d.Model(&Language{}) }, |
| 109 |
func(d *gorm.DB) *gorm.DB { |
| 110 |
return d. |
| 111 |
Or(DB.Scopes( |
| 112 |
func(d *gorm.DB) *gorm.DB { return d.Where("a = 1") }, |
| 113 |
func(d *gorm.DB) *gorm.DB { return d.Where("b = 2") }, |
| 114 |
)). |
| 115 |
Or("c = 3") |
| 116 |
}, |
| 117 |
func(d *gorm.DB) *gorm.DB { return d.Where("d = 4") }, |
| 118 |
).Find(&Language{}) |
| 119 |
}, |
| 120 |
expected: `SELECT * FROM "languages" WHERE d = 4 OR c = 3 OR (a = 1 AND b = 2)`, |
| 121 |
}, |
| 122 |
} |
| 123 |
|
| 124 |
for _, test := range tests { |
| 125 |
t.Run(test.name, func(t *testing.T) { |
| 126 |
assertEqualSQL(t, test.expected, DB.ToSQL(test.queryFn)) |
| 127 |
}) |
| 128 |
} |
| 129 |
} |