gorm/tests/associations_has_many_test.go

457 lines
12 KiB
Go
Raw Normal View History

2020-05-26 04:48:12 +03:00
package tests_test
import (
"testing"
2020-06-02 05:34:50 +03:00
. "gorm.io/gorm/utils/tests"
2020-05-26 04:48:12 +03:00
)
func TestHasManyAssociation(t *testing.T) {
var user = *GetUser("hasmany", Config{Pets: 2})
if err := DB.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
CheckUser(t, user, user)
// Find
var user2 User
DB.Find(&user2, "id = ?", user.ID)
DB.Model(&user2).Association("Pets").Find(&user2.Pets)
CheckUser(t, user2, user)
// Count
AssertAssociationCount(t, user, "Pets", 2, "")
// Append
var pet = Pet{Name: "pet-has-many-append"}
if err := DB.Model(&user2).Association("Pets").Append(&pet); err != nil {
t.Fatalf("Error happened when append account, got %v", err)
}
if pet.ID == 0 {
t.Fatalf("Pet's ID should be created")
}
user.Pets = append(user.Pets, &pet)
CheckUser(t, user2, user)
AssertAssociationCount(t, user, "Pets", 3, "AfterAppend")
var pets = []Pet{{Name: "pet-has-many-append-1-1"}, {Name: "pet-has-many-append-1-1"}}
if err := DB.Model(&user2).Association("Pets").Append(&pets); err != nil {
t.Fatalf("Error happened when append pet, got %v", err)
}
for _, pet := range pets {
var pet = pet
if pet.ID == 0 {
t.Fatalf("Pet's ID should be created")
}
user.Pets = append(user.Pets, &pet)
}
CheckUser(t, user2, user)
AssertAssociationCount(t, user, "Pets", 5, "AfterAppendSlice")
// Replace
var pet2 = Pet{Name: "pet-has-many-replace"}
if err := DB.Model(&user2).Association("Pets").Replace(&pet2); err != nil {
t.Fatalf("Error happened when append pet, got %v", err)
}
if pet2.ID == 0 {
t.Fatalf("pet2's ID should be created")
}
user.Pets = []*Pet{&pet2}
CheckUser(t, user2, user)
AssertAssociationCount(t, user2, "Pets", 1, "AfterReplace")
// Delete
if err := DB.Model(&user2).Association("Pets").Delete(&Pet{}); err != nil {
t.Fatalf("Error happened when delete pet, got %v", err)
}
AssertAssociationCount(t, user2, "Pets", 1, "after delete non-existing data")
if err := DB.Model(&user2).Association("Pets").Delete(&pet2); err != nil {
t.Fatalf("Error happened when delete Pets, got %v", err)
}
AssertAssociationCount(t, user2, "Pets", 0, "after delete")
// Prepare Data for Clear
if err := DB.Model(&user2).Association("Pets").Append(&pet); err != nil {
t.Fatalf("Error happened when append Pets, got %v", err)
}
AssertAssociationCount(t, user2, "Pets", 1, "after prepare data")
// Clear
if err := DB.Model(&user2).Association("Pets").Clear(); err != nil {
t.Errorf("Error happened when clear Pets, got %v", err)
}
AssertAssociationCount(t, user2, "Pets", 0, "after clear")
}
func TestSingleTableHasManyAssociation(t *testing.T) {
var user = *GetUser("hasmany", Config{Team: 2})
if err := DB.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
CheckUser(t, user, user)
// Find
var user2 User
DB.Find(&user2, "id = ?", user.ID)
DB.Model(&user2).Association("Team").Find(&user2.Team)
CheckUser(t, user2, user)
// Count
AssertAssociationCount(t, user, "Team", 2, "")
// Append
var team = *GetUser("team", Config{})
if err := DB.Model(&user2).Association("Team").Append(&team); err != nil {
t.Fatalf("Error happened when append account, got %v", err)
}
if team.ID == 0 {
t.Fatalf("Team's ID should be created")
}
user.Team = append(user.Team, team)
CheckUser(t, user2, user)
AssertAssociationCount(t, user, "Team", 3, "AfterAppend")
var teams = []User{*GetUser("team-append-1", Config{}), *GetUser("team-append-2", Config{})}
if err := DB.Model(&user2).Association("Team").Append(&teams); err != nil {
t.Fatalf("Error happened when append team, got %v", err)
}
for _, team := range teams {
var team = team
if team.ID == 0 {
t.Fatalf("Team's ID should be created")
}
user.Team = append(user.Team, team)
}
CheckUser(t, user2, user)
AssertAssociationCount(t, user, "Team", 5, "AfterAppendSlice")
// Replace
var team2 = *GetUser("team-replace", Config{})
if err := DB.Model(&user2).Association("Team").Replace(&team2); err != nil {
t.Fatalf("Error happened when append team, got %v", err)
}
if team2.ID == 0 {
t.Fatalf("team2's ID should be created")
}
user.Team = []User{team2}
CheckUser(t, user2, user)
AssertAssociationCount(t, user2, "Team", 1, "AfterReplace")
// Delete
if err := DB.Model(&user2).Association("Team").Delete(&User{}); err != nil {
t.Fatalf("Error happened when delete team, got %v", err)
}
AssertAssociationCount(t, user2, "Team", 1, "after delete non-existing data")
if err := DB.Model(&user2).Association("Team").Delete(&team2); err != nil {
t.Fatalf("Error happened when delete Team, got %v", err)
}
AssertAssociationCount(t, user2, "Team", 0, "after delete")
// Prepare Data for Clear
if err := DB.Model(&user2).Association("Team").Append(&team); err != nil {
t.Fatalf("Error happened when append Team, got %v", err)
}
AssertAssociationCount(t, user2, "Team", 1, "after prepare data")
// Clear
if err := DB.Model(&user2).Association("Team").Clear(); err != nil {
t.Errorf("Error happened when clear Team, got %v", err)
}
AssertAssociationCount(t, user2, "Team", 0, "after clear")
}
func TestHasManyAssociationForSlice(t *testing.T) {
var users = []User{
*GetUser("slice-hasmany-1", Config{Pets: 2}),
*GetUser("slice-hasmany-2", Config{Pets: 0}),
*GetUser("slice-hasmany-3", Config{Pets: 4}),
}
DB.Create(&users)
// Count
AssertAssociationCount(t, users, "Pets", 6, "")
// Find
var pets []Pet
if DB.Model(&users).Association("Pets").Find(&pets); len(pets) != 6 {
t.Errorf("pets count should be %v, but got %v", 6, len(pets))
}
// Append
DB.Model(&users).Association("Pets").Append(
&Pet{Name: "pet-slice-append-1"},
[]*Pet{{Name: "pet-slice-append-2-1"}, {Name: "pet-slice-append-2-2"}},
&Pet{Name: "pet-slice-append-3"},
)
AssertAssociationCount(t, users, "Pets", 10, "After Append")
// Replace -> same as append
DB.Model(&users).Association("Pets").Replace(
[]*Pet{{Name: "pet-slice-replace-1-1"}, {Name: "pet-slice-replace-1-2"}},
[]*Pet{{Name: "pet-slice-replace-2-1"}, {Name: "pet-slice-replace-2-2"}},
&Pet{Name: "pet-slice-replace-3"},
)
AssertAssociationCount(t, users, "Pets", 5, "After Append")
// Delete
if err := DB.Model(&users).Association("Pets").Delete(&users[2].Pets); err != nil {
t.Errorf("no error should happend when deleting pet, but got %v", err)
}
AssertAssociationCount(t, users, "Pets", 4, "after delete")
if err := DB.Model(&users).Association("Pets").Delete(users[0].Pets[0], users[1].Pets[1]); err != nil {
t.Errorf("no error should happend when deleting pet, but got %v", err)
}
AssertAssociationCount(t, users, "Pets", 2, "after delete")
// Clear
DB.Model(&users).Association("Pets").Clear()
AssertAssociationCount(t, users, "Pets", 0, "After Clear")
}
func TestSingleTableHasManyAssociationForSlice(t *testing.T) {
var users = []User{
*GetUser("slice-hasmany-1", Config{Team: 2}),
*GetUser("slice-hasmany-2", Config{Team: 0}),
*GetUser("slice-hasmany-3", Config{Team: 4}),
}
if err := DB.Create(&users).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
// Count
AssertAssociationCount(t, users, "Team", 6, "")
// Find
var teams []User
if DB.Model(&users).Association("Team").Find(&teams); len(teams) != 6 {
t.Errorf("teams count should be %v, but got %v", 6, len(teams))
}
// Append
DB.Model(&users).Association("Team").Append(
&User{Name: "pet-slice-append-1"},
[]*User{{Name: "pet-slice-append-2-1"}, {Name: "pet-slice-append-2-2"}},
&User{Name: "pet-slice-append-3"},
)
AssertAssociationCount(t, users, "Team", 10, "After Append")
// Replace -> same as append
DB.Model(&users).Association("Team").Replace(
[]*User{{Name: "pet-slice-replace-1-1"}, {Name: "pet-slice-replace-1-2"}},
[]*User{{Name: "pet-slice-replace-2-1"}, {Name: "pet-slice-replace-2-2"}},
&User{Name: "pet-slice-replace-3"},
)
AssertAssociationCount(t, users, "Team", 5, "After Append")
// Delete
if err := DB.Model(&users).Association("Team").Delete(&users[2].Team); err != nil {
t.Errorf("no error should happend when deleting pet, but got %v", err)
}
AssertAssociationCount(t, users, "Team", 4, "after delete")
if err := DB.Model(&users).Association("Team").Delete(users[0].Team[0], users[1].Team[1]); err != nil {
t.Errorf("no error should happend when deleting pet, but got %v", err)
}
AssertAssociationCount(t, users, "Team", 2, "after delete")
// Clear
DB.Model(&users).Association("Team").Clear()
AssertAssociationCount(t, users, "Team", 0, "After Clear")
}
func TestPolymorphicHasManyAssociation(t *testing.T) {
var user = *GetUser("hasmany", Config{Toys: 2})
if err := DB.Create(&user).Error; err != nil {
t.Fatalf("errors happened when create: %v", err)
}
CheckUser(t, user, user)
// Find
var user2 User
DB.Find(&user2, "id = ?", user.ID)
DB.Model(&user2).Association("Toys").Find(&user2.Toys)
CheckUser(t, user2, user)
// Count
AssertAssociationCount(t, user, "Toys", 2, "")
// Append
var toy = Toy{Name: "toy-has-many-append"}
if err := DB.Model(&user2).Association("Toys").Append(&toy); err != nil {
t.Fatalf("Error happened when append account, got %v", err)
}
if toy.ID == 0 {
t.Fatalf("Toy's ID should be created")
}
user.Toys = append(user.Toys, toy)
CheckUser(t, user2, user)
AssertAssociationCount(t, user, "Toys", 3, "AfterAppend")
var toys = []Toy{{Name: "toy-has-many-append-1-1"}, {Name: "toy-has-many-append-1-1"}}
if err := DB.Model(&user2).Association("Toys").Append(&toys); err != nil {
t.Fatalf("Error happened when append toy, got %v", err)
}
for _, toy := range toys {
var toy = toy
if toy.ID == 0 {
t.Fatalf("Toy's ID should be created")
}
user.Toys = append(user.Toys, toy)
}
CheckUser(t, user2, user)
AssertAssociationCount(t, user, "Toys", 5, "AfterAppendSlice")
// Replace
var toy2 = Toy{Name: "toy-has-many-replace"}
if err := DB.Model(&user2).Association("Toys").Replace(&toy2); err != nil {
t.Fatalf("Error happened when append toy, got %v", err)
}
if toy2.ID == 0 {
t.Fatalf("toy2's ID should be created")
}
user.Toys = []Toy{toy2}
CheckUser(t, user2, user)
AssertAssociationCount(t, user2, "Toys", 1, "AfterReplace")
// Delete
if err := DB.Model(&user2).Association("Toys").Delete(&Toy{}); err != nil {
t.Fatalf("Error happened when delete toy, got %v", err)
}
AssertAssociationCount(t, user2, "Toys", 1, "after delete non-existing data")
if err := DB.Model(&user2).Association("Toys").Delete(&toy2); err != nil {
t.Fatalf("Error happened when delete Toys, got %v", err)
}
AssertAssociationCount(t, user2, "Toys", 0, "after delete")
// Prepare Data for Clear
if err := DB.Model(&user2).Association("Toys").Append(&toy); err != nil {
t.Fatalf("Error happened when append Toys, got %v", err)
}
AssertAssociationCount(t, user2, "Toys", 1, "after prepare data")
// Clear
if err := DB.Model(&user2).Association("Toys").Clear(); err != nil {
t.Errorf("Error happened when clear Toys, got %v", err)
}
AssertAssociationCount(t, user2, "Toys", 0, "after clear")
}
func TestPolymorphicHasManyAssociationForSlice(t *testing.T) {
var users = []User{
*GetUser("slice-hasmany-1", Config{Toys: 2}),
*GetUser("slice-hasmany-2", Config{Toys: 0}),
*GetUser("slice-hasmany-3", Config{Toys: 4}),
}
DB.Create(&users)
// Count
AssertAssociationCount(t, users, "Toys", 6, "")
// Find
var toys []Toy
if DB.Model(&users).Association("Toys").Find(&toys); len(toys) != 6 {
t.Errorf("toys count should be %v, but got %v", 6, len(toys))
}
// Append
DB.Model(&users).Association("Toys").Append(
&Toy{Name: "toy-slice-append-1"},
[]Toy{{Name: "toy-slice-append-2-1"}, {Name: "toy-slice-append-2-2"}},
&Toy{Name: "toy-slice-append-3"},
)
AssertAssociationCount(t, users, "Toys", 10, "After Append")
// Replace -> same as append
DB.Model(&users).Association("Toys").Replace(
[]*Toy{{Name: "toy-slice-replace-1-1"}, {Name: "toy-slice-replace-1-2"}},
[]*Toy{{Name: "toy-slice-replace-2-1"}, {Name: "toy-slice-replace-2-2"}},
&Toy{Name: "toy-slice-replace-3"},
)
AssertAssociationCount(t, users, "Toys", 5, "After Append")
// Delete
if err := DB.Model(&users).Association("Toys").Delete(&users[2].Toys); err != nil {
t.Errorf("no error should happend when deleting toy, but got %v", err)
}
AssertAssociationCount(t, users, "Toys", 4, "after delete")
if err := DB.Model(&users).Association("Toys").Delete(users[0].Toys[0], users[1].Toys[1]); err != nil {
t.Errorf("no error should happend when deleting toy, but got %v", err)
}
AssertAssociationCount(t, users, "Toys", 2, "after delete")
// Clear
DB.Model(&users).Association("Toys").Clear()
AssertAssociationCount(t, users, "Toys", 0, "After Clear")
}