2020-05-23 18:50:48 +03:00
|
|
|
package tests_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/jinzhu/gorm/tests"
|
|
|
|
)
|
|
|
|
|
2020-05-24 15:44:37 +03:00
|
|
|
func AssertAssociationCount(t *testing.T, data interface{}, name string, result int64, reason string) {
|
|
|
|
if count := DB.Model(data).Association(name).Count(); count != result {
|
|
|
|
t.Errorf("invalid %v count %v, expects: %v got %v", name, reason, result, count)
|
|
|
|
}
|
|
|
|
|
|
|
|
var newUser User
|
|
|
|
if user, ok := data.(User); ok {
|
|
|
|
DB.Find(&newUser, "id = ?", user.ID)
|
|
|
|
} else if user, ok := data.(*User); ok {
|
|
|
|
DB.Find(&newUser, "id = ?", user.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if newUser.ID != 0 {
|
|
|
|
if count := DB.Model(&newUser).Association(name).Count(); count != result {
|
|
|
|
t.Errorf("invalid %v count %v, expects: %v got %v", name, reason, result, count)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBelongsToAssociation(t *testing.T) {
|
2020-05-23 18:50:48 +03:00
|
|
|
var user = *GetUser("belongs-to", Config{Company: true, Manager: true})
|
|
|
|
|
|
|
|
if err := DB.Create(&user).Error; err != nil {
|
|
|
|
t.Fatalf("errors happened when create: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckUser(t, user, user)
|
|
|
|
|
2020-05-24 12:24:23 +03:00
|
|
|
// Find
|
2020-05-23 18:50:48 +03:00
|
|
|
var user2 User
|
|
|
|
DB.Find(&user2, "id = ?", user.ID)
|
|
|
|
DB.Model(&user2).Association("Company").Find(&user2.Company)
|
|
|
|
user2.Manager = &User{}
|
|
|
|
DB.Model(&user2).Association("Manager").Find(user2.Manager)
|
|
|
|
CheckUser(t, user2, user)
|
2020-05-24 06:32:59 +03:00
|
|
|
|
2020-05-24 12:24:23 +03:00
|
|
|
// Count
|
2020-05-24 15:44:37 +03:00
|
|
|
AssertAssociationCount(t, user, "Company", 1, "")
|
|
|
|
AssertAssociationCount(t, user, "Manager", 1, "")
|
2020-05-24 12:24:23 +03:00
|
|
|
|
|
|
|
// Append
|
|
|
|
var company = Company{Name: "company-belongs-to-append"}
|
|
|
|
var manager = GetUser("manager-belongs-to-append", Config{})
|
|
|
|
|
|
|
|
if err := DB.Model(&user2).Association("Company").Append(&company); err != nil {
|
|
|
|
t.Fatalf("Error happened when append Company, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if company.ID == 0 {
|
|
|
|
t.Fatalf("Company's ID should be created")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Model(&user2).Association("Manager").Append(manager); err != nil {
|
|
|
|
t.Fatalf("Error happened when append Manager, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if manager.ID == 0 {
|
|
|
|
t.Fatalf("Manager's ID should be created")
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Company = company
|
|
|
|
user.Manager = manager
|
|
|
|
user.CompanyID = &company.ID
|
|
|
|
user.ManagerID = &manager.ID
|
|
|
|
CheckUser(t, user2, user)
|
|
|
|
|
2020-05-24 15:44:37 +03:00
|
|
|
AssertAssociationCount(t, user2, "Company", 1, "AfterAppend")
|
|
|
|
AssertAssociationCount(t, user2, "Manager", 1, "AfterAppend")
|
|
|
|
|
2020-05-24 12:24:23 +03:00
|
|
|
// Replace
|
|
|
|
var company2 = Company{Name: "company-belongs-to-replace"}
|
|
|
|
var manager2 = GetUser("manager-belongs-to-replace", Config{})
|
|
|
|
|
|
|
|
if err := DB.Model(&user2).Association("Company").Replace(&company2); err != nil {
|
|
|
|
t.Fatalf("Error happened when replace Company, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if company2.ID == 0 {
|
|
|
|
t.Fatalf("Company's ID should be created")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Model(&user2).Association("Manager").Replace(manager2); err != nil {
|
|
|
|
t.Fatalf("Error happened when replace Manager, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if manager2.ID == 0 {
|
|
|
|
t.Fatalf("Manager's ID should be created")
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Company = company2
|
|
|
|
user.Manager = manager2
|
|
|
|
user.CompanyID = &company2.ID
|
|
|
|
user.ManagerID = &manager2.ID
|
|
|
|
CheckUser(t, user2, user)
|
|
|
|
|
2020-05-24 15:44:37 +03:00
|
|
|
AssertAssociationCount(t, user2, "Company", 1, "AfterReplace")
|
|
|
|
AssertAssociationCount(t, user2, "Manager", 1, "AfterReplace")
|
|
|
|
|
2020-05-24 12:24:23 +03:00
|
|
|
// Delete
|
|
|
|
if err := DB.Model(&user2).Association("Company").Delete(&Company{}); err != nil {
|
|
|
|
t.Fatalf("Error happened when delete Company, got %v", err)
|
|
|
|
}
|
2020-05-24 15:44:37 +03:00
|
|
|
AssertAssociationCount(t, user2, "Company", 1, "after delete non-existing data")
|
2020-05-24 12:24:23 +03:00
|
|
|
|
|
|
|
if err := DB.Model(&user2).Association("Company").Delete(&company2); err != nil {
|
|
|
|
t.Fatalf("Error happened when delete Company, got %v", err)
|
|
|
|
}
|
2020-05-24 15:44:37 +03:00
|
|
|
AssertAssociationCount(t, user2, "Company", 0, "after delete")
|
2020-05-24 12:24:23 +03:00
|
|
|
|
|
|
|
if err := DB.Model(&user2).Association("Manager").Delete(&User{}); err != nil {
|
|
|
|
t.Fatalf("Error happened when delete Manager, got %v", err)
|
|
|
|
}
|
2020-05-24 15:44:37 +03:00
|
|
|
AssertAssociationCount(t, user2, "Manager", 1, "after delete non-existing data")
|
2020-05-24 12:24:23 +03:00
|
|
|
|
|
|
|
if err := DB.Model(&user2).Association("Manager").Delete(manager2); err != nil {
|
|
|
|
t.Fatalf("Error happened when delete Manager, got %v", err)
|
|
|
|
}
|
2020-05-24 15:44:37 +03:00
|
|
|
AssertAssociationCount(t, user2, "Manager", 0, "after delete")
|
2020-05-24 12:24:23 +03:00
|
|
|
|
2020-05-24 15:44:37 +03:00
|
|
|
// Prepare Data for Clear
|
2020-05-24 12:24:23 +03:00
|
|
|
if err := DB.Model(&user2).Association("Company").Append(&company); err != nil {
|
|
|
|
t.Fatalf("Error happened when append Company, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Model(&user2).Association("Manager").Append(manager); err != nil {
|
|
|
|
t.Fatalf("Error happened when append Manager, got %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-24 15:44:37 +03:00
|
|
|
AssertAssociationCount(t, user2, "Company", 1, "after prepare data")
|
|
|
|
AssertAssociationCount(t, user2, "Manager", 1, "after prepare data")
|
2020-05-24 12:24:23 +03:00
|
|
|
|
|
|
|
// Clear
|
|
|
|
if err := DB.Model(&user2).Association("Company").Clear(); err != nil {
|
|
|
|
t.Errorf("Error happened when clear Company, got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := DB.Model(&user2).Association("Manager").Clear(); err != nil {
|
|
|
|
t.Errorf("Error happened when clear Manager, got %v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-24 15:44:37 +03:00
|
|
|
AssertAssociationCount(t, user2, "Company", 0, "after clear")
|
|
|
|
AssertAssociationCount(t, user2, "Manager", 0, "after clear")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBelongsToAssociationForSlice(t *testing.T) {
|
|
|
|
var users = []User{
|
|
|
|
*GetUser("slice-belongs-to-1", Config{Company: true, Manager: true}),
|
|
|
|
*GetUser("slice-belongs-to-2", Config{Company: true, Manager: false}),
|
|
|
|
*GetUser("slice-belongs-to-3", Config{Company: true, Manager: true}),
|
|
|
|
}
|
|
|
|
|
|
|
|
DB.Create(&users)
|
|
|
|
|
|
|
|
AssertAssociationCount(t, users, "Company", 3, "")
|
|
|
|
AssertAssociationCount(t, users, "Manager", 2, "")
|
|
|
|
|
|
|
|
// Find
|
|
|
|
var companies []Company
|
|
|
|
if DB.Model(users).Association("Company").Find(&companies); len(companies) != 3 {
|
|
|
|
t.Errorf("companies count should be %v, but got %v", 3, len(companies))
|
2020-05-24 12:24:23 +03:00
|
|
|
}
|
|
|
|
|
2020-05-24 15:44:37 +03:00
|
|
|
var managers []User
|
|
|
|
if DB.Model(users).Association("Manager").Find(&managers); len(managers) != 2 {
|
|
|
|
t.Errorf("managers count should be %v, but got %v", 2, len(managers))
|
2020-05-24 12:24:23 +03:00
|
|
|
}
|
2020-05-24 15:44:37 +03:00
|
|
|
|
|
|
|
// Append
|
|
|
|
|
|
|
|
// Replace
|
|
|
|
|
|
|
|
// Delete
|
|
|
|
|
|
|
|
// Clear
|
|
|
|
DB.Model(&users).Association("Company").Clear()
|
|
|
|
AssertAssociationCount(t, users, "Company", 0, "After Clear")
|
|
|
|
|
|
|
|
DB.Model(&users).Association("Manager").Clear()
|
|
|
|
AssertAssociationCount(t, users, "Manager", 0, "After Clear")
|
2020-05-23 18:50:48 +03:00
|
|
|
}
|