gorm/tests/create.go

659 lines
16 KiB
Go
Raw Normal View History

2020-04-15 14:13:36 +03:00
package tests
import (
2020-04-17 03:40:07 +03:00
"fmt"
2020-04-15 14:13:36 +03:00
"testing"
"github.com/jinzhu/gorm"
)
func TestCreate(t *testing.T, db *gorm.DB) {
db.Migrator().DropTable(&User{})
db.AutoMigrate(&User{})
t.Run("Create", func(t *testing.T) {
var user = User{
Name: "create",
Age: 18,
Birthday: Now(),
}
if err := db.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
if user.ID == 0 {
t.Errorf("user's primary key should has value after create, got : %v", user.ID)
}
if user.CreatedAt.IsZero() {
t.Errorf("user's created at should be not zero")
}
if user.UpdatedAt.IsZero() {
t.Errorf("user's updated at should be not zero")
}
var newUser User
if err := db.Where("id = ?", user.ID).First(&newUser).Error; err != nil {
t.Errorf("errors happened when query: %v", err)
} else {
AssertObjEqual(t, newUser, user, "Name", "Age", "Birthday")
}
2020-04-15 18:58:26 +03:00
2020-04-20 06:47:29 +03:00
TestCreateAssociations(t, db)
})
2020-04-15 18:58:26 +03:00
}
func TestCreateAssociations(t *testing.T, db *gorm.DB) {
2020-04-20 18:35:18 +03:00
db.Migrator().DropTable(&Account{}, &Company{}, &Pet{}, &Toy{}, &Language{})
db.Migrator().AutoMigrate(&Account{}, &Company{}, &Pet{}, &Toy{}, &Language{})
2020-04-20 06:47:29 +03:00
TestCreateBelongsToAssociations(t, db)
TestCreateHasOneAssociations(t, db)
TestCreateHasManyAssociations(t, db)
TestCreateMany2ManyAssociations(t, db)
}
func TestCreateBelongsToAssociations(t *testing.T, db *gorm.DB) {
check := func(t *testing.T, user User) {
if user.Company.Name != "" {
if user.CompanyID == nil {
t.Errorf("Company's foreign key should be saved")
} else {
var company Company
db.First(&company, "id = ?", *user.CompanyID)
if company.Name != user.Company.Name {
t.Errorf("Company's name should be same")
}
}
} else if user.CompanyID != nil {
t.Errorf("Company should not be created for zero value, got: %+v", user.CompanyID)
}
if user.Manager != nil {
if user.ManagerID == nil {
t.Errorf("Manager's foreign key should be saved")
} else {
var manager User
db.First(&manager, "id = ?", *user.ManagerID)
if manager.Name != user.Manager.Name {
t.Errorf("Manager's name should be same")
}
}
} else if user.ManagerID != nil {
t.Errorf("Manager should not be created for zero value, got: %+v", user.ManagerID)
}
}
t.Run("BelongsTo", func(t *testing.T) {
2020-04-15 18:58:26 +03:00
var user = User{
Name: "create",
Age: 18,
Birthday: Now(),
Company: Company{Name: "company-belongs-to-association"},
Manager: &User{Name: "manager-belongs-to-association"},
}
if err := db.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
2020-04-20 06:47:29 +03:00
check(t, user)
})
t.Run("BelongsToForBulkInsert", func(t *testing.T) {
var users = []User{{
Name: "create-1",
Age: 18,
Birthday: Now(),
Company: Company{Name: "company-belongs-to-association-1"},
Manager: &User{Name: "manager-belongs-to-association-1"},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Company: Company{Name: "company-belongs-to-association-2"},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Company: Company{Name: "company-belongs-to-association-3"},
Manager: &User{Name: "manager-belongs-to-association-3"},
}}
if err := db.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
2020-04-15 18:58:26 +03:00
}
2020-04-20 06:47:29 +03:00
for _, user := range users {
check(t, user)
2020-04-15 18:58:26 +03:00
}
})
2020-04-17 03:23:47 +03:00
2020-04-20 06:47:29 +03:00
t.Run("BelongsToForBulkInsertPtrData", func(t *testing.T) {
var users = []*User{{
Name: "create-1",
2020-04-17 03:23:47 +03:00
Age: 18,
Birthday: Now(),
2020-04-20 06:47:29 +03:00
Company: Company{Name: "company-belongs-to-association-1"},
Manager: &User{Name: "manager-belongs-to-association-1"},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Company: Company{Name: "company-belongs-to-association-2"},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Company: Company{Name: "company-belongs-to-association-3"},
Manager: &User{Name: "manager-belongs-to-association-3"},
}}
if err := db.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
2020-04-17 03:23:47 +03:00
}
2020-04-20 06:47:29 +03:00
for _, user := range users {
check(t, *user)
}
})
t.Run("BelongsToForBulkInsertWithoutPtr", func(t *testing.T) {
var users = []*User{{
Name: "create-1",
Age: 18,
Birthday: Now(),
Company: Company{Name: "company-belongs-to-association-1"},
Manager: &User{Name: "manager-belongs-to-association-1"},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Company: Company{Name: "company-belongs-to-association-2"},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Company: Company{Name: "company-belongs-to-association-3"},
Manager: &User{Name: "manager-belongs-to-association-3"},
}}
if err := db.Create(users).Error; err != nil {
2020-04-17 03:23:47 +03:00
t.Fatalf("errors happened when create: %v", err)
}
2020-04-20 06:47:29 +03:00
for _, user := range users {
check(t, *user)
}
})
}
func TestCreateHasOneAssociations(t *testing.T, db *gorm.DB) {
check := func(t *testing.T, user User) {
2020-04-17 03:23:47 +03:00
if user.Account.ID == 0 {
2020-04-20 06:47:29 +03:00
t.Errorf("Account should be saved")
2020-04-17 03:23:47 +03:00
} else if user.Account.UserID.Int64 != int64(user.ID) {
2020-04-20 06:47:29 +03:00
t.Errorf("Account's foreign key should be saved")
2020-04-17 03:23:47 +03:00
} else {
var account Account
db.First(&account, "id = ?", user.Account.ID)
2020-04-20 06:47:29 +03:00
if account.Number != user.Account.Number {
t.Errorf("Account's number should be sme")
2020-04-17 03:23:47 +03:00
}
}
2020-04-20 06:47:29 +03:00
}
t.Run("HasOne", func(t *testing.T) {
var user = User{
Name: "create",
Age: 18,
Birthday: Now(),
Account: Account{Number: "account-has-one-association"},
}
if err := db.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
check(t, user)
2020-04-17 03:23:47 +03:00
})
2020-04-17 03:40:07 +03:00
2020-04-20 06:47:29 +03:00
t.Run("HasOneForBulkInsert", func(t *testing.T) {
var users = []User{{
Name: "create-1",
Age: 18,
Birthday: Now(),
Account: Account{Number: "account-has-one-association-1"},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Account: Account{Number: "account-has-one-association-2"},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Account: Account{Number: "account-has-one-association-3"},
}}
if err := db.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
2020-04-17 03:40:07 +03:00
}
2020-04-20 06:47:29 +03:00
for _, user := range users {
check(t, user)
}
})
t.Run("HasOneForBulkInsertPtrData", func(t *testing.T) {
var users = []*User{{
Name: "create-1",
Age: 18,
Birthday: Now(),
Account: Account{Number: "account-has-one-association-1"},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Account: Account{Number: "account-has-one-association-2"},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Account: Account{Number: "account-has-one-association-3"},
}}
if err := db.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
for _, user := range users {
check(t, *user)
}
})
t.Run("HasOneForBulkInsertWithoutPtr", func(t *testing.T) {
var users = []User{{
Name: "create-1",
Age: 18,
Birthday: Now(),
Account: Account{Number: "account-has-one-association-1"},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Account: Account{Number: "account-has-one-association-2"},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Account: Account{Number: "account-has-one-association-3"},
}}
if err := db.Create(users).Error; err != nil {
2020-04-17 03:40:07 +03:00
t.Fatalf("errors happened when create: %v", err)
}
2020-04-20 06:47:29 +03:00
for _, user := range users {
check(t, user)
}
})
checkPet := func(t *testing.T, pet Pet) {
2020-04-17 03:40:07 +03:00
if pet.Toy.OwnerID != fmt.Sprint(pet.ID) || pet.Toy.OwnerType != "pets" {
t.Errorf("Failed to create polymorphic has one association - toy owner id %v, owner type %v", pet.Toy.OwnerID, pet.Toy.OwnerType)
} else {
var toy Toy
db.First(&toy, "owner_id = ? and owner_type = ?", pet.Toy.OwnerID, pet.Toy.OwnerType)
2020-04-20 06:47:29 +03:00
if toy.Name != pet.Toy.Name {
2020-04-17 03:40:07 +03:00
t.Errorf("Failed to query saved polymorphic has one association")
}
}
2020-04-20 06:47:29 +03:00
}
t.Run("PolymorphicHasOne", func(t *testing.T) {
var pet = Pet{
Name: "create",
Toy: Toy{Name: "Create-HasOneAssociation-Polymorphic"},
}
if err := db.Create(&pet).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
checkPet(t, pet)
})
t.Run("PolymorphicHasOneForBulkInsert", func(t *testing.T) {
var pets = []Pet{{
Name: "create-1",
Toy: Toy{Name: "Create-HasOneAssociation-Polymorphic-1"},
}, {
Name: "create-2",
Toy: Toy{Name: "Create-HasOneAssociation-Polymorphic-2"},
}, {
Name: "create-3",
Toy: Toy{Name: "Create-HasOneAssociation-Polymorphic-3"},
}}
if err := db.Create(&pets).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
for _, pet := range pets {
checkPet(t, pet)
}
})
t.Run("PolymorphicHasOneForBulkInsertPtrData", func(t *testing.T) {
var pets = []*Pet{{
Name: "create-1",
Toy: Toy{Name: "Create-HasOneAssociation-Polymorphic-1"},
}, {
Name: "create-2",
Toy: Toy{Name: "Create-HasOneAssociation-Polymorphic-2"},
}, {
Name: "create-3",
Toy: Toy{Name: "Create-HasOneAssociation-Polymorphic-3"},
}}
if err := db.Create(&pets).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
for _, pet := range pets {
checkPet(t, *pet)
}
2020-04-17 03:40:07 +03:00
})
2020-04-19 09:29:31 +03:00
2020-04-20 06:47:29 +03:00
t.Run("PolymorphicHasOneForBulkInsertWithoutPtr", func(t *testing.T) {
var pets = []*Pet{{
Name: "create-1",
Toy: Toy{Name: "Create-HasOneAssociation-Polymorphic-1"},
}, {
Name: "create-2",
Toy: Toy{Name: "Create-HasOneAssociation-Polymorphic-2"},
}, {
Name: "create-3",
Toy: Toy{Name: "Create-HasOneAssociation-Polymorphic-3"},
}}
if err := db.Create(pets).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
for _, pet := range pets {
checkPet(t, *pet)
}
})
}
func TestCreateHasManyAssociations(t *testing.T, db *gorm.DB) {
2020-04-20 18:35:18 +03:00
check := func(t *testing.T, user User) {
for _, pet := range user.Pets {
if pet.ID == 0 {
t.Errorf("Pet's foreign key should be saved")
}
var result Pet
db.First(&result, "id = ?", pet.ID)
if result.Name != pet.Name {
t.Errorf("Pet's name should be same")
} else if result.UserID != user.ID {
t.Errorf("Pet's foreign key should be saved")
}
}
}
2020-04-20 06:47:29 +03:00
t.Run("HasMany", func(t *testing.T) {
2020-04-19 09:29:31 +03:00
var user = User{
Name: "create",
Age: 18,
Birthday: Now(),
Pets: []*Pet{{Name: "pet1"}, {Name: "pet2"}},
}
if err := db.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
2020-04-20 18:35:18 +03:00
check(t, user)
})
2020-04-19 09:29:31 +03:00
2020-04-20 18:35:18 +03:00
t.Run("HasManyForBulkInsert", func(t *testing.T) {
var users = []User{{
Name: "create-1",
Age: 18,
Birthday: Now(),
Pets: []*Pet{{Name: "pet-1-1"}, {Name: "pet-1-2"}},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Pets: []*Pet{{Name: "pet-2-1"}},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Pets: []*Pet{{Name: "pet-3-1"}, {Name: "pet-3-2"}},
}}
if err := db.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
for _, user := range users {
check(t, user)
2020-04-19 09:29:31 +03:00
}
})
2020-04-20 18:35:18 +03:00
t.Run("HasManyForBulkInsertPtrData", func(t *testing.T) {
var users = []*User{{
Name: "create-1",
2020-04-19 09:29:31 +03:00
Age: 18,
Birthday: Now(),
2020-04-20 18:35:18 +03:00
Pets: []*Pet{{Name: "pet-1-1"}, {Name: "pet-1-2"}},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Pets: []*Pet{{Name: "pet-2-1"}},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Pets: []*Pet{{Name: "pet-3-1"}, {Name: "pet-3-2"}},
}}
if err := db.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
2020-04-19 09:29:31 +03:00
}
2020-04-20 18:35:18 +03:00
for _, user := range users {
check(t, *user)
}
})
t.Run("HasManyForBulkInsertWithoutPtr", func(t *testing.T) {
var users = []*User{{
Name: "create-1",
Age: 18,
Birthday: Now(),
Pets: []*Pet{{Name: "pet-1-1"}, {Name: "pet-1-2"}},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Pets: []*Pet{{Name: "pet-2-1"}},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Pets: []*Pet{{Name: "pet-3-1"}, {Name: "pet-3-2"}},
}}
if err := db.Create(users).Error; err != nil {
2020-04-19 09:29:31 +03:00
t.Fatalf("errors happened when create: %v", err)
}
2020-04-20 18:35:18 +03:00
for _, user := range users {
check(t, *user)
}
})
checkToy := func(t *testing.T, user User) {
2020-04-19 09:29:31 +03:00
for idx, toy := range user.Toys {
if toy.ID == 0 {
t.Fatalf("Failed to create toy #%v", idx)
}
var result Toy
db.First(&result, "id = ?", toy.ID)
if result.Name != toy.Name {
t.Errorf("Failed to query saved toy")
} else if result.OwnerID != fmt.Sprint(user.ID) || result.OwnerType != "users" {
t.Errorf("Failed to save relation")
}
}
2020-04-20 18:35:18 +03:00
}
t.Run("PolymorphicHasMany", func(t *testing.T) {
var user = User{
Name: "create",
Age: 18,
Birthday: Now(),
Toys: []Toy{{Name: "toy1"}, {Name: "toy2"}},
}
if err := db.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
checkToy(t, user)
})
t.Run("PolymorphicHasManyForBulkInsert", func(t *testing.T) {
var users = []User{{
Name: "create-1",
Age: 18,
Birthday: Now(),
Toys: []Toy{{Name: "toy-1-1"}, {Name: "toy-1-2"}},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Toys: []Toy{{Name: "toy-2-1"}, {Name: "toy-2-2"}},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Toys: []Toy{{Name: "toy-3-1"}, {Name: "toy-3-2"}},
}}
if err := db.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
for _, user := range users {
checkToy(t, user)
}
})
t.Run("PolymorphicHasManyForBulkInsertPtrData", func(t *testing.T) {
var users = []*User{{
Name: "create-1",
Age: 18,
Birthday: Now(),
Toys: []Toy{{Name: "toy-1-1"}, {Name: "toy-1-2"}},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Toys: []Toy{{Name: "toy-2-1"}, {Name: "toy-2-2"}},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Toys: []Toy{{Name: "toy-3-1"}, {Name: "toy-3-2"}},
}}
if err := db.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
for _, user := range users {
checkToy(t, *user)
}
})
t.Run("PolymorphicHasManyForBulkInsertWithoutPtr", func(t *testing.T) {
var users = []User{{
Name: "create-1",
Age: 18,
Birthday: Now(),
Toys: []Toy{{Name: "toy-1-1"}, {Name: "toy-1-2"}},
}, {
Name: "create-2",
Age: 28,
Birthday: Now(),
Toys: []Toy{{Name: "toy-2-1"}, {Name: "toy-2-2"}},
}, {
Name: "create-3",
Age: 38,
Birthday: Now(),
Toys: []Toy{{Name: "toy-3-1"}, {Name: "toy-3-2"}},
}}
if err := db.Create(users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
for _, user := range users {
checkToy(t, user)
}
2020-04-19 09:29:31 +03:00
})
2020-04-15 14:13:36 +03:00
}
2020-04-20 06:47:29 +03:00
func TestCreateMany2ManyAssociations(t *testing.T, db *gorm.DB) {
2020-04-20 18:35:18 +03:00
check := func(t *testing.T, user User) {
for _, language := range user.Languages {
var result Language
db.First(&result, "code = ?", language.Code)
// TODO
// if result.Name != language.Name {
// t.Errorf("Language's name should be same")
// }
}
for _, f := range user.Friends {
if f.ID == 0 {
t.Errorf("Friend's foreign key should be saved")
}
var result User
db.First(&result, "id = ?", f.ID)
if result.Name != f.Name {
t.Errorf("Friend's name should be same")
}
}
}
t.Run("Many2Many", func(t *testing.T) {
var user = User{
Name: "create",
Age: 18,
Birthday: Now(),
Languages: []Language{{Code: "zh-CN", Name: "Chinese"}, {Code: "en", Name: "English"}},
Friends: []*User{{Name: "friend-1"}, {Name: "friend-2"}},
}
if err := db.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
check(t, user)
})
2020-04-20 06:47:29 +03:00
}