30
30
31
31
class AssociationsTest < ActiveRecord ::TestCase
32
32
fixtures :accounts , :companies , :developers , :projects , :developers_projects ,
33
- :computers , :people , :readers , :authors , :author_addresses , :author_favorites
33
+ :computers , :people , :readers , :authors , :author_addresses , :author_favorites ,
34
+ :comments , :posts
34
35
35
36
def test_eager_loading_should_not_change_count_of_children
36
37
liquid = Liquid . create ( name : "salty" )
@@ -407,12 +408,30 @@ def test_preload_groups_queries_with_same_scope
407
408
assert_queries ( 1 ) do
408
409
preloader = ActiveRecord ::Associations ::Preloader . new ( records : [ book , post ] , associations : :author )
409
410
preloader . call
411
+ end
410
412
413
+ assert_no_queries do
411
414
book . author
412
415
post . author
413
416
end
414
417
end
415
418
419
+ def test_preload_through
420
+ comments = [
421
+ comments ( :eager_sti_on_associations_s_comment1 ) ,
422
+ comments ( :eager_sti_on_associations_s_comment2 ) ,
423
+ ]
424
+
425
+ assert_queries ( 2 ) do
426
+ preloader = ActiveRecord ::Associations ::Preloader . new ( records : comments , associations : [ :author , :post ] )
427
+ preloader . call
428
+ end
429
+
430
+ assert_no_queries do
431
+ comments . each ( &:author )
432
+ end
433
+ end
434
+
416
435
def test_preload_with_grouping_sets_inverse_association
417
436
mary = authors ( :mary )
418
437
bob = authors ( :bob )
@@ -423,7 +442,9 @@ def test_preload_with_grouping_sets_inverse_association
423
442
assert_queries ( 1 ) do
424
443
preloader = ActiveRecord ::Associations ::Preloader . new ( records : favorites , associations : [ :author , :favorite_author ] )
425
444
preloader . call
445
+ end
426
446
447
+ assert_no_queries do
427
448
favorites . first . author
428
449
favorites . first . favorite_author
429
450
end
@@ -440,7 +461,9 @@ def test_preload_does_not_group_same_class_different_scope
440
461
assert_queries ( 2 ) do
441
462
preloader = ActiveRecord ::Associations ::Preloader . new ( records : [ post , postesque ] , associations : :author_with_the_letter_a )
442
463
preloader . call
464
+ end
443
465
466
+ assert_no_queries do
444
467
post . author_with_the_letter_a
445
468
postesque . author_with_the_letter_a
446
469
end
@@ -452,7 +475,9 @@ def test_preload_does_not_group_same_class_different_scope
452
475
assert_queries ( 3 ) do
453
476
preloader = ActiveRecord ::Associations ::Preloader . new ( records : [ post , postesque ] , associations : :author_with_address )
454
477
preloader . call
478
+ end
455
479
480
+ assert_no_queries do
456
481
post . author_with_address
457
482
postesque . author_with_address
458
483
end
@@ -466,7 +491,9 @@ def test_preload_does_not_group_same_scope_different_key_name
466
491
assert_queries ( 2 ) do
467
492
preloader = ActiveRecord ::Associations ::Preloader . new ( records : [ post , postesque ] , associations : :author )
468
493
preloader . call
494
+ end
469
495
496
+ assert_no_queries do
470
497
post . author
471
498
postesque . author
472
499
end
0 commit comments