| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"gorm.io/gorm" |
| 7 |
. "gorm.io/gorm/utils/tests" |
| 8 |
) |
| 9 |
|
| 10 |
func TestBelongsToAssociation(t *testing.T) { |
| 11 |
user := *GetUser("belongs-to", Config{Company: true, Manager: true}) |
| 12 |
|
| 13 |
if err := DB.Create(&user).Error; err != nil { |
| 14 |
t.Fatalf("errors happened when create: %v", err) |
| 15 |
} |
| 16 |
|
| 17 |
CheckUser(t, user, user) |
| 18 |
|
| 19 |
// Find |
| 20 |
var user2 User |
| 21 |
DB.Find(&user2, "id = ?", user.ID) |
| 22 |
pointerOfUser := &user2 |
| 23 |
if err := DB.Model(&pointerOfUser).Association("Company").Find(&user2.Company); err != nil { |
| 24 |
t.Errorf("failed to query users, got error %#v", err) |
| 25 |
} |
| 26 |
user2.Manager = &User{} |
| 27 |
DB.Model(&user2).Association("Manager").Find(user2.Manager) |
| 28 |
CheckUser(t, user2, user) |
| 29 |
|
| 30 |
// Count |
| 31 |
AssertAssociationCount(t, user, "Company", 1, "") |
| 32 |
AssertAssociationCount(t, user, "Manager", 1, "") |
| 33 |
|
| 34 |
// Append |
| 35 |
company := Company{Name: "company-belongs-to-append"} |
| 36 |
manager := GetUser("manager-belongs-to-append", Config{}) |
| 37 |
|
| 38 |
if err := DB.Model(&user2).Association("Company").Append(&company); err != nil { |
| 39 |
t.Fatalf("Error happened when append Company, got %v", err) |
| 40 |
} |
| 41 |
|
| 42 |
if company.ID == 0 { |
| 43 |
t.Fatalf("Company's ID should be created") |
| 44 |
} |
| 45 |
|
| 46 |
if err := DB.Model(&user2).Association("Manager").Append(manager); err != nil { |
| 47 |
t.Fatalf("Error happened when append Manager, got %v", err) |
| 48 |
} |
| 49 |
|
| 50 |
if manager.ID == 0 { |
| 51 |
t.Fatalf("Manager's ID should be created") |
| 52 |
} |
| 53 |
|
| 54 |
user.Company = company |
| 55 |
user.Manager = manager |
| 56 |
user.CompanyID = &company.ID |
| 57 |
user.ManagerID = &manager.ID |
| 58 |
CheckUser(t, user2, user) |
| 59 |
|
| 60 |
AssertAssociationCount(t, user2, "Company", 1, "AfterAppend") |
| 61 |
AssertAssociationCount(t, user2, "Manager", 1, "AfterAppend") |
| 62 |
|
| 63 |
// Replace |
| 64 |
company2 := Company{Name: "company-belongs-to-replace"} |
| 65 |
manager2 := GetUser("manager-belongs-to-replace", Config{}) |
| 66 |
|
| 67 |
if err := DB.Model(&user2).Association("Company").Replace(&company2); err != nil { |
| 68 |
t.Fatalf("Error happened when replace Company, got %v", err) |
| 69 |
} |
| 70 |
|
| 71 |
if company2.ID == 0 { |
| 72 |
t.Fatalf("Company's ID should be created") |
| 73 |
} |
| 74 |
|
| 75 |
if err := DB.Model(&user2).Association("Manager").Replace(manager2); err != nil { |
| 76 |
t.Fatalf("Error happened when replace Manager, got %v", err) |
| 77 |
} |
| 78 |
|
| 79 |
if manager2.ID == 0 { |
| 80 |
t.Fatalf("Manager's ID should be created") |
| 81 |
} |
| 82 |
|
| 83 |
user.Company = company2 |
| 84 |
user.Manager = manager2 |
| 85 |
user.CompanyID = &company2.ID |
| 86 |
user.ManagerID = &manager2.ID |
| 87 |
CheckUser(t, user2, user) |
| 88 |
|
| 89 |
AssertAssociationCount(t, user2, "Company", 1, "AfterReplace") |
| 90 |
AssertAssociationCount(t, user2, "Manager", 1, "AfterReplace") |
| 91 |
|
| 92 |
// Delete |
| 93 |
if err := DB.Model(&user2).Association("Company").Delete(&Company{}); err != nil { |
| 94 |
t.Fatalf("Error happened when delete Company, got %v", err) |
| 95 |
} |
| 96 |
AssertAssociationCount(t, user2, "Company", 1, "after delete non-existing data") |
| 97 |
|
| 98 |
if err := DB.Model(&user2).Association("Company").Delete(&company2); err != nil { |
| 99 |
t.Fatalf("Error happened when delete Company, got %v", err) |
| 100 |
} |
| 101 |
AssertAssociationCount(t, user2, "Company", 0, "after delete") |
| 102 |
|
| 103 |
if err := DB.Model(&user2).Association("Manager").Delete(&User{}); err != nil { |
| 104 |
t.Fatalf("Error happened when delete Manager, got %v", err) |
| 105 |
} |
| 106 |
AssertAssociationCount(t, user2, "Manager", 1, "after delete non-existing data") |
| 107 |
|
| 108 |
if err := DB.Model(&user2).Association("Manager").Delete(manager2); err != nil { |
| 109 |
t.Fatalf("Error happened when delete Manager, got %v", err) |
| 110 |
} |
| 111 |
AssertAssociationCount(t, user2, "Manager", 0, "after delete") |
| 112 |
|
| 113 |
// Prepare Data for Clear |
| 114 |
if err := DB.Model(&user2).Association("Company").Append(&company); err != nil { |
| 115 |
t.Fatalf("Error happened when append Company, got %v", err) |
| 116 |
} |
| 117 |
|
| 118 |
if err := DB.Model(&user2).Association("Manager").Append(manager); err != nil { |
| 119 |
t.Fatalf("Error happened when append Manager, got %v", err) |
| 120 |
} |
| 121 |
|
| 122 |
AssertAssociationCount(t, user2, "Company", 1, "after prepare data") |
| 123 |
AssertAssociationCount(t, user2, "Manager", 1, "after prepare data") |
| 124 |
|
| 125 |
// Clear |
| 126 |
if err := DB.Model(&user2).Association("Company").Clear(); err != nil { |
| 127 |
t.Errorf("Error happened when clear Company, got %v", err) |
| 128 |
} |
| 129 |
|
| 130 |
if err := DB.Model(&user2).Association("Manager").Clear(); err != nil { |
| 131 |
t.Errorf("Error happened when clear Manager, got %v", err) |
| 132 |
} |
| 133 |
|
| 134 |
AssertAssociationCount(t, user2, "Company", 0, "after clear") |
| 135 |
AssertAssociationCount(t, user2, "Manager", 0, "after clear") |
| 136 |
|
| 137 |
// unexist company id |
| 138 |
unexistCompanyID := company.ID + 9999999 |
| 139 |
user = User{Name: "invalid-user-with-invalid-belongs-to-foreign-key", CompanyID: &unexistCompanyID} |
| 140 |
if err := DB.Create(&user).Error; err == nil { |
| 141 |
tidbSkip(t, "not support the foreign key feature") |
| 142 |
t.Errorf("should have gotten foreign key violation error") |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func TestBelongsToAssociationForSlice(t *testing.T) { |
| 147 |
users := []User{ |
| 148 |
*GetUser("slice-belongs-to-1", Config{Company: true, Manager: true}), |
| 149 |
*GetUser("slice-belongs-to-2", Config{Company: true, Manager: false}), |
| 150 |
*GetUser("slice-belongs-to-3", Config{Company: true, Manager: true}), |
| 151 |
} |
| 152 |
|
| 153 |
DB.Create(&users) |
| 154 |
|
| 155 |
AssertAssociationCount(t, users, "Company", 3, "") |
| 156 |
AssertAssociationCount(t, users, "Manager", 2, "") |
| 157 |
|
| 158 |
// Find |
| 159 |
var companies []Company |
| 160 |
if DB.Model(&users).Association("Company").Find(&companies); len(companies) != 3 { |
| 161 |
t.Errorf("companies count should be %v, but got %v", 3, len(companies)) |
| 162 |
} |
| 163 |
|
| 164 |
var managers []User |
| 165 |
if DB.Model(&users).Association("Manager").Find(&managers); len(managers) != 2 { |
| 166 |
t.Errorf("managers count should be %v, but got %v", 2, len(managers)) |
| 167 |
} |
| 168 |
|
| 169 |
// Append |
| 170 |
DB.Model(&users).Association("Company").Append( |
| 171 |
&Company{Name: "company-slice-append-1"}, |
| 172 |
&Company{Name: "company-slice-append-2"}, |
| 173 |
&Company{Name: "company-slice-append-3"}, |
| 174 |
) |
| 175 |
|
| 176 |
AssertAssociationCount(t, users, "Company", 3, "After Append") |
| 177 |
|
| 178 |
DB.Model(&users).Association("Manager").Append( |
| 179 |
GetUser("manager-slice-belongs-to-1", Config{}), |
| 180 |
GetUser("manager-slice-belongs-to-2", Config{}), |
| 181 |
GetUser("manager-slice-belongs-to-3", Config{}), |
| 182 |
) |
| 183 |
AssertAssociationCount(t, users, "Manager", 3, "After Append") |
| 184 |
|
| 185 |
if err := DB.Model(&users).Association("Manager").Append( |
| 186 |
GetUser("manager-slice-belongs-to-test-1", Config{}), |
| 187 |
).Error; err == nil { |
| 188 |
t.Errorf("unmatched length when update user's manager") |
| 189 |
} |
| 190 |
|
| 191 |
// Replace -> same as append |
| 192 |
|
| 193 |
// Delete |
| 194 |
if err := DB.Model(&users).Association("Company").Delete(&users[0].Company); err != nil { |
| 195 |
t.Errorf("no error should happened when deleting company, but got %v", err) |
| 196 |
} |
| 197 |
|
| 198 |
if users[0].CompanyID != nil || users[0].Company.ID != 0 { |
| 199 |
t.Errorf("users[0]'s company should be deleted'") |
| 200 |
} |
| 201 |
|
| 202 |
AssertAssociationCount(t, users, "Company", 2, "After Delete") |
| 203 |
|
| 204 |
// Clear |
| 205 |
DB.Model(&users).Association("Company").Clear() |
| 206 |
AssertAssociationCount(t, users, "Company", 0, "After Clear") |
| 207 |
|
| 208 |
DB.Model(&users).Association("Manager").Clear() |
| 209 |
AssertAssociationCount(t, users, "Manager", 0, "After Clear") |
| 210 |
|
| 211 |
// shared company |
| 212 |
company := Company{Name: "shared"} |
| 213 |
if err := DB.Model(&users[0]).Association("Company").Append(&company); err != nil { |
| 214 |
t.Errorf("Error happened when append company to user, got %v", err) |
| 215 |
} |
| 216 |
|
| 217 |
if err := DB.Model(&users[1]).Association("Company").Append(&company); err != nil { |
| 218 |
t.Errorf("Error happened when append company to user, got %v", err) |
| 219 |
} |
| 220 |
|
| 221 |
if users[0].CompanyID == nil || users[1].CompanyID == nil || *users[0].CompanyID != *users[1].CompanyID { |
| 222 |
t.Errorf("user's company id should exists and equal, but its: %v, %v", users[0].CompanyID, users[1].CompanyID) |
| 223 |
} |
| 224 |
|
| 225 |
DB.Model(&users[0]).Association("Company").Delete(&company) |
| 226 |
AssertAssociationCount(t, users[0], "Company", 0, "After Delete") |
| 227 |
AssertAssociationCount(t, users[1], "Company", 1, "After other user Delete") |
| 228 |
} |
| 229 |
|
| 230 |
func TestBelongsToDefaultValue(t *testing.T) { |
| 231 |
type Org struct { |
| 232 |
ID string |
| 233 |
} |
| 234 |
type BelongsToUser struct { |
| 235 |
OrgID string |
| 236 |
Org Org `gorm:"default:NULL"` |
| 237 |
} |
| 238 |
|
| 239 |
tx := DB.Session(&gorm.Session{}) |
| 240 |
tx.Config.DisableForeignKeyConstraintWhenMigrating = true |
| 241 |
AssertEqual(t, DB.Config.DisableForeignKeyConstraintWhenMigrating, false) |
| 242 |
|
| 243 |
tx.Migrator().DropTable(&BelongsToUser{}, &Org{}) |
| 244 |
tx.AutoMigrate(&BelongsToUser{}, &Org{}) |
| 245 |
|
| 246 |
user := &BelongsToUser{ |
| 247 |
Org: Org{ |
| 248 |
ID: "BelongsToUser_Org_1", |
| 249 |
}, |
| 250 |
} |
| 251 |
err := DB.Create(&user).Error |
| 252 |
AssertEqual(t, err, nil) |
| 253 |
} |
| 254 |
|
| 255 |
func TestBelongsToAssociationUnscoped(t *testing.T) { |
| 256 |
type ItemParent struct { |
| 257 |
gorm.Model |
| 258 |
Logo string `gorm:"not null;type:varchar(50)"` |
| 259 |
} |
| 260 |
type ItemChild struct { |
| 261 |
gorm.Model |
| 262 |
Name string `gorm:"type:varchar(50)"` |
| 263 |
ItemParentID uint |
| 264 |
ItemParent ItemParent |
| 265 |
} |
| 266 |
|
| 267 |
tx := DB.Session(&gorm.Session{}) |
| 268 |
tx.Migrator().DropTable(&ItemParent{}, &ItemChild{}) |
| 269 |
tx.AutoMigrate(&ItemParent{}, &ItemChild{}) |
| 270 |
|
| 271 |
item := ItemChild{ |
| 272 |
Name: "name", |
| 273 |
ItemParent: ItemParent{ |
| 274 |
Logo: "logo", |
| 275 |
}, |
| 276 |
} |
| 277 |
if err := tx.Create(&item).Error; err != nil { |
| 278 |
t.Fatalf("failed to create items, got error: %v", err) |
| 279 |
} |
| 280 |
|
| 281 |
// test replace |
| 282 |
if err := tx.Model(&item).Association("ItemParent").Unscoped().Replace(&ItemParent{ |
| 283 |
Logo: "updated logo", |
| 284 |
}); err != nil { |
| 285 |
t.Errorf("failed to replace item parent, got error: %v", err) |
| 286 |
} |
| 287 |
|
| 288 |
var parents []ItemParent |
| 289 |
if err := tx.Find(&parents).Error; err != nil { |
| 290 |
t.Errorf("failed to find item parent, got error: %v", err) |
| 291 |
} |
| 292 |
if len(parents) != 1 { |
| 293 |
t.Errorf("expected %d parents, got %d", 1, len(parents)) |
| 294 |
} |
| 295 |
|
| 296 |
// test delete |
| 297 |
if err := tx.Model(&item).Association("ItemParent").Unscoped().Delete(&parents); err != nil { |
| 298 |
t.Errorf("failed to delete item parent, got error: %v", err) |
| 299 |
} |
| 300 |
if err := tx.Find(&parents).Error; err != nil { |
| 301 |
t.Errorf("failed to find item parent, got error: %v", err) |
| 302 |
} |
| 303 |
if len(parents) != 0 { |
| 304 |
t.Errorf("expected %d parents, got %d", 0, len(parents)) |
| 305 |
} |
| 306 |
} |