forked from mirror/gorm
Test HasOne Association for Slice
This commit is contained in:
parent
6a0ef985ff
commit
91eaf0bb21
|
@ -381,7 +381,7 @@ func (association *Association) saveAssociation(clear bool, values ...interface{
|
|||
for i := 0; i < reflectValue.Len(); i++ {
|
||||
association.Relationship.Field.Set(reflectValue.Index(i), reflect.New(association.Relationship.Field.IndirectFieldType).Interface())
|
||||
for _, ref := range association.Relationship.References {
|
||||
if !ref.OwnPrimaryKey {
|
||||
if !ref.OwnPrimaryKey && ref.PrimaryValue == "" {
|
||||
ref.ForeignKey.Set(reflectValue.Index(i), reflect.Zero(ref.ForeignKey.FieldType).Interface())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -310,6 +310,47 @@ func TestHasOneAssociation(t *testing.T) {
|
|||
AssertAssociationCount(t, user2, "Account", 0, "after clear")
|
||||
}
|
||||
|
||||
func TestHasOneAssociationForSlice(t *testing.T) {
|
||||
var users = []User{
|
||||
*GetUser("slice-hasone-1", Config{Account: true}),
|
||||
*GetUser("slice-hasone-2", Config{Account: false}),
|
||||
*GetUser("slice-hasone-3", Config{Account: true}),
|
||||
}
|
||||
|
||||
DB.Create(&users)
|
||||
|
||||
// Count
|
||||
AssertAssociationCount(t, users, "Account", 2, "")
|
||||
|
||||
// Find
|
||||
var accounts []Account
|
||||
if DB.Model(&users).Association("Account").Find(&accounts); len(accounts) != 2 {
|
||||
t.Errorf("accounts count should be %v, but got %v", 3, len(accounts))
|
||||
}
|
||||
|
||||
// Append
|
||||
DB.Model(&users).Association("Account").Append(
|
||||
&Account{Number: "account-slice-append-1"},
|
||||
&Account{Number: "account-slice-append-2"},
|
||||
&Account{Number: "account-slice-append-3"},
|
||||
)
|
||||
|
||||
AssertAssociationCount(t, users, "Account", 3, "After Append")
|
||||
|
||||
// Replace -> same as append
|
||||
|
||||
// Delete
|
||||
if err := DB.Model(&users).Association("Account").Delete(&users[0].Account); err != nil {
|
||||
t.Errorf("no error should happend when deleting account, but got %v", err)
|
||||
}
|
||||
|
||||
AssertAssociationCount(t, users, "Account", 2, "after delete")
|
||||
|
||||
// Clear
|
||||
DB.Model(&users).Association("Account").Clear()
|
||||
AssertAssociationCount(t, users, "Account", 0, "After Clear")
|
||||
}
|
||||
|
||||
func TestPolymorphicHasOneAssociation(t *testing.T) {
|
||||
var pet = Pet{Name: "hasone", Toy: Toy{Name: "toy-has-one"}}
|
||||
|
||||
|
@ -385,3 +426,44 @@ func TestPolymorphicHasOneAssociation(t *testing.T) {
|
|||
|
||||
AssertAssociationCount(t, pet2, "Toy", 0, "after clear")
|
||||
}
|
||||
|
||||
func TestPolymorphicHasOneAssociationForSlice(t *testing.T) {
|
||||
var pets = []Pet{
|
||||
{Name: "hasone-1", Toy: Toy{Name: "toy-has-one"}},
|
||||
{Name: "hasone-2", Toy: Toy{}},
|
||||
{Name: "hasone-3", Toy: Toy{Name: "toy-has-one"}},
|
||||
}
|
||||
|
||||
DB.Create(&pets)
|
||||
|
||||
// Count
|
||||
AssertAssociationCount(t, pets, "Toy", 2, "")
|
||||
|
||||
// Find
|
||||
var toys []Toy
|
||||
if DB.Model(&pets).Association("Toy").Find(&toys); len(toys) != 2 {
|
||||
t.Errorf("toys count should be %v, but got %v", 3, len(toys))
|
||||
}
|
||||
|
||||
// Append
|
||||
DB.Model(&pets).Association("Toy").Append(
|
||||
&Toy{Name: "toy-slice-append-1"},
|
||||
&Toy{Name: "toy-slice-append-2"},
|
||||
&Toy{Name: "toy-slice-append-3"},
|
||||
)
|
||||
|
||||
AssertAssociationCount(t, pets, "Toy", 3, "After Append")
|
||||
|
||||
// Replace -> same as append
|
||||
|
||||
// Delete
|
||||
if err := DB.Model(&pets).Association("Toy").Delete(&pets[0].Toy); err != nil {
|
||||
t.Errorf("no error should happend when deleting toy, but got %v", err)
|
||||
}
|
||||
|
||||
AssertAssociationCount(t, pets, "Toy", 2, "after delete")
|
||||
|
||||
// Clear
|
||||
DB.Model(&pets).Association("Toy").Clear()
|
||||
AssertAssociationCount(t, pets, "Toy", 0, "After Clear")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue