| 1 |
package tests_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"gorm.io/gorm" |
| 7 |
. "gorm.io/gorm/utils/tests" |
| 8 |
) |
| 9 |
|
| 10 |
func TestHasManyAssociation(t *testing.T) { |
| 11 |
user := *GetUser("hasmany", Config{Pets: 2}) |
| 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 |
DB.Model(&user2).Association("Pets").Find(&user2.Pets) |
| 23 |
CheckUser(t, user2, user) |
| 24 |
|
| 25 |
var pets []Pet |
| 26 |
DB.Model(&user).Where("name = ?", user.Pets[0].Name).Association("Pets").Find(&pets) |
| 27 |
|
| 28 |
if len(pets) != 1 { |
| 29 |
t.Fatalf("should only find one pets, but got %v", len(pets)) |
| 30 |
} |
| 31 |
|
| 32 |
CheckPet(t, pets[0], *user.Pets[0]) |
| 33 |
|
| 34 |
if count := DB.Model(&user).Where("name = ?", user.Pets[1].Name).Association("Pets").Count(); count != 1 { |
| 35 |
t.Fatalf("should only find one pets, but got %v", count) |
| 36 |
} |
| 37 |
|
| 38 |
if count := DB.Model(&user).Where("name = ?", "not found").Association("Pets").Count(); count != 0 { |
| 39 |
t.Fatalf("should only find no pet with invalid conditions, but got %v", count) |
| 40 |
} |
| 41 |
|
| 42 |
// Count |
| 43 |
AssertAssociationCount(t, user, "Pets", 2, "") |
| 44 |
|
| 45 |
// Append |
| 46 |
pet := Pet{Name: "pet-has-many-append"} |
| 47 |
|
| 48 |
if err := DB.Model(&user2).Association("Pets").Append(&pet); err != nil { |
| 49 |
t.Fatalf("Error happened when append account, got %v", err) |
| 50 |
} |
| 51 |
|
| 52 |
if pet.ID == 0 { |
| 53 |
t.Fatalf("Pet's ID should be created") |
| 54 |
} |
| 55 |
|
| 56 |
user.Pets = append(user.Pets, &pet) |
| 57 |
CheckUser(t, user2, user) |
| 58 |
|
| 59 |
AssertAssociationCount(t, user, "Pets", 3, "AfterAppend") |
| 60 |
|
| 61 |
pets2 := []Pet{{Name: "pet-has-many-append-1-1"}, {Name: "pet-has-many-append-1-1"}} |
| 62 |
|
| 63 |
if err := DB.Model(&user2).Association("Pets").Append(&pets2); err != nil { |
| 64 |
t.Fatalf("Error happened when append pet, got %v", err) |
| 65 |
} |
| 66 |
|
| 67 |
for _, pet := range pets2 { |
| 68 |
pet := pet |
| 69 |
if pet.ID == 0 { |
| 70 |
t.Fatalf("Pet's ID should be created") |
| 71 |
} |
| 72 |
|
| 73 |
user.Pets = append(user.Pets, &pet) |
| 74 |
} |
| 75 |
|
| 76 |
CheckUser(t, user2, user) |
| 77 |
|
| 78 |
AssertAssociationCount(t, user, "Pets", 5, "AfterAppendSlice") |
| 79 |
|
| 80 |
// Replace |
| 81 |
pet2 := Pet{Name: "pet-has-many-replace"} |
| 82 |
|
| 83 |
if err := DB.Model(&user2).Association("Pets").Replace(&pet2); err != nil { |
| 84 |
t.Fatalf("Error happened when append pet, got %v", err) |
| 85 |
} |
| 86 |
|
| 87 |
if pet2.ID == 0 { |
| 88 |
t.Fatalf("pet2's ID should be created") |
| 89 |
} |
| 90 |
|
| 91 |
user.Pets = []*Pet{&pet2} |
| 92 |
CheckUser(t, user2, user) |
| 93 |
|
| 94 |
AssertAssociationCount(t, user2, "Pets", 1, "AfterReplace") |
| 95 |
|
| 96 |
// Delete |
| 97 |
if err := DB.Model(&user2).Association("Pets").Delete(&Pet{}); err != nil { |
| 98 |
t.Fatalf("Error happened when delete pet, got %v", err) |
| 99 |
} |
| 100 |
AssertAssociationCount(t, user2, "Pets", 1, "after delete non-existing data") |
| 101 |
|
| 102 |
if err := DB.Model(&user2).Association("Pets").Delete(&pet2); err != nil { |
| 103 |
t.Fatalf("Error happened when delete Pets, got %v", err) |
| 104 |
} |
| 105 |
AssertAssociationCount(t, user2, "Pets", 0, "after delete") |
| 106 |
|
| 107 |
// Prepare Data for Clear |
| 108 |
if err := DB.Model(&user2).Association("Pets").Append(&pet); err != nil { |
| 109 |
t.Fatalf("Error happened when append Pets, got %v", err) |
| 110 |
} |
| 111 |
|
| 112 |
AssertAssociationCount(t, user2, "Pets", 1, "after prepare data") |
| 113 |
|
| 114 |
// Clear |
| 115 |
if err := DB.Model(&user2).Association("Pets").Clear(); err != nil { |
| 116 |
t.Errorf("Error happened when clear Pets, got %v", err) |
| 117 |
} |
| 118 |
|
| 119 |
AssertAssociationCount(t, user2, "Pets", 0, "after clear") |
| 120 |
} |
| 121 |
|
| 122 |
func TestSingleTableHasManyAssociation(t *testing.T) { |
| 123 |
user := *GetUser("hasmany", Config{Team: 2}) |
| 124 |
|
| 125 |
if err := DB.Create(&user).Error; err != nil { |
| 126 |
t.Fatalf("errors happened when create: %v", err) |
| 127 |
} |
| 128 |
|
| 129 |
CheckUser(t, user, user) |
| 130 |
|
| 131 |
// Find |
| 132 |
var user2 User |
| 133 |
DB.Find(&user2, "id = ?", user.ID) |
| 134 |
DB.Model(&user2).Association("Team").Find(&user2.Team) |
| 135 |
CheckUser(t, user2, user) |
| 136 |
|
| 137 |
// Count |
| 138 |
AssertAssociationCount(t, user, "Team", 2, "") |
| 139 |
|
| 140 |
// Append |
| 141 |
team := *GetUser("team", Config{}) |
| 142 |
|
| 143 |
if err := DB.Model(&user2).Association("Team").Append(&team); err != nil { |
| 144 |
t.Fatalf("Error happened when append account, got %v", err) |
| 145 |
} |
| 146 |
|
| 147 |
if team.ID == 0 { |
| 148 |
t.Fatalf("Team's ID should be created") |
| 149 |
} |
| 150 |
|
| 151 |
user.Team = append(user.Team, team) |
| 152 |
CheckUser(t, user2, user) |
| 153 |
|
| 154 |
AssertAssociationCount(t, user, "Team", 3, "AfterAppend") |
| 155 |
|
| 156 |
teams := []User{*GetUser("team-append-1", Config{}), *GetUser("team-append-2", Config{})} |
| 157 |
|
| 158 |
if err := DB.Model(&user2).Association("Team").Append(&teams); err != nil { |
| 159 |
t.Fatalf("Error happened when append team, got %v", err) |
| 160 |
} |
| 161 |
|
| 162 |
for _, team := range teams { |
| 163 |
team := team |
| 164 |
if team.ID == 0 { |
| 165 |
t.Fatalf("Team's ID should be created") |
| 166 |
} |
| 167 |
|
| 168 |
user.Team = append(user.Team, team) |
| 169 |
} |
| 170 |
|
| 171 |
CheckUser(t, user2, user) |
| 172 |
|
| 173 |
AssertAssociationCount(t, user, "Team", 5, "AfterAppendSlice") |
| 174 |
|
| 175 |
// Replace |
| 176 |
team2 := *GetUser("team-replace", Config{}) |
| 177 |
|
| 178 |
if err := DB.Model(&user2).Association("Team").Replace(&team2); err != nil { |
| 179 |
t.Fatalf("Error happened when append team, got %v", err) |
| 180 |
} |
| 181 |
|
| 182 |
if team2.ID == 0 { |
| 183 |
t.Fatalf("team2's ID should be created") |
| 184 |
} |
| 185 |
|
| 186 |
user.Team = []User{team2} |
| 187 |
CheckUser(t, user2, user) |
| 188 |
|
| 189 |
AssertAssociationCount(t, user2, "Team", 1, "AfterReplace") |
| 190 |
|
| 191 |
// Delete |
| 192 |
if err := DB.Model(&user2).Association("Team").Delete(&User{}); err != nil { |
| 193 |
t.Fatalf("Error happened when delete team, got %v", err) |
| 194 |
} |
| 195 |
AssertAssociationCount(t, user2, "Team", 1, "after delete non-existing data") |
| 196 |
|
| 197 |
if err := DB.Model(&user2).Association("Team").Delete(&team2); err != nil { |
| 198 |
t.Fatalf("Error happened when delete Team, got %v", err) |
| 199 |
} |
| 200 |
AssertAssociationCount(t, user2, "Team", 0, "after delete") |
| 201 |
|
| 202 |
// Prepare Data for Clear |
| 203 |
if err := DB.Model(&user2).Association("Team").Append(&team); err != nil { |
| 204 |
t.Fatalf("Error happened when append Team, got %v", err) |
| 205 |
} |
| 206 |
|
| 207 |
AssertAssociationCount(t, user2, "Team", 1, "after prepare data") |
| 208 |
|
| 209 |
// Clear |
| 210 |
if err := DB.Model(&user2).Association("Team").Clear(); err != nil { |
| 211 |
t.Errorf("Error happened when clear Team, got %v", err) |
| 212 |
} |
| 213 |
|
| 214 |
AssertAssociationCount(t, user2, "Team", 0, "after clear") |
| 215 |
} |
| 216 |
|
| 217 |
func TestHasManyAssociationForSlice(t *testing.T) { |
| 218 |
users := []User{ |
| 219 |
*GetUser("slice-hasmany-1", Config{Pets: 2}), |
| 220 |
*GetUser("slice-hasmany-2", Config{Pets: 0}), |
| 221 |
*GetUser("slice-hasmany-3", Config{Pets: 4}), |
| 222 |
} |
| 223 |
|
| 224 |
DB.Create(&users) |
| 225 |
|
| 226 |
// Count |
| 227 |
AssertAssociationCount(t, users, "Pets", 6, "") |
| 228 |
|
| 229 |
// Find |
| 230 |
var pets []Pet |
| 231 |
if DB.Model(&users).Association("Pets").Find(&pets); len(pets) != 6 { |
| 232 |
t.Errorf("pets count should be %v, but got %v", 6, len(pets)) |
| 233 |
} |
| 234 |
|
| 235 |
// Append |
| 236 |
DB.Model(&users).Association("Pets").Append( |
| 237 |
&Pet{Name: "pet-slice-append-1"}, |
| 238 |
[]*Pet{{Name: "pet-slice-append-2-1"}, {Name: "pet-slice-append-2-2"}}, |
| 239 |
&Pet{Name: "pet-slice-append-3"}, |
| 240 |
) |
| 241 |
|
| 242 |
AssertAssociationCount(t, users, "Pets", 10, "After Append") |
| 243 |
|
| 244 |
// Replace -> same as append |
| 245 |
DB.Model(&users).Association("Pets").Replace( |
| 246 |
[]*Pet{{Name: "pet-slice-replace-1-1"}, {Name: "pet-slice-replace-1-2"}}, |
| 247 |
[]*Pet{{Name: "pet-slice-replace-2-1"}, {Name: "pet-slice-replace-2-2"}}, |
| 248 |
&Pet{Name: "pet-slice-replace-3"}, |
| 249 |
) |
| 250 |
|
| 251 |
AssertAssociationCount(t, users, "Pets", 5, "After Append") |
| 252 |
|
| 253 |
// Delete |
| 254 |
if err := DB.Model(&users).Association("Pets").Delete(&users[2].Pets); err != nil { |
| 255 |
t.Errorf("no error should happened when deleting pet, but got %v", err) |
| 256 |
} |
| 257 |
|
| 258 |
AssertAssociationCount(t, users, "Pets", 4, "after delete") |
| 259 |
|
| 260 |
if err := DB.Model(&users).Association("Pets").Delete(users[0].Pets[0], users[1].Pets[1]); err != nil { |
| 261 |
t.Errorf("no error should happened when deleting pet, but got %v", err) |
| 262 |
} |
| 263 |
|
| 264 |
AssertAssociationCount(t, users, "Pets", 2, "after delete") |
| 265 |
|
| 266 |
// Clear |
| 267 |
DB.Model(&users).Association("Pets").Clear() |
| 268 |
AssertAssociationCount(t, users, "Pets", 0, "After Clear") |
| 269 |
} |
| 270 |
|
| 271 |
func TestSingleTableHasManyAssociationForSlice(t *testing.T) { |
| 272 |
users := []User{ |
| 273 |
*GetUser("slice-hasmany-1", Config{Team: 2}), |
| 274 |
*GetUser("slice-hasmany-2", Config{Team: 0}), |
| 275 |
*GetUser("slice-hasmany-3", Config{Team: 4}), |
| 276 |
} |
| 277 |
|
| 278 |
if err := DB.Create(&users).Error; err != nil { |
| 279 |
t.Fatalf("errors happened when create: %v", err) |
| 280 |
} |
| 281 |
|
| 282 |
// Count |
| 283 |
AssertAssociationCount(t, users, "Team", 6, "") |
| 284 |
|
| 285 |
// Find |
| 286 |
var teams []User |
| 287 |
if DB.Model(&users).Association("Team").Find(&teams); len(teams) != 6 { |
| 288 |
t.Errorf("teams count should be %v, but got %v", 6, len(teams)) |
| 289 |
} |
| 290 |
|
| 291 |
// Append |
| 292 |
DB.Model(&users).Association("Team").Append( |
| 293 |
&User{Name: "pet-slice-append-1"}, |
| 294 |
[]*User{{Name: "pet-slice-append-2-1"}, {Name: "pet-slice-append-2-2"}}, |
| 295 |
&User{Name: "pet-slice-append-3"}, |
| 296 |
) |
| 297 |
|
| 298 |
AssertAssociationCount(t, users, "Team", 10, "After Append") |
| 299 |
|
| 300 |
// Replace -> same as append |
| 301 |
DB.Model(&users).Association("Team").Replace( |
| 302 |
[]*User{{Name: "pet-slice-replace-1-1"}, {Name: "pet-slice-replace-1-2"}}, |
| 303 |
[]*User{{Name: "pet-slice-replace-2-1"}, {Name: "pet-slice-replace-2-2"}}, |
| 304 |
&User{Name: "pet-slice-replace-3"}, |
| 305 |
) |
| 306 |
|
| 307 |
AssertAssociationCount(t, users, "Team", 5, "After Append") |
| 308 |
|
| 309 |
// Delete |
| 310 |
if err := DB.Model(&users).Association("Team").Delete(&users[2].Team); err != nil { |
| 311 |
t.Errorf("no error should happened when deleting pet, but got %v", err) |
| 312 |
} |
| 313 |
|
| 314 |
AssertAssociationCount(t, users, "Team", 4, "after delete") |
| 315 |
|
| 316 |
if err := DB.Model(&users).Association("Team").Delete(users[0].Team[0], users[1].Team[1]); err != nil { |
| 317 |
t.Errorf("no error should happened when deleting pet, but got %v", err) |
| 318 |
} |
| 319 |
|
| 320 |
AssertAssociationCount(t, users, "Team", 2, "after delete") |
| 321 |
|
| 322 |
// Clear |
| 323 |
DB.Model(&users).Association("Team").Clear() |
| 324 |
AssertAssociationCount(t, users, "Team", 0, "After Clear") |
| 325 |
} |
| 326 |
|
| 327 |
func TestPolymorphicHasManyAssociation(t *testing.T) { |
| 328 |
user := *GetUser("hasmany", Config{Toys: 2}) |
| 329 |
|
| 330 |
if err := DB.Create(&user).Error; err != nil { |
| 331 |
t.Fatalf("errors happened when create: %v", err) |
| 332 |
} |
| 333 |
|
| 334 |
CheckUser(t, user, user) |
| 335 |
|
| 336 |
// Find |
| 337 |
var user2 User |
| 338 |
DB.Find(&user2, "id = ?", user.ID) |
| 339 |
DB.Model(&user2).Association("Toys").Find(&user2.Toys) |
| 340 |
CheckUser(t, user2, user) |
| 341 |
|
| 342 |
// Count |
| 343 |
AssertAssociationCount(t, user, "Toys", 2, "") |
| 344 |
|
| 345 |
// Append |
| 346 |
toy := Toy{Name: "toy-has-many-append"} |
| 347 |
|
| 348 |
if err := DB.Model(&user2).Association("Toys").Append(&toy); err != nil { |
| 349 |
t.Fatalf("Error happened when append account, got %v", err) |
| 350 |
} |
| 351 |
|
| 352 |
if toy.ID == 0 { |
| 353 |
t.Fatalf("Toy's ID should be created") |
| 354 |
} |
| 355 |
|
| 356 |
user.Toys = append(user.Toys, toy) |
| 357 |
CheckUser(t, user2, user) |
| 358 |
|
| 359 |
AssertAssociationCount(t, user, "Toys", 3, "AfterAppend") |
| 360 |
|
| 361 |
toys := []Toy{{Name: "toy-has-many-append-1-1"}, {Name: "toy-has-many-append-1-1"}} |
| 362 |
|
| 363 |
if err := DB.Model(&user2).Association("Toys").Append(&toys); err != nil { |
| 364 |
t.Fatalf("Error happened when append toy, got %v", err) |
| 365 |
} |
| 366 |
|
| 367 |
for _, toy := range toys { |
| 368 |
toy := toy |
| 369 |
if toy.ID == 0 { |
| 370 |
t.Fatalf("Toy's ID should be created") |
| 371 |
} |
| 372 |
|
| 373 |
user.Toys = append(user.Toys, toy) |
| 374 |
} |
| 375 |
|
| 376 |
CheckUser(t, user2, user) |
| 377 |
|
| 378 |
AssertAssociationCount(t, user, "Toys", 5, "AfterAppendSlice") |
| 379 |
|
| 380 |
// Replace |
| 381 |
toy2 := Toy{Name: "toy-has-many-replace"} |
| 382 |
|
| 383 |
if err := DB.Model(&user2).Association("Toys").Replace(&toy2); err != nil { |
| 384 |
t.Fatalf("Error happened when append toy, got %v", err) |
| 385 |
} |
| 386 |
|
| 387 |
if toy2.ID == 0 { |
| 388 |
t.Fatalf("toy2's ID should be created") |
| 389 |
} |
| 390 |
|
| 391 |
user.Toys = []Toy{toy2} |
| 392 |
CheckUser(t, user2, user) |
| 393 |
|
| 394 |
AssertAssociationCount(t, user2, "Toys", 1, "AfterReplace") |
| 395 |
|
| 396 |
// Delete |
| 397 |
if err := DB.Model(&user2).Association("Toys").Delete(&Toy{}); err != nil { |
| 398 |
t.Fatalf("Error happened when delete toy, got %v", err) |
| 399 |
} |
| 400 |
AssertAssociationCount(t, user2, "Toys", 1, "after delete non-existing data") |
| 401 |
|
| 402 |
if err := DB.Model(&user2).Association("Toys").Delete(&toy2); err != nil { |
| 403 |
t.Fatalf("Error happened when delete Toys, got %v", err) |
| 404 |
} |
| 405 |
AssertAssociationCount(t, user2, "Toys", 0, "after delete") |
| 406 |
|
| 407 |
// Prepare Data for Clear |
| 408 |
if err := DB.Model(&user2).Association("Toys").Append(&toy); err != nil { |
| 409 |
t.Fatalf("Error happened when append Toys, got %v", err) |
| 410 |
} |
| 411 |
|
| 412 |
AssertAssociationCount(t, user2, "Toys", 1, "after prepare data") |
| 413 |
|
| 414 |
// Clear |
| 415 |
if err := DB.Model(&user2).Association("Toys").Clear(); err != nil { |
| 416 |
t.Errorf("Error happened when clear Toys, got %v", err) |
| 417 |
} |
| 418 |
|
| 419 |
AssertAssociationCount(t, user2, "Toys", 0, "after clear") |
| 420 |
} |
| 421 |
|
| 422 |
func TestPolymorphicHasManyAssociationForSlice(t *testing.T) { |
| 423 |
users := []User{ |
| 424 |
*GetUser("slice-hasmany-1", Config{Toys: 2}), |
| 425 |
*GetUser("slice-hasmany-2", Config{Toys: 0, Tools: 2}), |
| 426 |
*GetUser("slice-hasmany-3", Config{Toys: 4}), |
| 427 |
} |
| 428 |
|
| 429 |
DB.Create(&users) |
| 430 |
|
| 431 |
// Count |
| 432 |
AssertAssociationCount(t, users, "Toys", 6, "") |
| 433 |
AssertAssociationCount(t, users, "Tools", 2, "") |
| 434 |
|
| 435 |
// Find |
| 436 |
var toys []Toy |
| 437 |
if DB.Model(&users).Association("Toys").Find(&toys); len(toys) != 6 { |
| 438 |
t.Errorf("toys count should be %v, but got %v", 6, len(toys)) |
| 439 |
} |
| 440 |
|
| 441 |
// Find Tools (polymorphic with custom type and id) |
| 442 |
var tools []Tools |
| 443 |
DB.Model(&users).Association("Tools").Find(&tools) |
| 444 |
|
| 445 |
if len(tools) != 2 { |
| 446 |
t.Errorf("tools count should be %v, but got %v", 2, len(tools)) |
| 447 |
} |
| 448 |
|
| 449 |
// Append |
| 450 |
DB.Model(&users).Association("Toys").Append( |
| 451 |
&Toy{Name: "toy-slice-append-1"}, |
| 452 |
[]Toy{{Name: "toy-slice-append-2-1"}, {Name: "toy-slice-append-2-2"}}, |
| 453 |
&Toy{Name: "toy-slice-append-3"}, |
| 454 |
) |
| 455 |
|
| 456 |
AssertAssociationCount(t, users, "Toys", 10, "After Append") |
| 457 |
|
| 458 |
// Replace -> same as append |
| 459 |
DB.Model(&users).Association("Toys").Replace( |
| 460 |
[]*Toy{{Name: "toy-slice-replace-1-1"}, {Name: "toy-slice-replace-1-2"}}, |
| 461 |
[]*Toy{{Name: "toy-slice-replace-2-1"}, {Name: "toy-slice-replace-2-2"}}, |
| 462 |
&Toy{Name: "toy-slice-replace-3"}, |
| 463 |
) |
| 464 |
|
| 465 |
AssertAssociationCount(t, users, "Toys", 5, "After Append") |
| 466 |
|
| 467 |
// Delete |
| 468 |
if err := DB.Model(&users).Association("Toys").Delete(&users[2].Toys); err != nil { |
| 469 |
t.Errorf("no error should happened when deleting toy, but got %v", err) |
| 470 |
} |
| 471 |
|
| 472 |
AssertAssociationCount(t, users, "Toys", 4, "after delete") |
| 473 |
|
| 474 |
if err := DB.Model(&users).Association("Toys").Delete(users[0].Toys[0], users[1].Toys[1]); err != nil { |
| 475 |
t.Errorf("no error should happened when deleting toy, but got %v", err) |
| 476 |
} |
| 477 |
|
| 478 |
AssertAssociationCount(t, users, "Toys", 2, "after delete") |
| 479 |
|
| 480 |
// Clear |
| 481 |
DB.Model(&users).Association("Toys").Clear() |
| 482 |
AssertAssociationCount(t, users, "Toys", 0, "After Clear") |
| 483 |
} |
| 484 |
|
| 485 |
func TestHasManyAssociationUnscoped(t *testing.T) { |
| 486 |
type ItemContent struct { |
| 487 |
gorm.Model |
| 488 |
ItemID uint `gorm:"not null"` |
| 489 |
Name string `gorm:"not null;type:varchar(50)"` |
| 490 |
LanguageCode string `gorm:"not null;type:varchar(2)"` |
| 491 |
} |
| 492 |
type Item struct { |
| 493 |
gorm.Model |
| 494 |
Logo string `gorm:"not null;type:varchar(50)"` |
| 495 |
Contents []ItemContent `gorm:"foreignKey:ItemID"` |
| 496 |
} |
| 497 |
|
| 498 |
tx := DB.Session(&gorm.Session{}) |
| 499 |
tx.Migrator().DropTable(&ItemContent{}, &Item{}) |
| 500 |
tx.AutoMigrate(&ItemContent{}, &Item{}) |
| 501 |
|
| 502 |
item := Item{ |
| 503 |
Logo: "logo", |
| 504 |
Contents: []ItemContent{ |
| 505 |
{Name: "name", LanguageCode: "en"}, |
| 506 |
{Name: "ar name", LanguageCode: "ar"}, |
| 507 |
}, |
| 508 |
} |
| 509 |
if err := tx.Create(&item).Error; err != nil { |
| 510 |
t.Fatalf("failed to create items, got error: %v", err) |
| 511 |
} |
| 512 |
|
| 513 |
// test Replace |
| 514 |
if err := tx.Model(&item).Association("Contents").Unscoped().Replace([]ItemContent{ |
| 515 |
{Name: "updated name", LanguageCode: "en"}, |
| 516 |
{Name: "ar updated name", LanguageCode: "ar"}, |
| 517 |
{Name: "le nom", LanguageCode: "fr"}, |
| 518 |
}); err != nil { |
| 519 |
t.Errorf("failed to replace item content, got error: %v", err) |
| 520 |
} |
| 521 |
|
| 522 |
if count := tx.Model(&item).Association("Contents").Count(); count != 3 { |
| 523 |
t.Errorf("expected %d contents, got %d", 3, count) |
| 524 |
} |
| 525 |
|
| 526 |
var contents []ItemContent |
| 527 |
if err := tx.Find(&contents).Error; err != nil { |
| 528 |
t.Errorf("failed to find contents, got error: %v", err) |
| 529 |
} |
| 530 |
if len(contents) != 3 { |
| 531 |
t.Errorf("expected %d contents, got %d", 3, len(contents)) |
| 532 |
} |
| 533 |
|
| 534 |
// test delete |
| 535 |
if err := tx.Model(&item).Association("Contents").Unscoped().Delete(&contents[0]); err != nil { |
| 536 |
t.Errorf("failed to delete Contents, got error: %v", err) |
| 537 |
} |
| 538 |
if count := tx.Model(&item).Association("Contents").Count(); count != 2 { |
| 539 |
t.Errorf("expected %d contents, got %d", 2, count) |
| 540 |
} |
| 541 |
|
| 542 |
// test clear |
| 543 |
if err := tx.Model(&item).Association("Contents").Unscoped().Clear(); err != nil { |
| 544 |
t.Errorf("failed to clear contents association, got error: %v", err) |
| 545 |
} |
| 546 |
if count := tx.Model(&item).Association("Contents").Count(); count != 0 { |
| 547 |
t.Errorf("expected %d contents, got %d", 0, count) |
| 548 |
} |
| 549 |
|
| 550 |
if err := tx.Find(&contents).Error; err != nil { |
| 551 |
t.Errorf("failed to find contents, got error: %v", err) |
| 552 |
} |
| 553 |
if len(contents) != 0 { |
| 554 |
t.Errorf("expected %d contents, got %d", 0, len(contents)) |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
func TestHasManyAssociationReplaceWithStructValue(t *testing.T) { |
| 559 |
user := User{Name: "HasManyAssociationReplaceWithStructValue", Languages: []Language{{Name: "EN", Code: "en"}}} |
| 560 |
|
| 561 |
if err := DB.Create(&user).Error; err != nil { |
| 562 |
t.Fatalf("errors happened when create: %v", err) |
| 563 |
} |
| 564 |
|
| 565 |
if err := DB.Model(&user).Association("Languages").Replace(Language{Name: "DE", Code: "de"}, Language{Name: "FR", Code: "fr"}); err != nil { |
| 566 |
t.Error("expected association error to be not nil") |
| 567 |
} |
| 568 |
|
| 569 |
var result User |
| 570 |
DB.Preload("Languages").Where("name = ?", "HasManyAssociationReplaceWithStructValue").Find(&result) |
| 571 |
if len(result.Languages) != 2 { |
| 572 |
t.Errorf("invalid languages found for user, got %v", result.Languages) |
| 573 |
} |
| 574 |
} |