@@ -558,7 +558,7 @@ func TestMany2ManyConstraintViolations(t *testing.T) {
558558 t .Logf ("Appending duplicate language resulted in: %v" , err )
559559 }
560560
561- // Verify count is still correct
561+ // Verify count is still correct after duplicate append attempt
562562 count := DB .Model (& user ).Association ("Languages" ).Count ()
563563 if count > 1 {
564564 t .Errorf ("Expected count 1 after duplicate append, got %d" , count )
@@ -584,6 +584,10 @@ func TestMany2ManyConstraintViolations(t *testing.T) {
584584 t .Logf ("Finding associations after FK deletion resulted in: %v" , err )
585585 }
586586
587+ // Get count before mass operation for verification
588+ countBeforeMass := DB .Model (& user ).Association ("Languages" ).Count ()
589+ t .Logf ("Language count before mass operation: %d" , countBeforeMass )
590+
587591 var manyLanguages []Language
588592 for i := 0 ; i < 10 ; i ++ {
589593 manyLanguages = append (manyLanguages , Language {
@@ -603,9 +607,25 @@ func TestMany2ManyConstraintViolations(t *testing.T) {
603607 t .Logf ("Mass append operation resulted in: %v" , err )
604608 }
605609
606- // Verify the operation completed
610+ // Verify the operation completed with proper count validation
607611 finalCount := DB .Model (& user ).Association ("Languages" ).Count ()
608- t .Logf ("Final language count after mass operation: %d" , finalCount )
612+ // Should have at least the previous count + 10 new languages
613+ expectedMinCount := countBeforeMass + 10
614+
615+ t .Logf ("Final language count after mass operation: %d (expected at least %d)" , finalCount , expectedMinCount )
616+
617+ if finalCount < expectedMinCount {
618+ t .Errorf ("Expected at least %d languages after mass append, got %d" , expectedMinCount , finalCount )
619+ }
620+
621+ var actualLanguages []Language
622+ if err := DB .Model (& user ).Association ("Languages" ).Find (& actualLanguages ); err == nil {
623+ actualCount := len (actualLanguages )
624+ if actualCount != int (finalCount ) {
625+ t .Errorf ("Count mismatch: Count() returned %d but Find() returned %d languages" ,
626+ finalCount , actualCount )
627+ }
628+ }
609629
610630 if err := DB .Model (& user ).Association ("Languages" ).Clear (); err != nil {
611631 t .Errorf ("Error clearing associations after mass operation: %v" , err )
0 commit comments