forked from toptal/chewy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9eafa2a
commit 269a673
Showing
9 changed files
with
375 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
require 'chewy/rspec/build_query' | ||
require 'chewy/rspec/helpers' | ||
require 'chewy/rspec/update_index' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Rspec helper to compare request and expected query | ||
# To use it - add `require 'chewy/rspec/build_query'` to the `spec_helper.rb` | ||
# Simple usage - just pass expected response as argument | ||
# and then call needed query. | ||
# | ||
# expect { method1.method2...methodN }.to build_query(expected_query) | ||
# | ||
RSpec::Matchers.define :build_query do |expected_query = {}| | ||
match do |request| | ||
request.render == expected_query | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
module Chewy | ||
module Rspec | ||
module Helpers | ||
extend ActiveSupport::Concern | ||
# Rspec helper to mock elasticsearch response | ||
# To use it - add `require 'chewy/rspec'` to the `spec_helper.rb` | ||
# | ||
# mock_elasticsearch_response(CitiesIndex, raw_response) | ||
# expect(CitiesIndex.query({}).hits).to eq(hits) | ||
# | ||
def mock_elasticsearch_response(index, raw_response) | ||
mocked_request = Chewy::Search::Request.new(index) | ||
allow(Chewy::Search::Request).to receive(:new).and_return(mocked_request) | ||
allow(mocked_request).to receive(:perform).and_return(raw_response) | ||
end | ||
|
||
# Rspec helper to mock Elasticsearch response source | ||
# To use it - add `require 'chewy/rspec'` to the `spec_helper.rb` | ||
# | ||
# mock_elasticsearch_response_sources(CitiesIndex, sources) | ||
# expect(CitiesIndex.query({}).hits).to eq(hits) | ||
# | ||
def mock_elasticsearch_response_sources(index, hits) | ||
raw_response = { | ||
'took' => 4, | ||
'timed_out' => false, | ||
'_shards' => { | ||
'total' => 1, | ||
'successful' => 1, | ||
'skipped' => 0, | ||
'failed' => 0 | ||
}, | ||
'hits' => { | ||
'total' => { | ||
'value' => hits.count, | ||
'relation' => 'eq' | ||
}, | ||
'max_score' => 1.0, | ||
'hits' => hits.each_with_index.map do |hit, i| | ||
{ | ||
'_index' => index.index_name, | ||
'_type' => '_doc', | ||
'_id' => (i + 1).to_s, | ||
'_score' => 3.14, | ||
'_source' => hit | ||
} | ||
end | ||
} | ||
} | ||
|
||
mock_elasticsearch_response(index, raw_response) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'spec_helper' | ||
|
||
describe :build_query do | ||
before do | ||
stub_model(:city) | ||
stub_index(:cities) { index_scope City } | ||
CitiesIndex.create | ||
end | ||
|
||
let(:expected_query) do | ||
{ | ||
index: ['cities'], | ||
body: { | ||
query: { | ||
match: {name: 'name'} | ||
} | ||
} | ||
} | ||
end | ||
let(:dummy_query) { {match: {name: 'name'}} } | ||
let(:unexpected_query) { {match: {name: 'name'}} } | ||
|
||
context 'build expected query' do | ||
specify do | ||
expect(CitiesIndex.query(dummy_query)).to build_query(expected_query) | ||
end | ||
end | ||
|
||
context 'not to build unexpected query' do | ||
specify do | ||
expect(CitiesIndex.query(dummy_query)).not_to build_query(unexpected_query) | ||
end | ||
end | ||
end |
Oops, something went wrong.