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

Function TestDeleteAssociationsWithUnscoped

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

Source from the content-addressed store, hash-verified

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})
158
159 if err := DB.Create(user).Error; err != nil {
160 t.Fatalf("failed to create user, got error %v", err)
161 }
162
163 if err := DB.Unscoped().Select(clause.Associations, "Pets.Toy").Delete(&user).Error; err != nil {
164 t.Fatalf("failed to delete user, got error %v", err)
165 }
166
167 for key, value := range map[string]int64{"Account": 0, "Pets": 0, "Toys": 0, "Company": 1, "Manager": 1, "Team": 0, "Languages": 0, "Friends": 0} {
168 if count := DB.Unscoped().Model(&user).Association(key).Count(); count != value {
169 t.Errorf("user's %v expects: %v, got %v", key, value, count)
170 }
171 }
172
173 for key, value := range map[string]int64{"Account": 0, "Pets": 0, "Toys": 0, "Company": 1, "Manager": 1, "Team": 0, "Languages": 0, "Friends": 0} {
174 if count := DB.Model(&user).Association(key).Count(); count != value {
175 t.Errorf("user's %v expects: %v, got %v", key, value, count)
176 }
177 }
178}
179
180func TestDeleteSliceWithAssociations(t *testing.T) {
181 users := []User{

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
UnscopedMethod · 0.45
AssociationMethod · 0.45

Tested by

no test coverage detected