Skip to content

Commit 154cd79

Browse files
timbleckestolfo
authored andcommitted
[MODEL] Fix import when preprocess returns empty collection (#720)
Fixes #719
1 parent aa65914 commit 154cd79

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ def __find_in_batches(options={}, &block)
103103
scope = scope.instance_exec(&query) if query
104104

105105
scope.find_in_batches(options) do |batch|
106-
yield (preprocess ? self.__send__(preprocess, batch) : batch)
106+
batch = self.__send__(preprocess, batch) if preprocess
107+
yield(batch) if batch.present?
107108
end
108109
end
109110

elasticsearch-model/test/unit/adapter_active_record_test.rb

+27-10
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,37 @@ def ids
121121
DummyClassForActiveRecord.__find_in_batches(query: -> { where(color: "red") }) do; end
122122
end
123123

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
131142
end
132143
end
133144

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
135151

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
138155
end
139156
end
140157

0 commit comments

Comments
 (0)