Add test for anonymous scanner

This commit is contained in:
Jinzhu 2014-03-26 10:28:34 +08:00
parent 1949baf5c8
commit 663c06cfb1
1 changed files with 39 additions and 6 deletions

View File

@ -35,11 +35,28 @@ func (i *Num) Scan(src interface{}) error {
return nil return nil
} }
type Role struct { type Company struct {
Id int64 Id int64
Name string Name string
} }
type Role struct {
Name string
}
func (role *Role) Scan(value interface{}) error {
role.Name = string(value.([]uint8))
return nil
}
func (role Role) Value() (driver.Value, error) {
return role.Name, nil
}
func (role Role) IsAdmin() bool {
return role.Name == "admin"
}
type User struct { type User struct {
Id int64 // Id: Primary key Id int64 // Id: Primary key
Age int64 Age int64
@ -58,8 +75,9 @@ type User struct {
When time.Time When time.Time
CreditCard CreditCard CreditCard CreditCard
Latitude float64 Latitude float64
CompanyId int64
Company
Role Role
RoleId int64
PasswordHash []byte PasswordHash []byte
IgnoreMe int64 `sql:"-"` IgnoreMe int64 `sql:"-"`
IgnoreStringSlice []string `sql:"-"` IgnoreStringSlice []string `sql:"-"`
@ -172,7 +190,7 @@ func init() {
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
} }
if err = db.AutoMigrate(Role{}).Error; err != nil { if err = db.AutoMigrate(Company{}).Error; err != nil {
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
} }
@ -1749,17 +1767,32 @@ func TestHaving(t *testing.T) {
} }
func TestAnonymousField(t *testing.T) { func TestAnonymousField(t *testing.T) {
user := User{Name: "anonymous_field", Role: Role{Name: "admin"}} user := User{Name: "anonymous_field", Company: Company{Name: "company"}}
db.Save(&user) db.Save(&user)
var user2 User var user2 User
db.First(&user2, "name = ?", "anonymous_field") db.First(&user2, "name = ?", "anonymous_field")
db.Model(&user2).Related(&user2.Role) db.Model(&user2).Related(&user2.Company)
if user2.Role.Name != "admin" { if user2.Company.Name != "company" {
t.Errorf("Should be able to get anonymous field") t.Errorf("Should be able to get anonymous field")
} }
} }
func TestAnonymousScanner(t *testing.T) {
user := User{Name: "anonymous_scanner", Role: Role{Name: "admin"}}
db.Save(&user)
var user2 User
db.First(&user2, "name = ?", "anonymous_scanner")
if user2.Role.Name != "admin" {
t.Errorf("Should be able to get anonymous scanner")
}
if !user2.IsAdmin() {
t.Errorf("Should be able to get anonymous scanner")
}
}
func TestExecRawSql(t *testing.T) { func TestExecRawSql(t *testing.T) {
db.Exec("update users set name=? where name in (?)", "jinzhu", []string{"1", "2", "3"}) db.Exec("update users set name=? where name in (?)", "jinzhu", []string{"1", "2", "3"})
if db.Where("name in (?)", []string{"1", "2", "3"}).First(&User{}).Error != gorm.RecordNotFound { if db.Where("name in (?)", []string{"1", "2", "3"}).First(&User{}).Error != gorm.RecordNotFound {