From 663c06cfb1bc0bb9d0135185d41180517fbb2ddb Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 26 Mar 2014 10:28:34 +0800 Subject: [PATCH] Add test for anonymous scanner --- main_test.go | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/main_test.go b/main_test.go index b6d1c17c..2852395b 100644 --- a/main_test.go +++ b/main_test.go @@ -35,11 +35,28 @@ func (i *Num) Scan(src interface{}) error { return nil } -type Role struct { +type Company struct { Id int64 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 { Id int64 // Id: Primary key Age int64 @@ -58,8 +75,9 @@ type User struct { When time.Time CreditCard CreditCard Latitude float64 + CompanyId int64 + Company Role - RoleId int64 PasswordHash []byte IgnoreMe int64 `sql:"-"` IgnoreStringSlice []string `sql:"-"` @@ -172,7 +190,7 @@ func init() { 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)) } @@ -1749,17 +1767,32 @@ func TestHaving(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) var user2 User db.First(&user2, "name = ?", "anonymous_field") - db.Model(&user2).Related(&user2.Role) - if user2.Role.Name != "admin" { + db.Model(&user2).Related(&user2.Company) + if user2.Company.Name != "company" { 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) { 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 {