|
| 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 |
0 commit comments