gorm/tests/scopes_test.go

49 lines
1.1 KiB
Go

package tests_test
import (
"testing"
"gorm.io/gorm"
. "gorm.io/gorm/tests"
)
func NameIn1And2(d *gorm.DB) *gorm.DB {
return d.Where("name in (?)", []string{"ScopeUser1", "ScopeUser2"})
}
func NameIn2And3(d *gorm.DB) *gorm.DB {
return d.Where("name in (?)", []string{"ScopeUser2", "ScopeUser3"})
}
func NameIn(names []string) func(d *gorm.DB) *gorm.DB {
return func(d *gorm.DB) *gorm.DB {
return d.Where("name in (?)", names)
}
}
func TestScopes(t *testing.T) {
var users = []*User{
GetUser("ScopeUser1", Config{}),
GetUser("ScopeUser2", Config{}),
GetUser("ScopeUser3", Config{}),
}
DB.Create(&users)
var users1, users2, users3 []User
DB.Scopes(NameIn1And2).Find(&users1)
if len(users1) != 2 {
t.Errorf("Should found two users's name in 1, 2, but got %v", len(users1))
}
DB.Scopes(NameIn1And2, NameIn2And3).Find(&users2)
if len(users2) != 1 {
t.Errorf("Should found one user's name is 2, but got %v", len(users2))
}
DB.Scopes(NameIn([]string{users[0].Name, users[2].Name})).Find(&users3)
if len(users3) != 2 {
t.Errorf("Should found two users's name in 1, 3, but got %v", len(users3))
}
}