gorm/tests/associations_test.go

34 lines
983 B
Go
Raw Normal View History

2020-05-23 18:50:48 +03:00
package tests_test
import (
"testing"
2020-06-02 04:16:07 +03:00
. "gorm.io/gorm/tests"
2020-05-23 18:50:48 +03:00
)
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 {
2020-06-01 14:41:33 +03:00
t.Fatalf("invalid %v count %v, expects: %v got %v", name, reason, result, count)
2020-05-24 15:44:37 +03:00
}
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 {
2020-06-01 14:41:33 +03:00
t.Fatalf("invalid %v count %v, expects: %v got %v", name, reason, result, count)
2020-05-24 15:44:37 +03:00
}
}
}
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 {
2020-06-01 14:41:33 +03:00
t.Fatalf("should return errors for invalid association, but got nil")
2020-05-24 18:28:06 +03:00
}
}