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