MCPcopy
hub / github.com/go-gorm/gorm / TestDeleteWithAssociations

Function TestDeleteWithAssociations

tests/delete_test.go:132–154  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

130}
131
132func TestDeleteWithAssociations(t *testing.T) {
133 user := GetUser("delete_with_associations", Config{Account: true, Pets: 2, Toys: 4, Company: true, Manager: true, Team: 1, Languages: 1, Friends: 1})
134
135 if err := DB.Create(user).Error; err != nil {
136 t.Fatalf("failed to create user, got error %v", err)
137 }
138
139 if err := DB.Select(clause.Associations, "Pets.Toy").Delete(&user).Error; err != nil {
140 t.Fatalf("failed to delete user, got error %v", err)
141 }
142
143 for key, value := range map[string]int64{"Account": 1, "Pets": 2, "Toys": 4, "Company": 1, "Manager": 1, "Team": 1, "Languages": 0, "Friends": 0} {
144 if count := DB.Unscoped().Model(&user).Association(key).Count(); count != value {
145 t.Errorf("user's %v expects: %v, got %v", key, value, count)
146 }
147 }
148
149 for key, value := range map[string]int64{"Account": 0, "Pets": 0, "Toys": 0, "Company": 1, "Manager": 1, "Team": 0, "Languages": 0, "Friends": 0} {
150 if count := DB.Model(&user).Association(key).Count(); count != value {
151 t.Errorf("user's %v expects: %v, got %v", key, value, count)
152 }
153 }
154}
155
156func TestDeleteAssociationsWithUnscoped(t *testing.T) {
157 user := GetUser("unscoped_delete_with_associations", Config{Account: true, Pets: 2, Toys: 4, Company: true, Manager: true, Team: 1, Languages: 1, Friends: 1})

Callers

nothing calls this directly

Calls 8

GetUserFunction · 0.85
ModelMethod · 0.80
CreateMethod · 0.65
DeleteMethod · 0.65
SelectMethod · 0.65
CountMethod · 0.65
AssociationMethod · 0.45
UnscopedMethod · 0.45

Tested by

no test coverage detected