| 130 | } |
| 131 | |
| 132 | func 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 | |
| 156 | func 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}) |