From 62fd13e04eef7321dc3d05dd735ea5738bba0a28 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 29 Jul 2014 16:25:38 +0800 Subject: [PATCH] Refact tests --- main_test.go | 15 +++++ migration_test.go | 4 -- relations_test.go | 2 +- structs_test.go | 137 +++++++++++++++++----------------------------- 4 files changed, 67 insertions(+), 91 deletions(-) diff --git a/main_test.go b/main_test.go index 0a197370..10cd022b 100644 --- a/main_test.go +++ b/main_test.go @@ -117,6 +117,16 @@ func TestSetTable(t *testing.T) { } } +type Order struct { +} + +type Cart struct { +} + +func (c Cart) TableName() string { + return "shopping_cart" +} + func TestTableName(t *testing.T) { db := db.Model("") if db.NewScope(Order{}).TableName() != "orders" { @@ -439,6 +449,11 @@ func TestTimeWithZone(t *testing.T) { } func TestHstore(t *testing.T) { + type Details struct { + Id int64 + Bulk gorm.Hstore + } + if dialect := os.Getenv("GORM_DIALECT"); dialect != "postgres" { t.Skip() } diff --git a/migration_test.go b/migration_test.go index 76ac4c17..08e04b27 100644 --- a/migration_test.go +++ b/migration_test.go @@ -51,10 +51,6 @@ func runMigration() { if err := db.AutoMigrate(Role{}).Error; err != nil { panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) } - - if err := db.AutoMigrate(UserCompany{}).Error; err != nil { - panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) - } } func TestIndexes(t *testing.T) { diff --git a/relations_test.go b/relations_test.go index 43e648e1..719aac9d 100644 --- a/relations_test.go +++ b/relations_test.go @@ -127,7 +127,7 @@ func TestRelated(t *testing.T) { } func TestQueryManyToManyWithRelated(t *testing.T) { - db.Model(&User{}).Related(&[]Language{}, "Languages") + // db.Model(&User{}).Related(&[]Language{}, "Languages") // SELECT `languages`.* FROM `languages` INNER JOIN `user_languages` ON `languages`.`id` = `user_languages`.`language_id` WHERE `user_languages`.`user_id` = 111 // db.Model(&User{}).Many2Many("Languages").Find(&[]Language{}) // db.Model(&User{}).Many2Many("Languages").Add(&Language{}) diff --git a/structs_test.go b/structs_test.go index ab44110b..95fd9c18 100644 --- a/structs_test.go +++ b/structs_test.go @@ -5,56 +5,10 @@ import ( "database/sql/driver" "errors" - "github.com/jinzhu/gorm" - "reflect" "time" ) -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 Language struct { - Id int - Name string -} - -type Ignored struct { - Name string -} - -type Num int64 - -func (i *Num) Scan(src interface{}) error { - switch s := src.(type) { - case []byte: - case int64: - *i = Num(s) - default: - return errors.New("Cannot scan NamedInt from " + reflect.ValueOf(src).String()) - } - return nil -} - type User struct { Id int64 Age int64 @@ -64,7 +18,6 @@ type User struct { CreatedAt time.Time // CreatedAt: Time of record is created, will be insert automatically UpdatedAt time.Time // UpdatedAt: Time of record is updated, will be updated automatically Emails []Email // Embedded structs - Ignored Ignored `sql:"-"` BillingAddress Address // Embedded struct BillingAddressId sql.NullInt64 // Embedded struct's foreign key ShippingAddress Address // Embedded struct @@ -75,18 +28,9 @@ type User struct { Company Role PasswordHash []byte - IgnoreMe int64 `sql:"-"` - IgnoreStringSlice []string `sql:"-"` -} - -type UserCompany struct { - Id int64 - UserId int64 - CompanyId int64 -} - -func (t UserCompany) TableName() string { - return "user_companies" + IgnoreMe int64 `sql:"-"` + IgnoreStringSlice []string `sql:"-"` + Ignored struct{ Name string } `sql:"-"` } type CreditCard struct { @@ -133,6 +77,41 @@ type Product struct { AfterDeleteCallTimes int64 } +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 Num int64 + +func (i *Num) Scan(src interface{}) error { + switch s := src.(type) { + case []byte: + case int64: + *i = Num(s) + default: + return errors.New("Cannot scan NamedInt from " + reflect.ValueOf(src).String()) + } + return nil +} + type Animal struct { Counter int64 `primaryKey:"yes"` Name string @@ -141,16 +120,6 @@ type Animal struct { UpdatedAt time.Time } -type Details struct { - Id int64 - Bulk gorm.Hstore -} - -type Category struct { - Id int64 - Name string -} - type Post struct { Id int64 CategoryId sql.NullInt64 @@ -162,6 +131,11 @@ type Post struct { MainCategory Category } +type Category struct { + Id int64 + Name string +} + type Comment struct { Id int64 PostId int64 @@ -169,14 +143,14 @@ type Comment struct { Post Post } -type Order struct { -} - -type Cart struct { -} - -func (c Cart) TableName() string { - return "shopping_cart" +// Scanner +type NullValue struct { + Id int64 + Name sql.NullString `sql:"not null"` + Age sql.NullInt64 + Male sql.NullBool + Height sql.NullFloat64 + AddedAt NullTime } type NullTime struct { @@ -199,12 +173,3 @@ func (nt NullTime) Value() (driver.Value, error) { } return nt.Time, nil } - -type NullValue struct { - Id int64 - Name sql.NullString `sql:"not null"` - Age sql.NullInt64 - Male sql.NullBool - Height sql.NullFloat64 - AddedAt NullTime -}