Skip to content
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
31 changes: 31 additions & 0 deletions spec/chewy/errors_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'

describe Chewy::UndefinedUpdateStrategy do
specify do
error = described_class.new('SomeIndex')
expect(error.message).to include('Index update strategy is undefined')
end
end

describe Chewy::ImportFailed do
specify do
errors = {
index: {
'mapper_parsing_exception' => %w[1 2 3]
}
}
error = described_class.new('CitiesIndex', errors)
expect(error.message).to include('Import failed for `CitiesIndex`')
expect(error.message).to include('mapper_parsing_exception')
expect(error.message).to include('3 documents')
end
end

describe Chewy::InvalidJoinFieldType do
specify do
error = described_class.new('invalid_type', 'hierarchy_link', %i[question answer])
expect(error.message).to include('invalid_type')
expect(error.message).to include('hierarchy_link')
expect(error.message).to include(':relations list')
end
end
92 changes: 92 additions & 0 deletions spec/chewy/index/adapter/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require 'spec_helper'

describe Chewy::Index::Adapter::Base do
subject { described_class.new }

describe '.accepts?' do
specify { expect(described_class.accepts?('anything')).to be true }
end

describe '#name' do
specify { expect { subject.name }.to raise_error(NotImplementedError) }
end

describe '#type_name' do
before { allow(subject).to receive(:name).and_return('SomeType') }

specify { expect(subject.type_name).to eq('some_type') }
end

describe '#identify' do
specify { expect { subject.identify([]) }.to raise_error(NotImplementedError) }
end

describe '#import' do
specify { expect { subject.import([]) }.to raise_error(NotImplementedError) }
end

describe '#import_fields' do
specify { expect { subject.import_fields([], 1000) }.to raise_error(NotImplementedError) }
end

describe '#import_references' do
specify { expect { subject.import_references(1000) }.to raise_error(NotImplementedError) }
end

describe '#load' do
specify { expect { subject.load([]) }.to raise_error(NotImplementedError) }
end

describe '#grouped_objects' do
let(:adapter) { described_class.new }

context 'without delete_if' do
before { allow(adapter).to receive(:options).and_return({}) }

it 'groups all objects under :index' do
objects = [double, double]
result = adapter.send(:grouped_objects, objects)
expect(result).to eq(index: objects)
end
end

context 'with delete_if symbol' do
let(:active_obj) { double(deleted: false) }
let(:deleted_obj) { double(deleted: true) }

before { allow(adapter).to receive(:options).and_return(delete_if: :deleted) }

it 'separates objects into index and delete groups' do
result = adapter.send(:grouped_objects, [active_obj, deleted_obj])
expect(result[:index]).to eq([active_obj])
expect(result[:delete]).to eq([deleted_obj])
end
end

context 'with delete_if proc (arity 1)' do
let(:obj1) { double(id: 1) }
let(:obj2) { double(id: 2) }

before { allow(adapter).to receive(:options).and_return(delete_if: ->(o) { o.id == 2 }) }

it 'uses the proc to determine deletion' do
result = adapter.send(:grouped_objects, [obj1, obj2])
expect(result[:index]).to eq([obj1])
expect(result[:delete]).to eq([obj2])
end
end

context 'with delete_if proc (arity 0, instance_exec)' do
let(:obj1) { double(id: 1) }
let(:obj2) { double(id: 2) }

before { allow(adapter).to receive(:options).and_return(delete_if: -> { id == 2 }) }

it 'uses instance_exec to evaluate the proc' do
result = adapter.send(:grouped_objects, [obj1, obj2])
expect(result[:index]).to eq([obj1])
expect(result[:delete]).to eq([obj2])
end
end
end
end
50 changes: 50 additions & 0 deletions spec/chewy/index/crutch_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'spec_helper'

describe Chewy::Index::Crutch do
before do
stub_index(:cities) do
crutch :names do |collection|
collection.to_h { |o| [o.id, o.name.upcase] }
end

crutch :ratings do |collection|
collection.to_h { |o| [o.id, o.rating * 10] }
end
end
end

describe Chewy::Index::Crutch::Crutches do
let(:collection) { [double(id: 1, name: 'London', rating: 5), double(id: 2, name: 'Paris', rating: 3)] }
subject { described_class.new(CitiesIndex, collection) }

describe '#[]' do
specify { expect(subject[:names]).to eq(1 => 'LONDON', 2 => 'PARIS') }
specify { expect(subject[:ratings]).to eq(1 => 50, 2 => 30) }
end

describe '#method_missing' do
specify { expect(subject.names).to eq(1 => 'LONDON', 2 => 'PARIS') }
specify { expect(subject.ratings).to eq(1 => 50, 2 => 30) }

specify do
expect { subject.nonexistent }.to raise_error(NoMethodError)
end
end

describe '#respond_to_missing?' do
specify { expect(subject).to respond_to(:names) }
specify { expect(subject).to respond_to(:ratings) }
specify { expect(subject).not_to respond_to(:nonexistent) }
end

it 'caches crutch results' do
expect(subject.names).to equal(subject.names)
end
end

describe '.crutch' do
specify { expect(CitiesIndex._crutches).to have_key(:names) }
specify { expect(CitiesIndex._crutches).to have_key(:ratings) }
specify { expect(CitiesIndex._crutches.size).to eq(2) }
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'

