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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 04:48:12 +03:00
|
|
|
func TestInvalidAssociation(t *testing.T) {
|
|
|
|
var user = *GetUser("invalid", Config{Company: true, Manager: true})
|
|
|
|
if err := DB.Model(&user).Association("Invalid").Find(&user.Company).Error; err == nil {
|
|
|
|
t.Errorf("should return errors for invalid association, but got nil")
|
2020-05-24 18:28:06 +03:00
|
|
|
}
|
2020-05-25 20:57:22 +03:00
|
|
|
}
|