Skip to content

Commit f71abc3

Browse files
committed
Add missing specs for untested source files
Cover Strategy::Base, error classes, Index::Crutch, Search::Scoping, Adapter::Base, and the IndicesBoost / AllowPartialSearchResults search parameters.
1 parent 2c99c51 commit f71abc3

File tree

7 files changed

+350
-0
lines changed

7 files changed

+350
-0
lines changed

spec/chewy/errors_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'spec_helper'
2+
3+
describe Chewy::UndefinedUpdateStrategy do
4+
specify do
5+
error = described_class.new('SomeIndex')
6+
expect(error.message).to include('Index update strategy is undefined')
7+
end
8+
end
9+
10+
describe Chewy::ImportFailed do
11+
specify do
12+
errors = {
13+
index: {
14+
'mapper_parsing_exception' => %w[1 2 3]
15+
}
16+
}
17+
error = described_class.new('CitiesIndex', errors)
18+
expect(error.message).to include('Import failed for `CitiesIndex`')
19+
expect(error.message).to include('mapper_parsing_exception')
20+
expect(error.message).to include('3 documents')
21+
end
22+
end
23+
24+
describe Chewy::InvalidJoinFieldType do
25+
specify do
26+
error = described_class.new('invalid_type', 'hierarchy_link', %i[question answer])
27+
expect(error.message).to include('invalid_type')
28+
expect(error.message).to include('hierarchy_link')
29+
expect(error.message).to include(':relations list')
30+
end
31+
end
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
require 'spec_helper'
2+
3+
describe Chewy::Index::Adapter::Base do
4+
subject { described_class.new }
5+
6+
describe '.accepts?' do
7+
specify { expect(described_class.accepts?('anything')).to be true }
8+
end
9+
10+
describe '#name' do
11+
specify { expect { subject.name }.to raise_error(NotImplementedError) }
12+
end
13+
14+
describe '#type_name' do
15+
before { allow(subject).to receive(:name).and_return('SomeType') }
16+
17+
specify { expect(subject.type_name).to eq('some_type') }
18+
end
19+
20+
describe '#identify' do
21+
specify { expect { subject.identify([]) }.to raise_error(NotImplementedError) }
22+
end
23+
24+
describe '#import' do
25+
specify { expect { subject.import([]) }.to raise_error(NotImplementedError) }
26+
end
27+
28+
describe '#import_fields' do
29+
specify { expect { subject.import_fields([], 1000) }.to raise_error(NotImplementedError) }
30+
end
31+
32+
describe '#import_references' do
33+
specify { expect { subject.import_references(1000) }.to raise_error(NotImplementedError) }
34+
end
35+
36+
describe '#load' do
37+
specify { expect { subject.load([]) }.to raise_error(NotImplementedError) }
38+
end
39+
40+
describe '#grouped_objects' do
41+
let(:adapter) { described_class.new }
42+
43+
context 'without delete_if' do
44+
before { allow(adapter).to receive(:options).and_return({}) }
45+
46+
it 'groups all objects under :index' do
47+
objects = [double, double]
48+
result = adapter.send(:grouped_objects, objects)
49+
expect(result).to eq(index: objects)
50+
end
51+
end
52+
53+
context 'with delete_if symbol' do
54+
let(:active_obj) { double(deleted: false) }
55+
let(:deleted_obj) { double(deleted: true) }
56+
57+
before { allow(adapter).to receive(:options).and_return(delete_if: :deleted) }
58+
59+
it 'separates objects into index and delete groups' do
60+
result = adapter.send(:grouped_objects, [active_obj, deleted_obj])
61+
expect(result[:index]).to eq([active_obj])
62+
expect(result[:delete]).to eq([deleted_obj])
63+
end
64+
end
65+
66+
context 'with delete_if proc (arity 1)' do
67+
let(:obj1) { double(id: 1) }
68+
let(:obj2) { double(id: 2) }
69+
70+
before { allow(adapter).to receive(:options).and_return(delete_if: ->(o) { o.id == 2 }) }
71+
72+
it 'uses the proc to determine deletion' do
73+
result = adapter.send(:grouped_objects, [obj1, obj2])
74+
expect(result[:index]).to eq([obj1])
75+
expect(result[:delete]).to eq([obj2])
76+
end
77+
end
78+
79+
context 'with delete_if proc (arity 0, instance_exec)' do
80+
let(:obj1) { double(id: 1) }
81+
let(:obj2) { double(id: 2) }
82+
83+
before { allow(adapter).to receive(:options).and_return(delete_if: -> { id == 2 }) }
84+
85+
it 'uses instance_exec to evaluate the proc' do
86+
result = adapter.send(:grouped_objects, [obj1, obj2])
87+
expect(result[:index]).to eq([obj1])
88+
expect(result[:delete]).to eq([obj2])
89+
end
90+
end
91+
end
92+
end