describe Chewy::Search::Parameters::AllowPartialSearchResults do
subject { described_class.new(true) }

describe '#initialize' do
specify { expect(described_class.new.value).to be_nil }
specify { expect(described_class.new(nil).value).to be_nil }
specify { expect(described_class.new(true).value).to be true }
specify { expect(described_class.new(false).value).to be false }
end

describe '#replace!' do
specify { expect { subject.replace!(false) }.to change { subject.value }.from(true).to(false) }
specify { expect { subject.replace!(nil) }.to change { subject.value }.from(true).to(nil) }
end

describe '#update!' do
specify { expect { subject.update!(false) }.to change { subject.value }.from(true).to(false) }
specify { expect { subject.update!(nil) }.not_to change { subject.value }.from(true) }
end

describe '#merge!' do
specify { expect { subject.merge!(described_class.new(false)) }.to change { subject.value }.from(true).to(false) }
specify { expect { subject.merge!(described_class.new) }.not_to change { subject.value }.from(true) }
end

describe '#render' do
specify { expect(described_class.new.render).to be_nil }
specify { expect(described_class.new(true).render).to eq(allow_partial_search_results: true) }
specify { expect(described_class.new(false).render).to eq(allow_partial_search_results: false) }
end
end
68 changes: 68 additions & 0 deletions spec/chewy/search/parameters/indices_boost_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'spec_helper'

describe Chewy::Search::Parameters::IndicesBoost do
subject { described_class.new(cities: 1.2) }

describe '#initialize' do
specify { expect(described_class.new.value).to eq({}) }
specify { expect(described_class.new(nil).value).to eq({}) }
specify { expect(described_class.new(cities: 1.2).value).to eq('cities' => 1.2) }
specify { expect(described_class.new(cities: '1.2').value).to eq('cities' => 1.2) }
specify { expect(described_class.new(cities: 2).value).to eq('cities' => 2.0) }
end

describe '#update!' do
specify do
expect { subject.update!(countries: 0.5) }
.to change { subject.value }
.from('cities' => 1.2)
.to('cities' => 1.2, 'countries' => 0.5)
end

specify do
expect { subject.update!(cities: 2.0) }
.to change { subject.value }
.from('cities' => 1.2)
.to('cities' => 2.0)
end
end

describe '#merge!' do
specify do
expect { subject.merge!(described_class.new(countries: 0.5)) }
.to change { subject.value }
.from('cities' => 1.2)
.to('cities' => 1.2, 'countries' => 0.5)
end

specify do
expect { subject.merge!(described_class.new) }
.not_to change { subject.value }
end
end

describe '#render' do
specify { expect(described_class.new.render).to be_nil }
specify { expect(described_class.new(cities: 1.2).render).to eq(indices_boost: [{'cities' => 1.2}]) }
specify do
param = described_class.new(cities: 1.2)
param.update!(countries: 0.5)
expect(param.render).to eq(indices_boost: [{'cities' => 1.2}, {'countries' => 0.5}])
end
end

describe '#==' do
specify { expect(described_class.new(cities: 1.2)).to eq(described_class.new(cities: 1.2)) }
specify { expect(described_class.new(cities: 1.2)).not_to eq(described_class.new(cities: 2.0)) }

it 'considers key order' do
a = described_class.new(cities: 1.2)
a.update!(countries: 0.5)

b = described_class.new(countries: 0.5)
b.update!(cities: 1.2)

expect(a).not_to eq(b)
end
end
end
47 changes: 47 additions & 0 deletions spec/chewy/search/scoping_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'spec_helper'

describe Chewy::Search::Scoping do
before do
stub_index(:cities) do
field :name
end
end

describe '#scoping' do
let(:request) { CitiesIndex.query(match: {name: 'London'}) }

it 'pushes and pops from the scopes stack' do
expect(Chewy::Search::Request.scopes).to be_empty

request.scoping do
expect(Chewy::Search::Request.scopes.size).to eq(1)
expect(Chewy::Search::Request.scopes.last).to eq(request)
end

expect(Chewy::Search::Request.scopes).to be_empty
end

it 'pops scope even when block raises' do
expect do
request.scoping { raise 'boom' }
end.to raise_error('boom')

expect(Chewy::Search::Request.scopes).to be_empty
end

it 'supports nesting' do
inner = CitiesIndex.filter(term: {name: 'Bangkok'})

request.scoping do
expect(Chewy::Search::Request.scopes.size).to eq(1)

inner.scoping do
expect(Chewy::Search::Request.scopes.size).to eq(2)
expect(Chewy::Search::Request.scopes.last).to eq(inner)
end

expect(Chewy::Search::Request.scopes.size).to eq(1)
end
end
end
end
29 changes: 29 additions & 0 deletions spec/chewy/strategy/base_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'

describe Chewy::Strategy::Base do
subject { described_class.new }

describe '#name' do
specify { expect(subject.name).to eq(:base) }
end

describe '#update' do
specify do
expect { subject.update('SomeIndex', [1, 2]) }
.to raise_error(Chewy::UndefinedUpdateStrategy)
end
end

describe '#leave' do
specify { expect(subject.leave).to be_nil }
end

describe '#update_chewy_indices' do
let(:object) { double(run_chewy_callbacks: true) }

specify do
expect(object).to receive(:run_chewy_callbacks)
subject.update_chewy_indices(object)
end
end
end
Loading