@@ -121,20 +121,37 @@ def ids
121
121
DummyClassForActiveRecord . __find_in_batches ( query : -> { where ( color : "red" ) } ) do ; end
122
122
end
123
123
124
- should "preprocess the batch if option provided" do
125
- class << DummyClassForActiveRecord
126
- # Updates/transforms the batch while fetching it from the database
127
- # (eg. with information from an external system)
128
- #
129
- def update_batch ( batch )
130
- batch . collect { |b | b . to_s + '!' }
124
+ context "when preprocessing batches" do
125
+ setup do
126
+ class << DummyClassForActiveRecord
127
+ def find_in_batches ( options = { } , &block )
128
+ yield [ :a , :b ]
129
+ end
130
+ end
131
+ end
132
+
133
+ should "preprocess the batch if option provided" do
134
+ class << DummyClassForActiveRecord
135
+ def update_batch ( batch )
136
+ batch . collect { |b | b . to_s + '!' }
137
+ end
138
+ end
139
+
140
+ DummyClassForActiveRecord . __find_in_batches ( preprocess : :update_batch ) do |batch |
141
+ assert_same_elements [ "a!" , "b!" ] , batch
131
142
end
132
143
end
133
144
134
- DummyClassForActiveRecord . expects ( :__find_in_batches ) . returns ( [ :a , :b ] )
145
+ should "skip batch if preprocess option provided and returns empty collection" do
146
+ class << DummyClassForActiveRecord
147
+ def update_batch ( batch )
148
+ [ ]
149
+ end
150
+ end
135
151
136
- DummyClassForActiveRecord . __find_in_batches ( preprocess : :update_batch ) do |batch |
137
- assert_same_elements [ "a!" , "b!" ] , batch
152
+ DummyClassForActiveRecord . __find_in_batches ( preprocess : :update_batch ) do |batch |
153
+ flunk ( "block should not have been called on empty batch" )
154
+ end
138
155
end
139
156
end
140
157
0 commit comments