spec/chewy/index/crutch_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'spec_helper'
2+
3+
describe Chewy::Index::Crutch do
4+
before do
5+
stub_index(:cities) do
6+
crutch :names do |collection|
7+
collection.to_h { |o| [o.id, o.name.upcase] }
8+
end
9+
10+
crutch :ratings do |collection|
11+
collection.to_h { |o| [o.id, o.rating * 10] }
12+
end
13+
end
14+
end
15+
16+
describe Chewy::Index::Crutch::Crutches do
17+
let(:collection) { [double(id: 1, name: 'London', rating: 5), double(id: 2, name: 'Paris', rating: 3)] }
18+
subject { described_class.new(CitiesIndex, collection) }
19+
20+
describe '#[]' do
21+
specify { expect(subject[:names]).to eq(1 => 'LONDON', 2 => 'PARIS') }
22+
specify { expect(subject[:ratings]).to eq(1 => 50, 2 => 30) }
23+
end
24+
25+
describe '#method_missing' do
26+
specify { expect(subject.names).to eq(1 => 'LONDON', 2 => 'PARIS') }
27+
specify { expect(subject.ratings).to eq(1 => 50, 2 => 30) }
28+
29+
specify do
30+
expect { subject.nonexistent }.to raise_error(NoMethodError)
31+
end
32+
end
33+
34+
describe '#respond_to_missing?' do
35+
specify { expect(subject).to respond_to(:names) }
36+
specify { expect(subject).to respond_to(:ratings) }
37+
specify { expect(subject).not_to respond_to(:nonexistent) }
38+
end
39+
40+
it 'caches crutch results' do
41+
expect(subject.names).to equal(subject.names)
42+
end
43+
end
44+
45+
describe '.crutch' do
46+
specify { expect(CitiesIndex._crutches).to have_key(:names) }
47+
specify { expect(CitiesIndex._crutches).to have_key(:ratings) }
48+
specify { expect(CitiesIndex._crutches.size).to eq(2) }
49+
end
50+
end
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'spec_helper'
2+
3+
describe Chewy::Search::Parameters::AllowPartialSearchResults do
4+
subject { described_class.new(true) }
5+
6+
describe '#initialize' do
7+
specify { expect(described_class.new.value).to be_nil }
8+
specify { expect(described_class.new(nil).value).to be_nil }
9+
specify { expect(described_class.new(true).value).to be true }
10+
specify { expect(described_class.new(false).value).to be false }
11+
end
12+
13+
describe '#replace!' do
14+
specify { expect { subject.replace!(false) }.to change { subject.value }.from(true).to(false) }
15+
specify { expect { subject.replace!(nil) }.to change { subject.value }.from(true).to(nil) }
16+
end
17+
18+
describe '#update!' do
19+
specify { expect { subject.update!(false) }.to change { subject.value }.from(true).to(false) }
20+
specify { expect { subject.update!(nil) }.not_to change { subject.value }.from(true) }
21+
end
22+
23+
describe '#merge!' do
24+
specify { expect { subject.merge!(described_class.new(false)) }.to change { subject.value }.from(true).to(false) }
25+
specify { expect { subject.merge!(described_class.new) }.not_to change { subject.value }.from(true) }
26+
end
27+
28+
describe '#render' do
29+
specify { expect(described_class.new.render).to be_nil }
30+
specify { expect(described_class.new(true).render).to eq(allow_partial_search_results: true) }
31+
specify { expect(described_class.new(false).render).to eq(allow_partial_search_results: false) }
32+
end
33+
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'spec_helper'
2+
3+
describe Chewy::Search::Parameters::IndicesBoost do
4+
subject { described_class.new(cities: 1.2) }
5+
6+
describe '#initialize' do
7+
specify { expect(described_class.new.value).to eq({}) }
8+
specify { expect(described_class.new(nil).value).to eq({}) }
9+
specify { expect(described_class.new(cities: 1.2).value).to eq('cities' => 1.2) }
10+
specify { expect(described_class.new(cities: '1.2').value).to eq('cities' => 1.2) }
11+
specify { expect(described_class.new(cities: 2).value).to eq('cities' => 2.0) }
12+
end
13+
14+
describe '#update!' do
15+
specify do
16+
expect { subject.update!(countries: 0.5) }
17+
.to change { subject.value }
18+
.from('cities' => 1.2)
19+
.to('cities' => 1.2, 'countries' => 0.5)
20+
end
21+
22+
specify do
23+
expect { subject.update!(cities: 2.0) }
24+
.to change { subject.value }
25+
.from('cities' => 1.2)
26+
.to('cities' => 2.0)
27+
end
28+
end
29+
30+
describe '#merge!' do
31+
specify do
32+
expect { subject.merge!(described_class.new(countries: 0.5)) }
33+
.to change { subject.value }
34+
.from('cities' => 1.2)
35+
.to('cities' => 1.2, 'countries' => 0.5)
36+
end
37+
38+
specify do
39+
expect { subject.merge!(described_class.new) }
40+
.not_to change { subject.value }
41+
end
42+
end
43+
44+
describe '#render' do
45+
specify { expect(described_class.new.render).to be_nil }
46+
specify { expect(described_class.new(cities: 1.2).render).to eq(indices_boost: [{'cities' => 1.2}]) }
47+
specify do
48+
param = described_class.new(cities: 1.2)
49+
param.update!(countries: 0.5)
50+
expect(param.render).to eq(indices_boost: [{'cities' => 1.2}, {'countries' => 0.5}])
51+
end
52+
end
53+
54+
describe '#==' do
55+
specify { expect(described_class.new(cities: 1.2)).to eq(described_class.new(cities: 1.2)) }
56+
specify { expect(described_class.new(cities: 1.2)).not_to eq(described_class.new(cities: 2.0)) }
57+
58+
it 'considers key order' do
59+
a = described_class.new(cities: 1.2)
60+
a.update!(countries: 0.5)
61+
62+
b = described_class.new(countries: 0.5)
63+
b.update!(cities: 1.2)
64+
65+
expect(a).not_to eq(b)
66+
end
67+
end
68+
end

