2020-05-23 18:50:48 +03:00
|
|
|
package tests_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
. "github.com/jinzhu/gorm/tests"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAssociationForBelongsTo(t *testing.T) {
|
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if count := DB.Model(&user).Association("Company").Count(); count != 1 {
|
|
|
|
t.Errorf("invalid company count, got %v", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
if count := DB.Model(&user).Association("Manager").Count(); count != 1 {
|
|
|
|
t.Errorf("invalid manager count, got %v", count)
|
|
|
|
}
|
2020-05-23 18:50:48 +03:00
|
|
|
}
|