Debian Codegraph @

tests/preload_suits_test.go

raw
1 package tests_test
2
3 import (
4 "database/sql"
5 "encoding/json"
6 "reflect"
7 "sort"
8 "sync/atomic"
9 "testing"
10
11 "gorm.io/gorm"
12 )
13
14 func toJSONString(v interface{}) []byte {
15 r, _ := json.Marshal(v)
16 return r
17 }
18
19 func TestNestedPreload1(t *testing.T) {
20 type (
21 Level1 struct {
22 ID uint
23 Value string
24 Level2ID uint
25 }
26 Level2 struct {
27 ID uint
28 Level1 Level1
29 Level3ID uint
30 }
31 Level3 struct {
32 ID uint
33 Name string
34 Level2 Level2
35 }
36 )
37 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
38 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
39 t.Error(err)
40 }
41
42 want := Level3{Level2: Level2{Level1: Level1{Value: "value"}}}
43 if err := DB.Create(&want).Error; err != nil {
44 t.Error(err)
45 }
46
47 var got Level3
48 if err := DB.Preload("Level2").Preload("Level2.Level1").Find(&got).Error; err != nil {
49 t.Error(err)
50 }
51
52 if !reflect.DeepEqual(got, want) {
53 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
54 }
55
56 if err := DB.Preload("Level2").Preload("Level2.Level1").First(&got, "name = ?", "not_found").Error; err != gorm.ErrRecordNotFound {
57 t.Error(err)
58 }
59 }
60
61 func TestNestedPreload2(t *testing.T) {
62 type (
63 Level1 struct {
64 ID uint
65 Value string
66 Level2ID uint
67 }
68 Level2 struct {
69 ID uint
70 Level1s []*Level1
71 Level3ID uint
72 }
73 Level3 struct {
74 ID uint
75 Name string
76 Level2s []Level2
77 }
78 )
79 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
80 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
81 t.Error(err)
82 }
83
84 want := Level3{
85 Level2s: []Level2{
86 {
87 Level1s: []*Level1{
88 {Value: "value1"},
89 {Value: "value2"},
90 },
91 },
92 {
93 Level1s: []*Level1{
94 {Value: "value3"},
95 },
96 },
97 },
98 }
99 if err := DB.Create(&want).Error; err != nil {
100 t.Error(err)
101 }
102
103 var got Level3
104 if err := DB.Preload("Level2s.Level1s").Find(&got).Error; err != nil {
105 t.Error(err)
106 }
107
108 if !reflect.DeepEqual(got, want) {
109 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
110 }
111 }
112
113 func TestNestedPreload3(t *testing.T) {
114 type (
115 Level1 struct {
116 ID uint
117 Value string
118 Level2ID uint
119 }
120 Level2 struct {
121 ID uint
122 Level1 Level1
123 Level3ID uint
124 }
125 Level3 struct {
126 Name string
127 ID uint
128 Level2s []Level2
129 }
130 )
131 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
132 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
133 t.Error(err)
134 }
135
136 want := Level3{
137 Level2s: []Level2{
138 {Level1: Level1{Value: "value1"}},
139 {Level1: Level1{Value: "value2"}},
140 },
141 }
142 if err := DB.Create(&want).Error; err != nil {
143 t.Error(err)
144 }
145
146 var got Level3
147 if err := DB.Preload("Level2s.Level1").Find(&got).Error; err != nil {
148 t.Error(err)
149 }
150
151 if !reflect.DeepEqual(got, want) {
152 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
153 }
154 }
155
156 func TestNestedPreload4(t *testing.T) {
157 type (
158 Level1 struct {
159 ID uint
160 Value string
161 Level2ID uint
162 }
163 Level2 struct {
164 ID uint
165 Level1s []Level1
166 Level3ID uint
167 }
168 Level3 struct {
169 ID uint
170 Name string
171 Level2 Level2
172 }
173 )
174 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
175 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
176 t.Error(err)
177 }
178
179 want := Level3{
180 Level2: Level2{
181 Level1s: []Level1{
182 {Value: "value1"},
183 {Value: "value2"},
184 },
185 },
186 }
187 if err := DB.Create(&want).Error; err != nil {
188 t.Error(err)
189 }
190
191 var got Level3
192 if err := DB.Preload("Level2.Level1s").Find(&got).Error; err != nil {
193 t.Error(err)
194 }
195
196 if !reflect.DeepEqual(got, want) {
197 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
198 }
199 }
200
201 // Slice: []Level3
202 func TestNestedPreload5(t *testing.T) {
203 type (
204 Level1 struct {
205 ID uint
206 Value string
207 Level2ID uint
208 }
209 Level2 struct {
210 ID uint
211 Level1 Level1
212 Level3ID uint
213 }
214 Level3 struct {
215 ID uint
216 Name string
217 Level2 Level2
218 }
219 )
220 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
221 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
222 t.Error(err)
223 }
224
225 want := make([]Level3, 2)
226 want[0] = Level3{Level2: Level2{Level1: Level1{Value: "value"}}}
227 if err := DB.Create(&want[0]).Error; err != nil {
228 t.Error(err)
229 }
230 want[1] = Level3{Level2: Level2{Level1: Level1{Value: "value2"}}}
231 if err := DB.Create(&want[1]).Error; err != nil {
232 t.Error(err)
233 }
234
235 var got []Level3
236 if err := DB.Preload("Level2").Preload("Level2.Level1").Find(&got).Error; err != nil {
237 t.Error(err)
238 }
239
240 if !reflect.DeepEqual(got, want) {
241 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
242 }
243 }
244
245 func TestNestedPreload6(t *testing.T) {
246 type (
247 Level1 struct {
248 ID uint
249 Value string
250 Level2ID uint
251 }
252 Level2 struct {
253 ID uint
254 Level1s []Level1
255 Level3ID uint
256 }
257 Level3 struct {
258 ID uint
259 Name string
260 Level2s []Level2
261 }
262 )
263 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
264 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
265 t.Error(err)
266 }
267
268 want := make([]Level3, 2)
269 want[0] = Level3{
270 Level2s: []Level2{
271 {
272 Level1s: []Level1{
273 {Value: "value1"},
274 {Value: "value2"},
275 },
276 },
277 {
278 Level1s: []Level1{
279 {Value: "value3"},
280 },
281 },
282 },
283 }
284 if err := DB.Create(&want[0]).Error; err != nil {
285 t.Error(err)
286 }
287
288 want[1] = Level3{
289 Level2s: []Level2{
290 {
291 Level1s: []Level1{
292 {Value: "value3"},
293 {Value: "value4"},
294 },
295 },
296 {
297 Level1s: []Level1{
298 {Value: "value5"},
299 },
300 },
301 },
302 }
303 if err := DB.Create(&want[1]).Error; err != nil {
304 t.Error(err)
305 }
306
307 var got []Level3
308 if err := DB.Preload("Level2s.Level1s").Find(&got).Error; err != nil {
309 t.Error(err)
310 }
311
312 if !reflect.DeepEqual(got, want) {
313 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
314 }
315 }
316
317 func TestNestedPreload7(t *testing.T) {
318 type (
319 Level1 struct {
320 ID uint
321 Value string
322 Level2ID uint
323 }
324 Level2 struct {
325 ID uint
326 Level1 Level1
327 Level3ID uint
328 }
329 Level3 struct {
330 ID uint
331 Name string
332 Level2s []Level2
333 }
334 )
335 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
336 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
337 t.Error(err)
338 }
339
340 want := make([]Level3, 2)
341 want[0] = Level3{
342 Level2s: []Level2{
343 {Level1: Level1{Value: "value1"}},
344 {Level1: Level1{Value: "value2"}},
345 },
346 }
347 if err := DB.Create(&want[0]).Error; err != nil {
348 t.Error(err)
349 }
350
351 want[1] = Level3{
352 Level2s: []Level2{
353 {Level1: Level1{Value: "value3"}},
354 {Level1: Level1{Value: "value4"}},
355 },
356 }
357 if err := DB.Create(&want[1]).Error; err != nil {
358 t.Error(err)
359 }
360
361 var got []Level3
362 if err := DB.Preload("Level2s.Level1").Find(&got).Error; err != nil {
363 t.Error(err)
364 }
365
366 if !reflect.DeepEqual(got, want) {
367 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
368 }
369 }
370
371 func TestNestedPreload8(t *testing.T) {
372 type (
373 Level1 struct {
374 ID uint
375 Value string
376 Level2ID uint
377 }
378 Level2 struct {
379 ID uint
380 Level1s []Level1
381 Level3ID uint
382 }
383 Level3 struct {
384 ID uint
385 Name string
386 Level2 Level2
387 }
388 )
389 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
390 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
391 t.Error(err)
392 }
393
394 want := make([]Level3, 2)
395 want[0] = Level3{
396 Level2: Level2{
397 Level1s: []Level1{
398 {Value: "value1"},
399 {Value: "value2"},
400 },
401 },
402 }
403 if err := DB.Create(&want[0]).Error; err != nil {
404 t.Error(err)
405 }
406 want[1] = Level3{
407 Level2: Level2{
408 Level1s: []Level1{
409 {Value: "value3"},
410 {Value: "value4"},
411 },
412 },
413 }
414 if err := DB.Create(&want[1]).Error; err != nil {
415 t.Error(err)
416 }
417
418 var got []Level3
419 if err := DB.Preload("Level2.Level1s").Find(&got).Error; err != nil {
420 t.Error(err)
421 }
422
423 if !reflect.DeepEqual(got, want) {
424 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
425 }
426 }
427
428 func TestNestedPreload9(t *testing.T) {
429 type (
430 Level0 struct {
431 ID uint
432 Value string
433 Level1ID uint
434 }
435 Level1 struct {
436 ID uint
437 Value string
438 Level2ID *uint
439 Level2_1ID *uint
440 Level0s []Level0 `json:",omitempty"`
441 }
442 Level2 struct {
443 ID uint
444 Level1s []Level1
445 Level3ID uint
446 }
447 Level2_1 struct {
448 ID uint
449 Level1s []Level1 `json:",omitempty"`
450 Level3ID uint
451 }
452 Level3 struct {
453 ID uint
454 Name string
455 Level2 Level2
456 Level2_1 Level2_1
457 }
458 )
459 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, &Level2_1{}, &Level0{})
460 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}, &Level2_1{}, &Level0{}); err != nil {
461 t.Error(err)
462 }
463
464 want := make([]Level3, 2)
465 want[0] = Level3{
466 Level2: Level2{
467 Level1s: []Level1{
468 {Value: "value1"},
469 {Value: "value2"},
470 },
471 },
472 Level2_1: Level2_1{
473 Level1s: []Level1{
474 {
475 Value: "value1-1",
476 Level0s: []Level0{{Value: "Level0-1"}},
477 },
478 {
479 Value: "value2-2",
480 Level0s: []Level0{{Value: "Level0-2"}},
481 },
482 },
483 },
484 }
485 if err := DB.Create(&want[0]).Error; err != nil {
486 t.Error(err)
487 }
488 want[1] = Level3{
489 Level2: Level2{
490 Level1s: []Level1{
491 {Value: "value3"},
492 {Value: "value4"},
493 },
494 },
495 Level2_1: Level2_1{
496 Level1s: []Level1{
497 {
498 Value: "value3-3",
499 Level0s: []Level0{},
500 },
501 {
502 Value: "value4-4",
503 Level0s: []Level0{},
504 },
505 },
506 },
507 }
508 if err := DB.Create(&want[1]).Error; err != nil {
509 t.Error(err)
510 }
511
512 var got []Level3
513 if err := DB.Preload("Level2").Preload("Level2.Level1s").Preload("Level2_1").Preload("Level2_1.Level1s").Preload("Level2_1.Level1s.Level0s").Find(&got).Error; err != nil {
514 t.Error(err)
515 }
516
517 if string(toJSONString(got)) != string(toJSONString(want)) {
518 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
519 }
520 }
521
522 type LevelA1 struct {
523 ID uint
524 Value string
525 }
526
527 type LevelA2 struct {
528 ID uint
529 Value string
530 LevelA3s []*LevelA3 `json:",omitempty"`
531 }
532
533 type LevelA3 struct {
534 ID uint
535 Value string
536 LevelA1ID sql.NullInt64
537 LevelA1 *LevelA1
538 LevelA2ID sql.NullInt64
539 LevelA2 *LevelA2
540 }
541
542 func TestNestedPreload10(t *testing.T) {
543 DB.Migrator().DropTable(&LevelA3{}, &LevelA2{}, &LevelA1{})
544 if err := DB.AutoMigrate(&LevelA1{}, &LevelA2{}, &LevelA3{}); err != nil {
545 t.Error(err)
546 }
547
548 levelA1 := &LevelA1{Value: "foo"}
549 if err := DB.Save(levelA1).Error; err != nil {
550 t.Error(err)
551 }
552
553 want := []*LevelA2{
554 {
555 Value: "bar",
556 LevelA3s: []*LevelA3{
557 {
558 Value: "qux",
559 LevelA1: levelA1,
560 },
561 },
562 },
563 {
564 Value: "bar 2",
565 LevelA3s: []*LevelA3{},
566 },
567 }
568 for _, levelA2 := range want {
569 if err := DB.Save(levelA2).Error; err != nil {
570 t.Error(err)
571 }
572 }
573
574 var got []*LevelA2
575 if err := DB.Preload("LevelA3s.LevelA1").Find(&got).Error; err != nil {
576 t.Error(err)
577 }
578
579 if !reflect.DeepEqual(toJSONString(got), toJSONString(want)) {
580 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
581 }
582 }
583
584 type LevelB1 struct {
585 ID uint
586 Value string
587 LevelB3s []*LevelB3
588 }
589
590 type LevelB2 struct {
591 ID uint
592 Value string
593 }
594
595 type LevelB3 struct {
596 ID uint
597 Value string
598 LevelB1ID sql.NullInt64
599 LevelB1 *LevelB1
600 LevelB2s []*LevelB2 `gorm:"many2many:levelb1_levelb3_levelb2s" json:",omitempty"`
601 }
602
603 func TestNestedPreload11(t *testing.T) {
604 DB.Migrator().DropTable(&LevelB3{}, &LevelB2{}, &LevelB1{})
605 if err := DB.AutoMigrate(&LevelB1{}, &LevelB2{}, &LevelB3{}); err != nil {
606 t.Error(err)
607 }
608
609 levelB1 := &LevelB1{Value: "foo"}
610 if err := DB.Create(levelB1).Error; err != nil {
611 t.Error(err)
612 }
613
614 levelB3 := &LevelB3{
615 Value: "bar",
616 LevelB1ID: sql.NullInt64{Valid: true, Int64: int64(levelB1.ID)},
617 LevelB2s: []*LevelB2{},
618 }
619 if err := DB.Create(levelB3).Error; err != nil {
620 t.Error(err)
621 }
622 levelB1.LevelB3s = []*LevelB3{levelB3}
623
624 want := []*LevelB1{levelB1}
625 var got []*LevelB1
626 if err := DB.Preload("LevelB3s.LevelB2s").Find(&got).Error; err != nil {
627 t.Error(err)
628 }
629
630 if !reflect.DeepEqual(toJSONString(got), toJSONString(want)) {
631 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
632 }
633 }
634
635 type LevelC1 struct {
636 ID uint
637 Value string
638 LevelC2ID uint
639 }
640
641 type LevelC2 struct {
642 ID uint
643 Value string
644 LevelC1 LevelC1
645 }
646
647 type LevelC3 struct {
648 ID uint
649 Value string
650 LevelC2ID uint
651 LevelC2 LevelC2
652 }
653
654 func TestNestedPreload12(t *testing.T) {
655 DB.Migrator().DropTable(&LevelC3{}, &LevelC2{}, &LevelC1{})
656 if err := DB.AutoMigrate(&LevelC1{}, &LevelC2{}, &LevelC3{}); err != nil {
657 t.Error(err)
658 }
659
660 level2 := LevelC2{
661 Value: "c2",
662 LevelC1: LevelC1{
663 Value: "c1",
664 },
665 }
666 DB.Create(&level2)
667
668 want := []LevelC3{
669 {
670 Value: "c3-1",
671 LevelC2: level2,
672 }, {
673 Value: "c3-2",
674 LevelC2: level2,
675 },
676 }
677
678 for i := range want {
679 if err := DB.Create(&want[i]).Error; err != nil {
680 t.Error(err)
681 }
682 }
683
684 var got []LevelC3
685 if err := DB.Preload("LevelC2").Preload("LevelC2.LevelC1").Find(&got).Error; err != nil {
686 t.Error(err)
687 }
688
689 if !reflect.DeepEqual(got, want) {
690 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
691 }
692 }
693
694 func TestManyToManyPreloadWithMultiPrimaryKeys(t *testing.T) {
695 if name := DB.Dialector.Name(); name == "sqlite" || name == "sqlserver" {
696 t.Skip("skip sqlite, sqlserver due to it doesn't support multiple primary keys with auto increment")
697 }
698
699 if name := DB.Dialector.Name(); name == "mysql" {
700 t.Skip("skip mysql due to it only allow unique constraint matching given keys")
701 }
702
703 type (
704 Level1 struct {
705 ID uint `gorm:"primary_key;"`
706 LanguageCode string `gorm:"primary_key"`
707 Value string
708 }
709 Level2 struct {
710 ID uint `gorm:"primary_key;"`
711 LanguageCode string `gorm:"primary_key"`
712 Value string
713 Level1s []Level1 `gorm:"many2many:levels;"`
714 }
715 )
716
717 DB.Migrator().DropTable(&Level2{}, &Level1{})
718 DB.Migrator().DropTable("levels")
719
720 if err := DB.AutoMigrate(&Level2{}, &Level1{}); err != nil {
721 t.Error(err)
722 }
723
724 want := Level2{Value: "Bob", LanguageCode: "ru", Level1s: []Level1{
725 {Value: "ru", LanguageCode: "ru"},
726 {Value: "en", LanguageCode: "en"},
727 }}
728 if err := DB.Save(&want).Error; err != nil {
729 t.Error(err)
730 }
731
732 want2 := Level2{Value: "Tom", LanguageCode: "zh", Level1s: []Level1{
733 {Value: "zh", LanguageCode: "zh"},
734 {Value: "de", LanguageCode: "de"},
735 }}
736 if err := DB.Save(&want2).Error; err != nil {
737 t.Error(err)
738 }
739
740 var got Level2
741 if err := DB.Preload("Level1s").Find(&got, "value = ?", "Bob").Error; err != nil {
742 t.Error(err)
743 }
744
745 if !reflect.DeepEqual(got, want) {
746 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
747 }
748
749 var got2 Level2
750 if err := DB.Preload("Level1s").Find(&got2, "value = ?", "Tom").Error; err != nil {
751 t.Error(err)
752 }
753
754 if !reflect.DeepEqual(got2, want2) {
755 t.Errorf("got %s; want %s", toJSONString(got2), toJSONString(want2))
756 }
757
758 var got3 []Level2
759 if err := DB.Preload("Level1s").Find(&got3, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {
760 t.Error(err)
761 }
762
763 if !reflect.DeepEqual(got3, []Level2{got, got2}) {
764 t.Errorf("got %s; want %s", toJSONString(got3), toJSONString([]Level2{got, got2}))
765 }
766
767 var got4 []Level2
768 if err := DB.Preload("Level1s", "value IN (?)", []string{"zh", "ru"}).Find(&got4, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {
769 t.Error(err)
770 }
771
772 var ruLevel1 Level1
773 var zhLevel1 Level1
774 DB.First(&ruLevel1, "value = ?", "ru")
775 DB.First(&zhLevel1, "value = ?", "zh")
776
777 got.Level1s = []Level1{ruLevel1}
778 got2.Level1s = []Level1{zhLevel1}
779 if !reflect.DeepEqual(got4, []Level2{got, got2}) {
780 t.Errorf("got %s; want %s", toJSONString(got4), toJSONString([]Level2{got, got2}))
781 }
782
783 if err := DB.Preload("Level1s").Find(&got4, "value IN (?)", []string{"non-existing"}).Error; err != nil {
784 t.Error(err)
785 }
786 }
787
788 func TestManyToManyPreloadForNestedPointer(t *testing.T) {
789 type (
790 Level1 struct {
791 ID uint
792 Value string
793 }
794 Level2 struct {
795 ID uint
796 Value string
797 Level1s []*Level1 `gorm:"many2many:levels;"`
798 }
799 Level3 struct {
800 ID uint
801 Value string
802 Level2ID sql.NullInt64
803 Level2 *Level2
804 }
805 )
806
807 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
808 DB.Migrator().DropTable("levels")
809
810 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
811 t.Error(err)
812 }
813
814 want := Level3{
815 Value: "Bob",
816 Level2: &Level2{
817 Value: "Foo",
818 Level1s: []*Level1{
819 {Value: "ru"},
820 {Value: "en"},
821 },
822 },
823 }
824 if err := DB.Save(&want).Error; err != nil {
825 t.Error(err)
826 }
827
828 want2 := Level3{
829 Value: "Tom",
830 Level2: &Level2{
831 Value: "Bar",
832 Level1s: []*Level1{
833 {Value: "zh"},
834 {Value: "de"},
835 },
836 },
837 }
838 if err := DB.Save(&want2).Error; err != nil {
839 t.Error(err)
840 }
841
842 var got Level3
843 if err := DB.Preload("Level2.Level1s").Find(&got, "value = ?", "Bob").Error; err != nil {
844 t.Error(err)
845 }
846
847 if !reflect.DeepEqual(got, want) {
848 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
849 }
850
851 var got2 Level3
852 if err := DB.Preload("Level2.Level1s").Find(&got2, "value = ?", "Tom").Error; err != nil {
853 t.Error(err)
854 }
855
856 if !reflect.DeepEqual(got2, want2) {
857 t.Errorf("got %s; want %s", toJSONString(got2), toJSONString(want2))
858 }
859
860 var got3 []Level3
861 if err := DB.Preload("Level2.Level1s").Find(&got3, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {
862 t.Error(err)
863 }
864
865 if !reflect.DeepEqual(got3, []Level3{got, got2}) {
866 t.Errorf("got %s; want %s", toJSONString(got3), toJSONString([]Level3{got, got2}))
867 }
868
869 var got4 []Level3
870 if err := DB.Preload("Level2.Level1s", "value IN (?)", []string{"zh", "ru"}).Find(&got4, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {
871 t.Error(err)
872 }
873
874 var got5 Level3
875 DB.Preload("Level2.Level1s").Find(&got5, "value = ?", "bogus")
876
877 var ruLevel1 Level1
878 var zhLevel1 Level1
879 DB.First(&ruLevel1, "value = ?", "ru")
880 DB.First(&zhLevel1, "value = ?", "zh")
881
882 got.Level2.Level1s = []*Level1{&ruLevel1}
883 got2.Level2.Level1s = []*Level1{&zhLevel1}
884 if !reflect.DeepEqual(got4, []Level3{got, got2}) {
885 t.Errorf("got %s; want %s", toJSONString(got4), toJSONString([]Level3{got, got2}))
886 }
887 }
888
889 func TestNestedManyToManyPreload(t *testing.T) {
890 type (
891 Level1 struct {
892 ID uint
893 Value string
894 }
895 Level2 struct {
896 ID uint
897 Value string
898 Level1s []*Level1 `gorm:"many2many:level1_level2;"`
899 }
900 Level3 struct {
901 ID uint
902 Value string
903 Level2s []Level2 `gorm:"many2many:level2_level3;"`
904 }
905 )
906
907 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, "level1_level2", "level2_level3")
908
909 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
910 t.Error(err)
911 }
912
913 want := Level3{
914 Value: "Level3",
915 Level2s: []Level2{
916 {
917 Value: "Bob",
918 Level1s: []*Level1{
919 {Value: "ru"},
920 {Value: "en"},
921 },
922 }, {
923 Value: "Tom",
924 Level1s: []*Level1{
925 {Value: "zh"},
926 {Value: "de"},
927 },
928 },
929 },
930 }
931
932 if err := DB.Save(&want).Error; err != nil {
933 t.Error(err)
934 }
935
936 var got Level3
937 if err := DB.Preload("Level2s").Preload("Level2s.Level1s").Find(&got, "value = ?", "Level3").Error; err != nil {
938 t.Error(err)
939 }
940
941 if !reflect.DeepEqual(got, want) {
942 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
943 }
944
945 if err := DB.Preload("Level2s.Level1s").First(&got, "value = ?", "not_found").Error; err != gorm.ErrRecordNotFound {
946 t.Error(err)
947 }
948 }
949
950 func TestNestedManyToManyPreload2(t *testing.T) {
951 type (
952 Level1 struct {
953 ID uint
954 Value string
955 }
956 Level2 struct {
957 ID uint
958 Value string
959 Level1s []*Level1 `gorm:"many2many:level1_level2;"`
960 }
961 Level3 struct {
962 ID uint
963 Value string
964 Level2ID sql.NullInt64
965 Level2 *Level2
966 }
967 )
968
969 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
970 DB.Migrator().DropTable("level1_level2")
971
972 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
973 t.Error(err)
974 }
975
976 want := Level3{
977 Value: "Level3",
978 Level2: &Level2{
979 Value: "Bob",
980 Level1s: []*Level1{
981 {Value: "ru"},
982 {Value: "en"},
983 },
984 },
985 }
986
987 if err := DB.Save(&want).Error; err != nil {
988 t.Error(err)
989 }
990
991 var got Level3
992 if err := DB.Preload("Level2.Level1s").Find(&got, "value = ?", "Level3").Error; err != nil {
993 t.Error(err)
994 }
995
996 if !reflect.DeepEqual(got, want) {
997 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
998 }
999
1000 if err := DB.Preload("Level2.Level1s").First(&got, "value = ?", "not_found").Error; err != gorm.ErrRecordNotFound {
1001 t.Error(err)
1002 }
1003 }
1004
1005 func TestNestedManyToManyPreload3(t *testing.T) {
1006 type (
1007 Level1 struct {
1008 ID uint
1009 Value string
1010 }
1011 Level2 struct {
1012 ID uint
1013 Value string
1014 Level1s []*Level1 `gorm:"many2many:level1_level2;"`
1015 }
1016 Level3 struct {
1017 ID uint
1018 Value string
1019 Level2ID sql.NullInt64
1020 Level2 *Level2
1021 }
1022 )
1023
1024 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, "level1_level2")
1025
1026 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
1027 t.Error(err)
1028 }
1029
1030 level1Zh := &Level1{Value: "zh"}
1031 level1Ru := &Level1{Value: "ru"}
1032 level1En := &Level1{Value: "en"}
1033
1034 level21 := &Level2{
1035 Value: "Level2-1",
1036 Level1s: []*Level1{level1Zh, level1Ru},
1037 }
1038
1039 level22 := &Level2{
1040 Value: "Level2-2",
1041 Level1s: []*Level1{level1Zh, level1En},
1042 }
1043
1044 wants := []*Level3{
1045 {
1046 Value: "Level3-1",
1047 Level2: level21,
1048 },
1049 {
1050 Value: "Level3-2",
1051 Level2: level22,
1052 },
1053 {
1054 Value: "Level3-3",
1055 Level2: level21,
1056 },
1057 }
1058
1059 for _, want := range wants {
1060 if err := DB.Save(want).Error; err != nil {
1061 t.Error(err)
1062 }
1063 }
1064
1065 var gots []*Level3
1066 if err := DB.Preload("Level2.Level1s", func(db *gorm.DB) *gorm.DB {
1067 return db.Order("level1.id ASC")
1068 }).Find(&gots).Error; err != nil {
1069 t.Error(err)
1070 }
1071
1072 if !reflect.DeepEqual(gots, wants) {
1073 t.Errorf("got %s; want %s", toJSONString(gots), toJSONString(wants))
1074 }
1075 }
1076
1077 func TestNestedManyToManyPreload3ForStruct(t *testing.T) {
1078 type (
1079 Level1 struct {
1080 ID uint
1081 Value string
1082 }
1083 Level2 struct {
1084 ID uint
1085 Value string
1086 Level1s []Level1 `gorm:"many2many:level1_level2;"`
1087 }
1088 Level3 struct {
1089 ID uint
1090 Value string
1091 Level2ID sql.NullInt64
1092 Level2 Level2
1093 }
1094 )
1095
1096 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
1097 DB.Migrator().DropTable("level1_level2")
1098
1099 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
1100 t.Error(err)
1101 }
1102
1103 level1Zh := Level1{Value: "zh"}
1104 level1Ru := Level1{Value: "ru"}
1105 level1En := Level1{Value: "en"}
1106
1107 level21 := Level2{
1108 Value: "Level2-1",
1109 Level1s: []Level1{level1Zh, level1Ru},
1110 }
1111
1112 level22 := Level2{
1113 Value: "Level2-2",
1114 Level1s: []Level1{level1Zh, level1En},
1115 }
1116
1117 wants := []*Level3{
1118 {
1119 Value: "Level3-1",
1120 Level2: level21,
1121 },
1122 {
1123 Value: "Level3-2",
1124 Level2: level22,
1125 },
1126 {
1127 Value: "Level3-3",
1128 Level2: level21,
1129 },
1130 }
1131
1132 for _, want := range wants {
1133 if err := DB.Save(want).Error; err != nil {
1134 t.Error(err)
1135 }
1136 }
1137
1138 var gots []*Level3
1139 if err := DB.Preload("Level2.Level1s", func(db *gorm.DB) *gorm.DB {
1140 return db.Order("level1.id ASC")
1141 }).Find(&gots).Error; err != nil {
1142 t.Error(err)
1143 }
1144
1145 if !reflect.DeepEqual(gots, wants) {
1146 t.Errorf("got %s; want %s", toJSONString(gots), toJSONString(wants))
1147 }
1148 }
1149
1150 func TestNestedManyToManyPreload4(t *testing.T) {
1151 type (
1152 Level4 struct {
1153 ID uint
1154 Value string
1155 Level3ID uint
1156 }
1157 Level3 struct {
1158 ID uint
1159 Value string
1160 Level4s []*Level4
1161 }
1162 Level2 struct {
1163 ID uint
1164 Value string
1165 Level3s []*Level3 `gorm:"many2many:level2_level3;"`
1166 }
1167 Level1 struct {
1168 ID uint
1169 Value string
1170 Level2s []*Level2 `gorm:"many2many:level1_level2;"`
1171 }
1172 )
1173
1174 DB.Migrator().DropTable("level1_level2", "level2_level3")
1175 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, &Level4{})
1176
1177 dummy := Level1{
1178 Value: "Level1",
1179 Level2s: []*Level2{{
1180 Value: "Level2",
1181 Level3s: []*Level3{{
1182 Value: "Level3",
1183 Level4s: []*Level4{{
1184 Value: "Level4",
1185 }},
1186 }},
1187 }},
1188 }
1189
1190 if err := DB.AutoMigrate(&Level4{}, &Level3{}, &Level2{}, &Level1{}); err != nil {
1191 t.Error(err)
1192 }
1193
1194 if err := DB.Save(&dummy).Error; err != nil {
1195 t.Error(err)
1196 }
1197
1198 var level1 Level1
1199 if err := DB.Preload("Level2s").Preload("Level2s.Level3s").Preload("Level2s.Level3s.Level4s").First(&level1).Error; err != nil {
1200 t.Error(err)
1201 }
1202 }
1203
1204 func TestManyToManyPreloadForPointer(t *testing.T) {
1205 type (
1206 Level1 struct {
1207 ID uint
1208 Value string
1209 }
1210 Level2 struct {
1211 ID uint
1212 Value string
1213 Level1s []*Level1 `gorm:"many2many:levels;"`
1214 }
1215 )
1216
1217 DB.Migrator().DropTable("levels", &Level2{}, &Level1{})
1218
1219 if err := DB.AutoMigrate(&Level2{}, &Level1{}); err != nil {
1220 t.Error(err)
1221 }
1222
1223 want := Level2{Value: "Bob", Level1s: []*Level1{
1224 {Value: "ru"},
1225 {Value: "en"},
1226 }}
1227 if err := DB.Save(&want).Error; err != nil {
1228 t.Error(err)
1229 }
1230
1231 want2 := Level2{Value: "Tom", Level1s: []*Level1{
1232 {Value: "zh"},
1233 {Value: "de"},
1234 }}
1235 if err := DB.Save(&want2).Error; err != nil {
1236 t.Error(err)
1237 }
1238
1239 var got Level2
1240 if err := DB.Preload("Level1s").Find(&got, "value = ?", "Bob").Error; err != nil {
1241 t.Error(err)
1242 }
1243
1244 if !reflect.DeepEqual(got, want) {
1245 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
1246 }
1247
1248 var got2 Level2
1249 if err := DB.Preload("Level1s").Find(&got2, "value = ?", "Tom").Error; err != nil {
1250 t.Error(err)
1251 }
1252
1253 if !reflect.DeepEqual(got2, want2) {
1254 t.Errorf("got %s; want %s", toJSONString(got2), toJSONString(want2))
1255 }
1256
1257 var got3 []Level2
1258 if err := DB.Preload("Level1s").Find(&got3, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {
1259 t.Error(err)
1260 }
1261
1262 if !reflect.DeepEqual(got3, []Level2{got, got2}) {
1263 t.Errorf("got %s; want %s", toJSONString(got3), toJSONString([]Level2{got, got2}))
1264 }
1265
1266 var got4 []Level2
1267 if err := DB.Preload("Level1s", "value IN (?)", []string{"zh", "ru"}).Find(&got4, "value IN (?)", []string{"Bob", "Tom"}).Error; err != nil {
1268 t.Error(err)
1269 }
1270
1271 var got5 Level2
1272 DB.Preload("Level1s").First(&got5, "value = ?", "bogus")
1273
1274 var ruLevel1 Level1
1275 var zhLevel1 Level1
1276 DB.First(&ruLevel1, "value = ?", "ru")
1277 DB.First(&zhLevel1, "value = ?", "zh")
1278
1279 got.Level1s = []*Level1{&ruLevel1}
1280 got2.Level1s = []*Level1{&zhLevel1}
1281 if !reflect.DeepEqual(got4, []Level2{got, got2}) {
1282 t.Errorf("got %s; want %s", toJSONString(got4), toJSONString([]Level2{got, got2}))
1283 }
1284 }
1285
1286 func TestNilPointerSlice(t *testing.T) {
1287 type (
1288 Level3 struct {
1289 ID uint
1290 Value string
1291 }
1292 Level2 struct {
1293 ID uint
1294 Value string
1295 Level3ID uint
1296 Level3 *Level3
1297 }
1298 Level1 struct {
1299 ID uint
1300 Value string
1301 Level2ID *uint
1302 Level2 *Level2
1303 }
1304 )
1305
1306 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{})
1307 if err := DB.AutoMigrate(&Level3{}, &Level2{}, &Level1{}); err != nil {
1308 t.Error(err)
1309 }
1310
1311 want := Level1{
1312 Value: "Bob",
1313 Level2: &Level2{
1314 Value: "en",
1315 Level3: &Level3{
1316 Value: "native",
1317 },
1318 },
1319 }
1320 if err := DB.Save(&want).Error; err != nil {
1321 t.Error(err)
1322 }
1323
1324 want2 := Level1{
1325 Value: "Tom",
1326 Level2: nil,
1327 }
1328 if err := DB.Save(&want2).Error; err != nil {
1329 t.Fatalf("Got error %v", err)
1330 }
1331
1332 var got []Level1
1333 if err := DB.Preload("Level2").Preload("Level2.Level3").Find(&got).Error; err != nil {
1334 t.Error(err)
1335 }
1336
1337 if len(got) != 2 {
1338 t.Errorf("got %v items, expected 2", len(got))
1339 }
1340
1341 if !reflect.DeepEqual(got[0], want) && !reflect.DeepEqual(got[1], want) {
1342 t.Fatalf("got %s; want array containing %s", toJSONString(got), toJSONString(want))
1343 }
1344
1345 if !reflect.DeepEqual(got[0], want2) && !reflect.DeepEqual(got[1], want2) {
1346 t.Errorf("got %s; want array containing %s", toJSONString(got), toJSONString(want2))
1347 }
1348 }
1349
1350 func TestNilPointerSlice2(t *testing.T) {
1351 type (
1352 Level4 struct {
1353 ID uint
1354 }
1355 Level3 struct {
1356 ID uint
1357 Level4ID sql.NullInt64 `sql:"index"`
1358 Level4 *Level4
1359 }
1360 Level2 struct {
1361 ID uint
1362 Level3s []*Level3 `gorm:"many2many:level2_level3s"`
1363 }
1364 Level1 struct {
1365 ID uint
1366 Level2ID sql.NullInt64 `sql:"index"`
1367 Level2 *Level2
1368 }
1369 )
1370
1371 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, &Level4{})
1372
1373 if err := DB.AutoMigrate(new(Level4), new(Level3), new(Level2), new(Level1)); err != nil {
1374 t.Error(err)
1375 }
1376
1377 want := new(Level1)
1378 if err := DB.Save(want).Error; err != nil {
1379 t.Error(err)
1380 }
1381
1382 got := new(Level1)
1383 err := DB.Preload("Level2.Level3s.Level4").Last(&got).Error
1384 if err != nil {
1385 t.Error(err)
1386 }
1387
1388 if !reflect.DeepEqual(got, want) {
1389 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
1390 }
1391 }
1392
1393 func TestPrefixedPreloadDuplication(t *testing.T) {
1394 type (
1395 Level4 struct {
1396 ID uint
1397 Name string
1398 Level3ID uint
1399 }
1400 Level3 struct {
1401 ID uint
1402 Name string
1403 Level4s []*Level4 `json:",omitempty"`
1404 }
1405 Level2 struct {
1406 ID uint
1407 Name string
1408 Level3ID sql.NullInt64 `sql:"index"`
1409 Level3 *Level3
1410 }
1411 Level1 struct {
1412 ID uint
1413 Name string
1414 Level2ID sql.NullInt64 `sql:"index"`
1415 Level2 *Level2
1416 }
1417 )
1418
1419 DB.Migrator().DropTable(&Level3{}, &Level2{}, &Level1{}, &Level4{})
1420
1421 if err := DB.AutoMigrate(new(Level3), new(Level4), new(Level2), new(Level1)); err != nil {
1422 t.Error(err)
1423 }
1424
1425 lvl := &Level3{}
1426 if err := DB.Save(lvl).Error; err != nil {
1427 t.Error(err)
1428 }
1429
1430 sublvl1 := &Level4{Level3ID: lvl.ID}
1431 if err := DB.Save(sublvl1).Error; err != nil {
1432 t.Error(err)
1433 }
1434 sublvl2 := &Level4{Level3ID: lvl.ID}
1435 if err := DB.Save(sublvl2).Error; err != nil {
1436 t.Error(err)
1437 }
1438
1439 lvl.Level4s = []*Level4{sublvl1, sublvl2}
1440
1441 want1 := Level1{
1442 Level2: &Level2{
1443 Level3: lvl,
1444 },
1445 }
1446 if err := DB.Save(&want1).Error; err != nil {
1447 t.Error(err)
1448 }
1449
1450 want2 := Level1{
1451 Level2: &Level2{
1452 Level3: lvl,
1453 },
1454 }
1455 if err := DB.Save(&want2).Error; err != nil {
1456 t.Error(err)
1457 }
1458
1459 want := []Level1{want1, want2}
1460
1461 var got []Level1
1462 err := DB.Preload("Level2.Level3.Level4s").Find(&got).Error
1463 if err != nil {
1464 t.Error(err)
1465 }
1466
1467 for _, level1 := range append(got, want...) {
1468 sort.Slice(level1.Level2.Level3.Level4s, func(i, j int) bool {
1469 return level1.Level2.Level3.Level4s[i].ID > level1.Level2.Level3.Level4s[j].ID
1470 })
1471 }
1472
1473 if !reflect.DeepEqual(got, want) {
1474 t.Errorf("got %s; want %s", toJSONString(got), toJSONString(want))
1475 }
1476 }
1477
1478 func TestPreloadManyToManyCallbacks(t *testing.T) {
1479 type (
1480 Level2 struct {
1481 ID uint
1482 Name string
1483 }
1484 Level1 struct {
1485 ID uint
1486 Name string
1487 Level2s []Level2 `gorm:"many2many:level1_level2s"`
1488 }
1489 )
1490
1491 DB.Migrator().DropTable("level1_level2s", &Level2{}, &Level1{})
1492
1493 if err := DB.AutoMigrate(new(Level1), new(Level2)); err != nil {
1494 t.Error(err)
1495 }
1496
1497 lvl := Level1{
1498 Name: "l1",
1499 Level2s: []Level2{
1500 {Name: "l2-1"}, {Name: "l2-2"},
1501 },
1502 }
1503 DB.Save(&lvl)
1504
1505 var called int64
1506 DB.Callback().Query().After("gorm:query").Register("TestPreloadManyToManyCallbacks", func(_ *gorm.DB) {
1507 atomic.AddInt64(&called, 1)
1508 })
1509
1510 DB.Preload("Level2s").First(&Level1{}, "id = ?", lvl.ID)
1511
1512 if called != 3 {
1513 t.Errorf("Wanted callback to be called 3 times but got %d", called)
1514 }
1515 }