spec/chewy/search/scoping_spec.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'spec_helper'
2+
3+
describe Chewy::Search::Scoping do
4+
before do
5+
stub_index(:cities) do
6+
field :name
7+
end
8+
end
9+
10+
describe '#scoping' do
11+
let(:request) { CitiesIndex.query(match: {name: 'London'}) }
12+
13+
it 'pushes and pops from the scopes stack' do
14+
expect(Chewy::Search::Request.scopes).to be_empty
15+
16+
request.scoping do
17+
expect(Chewy::Search::Request.scopes.size).to eq(1)
18+
expect(Chewy::Search::Request.scopes.last).to eq(request)
19+
end
20+
21+
expect(Chewy::Search::Request.scopes).to be_empty
22+
end
23+
24+
it 'pops scope even when block raises' do
25+
expect {
26+
request.scoping { raise 'boom' }
27+
}.to raise_error('boom')
28+
29+
expect(Chewy::Search::Request.scopes).to be_empty
30+
end
31+
32+
it 'supports nesting' do
33+
inner = CitiesIndex.filter(term: {name: 'Bangkok'})
34+
35+
request.scoping do
36+
expect(Chewy::Search::Request.scopes.size).to eq(1)
37+
38+
inner.scoping do
39+
expect(Chewy::Search::Request.scopes.size).to eq(2)
40+
expect(Chewy::Search::Request.scopes.last).to eq(inner)
41+
end
42+
43+
expect(Chewy::Search::Request.scopes.size).to eq(1)
44+
end
45+
end
46+
end
47+
end

spec/chewy/strategy/base_spec.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'spec_helper'
2+
3+
describe Chewy::Strategy::Base do
4+
subject { described_class.new }
5+
6+
describe '#name' do
7+
specify { expect(subject.name).to eq(:base) }
8+
end
9+
10+
describe '#update' do
11+
specify do
12+
expect { subject.update('SomeIndex', [1, 2]) }
13+
.to raise_error(Chewy::UndefinedUpdateStrategy)
14+
end
15+
end
16+
17+
describe '#leave' do
18+
specify { expect(subject.leave).to be_nil }
19+
end
20+
21+
describe '#update_chewy_indices' do
22+
let(:object) { double(run_chewy_callbacks: true) }
23+
24+
specify do
25+
expect(object).to receive(:run_chewy_callbacks)
26+
subject.update_chewy_indices(object)
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)