Navigation Menu

Skip to content
This repository has been archived by the owner on Jun 30, 2018. It is now read-only.

[BULK] Raise instead of just writing to stderr #224

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/tire/index.rb
Expand Up @@ -64,7 +64,7 @@ def store(*args)
logged([type, id].join('/'), curl)
end

def bulk_store documents
def bulk_store(documents, options={})
payload = documents.map do |document|
id = get_id_from_document(document)
type = get_type_from_document(document)
Expand Down Expand Up @@ -92,6 +92,7 @@ def bulk_store documents
retry
else
STDERR.puts "[ERROR] Too many exceptions occured, giving up. The HTTP response was: #{error.message}"
raise if options[:raise]
end

ensure
Expand All @@ -109,13 +110,13 @@ def import(klass_or_collection, method=nil, options={})

documents = yield documents if block_given?

bulk_store documents
bulk_store documents, options.slice(:raise)
options[:page] += 1
end

when klass_or_collection.respond_to?(:map)
documents = block_given? ? yield(klass_or_collection) : klass_or_collection
bulk_store documents
bulk_store documents, options.slice(:raise)

else
raise ArgumentError, "Please pass either a collection of objects, " +
Expand Down
41 changes: 31 additions & 10 deletions test/unit/index_test.rb
Expand Up @@ -359,6 +359,14 @@ class MyDocument;end; document = MyDocument.new
assert !@index.bulk_store([ {:id => '1', :title => 'One'}, {:id => '2', :title => 'Two'} ])
end

should "try again and the raise when an exception occurs" do
Configuration.client.expects(:post).returns(mock_response('Server error', 503)).at_least(2)

assert_raise(RuntimeError) do
@index.bulk_store([ {:id => '1', :title => 'One'}, {:id => '2', :title => 'Two'} ], {:raise => true})
end
end

should "display error message when collection item does not have ID" do
Configuration.client.expects(:post).with{ |url, json| url == "#{Configuration.url}/_bulk" }.returns(mock_response('success', 200))
STDERR.expects(:puts).once
Expand Down Expand Up @@ -403,42 +411,55 @@ def self.count; DATA.size; end

should "just store it in bulk" do
collection = [{ :id => 1, :title => 'Article' }]
@index.expects(:bulk_store).with( collection ).returns(true)
@index.expects(:bulk_store).with( collection, options={} ).returns(true)

@index.import collection
end

should "pass the :raise option only to bulk_store" do
collection = [{ :id => 1, :title => 'Article' }]
@index.expects(:bulk_store).with( collection, options={:raise => true} ).returns(true)

@index.import collection, method=nil, options={:raise => true, :lol => 'cats'}
end

end

context "class" do

should "call the passed method and bulk store the results" do
@index.expects(:bulk_store).with([1, 2, 3, 4]).returns(true)
@index.expects(:bulk_store).with([1, 2, 3, 4], options={}).returns(true)

@index.import ImportData, :paginate
end

should "pass the params to the passed method and bulk store the results" do
@index.expects(:bulk_store).with([1, 2]).returns(true)
@index.expects(:bulk_store).with([3, 4]).returns(true)
@index.expects(:bulk_store).with([1, 2], options={}).returns(true)
@index.expects(:bulk_store).with([3, 4], options={}).returns(true)

@index.import ImportData, :paginate, :page => 1, :per_page => 2
end

should "pass the class when method not passed" do
@index.expects(:bulk_store).with(ImportData).returns(true)
@index.expects(:bulk_store).with(ImportData, options={}).returns(true)

@index.import ImportData
end

should "pass the :raise option only to bulk_store" do
@index.expects(:bulk_store).with(ImportData, options={:raise => true}).returns(true)

@index.import ImportData, method=nil, options={:raise => true, :lol => 'cats'}
end

end

context "with passed block" do

context "and plain collection" do

should "allow to manipulate the collection in the block" do
Tire::Index.any_instance.expects(:bulk_store).with([{ :id => 1, :title => 'ARTICLE' }])
Tire::Index.any_instance.expects(:bulk_store).with([{ :id => 1, :title => 'ARTICLE' }], options={})


@index.import [{ :id => 1, :title => 'Article' }] do |articles|
Expand All @@ -451,8 +472,8 @@ def self.count; DATA.size; end
context "and object" do

should "call the passed block on every batch" do
Tire::Index.any_instance.expects(:bulk_store).with([1, 2])
Tire::Index.any_instance.expects(:bulk_store).with([3, 4])
Tire::Index.any_instance.expects(:bulk_store).with([1, 2], options={})
Tire::Index.any_instance.expects(:bulk_store).with([3, 4], options={})

runs = 0
@index.import ImportData, :paginate, :per_page => 2 do |documents|
Expand All @@ -465,8 +486,8 @@ def self.count; DATA.size; end
end

should "allow to manipulate the documents in passed block" do
Tire::Index.any_instance.expects(:bulk_store).with([2, 3])
Tire::Index.any_instance.expects(:bulk_store).with([4, 5])
Tire::Index.any_instance.expects(:bulk_store).with([2, 3], options={})
Tire::Index.any_instance.expects(:bulk_store).with([4, 5], options={})


@index.import ImportData, :paginate, :per_page => 2 do |documents|
Expand Down
8 changes: 4 additions & 4 deletions test/unit/model_import_test.rb
Expand Up @@ -39,8 +39,8 @@ class ImportTest < Test::Unit::TestCase
end

should "call the passed block on every batch, and NOT manipulate the documents array" do
Tire::Index.any_instance.expects(:bulk_store).with([1, 2])
Tire::Index.any_instance.expects(:bulk_store).with([3, 4])
Tire::Index.any_instance.expects(:bulk_store).with([1, 2], options={})
Tire::Index.any_instance.expects(:bulk_store).with([3, 4], options={})

runs = 0
ImportModel.import :per_page => 2 do |documents|
Expand All @@ -53,8 +53,8 @@ class ImportTest < Test::Unit::TestCase
end

should "manipulate the documents in passed block" do
Tire::Index.any_instance.expects(:bulk_store).with([2, 3])
Tire::Index.any_instance.expects(:bulk_store).with([4, 5])
Tire::Index.any_instance.expects(:bulk_store).with([2, 3], options={})
Tire::Index.any_instance.expects(:bulk_store).with([4, 5], options={})

ImportModel.import :per_page => 2 do |documents|
# Add 1 to every "document" and return them
Expand Down