@@ -335,15 +335,15 @@ def test_in_batches_has_attribute_readers
335
335
end
336
336
337
337
def test_in_batches_should_yield_relation_if_block_given
338
- assert_queries_count ( 6 ) do
338
+ assert_queries_count ( 7 ) do
339
339
Post . in_batches ( of : 2 ) do |relation |
340
340
assert_kind_of ActiveRecord ::Relation , relation
341
341
end
342
342
end
343
343
end
344
344
345
345
def test_in_batches_should_be_enumerable_if_no_block_given
346
- assert_queries_count ( 6 ) do
346
+ assert_queries_count ( 7 ) do
347
347
Post . in_batches ( of : 2 ) . each do |relation |
348
348
assert_kind_of ActiveRecord ::Relation , relation
349
349
end
@@ -378,10 +378,10 @@ def test_in_batches_each_record_should_be_ordered_by_id
378
378
end
379
379
380
380
def test_in_batches_update_all_affect_all_records
381
- assert_queries_count ( 6 + 6 ) do # 6 selects, 6 updates
381
+ assert_queries_count ( 7 + 6 ) do # 7 selects, 6 updates
382
382
Post . in_batches ( of : 2 ) . update_all ( title : "updated-title" )
383
383
end
384
- assert_equal Post . all . pluck ( :title ) , [ "updated-title" ] * Post . count
384
+ assert_equal [ "updated-title" ] * Post . count , Post . all . pluck ( :title )
385
385
end
386
386
387
387
def test_in_batches_update_all_returns_rows_affected
@@ -394,7 +394,7 @@ def test_in_batches_update_all_returns_zero_when_no_batches
394
394
395
395
def test_in_batches_touch_all_affect_all_records
396
396
time = Time . new ( 2000 , 1 , 1 , 0 , 0 , 0 )
397
- assert_queries_count ( 6 + 6 ) do # 6 selects, 6 updates
397
+ assert_queries_count ( 7 + 6 ) do # 7 selects, 6 updates
398
398
Developer . in_batches ( of : 2 ) . touch_all ( time : time )
399
399
end
400
400
assert_equal [ time ] * Developer . count , Developer . all . pluck ( :updated_at )
@@ -486,7 +486,7 @@ def test_in_scoped_batches_preserves_order_within_batches
486
486
end
487
487
488
488
def test_in_batches_if_not_loaded_executes_more_queries
489
- assert_queries_count ( @total + 1 ) do
489
+ assert_queries_count ( @total + 2 ) do
490
490
Post . in_batches ( of : 1 , load : false ) do |relation |
491
491
assert_not_predicate relation , :loaded?
492
492
end
@@ -617,7 +617,7 @@ def test_in_batches_when_loaded_iterates_using_custom_column
617
617
end
618
618
619
619
def test_in_batches_should_return_relations
620
- assert_queries_count ( @total + 1 ) do
620
+ assert_queries_count ( @total + 2 ) do
621
621
Post . in_batches ( of : 1 ) do |relation |
622
622
assert_kind_of ActiveRecord ::Relation , relation
623
623
end
@@ -634,46 +634,70 @@ def test_in_batches_should_start_from_the_start_option
634
634
635
635
def test_in_batches_should_end_at_the_finish_option
636
636
post = Post . order ( "id DESC" ) . where ( "id <= ?" , 5 ) . first
637
- assert_queries_count ( 7 ) do
637
+ assert_queries_count ( 8 ) do
638
638
relation = Post . in_batches ( of : 1 , finish : 5 , load : true ) . reverse_each . first
639
639
assert_equal post , relation . last
640
640
end
641
641
end
642
642
643
643
def test_in_batches_executes_range_queries_when_unconstrained
644
644
quoted_posts_id = Regexp . escape ( quote_table_name ( "posts.id" ) )
645
+
646
+ relations = assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC LIMIT \S + OFFSET \S +\z /i , count : 6 ) do
647
+ assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC LIMIT \S +\z /i , count : 1 ) do
648
+ Post . in_batches ( of : 2 ) . to_a
649
+ end
650
+ end
651
+
645
652
assert_queries_match ( /WHERE #{ quoted_posts_id } > .+ AND #{ quoted_posts_id } <= .+/i ) do
646
- Post . in_batches ( of : 2 ) { |relation | assert_kind_of Post , relation . first }
653
+ relations . each { |relation | assert_kind_of Post , relation . first }
647
654
end
648
655
end
649
656
650
657
def test_in_batches_executes_in_queries_when_unconstrained_and_opted_out_of_ranges
651
658
quoted_posts_id = Regexp . escape ( quote_table_name ( "posts.id" ) )
659
+
660
+ relations = assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC LIMIT \S +\z /i , count : 6 ) do
661
+ Post . in_batches ( of : 2 , use_ranges : false ) . to_a
662
+ end
663
+
652
664
assert_queries_match ( /#{ quoted_posts_id } IN \( .+\) /i ) do
653
- Post . in_batches ( of : 2 , use_ranges : false ) { |relation | assert_kind_of Post , relation . first }
665
+ relations . each { |relation | assert_kind_of Post , relation . first }
654
666
end
655
667
end
656
668
657
669
def test_in_batches_executes_in_queries_when_constrained
658
670
quoted_posts_id = Regexp . escape ( quote_table_name ( "posts.id" ) )
671
+
672
+ relations = assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC LIMIT \S +\z /i , count : 3 ) do
673
+ Post . where ( "id < ?" , 5 ) . in_batches ( of : 2 ) . to_a
674
+ end
675
+
659
676
assert_queries_match ( /#{ quoted_posts_id } IN \( .+\) /i ) do
660
- Post . where ( "id < ?" , 5 ) . in_batches ( of : 2 ) { |relation | assert_kind_of Post , relation . first }
677
+ relations . each { |relation | assert_kind_of Post , relation . first }
661
678
end
662
679
end
663
680
664
681
def test_in_batches_executes_range_queries_when_constrained_and_opted_in_into_ranges
665
682
quoted_posts_id = Regexp . escape ( quote_table_name ( "posts.id" ) )
683
+
684
+ relations = assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC LIMIT \S + OFFSET \S +\z /i , count : 3 ) do
685
+ assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC LIMIT \S +\z /i , count : 1 ) do
686
+ Post . where ( "id < ?" , 5 ) . in_batches ( of : 2 , use_ranges : true ) . to_a
687
+ end
688
+ end
689
+
666
690
assert_queries_match ( /#{ quoted_posts_id } > .+ AND #{ quoted_posts_id } <= .+/i ) do
667
- Post . where ( "id < ?" , 5 ) . in_batches ( of : 2 , use_ranges : true ) { |relation | assert_kind_of Post , relation . first }
691
+ relations . each { |relation | assert_kind_of Post , relation . first }
668
692
end
669
693
end
670
694
671
695
def test_in_batches_shouldnt_execute_query_unless_needed
672
- assert_queries_count ( 2 ) do
696
+ assert_queries_count ( 3 ) do
673
697
Post . in_batches ( of : @total ) { |relation | assert_kind_of ActiveRecord ::Relation , relation }
674
698
end
675
699
676
- assert_queries_count ( 1 ) do
700
+ assert_queries_count ( 2 ) do
677
701
Post . in_batches ( of : @total + 1 ) { |relation | assert_kind_of ActiveRecord ::Relation , relation }
678
702
end
679
703
end
@@ -997,7 +1021,7 @@ def test_find_in_batches_should_return_a_sized_enumerator
997
1021
998
1022
test ".in_batches bypasses the query cache for its own queries" do
999
1023
Post . cache do
1000
- assert_queries_count ( 2 ) do
1024
+ assert_queries_count ( 4 ) do
1001
1025
Post . in_batches { }
1002
1026
Post . in_batches { }
1003
1027
end
0 commit comments