Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[MODEL] Fix import when preprocess returns empty collection #720

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def __find_in_batches(options={}, &block)
scope = scope.instance_exec(&query) if query

scope.find_in_batches(options) do |batch|
yield (preprocess ? self.__send__(preprocess, batch) : batch)
batch = self.__send__(preprocess, batch) if preprocess
yield(batch) if batch.present?
end
end

Expand Down
37 changes: 27 additions & 10 deletions elasticsearch-model/test/unit/adapter_active_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,37 @@ def ids
DummyClassForActiveRecord.__find_in_batches(query: -> { where(color: "red") }) do; end
end

should "preprocess the batch if option provided" do
class << DummyClassForActiveRecord
# Updates/transforms the batch while fetching it from the database
# (eg. with information from an external system)
#
def update_batch(batch)
batch.collect { |b| b.to_s + '!' }
context "when preprocessing batches" do
setup do
class << DummyClassForActiveRecord
def find_in_batches(options = {}, &block)
yield [:a, :b]
end
end
end

should "preprocess the batch if option provided" do
class << DummyClassForActiveRecord
def update_batch(batch)
batch.collect { |b| b.to_s + '!' }
end
end

DummyClassForActiveRecord.__find_in_batches(preprocess: :update_batch) do |batch|
assert_same_elements ["a!", "b!"], batch
end
end

DummyClassForActiveRecord.expects(:__find_in_batches).returns( [:a, :b] )
should "skip batch if preprocess option provided and returns empty collection" do
class << DummyClassForActiveRecord
def update_batch(batch)
[]
end
end

DummyClassForActiveRecord.__find_in_batches(preprocess: :update_batch) do |batch|
assert_same_elements ["a!", "b!"], batch
DummyClassForActiveRecord.__find_in_batches(preprocess: :update_batch) do |batch|
flunk("block should not have been called on empty batch")
end
end
end

Expand Down