2014-07-29 08:32:58 +04:00
|
|
|
package gorm_test
|
|
|
|
|
|
|
|
import (
|
2017-08-30 22:52:45 +03:00
|
|
|
"encoding/hex"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
2014-07-29 08:32:58 +04:00
|
|
|
"testing"
|
2018-02-11 07:48:08 +03:00
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
2014-07-29 08:32:58 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
|
|
|
user1 := User{Name: "ScopeUser1", Age: 1}
|
|
|
|
user2 := User{Name: "ScopeUser2", Age: 1}
|
|
|
|
user3 := User{Name: "ScopeUser3", Age: 2}
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.Save(&user1).Save(&user2).Save(&user3)
|
2014-07-29 08:32:58 +04:00
|
|
|
|
|
|
|
var users1, users2, users3 []User
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.Scopes(NameIn1And2).Find(&users1)
|
2014-07-29 08:32:58 +04:00
|
|
|
if len(users1) != 2 {
|
|
|
|
t.Errorf("Should found two users's name in 1, 2")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.Scopes(NameIn1And2, NameIn2And3).Find(&users2)
|
2014-07-29 08:32:58 +04:00
|
|
|
if len(users2) != 1 {
|
|
|
|
t.Errorf("Should found one user's name is 2")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.Scopes(NameIn([]string{user1.Name, user3.Name})).Find(&users3)
|
2014-07-29 08:32:58 +04:00
|
|
|
if len(users3) != 2 {
|
|
|
|
t.Errorf("Should found two users's name in 1, 3")
|
|
|
|
}
|
|
|
|
}
|
2017-08-30 22:52:45 +03:00
|
|
|
|
|
|
|
func randName() string {
|
|
|
|
data := make([]byte, 8)
|
|
|
|
rand.Read(data)
|
|
|
|
|
|
|
|
return "n-" + hex.EncodeToString(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValuer(t *testing.T) {
|
|
|
|
name := randName()
|
|
|
|
|
|
|
|
origUser := User{Name: name, Age: 1, Password: EncryptedData("pass1"), PasswordHash: []byte("abc")}
|
2018-02-11 07:48:08 +03:00
|
|
|
if err := DB.Save(&origUser).Error; err != nil {
|
|
|
|
t.Errorf("No error should happen when saving user, but got %v", err)
|
2017-08-30 22:52:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var user2 User
|
2018-02-11 07:48:08 +03:00
|
|
|
if err := DB.Where("name = ? AND password = ? AND password_hash = ?", name, EncryptedData("pass1"), []byte("abc")).First(&user2).Error; err != nil {
|
|
|
|
t.Errorf("No error should happen when querying user with valuer, but got %v", err)
|
2017-08-30 22:52:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFailedValuer(t *testing.T) {
|
|
|
|
name := randName()
|
|
|
|
|
|
|
|
err := DB.Exec("INSERT INTO users(name, password) VALUES(?, ?)", name, EncryptedData("xpass1")).Error
|
|
|
|
|
2018-02-11 07:48:08 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("There should be an error should happen when insert data")
|
|
|
|
} else if !strings.HasPrefix(err.Error(), "Should not start with") {
|
|
|
|
t.Errorf("The error should be returned from Valuer, but get %v", err)
|
2017-08-30 22:52:45 +03:00
|
|
|
}
|
|
|
|
}
|