@@ -455,6 +455,36 @@ def test_in_batches_should_be_loaded
455
455
end
456
456
end
457
457
458
+ def test_in_loaded_batches_preserves_order_within_batches
459
+ expected_posts = Post . order ( id : :desc ) . to_a
460
+ posts = [ ]
461
+
462
+ Post . in_batches ( of : 2 , load : true , order : :desc ) do |relation |
463
+ posts . concat ( relation . where ( "1 = 1" ) )
464
+ end
465
+ assert_equal expected_posts , posts
466
+ end
467
+
468
+ def test_in_range_batches_preserves_order_within_batches
469
+ expected_posts = Post . order ( id : :desc ) . to_a
470
+ posts = [ ]
471
+
472
+ Post . in_batches ( of : 2 , order : :desc , use_ranges : true ) do |relation |
473
+ posts . concat ( relation )
474
+ end
475
+ assert_equal expected_posts , posts
476
+ end
477
+
478
+ def test_in_scoped_batches_preserves_order_within_batches
479
+ expected_posts = Post . order ( id : :desc ) . to_a
480
+ posts = [ ]
481
+
482
+ Post . where ( "id > 0" ) . in_batches ( of : 2 , order : :desc ) do |relation |
483
+ posts . concat ( relation )
484
+ end
485
+ assert_equal expected_posts , posts
486
+ end
487
+
458
488
def test_in_batches_if_not_loaded_executes_more_queries
459
489
assert_queries_count ( @total + 1 ) do
460
490
Post . in_batches ( of : 1 , load : false ) do |relation |
@@ -638,13 +668,6 @@ def test_in_batches_executes_range_queries_when_constrained_and_opted_in_into_ra
638
668
end
639
669
end
640
670
641
- def test_in_batches_no_subqueries_for_whole_tables_batching
642
- quoted_posts_id = Regexp . escape ( quote_table_name ( "posts.id" ) )
643
- assert_queries_match ( /DELETE FROM #{ Regexp . escape ( quote_table_name ( "posts" ) ) } WHERE #{ quoted_posts_id } > .+ AND #{ quoted_posts_id } <=/i ) do
644
- Post . in_batches ( of : 2 ) . delete_all
645
- end
646
- end
647
-
648
671
def test_in_batches_shouldnt_execute_query_unless_needed
649
672
assert_queries_count ( 2 ) do
650
673
Post . in_batches ( of : @total ) { |relation | assert_kind_of ActiveRecord ::Relation , relation }
0 commit comments