spec/units/search_spec.rb in es-elasticity-0.3.9 vs spec/units/search_spec.rb in es-elasticity-0.3.10

- old
+ new

@@ -57,19 +57,21 @@ { "_id" => 1, "_source" => { "name" => "foo" } }, { "_id" => 2, "_source" => { "name" => "bar" } }, ]}} end + let :mapper do + -> (hit) { + klass.new(_id: hit["_id"], name: hit["_source"]["name"], age: hit["_source"]["age"]) + } + end + let :klass do Class.new do include ActiveModel::Model attr_accessor :_id, :name, :age - def self.from_hit(hit) - new(_id: hit["_id"], name: hit["_source"]["name"], age: hit["_source"]["age"]) - end - def ==(other) self._id == other._id && self.name == other.name end end end @@ -80,11 +82,11 @@ end it "searches the index and return document models" do expect(client).to receive(:search).with(index: index_name, type: document_type, body: body).and_return(full_response) - docs = subject.documents(klass) + docs = subject.documents(mapper) expected = [klass.new(_id: 1, name: "foo"), klass.new(_id: 2, name: "bar")] expect(docs.total).to eq 2 expect(docs.size).to eq expected.size @@ -99,20 +101,20 @@ end it "searches and the index returns aggregations" do expect(client).to receive(:search).with(index: index_name, type: document_type, body: body).and_return(full_response_with_aggregations) - docs = subject.documents(klass) + docs = subject.documents(mapper) expect(docs.aggregations).to eq aggregations end it "searches using scan&scroll" do expect(client).to receive(:search).with(index: index_name, type: document_type, body: body, search_type: "scan", size: 100, scroll: "1m").and_return(scan_response) expect(client).to receive(:scroll).with(scroll_id: "abc123", scroll: "1m").and_return(scroll_response) expect(client).to receive(:scroll).with(scroll_id: "abc456", scroll: "1m").and_return(empty_response) - docs = subject.scan_documents(klass) + docs = subject.scan_documents(mapper) expected = [klass.new(_id: 1, name: "foo"), klass.new(_id: 2, name: "bar")] expect(docs.total).to eq 2 expect(docs).to_not be_empty @@ -142,11 +144,11 @@ describe Elasticity::Search::LazySearch do it "provides defaul properties for pagination" do subject = Elasticity::Search::Facade.new(client, Elasticity::Search::Definition.new(index_name, document_type, body)) expect(client).to receive(:search).with(index: index_name, type: document_type, body: body).and_return(full_response) - docs = subject.documents(klass) + docs = subject.documents(mapper) expect(docs.per_page).to eq(10) expect(docs.total_pages).to eq(1) expect(docs.current_page).to eq(1) end @@ -164,11 +166,11 @@ with( index: index_name, type: document_type, body: { size: 14, from: 25, filter: {} } ).and_return({ "hits" => { "total" => 112 } }) - docs = subject.documents(klass) + docs = subject.documents(mapper) expect(docs.per_page).to eq(14) expect(docs.total_pages).to eq(8) expect(docs.current_page).to eq(2) end @@ -178,10 +180,10 @@ let :search do Elasticity::Search::Facade.new(client, Elasticity::Search::Definition.new(index_name, "document", body)) end subject do - described_class.new(search, klass) + described_class.new(search, mapper) end it "automatically maps the documents into the provided Document class" do expect(client).to receive(:search).with(index: index_name, type: document_type, body: body).and_return(full_response) expect(Array(subject)).to eq [klass.new(_id: 1, name: "foo"), klass.new(_id: 2, name: "bar")]