package gorm_test import ( "fmt" "testing" "time" ) func runMigration() { if err := DB.DropTable(&User{}).Error; err != nil { fmt.Printf("Got error when try to delete table users, %+v\n", err) } DB.Exec("drop table products;") DB.Exec("drop table emails;") DB.Exec("drop table addresses") DB.Exec("drop table credit_cards") DB.Exec("drop table roles") DB.Exec("drop table companies") DB.Exec("drop table animals") DB.Exec("drop table user_languages") DB.Exec("drop table languages") if err := DB.CreateTable(&Animal{}).Error; err != nil { panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) } if err := DB.CreateTable(User{}).Error; err != nil { panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) } if err := DB.AutoMigrate(&Product{}, Email{}, Address{}, CreditCard{}, Company{}, Role{}, Language{}, HNPost{}, EngadgetPost{}).Error; err != nil { panic(fmt.Sprintf("No error should happen when create table, but got %+v", err)) } } func TestIndexes(t *testing.T) { if err := DB.Model(&Email{}).AddIndex("idx_email_email", "email").Error; err != nil { t.Errorf("Got error when tried to create index: %+v", err) } if err := DB.Model(&Email{}).RemoveIndex("idx_email_email").Error; err != nil { t.Errorf("Got error when tried to remove index: %+v", err) } if err := DB.Model(&Email{}).AddIndex("idx_email_email_and_user_id", "user_id", "email").Error; err != nil { t.Errorf("Got error when tried to create index: %+v", err) } if err := DB.Model(&Email{}).RemoveIndex("idx_email_email_and_user_id").Error; err != nil { t.Errorf("Got error when tried to remove index: %+v", err) } if err := DB.Model(&Email{}).AddUniqueIndex("idx_email_email_and_user_id", "user_id", "email").Error; err != nil { t.Errorf("Got error when tried to create index: %+v", err) } if DB.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "user1@example.comiii"}, {Email: "user1@example.com"}, {Email: "user1@example.com"}}}).Error == nil { t.Errorf("Should get to create duplicate record when having unique index") } if err := DB.Model(&Email{}).RemoveIndex("idx_email_email_and_user_id").Error; err != nil { t.Errorf("Got error when tried to remove index: %+v", err) } if DB.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "user1@example.com"}, {Email: "user1@example.com"}}}).Error != nil { t.Errorf("Should be able to create duplicated emails after remove unique index") } } type BigEmail struct { Id int64 UserId int64 Email string UserAgent string RegisteredAt time.Time CreatedAt time.Time UpdatedAt time.Time } func (b BigEmail) TableName() string { return "emails" } func TestAutoMigration(t *testing.T) { DB.AutoMigrate(Address{}) if err := DB.Table("emails").AutoMigrate(BigEmail{}).Error; err != nil { t.Errorf("Auto Migrate should not raise any error") } DB.Save(&BigEmail{Email: "jinzhu@example.org", UserAgent: "pc", RegisteredAt: time.Now()}) var bigemail BigEmail DB.First(&bigemail, "user_agent = ?", "pc") if bigemail.Email != "jinzhu@example.org" || bigemail.UserAgent != "pc" || bigemail.RegisteredAt.IsZero() { t.Error("Big Emails should be saved and fetched correctly") } }