|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Elasticsearch::Model::Importing do |
| 4 | + |
| 5 | + before(:all) do |
| 6 | + class DummyImportingModel |
| 7 | + end |
| 8 | + |
| 9 | + module DummyImportingAdapter |
| 10 | + module ImportingMixin |
| 11 | + def __find_in_batches(options={}, &block) |
| 12 | + yield if block_given? |
| 13 | + end |
| 14 | + def __transform |
| 15 | + lambda {|a|} |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + def importing_mixin |
| 20 | + ImportingMixin |
| 21 | + end; module_function :importing_mixin |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + after(:all) do |
| 26 | + Object.send(:remove_const, :DummyImportingModel) if defined?(DummyImportingModel) |
| 27 | + Object.send(:remove_const, :DummyImportingAdapter) if defined?(DummyImportingAdapter) |
| 28 | + end |
| 29 | + |
| 30 | + before do |
| 31 | + allow(Elasticsearch::Model::Adapter).to receive(:from_class).with(DummyImportingModel).and_return(DummyImportingAdapter) |
| 32 | + DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing |
| 33 | + end |
| 34 | + |
| 35 | + context 'when a model includes the Importing module' do |
| 36 | + |
| 37 | + it 'provides importing methods' do |
| 38 | + expect(DummyImportingModel.respond_to?(:import)).to be(true) |
| 39 | + expect(DummyImportingModel.respond_to?(:__find_in_batches)).to be(true) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + describe '#import' do |
| 44 | + |
| 45 | + before do |
| 46 | + allow(DummyImportingModel).to receive(:index_name).and_return('foo') |
| 47 | + allow(DummyImportingModel).to receive(:document_type).and_return('foo') |
| 48 | + allow(DummyImportingModel).to receive(:index_exists?).and_return(true) |
| 49 | + allow(DummyImportingModel).to receive(:__batch_to_bulk) |
| 50 | + allow(client).to receive(:bulk).and_return(response) |
| 51 | + end |
| 52 | + |
| 53 | + let(:client) do |
| 54 | + double('client') |
| 55 | + end |
| 56 | + |
| 57 | + let(:response) do |
| 58 | + { 'items' => [] } |
| 59 | + end |
| 60 | + |
| 61 | + context 'when no options are provided' do |
| 62 | + |
| 63 | + before do |
| 64 | + expect(DummyImportingModel).to receive(:client).and_return(client) |
| 65 | + allow(DummyImportingModel).to receive(:index_exists?).and_return(true) |
| 66 | + end |
| 67 | + |
| 68 | + it 'uses the client to import documents' do |
| 69 | + expect(DummyImportingModel.import).to eq(0) |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + context 'when there is an error' do |
| 74 | + |
| 75 | + before do |
| 76 | + expect(DummyImportingModel).to receive(:client).and_return(client) |
| 77 | + allow(DummyImportingModel).to receive(:index_exists?).and_return(true) |
| 78 | + end |
| 79 | + |
| 80 | + let(:response) do |
| 81 | + { 'items' => [{ 'index' => { } }, { 'index' => { 'error' => 'FAILED' } }] } |
| 82 | + end |
| 83 | + |
| 84 | + it 'returns the number of errors' do |
| 85 | + expect(DummyImportingModel.import).to eq(1) |
| 86 | + end |
| 87 | + |
| 88 | + context 'when the method is called with the option to return the errors' do |
| 89 | + |
| 90 | + it 'returns the errors' do |
| 91 | + expect(DummyImportingModel.import(return: 'errors')).to eq([{ 'index' => { 'error' => 'FAILED' } }]) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + context 'when the method is called with a block' do |
| 96 | + |
| 97 | + it 'yields the response to the block' do |
| 98 | + DummyImportingModel.import do |response| |
| 99 | + expect(response['items'].size).to eq(2) |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | + end |
| 104 | + |
| 105 | + context 'when the index does not exist' do |
| 106 | + |
| 107 | + before do |
| 108 | + allow(DummyImportingModel).to receive(:index_exists?).and_return(false) |
| 109 | + end |
| 110 | + |
| 111 | + it 'raises an exception' do |
| 112 | + expect { |
| 113 | + DummyImportingModel.import |
| 114 | + }.to raise_exception(ArgumentError) |
| 115 | + end |
| 116 | + end |
| 117 | + |
| 118 | + context 'when the method is called with the force option' do |
| 119 | + |
| 120 | + before do |
| 121 | + expect(DummyImportingModel).to receive(:create_index!).with(force: true, index: 'foo').and_return(true) |
| 122 | + expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true) |
| 123 | + end |
| 124 | + |
| 125 | + it 'deletes and creates the index' do |
| 126 | + expect(DummyImportingModel.import(force: true, foo: 'bar')).to eq(0) |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + context 'when the method is called with the refresh option' do |
| 131 | + |
| 132 | + before do |
| 133 | + expect(DummyImportingModel).to receive(:refresh_index!).with(index: 'foo').and_return(true) |
| 134 | + expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true) |
| 135 | + end |
| 136 | + |
| 137 | + it 'refreshes the index' do |
| 138 | + expect(DummyImportingModel.import(refresh: true, foo: 'bar')).to eq(0) |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context 'when a different index name is provided' do |
| 143 | + |
| 144 | + before do |
| 145 | + expect(DummyImportingModel).to receive(:client).and_return(client) |
| 146 | + expect(client).to receive(:bulk).with(body: nil, index: 'my-new-index', type: 'foo').and_return(response) |
| 147 | + end |
| 148 | + |
| 149 | + it 'uses the alternate index name' do |
| 150 | + expect(DummyImportingModel.import(index: 'my-new-index')).to eq(0) |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + context 'when a different document type is provided' do |
| 155 | + |
| 156 | + before do |
| 157 | + expect(DummyImportingModel).to receive(:client).and_return(client) |
| 158 | + expect(client).to receive(:bulk).with(body: nil, index: 'foo', type: 'my-new-type').and_return(response) |
| 159 | + end |
| 160 | + |
| 161 | + it 'uses the alternate index name' do |
| 162 | + expect(DummyImportingModel.import(type: 'my-new-type')).to eq(0) |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + context 'the transform method' do |
| 167 | + |
| 168 | + before do |
| 169 | + expect(DummyImportingModel).to receive(:client).and_return(client) |
| 170 | + expect(DummyImportingModel).to receive(:__transform).and_return(transform) |
| 171 | + expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform) |
| 172 | + end |
| 173 | + |
| 174 | + let(:transform) do |
| 175 | + lambda {|a|} |
| 176 | + end |
| 177 | + |
| 178 | + it 'applies the transform method to the results' do |
| 179 | + expect(DummyImportingModel.import).to eq(0) |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + context 'when a transform is provided as an option' do |
| 184 | + |
| 185 | + context 'when the transform option is not a lambda' do |
| 186 | + |
| 187 | + let(:transform) do |
| 188 | + 'not_callable' |
| 189 | + end |
| 190 | + |
| 191 | + it 'raises an error' do |
| 192 | + expect { |
| 193 | + DummyImportingModel.import(transform: transform) |
| 194 | + }.to raise_exception(ArgumentError) |
| 195 | + end |
| 196 | + end |
| 197 | + |
| 198 | + context 'when the transform option is a lambda' do |
| 199 | + |
| 200 | + before do |
| 201 | + expect(DummyImportingModel).to receive(:client).and_return(client) |
| 202 | + expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform) |
| 203 | + end |
| 204 | + |
| 205 | + let(:transform) do |
| 206 | + lambda {|a|} |
| 207 | + end |
| 208 | + |
| 209 | + it 'applies the transform lambda to the results' do |
| 210 | + expect(DummyImportingModel.import(transform: transform)).to eq(0) |
| 211 | + end |
| 212 | + end |
| 213 | + end |
| 214 | + end |
| 215 | +end |
0 commit comments