@@ -367,8 +367,8 @@ func TestAssociationEmptyQueryClause(t *testing.T) {
367
367
Organizations []Organization `gorm:"many2many:region_orgs;"`
368
368
}
369
369
type RegionOrg struct {
370
- RegionId uint
371
- OrganizationId uint
370
+ RegionID uint
371
+ OrganizationID uint
372
372
Empty myType
373
373
}
374
374
if err := DB .SetupJoinTable (& Region {}, "Organizations" , & RegionOrg {}); err != nil {
@@ -394,3 +394,154 @@ func TestAssociationEmptyQueryClause(t *testing.T) {
394
394
tests .AssertEqual (t , len (orgs ), 0 )
395
395
}
396
396
}
397
+
398
+ func TestBasicBelongsToAssociation (t * testing.T ) {
399
+ // Test basic BelongsTo association operations
400
+ user := GetUser ("TestBelongsTo" , Config {Company : true })
401
+
402
+ if err := DB .Create (& user ).Error ; err != nil {
403
+ t .Fatalf ("Failed to create user with company: %v" , err )
404
+ }
405
+
406
+ // finding the association
407
+ var foundUser User
408
+ if err := DB .Preload ("Company" ).First (& foundUser , "\" id\" = ?" , user .ID ).Error ; err != nil {
409
+ t .Fatalf ("Failed to find user with company: %v" , err )
410
+ }
411
+
412
+ if foundUser .Company .ID != user .Company .ID {
413
+ t .Fatalf ("Company ID mismatch: expected %d, got %d" , user .Company .ID , foundUser .Company .ID )
414
+ }
415
+
416
+ // association count
417
+ AssertAssociationCount (t , user , "Company" , 1 , "after creation" )
418
+
419
+ // replacing association
420
+ newCompany := Company {Name : "New Test Company" }
421
+ if err := DB .Create (& newCompany ).Error ; err != nil {
422
+ t .Fatalf ("Failed to create new company: %v" , err )
423
+ }
424
+
425
+ if err := DB .Model (& user ).Association ("Company" ).Replace (& newCompany ); err != nil {
426
+ t .Fatalf ("Failed to replace company association: %v" , err )
427
+ }
428
+
429
+ var updatedUser User
430
+ if err := DB .Preload ("Company" ).First (& updatedUser , "\" id\" = ?" , user .ID ).Error ; err != nil {
431
+ t .Fatalf ("Failed to find updated user: %v" , err )
432
+ }
433
+
434
+ if updatedUser .Company .ID != newCompany .ID {
435
+ t .Fatalf ("Company was not replaced: expected %d, got %d" , newCompany .ID , updatedUser .Company .ID )
436
+ }
437
+ }
438
+
439
+ func TestBasicHasManyAssociation (t * testing.T ) {
440
+ // Test basic HasMany association operations
441
+ user := GetUser ("TestHasMany" , Config {Pets : 3 })
442
+
443
+ if err := DB .Create (& user ).Error ; err != nil {
444
+ t .Fatalf ("Failed to create user with pets: %v" , err )
445
+ }
446
+
447
+ // association count
448
+ AssertAssociationCount (t , user , "Pets" , 3 , "after creation" )
449
+
450
+ // finding pets
451
+ var pets []Pet
452
+ if err := DB .Model (& user ).Association ("Pets" ).Find (& pets ); err != nil {
453
+ t .Fatalf ("Failed to find pets: %v" , err )
454
+ }
455
+
456
+ if len (pets ) != 3 {
457
+ t .Fatalf ("Expected 3 pets, got %d" , len (pets ))
458
+ }
459
+
460
+ // appending new pet
461
+ newPet := Pet {Name : "Additional Pet" , UserID : & user .ID }
462
+ if err := DB .Model (& user ).Association ("Pets" ).Append (& newPet ); err != nil {
463
+ t .Fatalf ("Failed to append pet: %v" , err )
464
+ }
465
+
466
+ AssertAssociationCount (t , user , "Pets" , 4 , "after append" )
467
+
468
+ // deleting one pet from association
469
+ if err := DB .Model (& user ).Association ("Pets" ).Delete (& pets [0 ]); err != nil {
470
+ t .Fatalf ("Failed to delete pet from association: %v" , err )
471
+ }
472
+
473
+ AssertAssociationCount (t , user , "Pets" , 3 , "after delete" )
474
+ }
475
+
476
+ func TestBasicManyToManyAssociation (t * testing.T ) {
477
+ // Test basic ManyToMany association operations
478
+ user := GetUser ("TestManyToMany" , Config {Languages : 2 })
479
+
480
+ if err := DB .Create (& user ).Error ; err != nil {
481
+ t .Fatalf ("Failed to create user with languages: %v" , err )
482
+ }
483
+
484
+ // association count
485
+ AssertAssociationCount (t , user , "Languages" , 2 , "after creation" )
486
+
487
+ // finding languages
488
+ var languages []Language
489
+ if err := DB .Model (& user ).Association ("Languages" ).Find (& languages ); err != nil {
490
+ t .Fatalf ("Failed to find languages: %v" , err )
491
+ }
492
+
493
+ if len (languages ) != 2 {
494
+ t .Fatalf ("Expected 2 languages, got %d" , len (languages ))
495
+ }
496
+
497
+ // appending new language
498
+ newLanguage := Language {Code : "FR" , Name : "French" }
499
+ if err := DB .Create (& newLanguage ).Error ; err != nil {
500
+ t .Fatalf ("Failed to create new language: %v" , err )
501
+ }
502
+
503
+ if err := DB .Model (& user ).Association ("Languages" ).Append (& newLanguage ); err != nil {
504
+ t .Fatalf ("Failed to append language: %v" , err )
505
+ }
506
+
507
+ AssertAssociationCount (t , user , "Languages" , 3 , "after append" )
508
+
509
+ // replacing all languages
510
+ replaceLanguages := []Language {
511
+ {Code : "DE" , Name : "German" },
512
+ {Code : "IT" , Name : "Italian" },
513
+ }
514
+
515
+ for i := range replaceLanguages {
516
+ if err := DB .Create (& replaceLanguages [i ]).Error ; err != nil {
517
+ t .Fatalf ("Failed to create replacement language: %v" , err )
518
+ }
519
+ }
520
+
521
+ if err := DB .Model (& user ).Association ("Languages" ).Replace (replaceLanguages ); err != nil {
522
+ t .Fatalf ("Failed to replace languages: %v" , err )
523
+ }
524
+
525
+ AssertAssociationCount (t , user , "Languages" , 2 , "after replace" )
526
+
527
+ var finalLanguages []Language
528
+ if err := DB .Model (& user ).Association ("Languages" ).Find (& finalLanguages ); err != nil {
529
+ t .Fatalf ("Failed to find final languages: %v" , err )
530
+ }
531
+
532
+ languageCodes := make (map [string ]bool )
533
+ for _ , lang := range finalLanguages {
534
+ languageCodes [lang .Code ] = true
535
+ }
536
+
537
+ if ! languageCodes ["DE" ] || ! languageCodes ["IT" ] {
538
+ t .Fatal ("Languages were not replaced correctly" )
539
+ }
540
+
541
+ // clearing all associations
542
+ if err := DB .Model (& user ).Association ("Languages" ).Clear (); err != nil {
543
+ t .Fatalf ("Failed to clear languages: %v" , err )
544
+ }
545
+
546
+ AssertAssociationCount (t , user , "Languages" , 0 , "after clear" )
547
+ }
0 commit comments