@@ -490,9 +490,10 @@ func TestPreloadCountUserVideos(t *testing.T) {
490490 defer tx.Rollback (ctx)
491491
492492 // Create users with different numbers of videos
493- expectedCounts := [ ]int{2, 3, 1 }
494- for _, count := range expectedCounts {
493+ expectedCounts := map[int64 ]int{}
494+ for _, count := range []int{2, 3, 1} {
495495 user := New().NewUserWithContext (ctx).CreateOrFail (ctx, t, tx)
496+ expectedCounts[int64(user.ID )] = count
496497 for j := 0; j < count; j++ {
497498 New().NewVideoWithContext (ctx,
498499 VideoMods.WithExistingUser (user),
@@ -508,20 +509,24 @@ func TestPreloadCountUserVideos(t *testing.T) {
508509 t.Fatalf (" Error querying users with PreloadCount: %v " , err)
509510 }
510511
511- if len (users) != 3 {
512- t.Fatalf (" Expected 3 users, got %d " , len (users))
513- }
514-
515- // Verify counts are loaded
512+ found := 0
516513 for i, user := range users {
517514 if user.C.Videos == nil {
518515 t.Fatalf (" Expected Videos count to be set for user %d , got nil" , i)
519516 }
520- expectedCount := int64(expectedCounts[i])
521- if *user.C.Videos != expectedCount {
522- t.Fatalf (" Expected Videos count for user %d to be %d , got %d " , i, expectedCount, *user.C.Videos )
517+ expectedCount, ok := expectedCounts[int64(user.ID )]
518+ if !ok {
519+ continue
520+ }
521+ found++
522+ count := int64(expectedCount)
523+ if *user.C.Videos != count {
524+ t.Fatalf (" Expected Videos count for user %d to be %d , got %d " , i, count, *user.C.Videos )
523525 }
524526 }
527+ if found != len (expectedCounts) {
528+ t.Fatalf (" Expected %d users, got %d " , len (expectedCounts), found)
529+ }
525530}
526531
527532// TestThenLoadCountUserVideos tests using ThenLoadCount to load counts in a separate query
@@ -538,9 +543,10 @@ func TestThenLoadCountUserVideos(t *testing.T) {
538543 defer tx.Rollback (ctx)
539544
540545 // Create users with different numbers of videos
541- expectedCounts := [ ]int{2, 3, 1 }
542- for _, count := range expectedCounts {
546+ expectedCounts := map[int64 ]int{}
547+ for _, count := range []int{2, 3, 1} {
543548 user := New().NewUserWithContext (ctx).CreateOrFail (ctx, t, tx)
549+ expectedCounts[int64(user.ID )] = count
544550 for j := 0; j < count; j++ {
545551 New().NewVideoWithContext (ctx,
546552 VideoMods.WithExistingUser (user),
@@ -556,20 +562,24 @@ func TestThenLoadCountUserVideos(t *testing.T) {
556562 t.Fatalf (" Error querying users with ThenLoadCount: %v " , err)
557563 }
558564
559- if len (users) != 3 {
560- t.Fatalf (" Expected 3 users, got %d " , len (users))
561- }
562-
563- // Verify counts are loaded
565+ found := 0
564566 for i, user := range users {
565567 if user.C.Videos == nil {
566568 t.Fatalf (" Expected Videos count to be set for user %d , got nil" , i)
567569 }
568- expectedCount := int64(expectedCounts[i])
569- if *user.C.Videos != expectedCount {
570- t.Fatalf (" Expected Videos count for user %d to be %d , got %d " , i, expectedCount, *user.C.Videos )
570+ expectedCount, ok := expectedCounts[int64(user.ID )]
571+ if !ok {
572+ continue
573+ }
574+ found++
575+ count := int64(expectedCount)
576+ if *user.C.Videos != count {
577+ t.Fatalf (" Expected Videos count for user %d to be %d , got %d " , i, count, *user.C.Videos )
571578 }
572579 }
580+ if found != len (expectedCounts) {
581+ t.Fatalf (" Expected %d users, got %d " , len (expectedCounts), found)
582+ }
573583}
574584
575585// TestPreloadCountWithFilter tests PreloadCount with filtering mods
@@ -1459,9 +1469,10 @@ func TestThenLoadToMany(t *testing.T) {
14591469
14601470 // Create users with videos
14611471 var users models.UserSlice
1462- expectedCounts := [ ]int{2, 3, 1 }
1463- for _, count := range expectedCounts {
1472+ expectedCounts := map[int64 ]int{}
1473+ for _, count := range []int{2, 3, 1} {
14641474 user := New().NewUserWithContext (ctx).CreateOrFail (ctx, t, tx)
1475+ expectedCounts[int64(user.ID )] = count
14651476 for j := 0; j < count; j++ {
14661477 New().NewVideoWithContext (ctx,
14671478 VideoMods.WithExistingUser (user),
@@ -1478,18 +1489,22 @@ func TestThenLoadToMany(t *testing.T) {
14781489 t.Fatalf (" Error querying users with ThenLoad: %v " , err)
14791490 }
14801491
1481- if len (users) != 3 {
1482- t.Fatalf (" Expected 3 users, got %d " , len (users))
1483- }
1484-
1485- // Verify Videos are preloaded for each user
1492+ found := 0
14861493 for i, user := range users {
14871494 if user.R.Videos == nil {
14881495 t.Fatalf (" Expected Videos to be preloaded for user %d , got nil" , i)
14891496 }
1490- if len (user.R.Videos ) != expectedCounts[i] {
1491- t.Fatalf (" Expected user %d to have %d videos, got %d " , i, expectedCounts[i], len (user.R.Videos ))
1497+ expectedCount, ok := expectedCounts[int64(user.ID )]
1498+ if !ok {
1499+ continue
14921500 }
1501+ found++
1502+ if len (user.R.Videos ) != expectedCount {
1503+ t.Fatalf (" Expected user %d to have %d videos, got %d " , i, expectedCount, len (user.R.Videos ))
1504+ }
1505+ }
1506+ if found != len (expectedCounts) {
1507+ t.Fatalf (" Expected %d users, got %d " , len (expectedCounts), found)
14931508 }
14941509}
14951510{{- end }}
0 commit comments