From cb7c41e0b6e3863e7934a50c0aed76b8cfb61bfd Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 10 Feb 2018 22:14:18 +0800 Subject: [PATCH] Add more tests for self-referencing many2many relationship --- customize_column_test.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/customize_column_test.go b/customize_column_test.go index 629d85f9..5e19d6f4 100644 --- a/customize_column_test.go +++ b/customize_column_test.go @@ -322,4 +322,25 @@ func TestSelfReferencingMany2ManyColumn(t *testing.T) { if len(newUser.Friends) != 2 { t.Errorf("Should preload created frineds for self reference m2m") } + + DB.Model(&newUser).Association("Friends").Append(&SelfReferencingUser{Name: "friend3_m2m"}) + if DB.Model(&user).Association("Friends").Count() != 3 { + t.Errorf("Should find created friends correctly") + } + + DB.Model(&newUser).Association("Friends").Replace(&SelfReferencingUser{Name: "friend4_m2m"}) + if DB.Model(&user).Association("Friends").Count() != 1 { + t.Errorf("Should find created friends correctly") + } + + friend := SelfReferencingUser{} + DB.Model(&newUser).Association("Friends").Find(&friend) + if friend.Name != "friend4_m2m" { + t.Errorf("Should find created friends correctly") + } + + DB.Model(&newUser).Association("Friends").Delete(friend) + if DB.Model(&user).Association("Friends").Count() != 0 { + t.Errorf("All friends should be deleted") + } }