@@ -2898,3 +2898,65 @@ def raw_transaction_open?(connection)
28982898 end
28992899 end
29002900end
2901+
2902+ class EachTest < ActiveRecord ::TestCase
2903+ # Match SQL Server limit implementation.
2904+ coerce_tests! :test_in_batches_executes_range_queries_when_unconstrained
2905+ def test_in_batches_executes_range_queries_when_unconstrained_coerced
2906+ quoted_posts_id = Regexp . escape ( quote_table_name ( "posts.id" ) )
2907+
2908+ relations = assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC OFFSET @\S ROWS FETCH NEXT @\S ROWS ONLY/i , count : 6 ) do
2909+ assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC OFFSET 0 ROWS FETCH NEXT @\S ROWS ONLY/i , count : 1 ) do
2910+ Post . in_batches ( of : 2 ) . to_a
2911+ end
2912+ end
2913+
2914+ assert_queries_match ( /WHERE #{ quoted_posts_id } > .+ AND #{ quoted_posts_id } <= .+/i ) do
2915+ relations . each { |relation | assert_kind_of Post , relation . first }
2916+ end
2917+ end
2918+
2919+ # Match SQL Server limit implementation.
2920+ coerce_tests! :test_in_batches_executes_in_queries_when_unconstrained_and_opted_out_of_ranges
2921+ def test_in_batches_executes_in_queries_when_unconstrained_and_opted_out_of_ranges_coerced
2922+ quoted_posts_id = Regexp . escape ( quote_table_name ( "posts.id" ) )
2923+
2924+ relations = assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC OFFSET 0 ROWS FETCH NEXT @\S ROWS ONLY/i , count : 6 ) do
2925+ Post . in_batches ( of : 2 , use_ranges : false ) . to_a
2926+ end
2927+
2928+ assert_queries_match ( /#{ quoted_posts_id } IN \( .+\) /i ) do
2929+ relations . each { |relation | assert_kind_of Post , relation . first }
2930+ end
2931+ end
2932+
2933+ # Match SQL Server limit implementation.
2934+ coerce_tests! :test_in_batches_executes_in_queries_when_constrained
2935+ def test_in_batches_executes_in_queries_when_constrained_coerced
2936+ quoted_posts_id = Regexp . escape ( quote_table_name ( "posts.id" ) )
2937+
2938+ relations = assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC OFFSET 0 ROWS FETCH NEXT @\S ROWS ONLY/i , count : 3 ) do
2939+ Post . where ( "id < ?" , 5 ) . in_batches ( of : 2 ) . to_a
2940+ end
2941+
2942+ assert_queries_match ( /#{ quoted_posts_id } IN \( .+\) /i ) do
2943+ relations . each { |relation | assert_kind_of Post , relation . first }
2944+ end
2945+ end
2946+
2947+ # Match SQL Server limit implementation.
2948+ coerce_tests! :test_in_batches_executes_range_queries_when_constrained_and_opted_in_into_ranges
2949+ def test_in_batches_executes_range_queries_when_constrained_and_opted_in_into_ranges_coerced
2950+ quoted_posts_id = Regexp . escape ( quote_table_name ( "posts.id" ) )
2951+
2952+ relations = assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC OFFSET @\S ROWS FETCH NEXT @\S ROWS ONLY/i , count : 3 ) do
2953+ assert_queries_match ( /ORDER BY #{ quoted_posts_id } ASC OFFSET 0 ROWS FETCH NEXT @\S ROWS ONLY/i , count : 1 ) do
2954+ Post . where ( "id < ?" , 5 ) . in_batches ( of : 2 , use_ranges : true ) . to_a
2955+ end
2956+ end
2957+
2958+ assert_queries_match ( /#{ quoted_posts_id } > .+ AND #{ quoted_posts_id } <= .+/i ) do
2959+ relations . each { |relation | assert_kind_of Post , relation . first }
2960+ end
2961+ end
2962+ end
0 commit comments