2014-07-29 06:59:13 +04:00
|
|
|
package gorm_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2014-07-29 08:32:58 +04:00
|
|
|
"time"
|
2014-07-29 06:59:13 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
func runMigration() {
|
2015-02-28 12:01:27 +03:00
|
|
|
if err := DB.DropTableIfExists(&User{}).Error; err != nil {
|
2014-07-29 06:59:13 +04:00
|
|
|
fmt.Printf("Got error when try to delete table users, %+v\n", err)
|
|
|
|
}
|
|
|
|
|
2014-08-28 14:53:27 +04:00
|
|
|
for _, table := range []string{"animals", "user_languages"} {
|
|
|
|
DB.Exec(fmt.Sprintf("drop table %v;", table))
|
|
|
|
}
|
|
|
|
|
2015-12-26 10:19:56 +03:00
|
|
|
values := []interface{}{&Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Toy{}}
|
2014-08-28 14:53:27 +04:00
|
|
|
for _, value := range values {
|
|
|
|
DB.DropTable(value)
|
|
|
|
}
|
2014-08-28 11:33:43 +04:00
|
|
|
|
2014-08-28 14:53:27 +04:00
|
|
|
if err := DB.AutoMigrate(values...).Error; err != nil {
|
2014-07-29 13:28:10 +04:00
|
|
|
panic(fmt.Sprintf("No error should happen when create table, but got %+v", err))
|
|
|
|
}
|
2014-07-29 06:59:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIndexes(t *testing.T) {
|
2014-08-28 11:33:43 +04:00
|
|
|
if err := DB.Model(&Email{}).AddIndex("idx_email_email", "email").Error; err != nil {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("Got error when tried to create index: %+v", err)
|
|
|
|
}
|
|
|
|
|
2015-03-02 18:02:40 +03:00
|
|
|
scope := DB.NewScope(&Email{})
|
2016-02-15 09:09:24 +03:00
|
|
|
if !scope.Dialect().HasIndex(scope.TableName(), "idx_email_email") {
|
2015-03-02 18:02:40 +03:00
|
|
|
t.Errorf("Email should have index idx_email_email")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if err := DB.Model(&Email{}).RemoveIndex("idx_email_email").Error; err != nil {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("Got error when tried to remove index: %+v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-15 09:09:24 +03:00
|
|
|
if scope.Dialect().HasIndex(scope.TableName(), "idx_email_email") {
|
2015-03-02 18:02:40 +03:00
|
|
|
t.Errorf("Email's index idx_email_email should be deleted")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if err := DB.Model(&Email{}).AddIndex("idx_email_email_and_user_id", "user_id", "email").Error; err != nil {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("Got error when tried to create index: %+v", err)
|
|
|
|
}
|
2014-07-29 08:02:03 +04:00
|
|
|
|
2016-02-15 09:09:24 +03:00
|
|
|
if !scope.Dialect().HasIndex(scope.TableName(), "idx_email_email_and_user_id") {
|
2015-03-02 18:02:40 +03:00
|
|
|
t.Errorf("Email should have index idx_email_email_and_user_id")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if err := DB.Model(&Email{}).RemoveIndex("idx_email_email_and_user_id").Error; err != nil {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("Got error when tried to remove index: %+v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-15 09:09:24 +03:00
|
|
|
if scope.Dialect().HasIndex(scope.TableName(), "idx_email_email_and_user_id") {
|
2015-03-02 18:02:40 +03:00
|
|
|
t.Errorf("Email's index idx_email_email_and_user_id should be deleted")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if err := DB.Model(&Email{}).AddUniqueIndex("idx_email_email_and_user_id", "user_id", "email").Error; err != nil {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("Got error when tried to create index: %+v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-15 09:09:24 +03:00
|
|
|
if !scope.Dialect().HasIndex(scope.TableName(), "idx_email_email_and_user_id") {
|
2015-03-02 18:02:40 +03:00
|
|
|
t.Errorf("Email should have index idx_email_email_and_user_id")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if DB.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "user1@example.comiii"}, {Email: "user1@example.com"}, {Email: "user1@example.com"}}}).Error == nil {
|
2014-07-29 06:59:13 +04:00
|
|
|
t.Errorf("Should get to create duplicate record when having unique index")
|
|
|
|
}
|
2014-07-29 08:02:03 +04:00
|
|
|
|
2016-01-03 12:52:16 +03:00
|
|
|
var user = User{Name: "sample_user"}
|
|
|
|
DB.Save(&user)
|
|
|
|
if DB.Model(&user).Association("Emails").Append(Email{Email: "not-1duplicated@gmail.com"}, Email{Email: "not-duplicated2@gmail.com"}).Error != nil {
|
|
|
|
t.Errorf("Should get no error when append two emails for user")
|
|
|
|
}
|
|
|
|
|
|
|
|
if DB.Model(&user).Association("Emails").Append(Email{Email: "duplicated@gmail.com"}, Email{Email: "duplicated@gmail.com"}).Error == nil {
|
|
|
|
t.Errorf("Should get no duplicated email error when insert duplicated emails for a user")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if err := DB.Model(&Email{}).RemoveIndex("idx_email_email_and_user_id").Error; err != nil {
|
2014-07-29 08:02:03 +04:00
|
|
|
t.Errorf("Got error when tried to remove index: %+v", err)
|
|
|
|
}
|
|
|
|
|
2016-02-15 09:09:24 +03:00
|
|
|
if scope.Dialect().HasIndex(scope.TableName(), "idx_email_email_and_user_id") {
|
2015-03-02 18:02:40 +03:00
|
|
|
t.Errorf("Email's index idx_email_email_and_user_id should be deleted")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
if DB.Save(&User{Name: "unique_indexes", Emails: []Email{{Email: "user1@example.com"}, {Email: "user1@example.com"}}}).Error != nil {
|
2014-07-29 08:02:03 +04:00
|
|
|
t.Errorf("Should be able to create duplicated emails after remove unique index")
|
|
|
|
}
|
2014-07-29 06:59:13 +04:00
|
|
|
}
|
2014-07-29 08:32:58 +04:00
|
|
|
|
|
|
|
type BigEmail struct {
|
|
|
|
Id int64
|
|
|
|
UserId int64
|
2015-03-09 12:22:16 +03:00
|
|
|
Email string `sql:"index:idx_email_agent"`
|
|
|
|
UserAgent string `sql:"index:idx_email_agent"`
|
|
|
|
RegisteredAt time.Time `sql:"unique_index"`
|
2014-07-29 08:32:58 +04:00
|
|
|
CreatedAt time.Time
|
|
|
|
UpdatedAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b BigEmail) TableName() string {
|
|
|
|
return "emails"
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAutoMigration(t *testing.T) {
|
2014-11-25 09:41:09 +03:00
|
|
|
DB.AutoMigrate(&Address{})
|
|
|
|
if err := DB.Table("emails").AutoMigrate(&BigEmail{}).Error; err != nil {
|
2014-07-29 08:32:58 +04:00
|
|
|
t.Errorf("Auto Migrate should not raise any error")
|
|
|
|
}
|
|
|
|
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.Save(&BigEmail{Email: "jinzhu@example.org", UserAgent: "pc", RegisteredAt: time.Now()})
|
2014-07-29 08:32:58 +04:00
|
|
|
|
2015-03-09 12:22:16 +03:00
|
|
|
scope := DB.NewScope(&BigEmail{})
|
2016-02-15 09:09:24 +03:00
|
|
|
if !scope.Dialect().HasIndex(scope.TableName(), "idx_email_agent") {
|
2015-03-09 12:22:16 +03:00
|
|
|
t.Errorf("Failed to create index")
|
|
|
|
}
|
|
|
|
|
2016-02-15 09:09:24 +03:00
|
|
|
if !scope.Dialect().HasIndex(scope.TableName(), "uix_emails_registered_at") {
|
2015-03-09 12:22:16 +03:00
|
|
|
t.Errorf("Failed to create index")
|
|
|
|
}
|
|
|
|
|
2014-07-29 08:32:58 +04:00
|
|
|
var bigemail BigEmail
|
2014-08-28 11:33:43 +04:00
|
|
|
DB.First(&bigemail, "user_agent = ?", "pc")
|
2014-07-29 08:32:58 +04:00
|
|
|
if bigemail.Email != "jinzhu@example.org" || bigemail.UserAgent != "pc" || bigemail.RegisteredAt.IsZero() {
|
|
|
|
t.Error("Big Emails should be saved and fetched correctly")
|
|
|
|
}
|
|
|
|
}
|