File: importing_spec.rb

package info (click to toggle)
ruby-elasticsearch-model 7.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 736 kB
  • sloc: ruby: 6,603; makefile: 4
file content (243 lines) | stat: -rw-r--r-- 7,406 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

require 'spec_helper'

describe Elasticsearch::Model::Importing do

  before(:all) do
    class DummyImportingModel
    end

    module DummyImportingAdapter
      module ImportingMixin
        def __find_in_batches(options={}, &block)
          yield if block_given?
        end
        def __transform
          lambda {|a|}
        end
      end

      def importing_mixin
        ImportingMixin
      end; module_function :importing_mixin
    end
  end

  after(:all) do
    remove_classes(DummyImportingModel, DummyImportingAdapter)
  end

  before do
    allow(Elasticsearch::Model::Adapter).to receive(:from_class).with(DummyImportingModel).and_return(DummyImportingAdapter)
    DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
  end

  context 'when a model includes the Importing module' do

    it 'provides importing methods' do
      expect(DummyImportingModel.respond_to?(:import)).to be(true)
      expect(DummyImportingModel.respond_to?(:__find_in_batches)).to be(true)
    end
  end

  describe '#import' do

    before do
      allow(DummyImportingModel).to receive(:index_name).and_return('foo')
      allow(DummyImportingModel).to receive(:document_type).and_return('foo')
      allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
      allow(DummyImportingModel).to receive(:__batch_to_bulk)
      allow(client).to receive(:bulk).and_return(response)
    end

    let(:client) do
      double('client')
    end

    let(:response) do
      { 'items' => [] }
    end

    context 'when no options are provided' do

      before do
        expect(DummyImportingModel).to receive(:client).and_return(client)
        allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
      end

      it 'uses the client to import documents' do
        expect(DummyImportingModel.import).to eq(0)
      end
    end

    context 'when there is an error' do

      before do
        expect(DummyImportingModel).to receive(:client).and_return(client)
        allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
      end

      let(:response) do
        { 'items' => [{ 'index' => { } }, { 'index' => { 'error' => 'FAILED' } }] }
      end

      it 'returns the number of errors' do
        expect(DummyImportingModel.import).to eq(1)
      end

      context 'when the method is called with the option to return the errors' do

        it 'returns the errors' do
          expect(DummyImportingModel.import(return: 'errors')).to eq([{ 'index' => { 'error' => 'FAILED' } }])
        end
      end

      context 'when the method is called with a block' do

        it 'yields the response to the block' do
          DummyImportingModel.import do |response|
            expect(response['items'].size).to eq(2)
          end
        end
      end
    end

    context 'when the index does not exist' do

      before do
        allow(DummyImportingModel).to receive(:index_exists?).and_return(false)
      end

      it 'raises an exception' do
        expect {
          DummyImportingModel.import
        }.to raise_exception(ArgumentError)
      end
    end

    context 'when the method is called with the force option' do

      before do
        expect(DummyImportingModel).to receive(:create_index!).with(force: true, index: 'foo').and_return(true)
        expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true)
      end

      it 'deletes and creates the index' do
        expect(DummyImportingModel.import(force: true, foo: 'bar')).to eq(0)
      end
    end

    context 'when the method is called with the refresh option' do

      before do
        expect(DummyImportingModel).to receive(:refresh_index!).with(index: 'foo').and_return(true)
        expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true)
      end

      it 'refreshes the index' do
        expect(DummyImportingModel.import(refresh: true, foo: 'bar')).to eq(0)
      end
    end

    context 'when a different index name is provided' do

      before do
        expect(DummyImportingModel).to receive(:client).and_return(client)
        expect(client).to receive(:bulk).with(body: nil, index: 'my-new-index', type: 'foo').and_return(response)
      end

      it 'uses the alternate index name' do
        expect(DummyImportingModel.import(index: 'my-new-index')).to eq(0)
      end
    end

    context 'when a different document type is provided' do

      before do
        expect(DummyImportingModel).to receive(:client).and_return(client)
        expect(client).to receive(:bulk).with(body: nil, index: 'foo', type: 'my-new-type').and_return(response)
      end

      it 'uses the alternate index name' do
        expect(DummyImportingModel.import(type: 'my-new-type')).to eq(0)
      end
    end

    context 'the transform method' do

      before do
        expect(DummyImportingModel).to receive(:client).and_return(client)
        expect(DummyImportingModel).to receive(:__transform).and_return(transform)
        expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform)
      end

      let(:transform) do
        lambda {|a|}
      end

      it 'applies the transform method to the results' do
        expect(DummyImportingModel.import).to eq(0)
      end
    end

    context 'when a transform is provided as an option' do

      context 'when the transform option is not a lambda' do

        let(:transform) do
          'not_callable'
        end

        it 'raises an error' do
          expect {
            DummyImportingModel.import(transform: transform)
          }.to raise_exception(ArgumentError)
        end
      end

      context 'when the transform option is a lambda' do

        before do
          expect(DummyImportingModel).to receive(:client).and_return(client)
          expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform)
        end

        let(:transform) do
          lambda {|a|}
        end

        it 'applies the transform lambda to the results' do
          expect(DummyImportingModel.import(transform: transform)).to eq(0)
        end
      end
    end

    context 'when a pipeline is provided as an options' do

      before do
        expect(DummyImportingModel).to receive(:client).and_return(client)
        expect(client).to receive(:bulk).with(body: nil, index: 'foo', type: 'foo', pipeline: 'my-pipeline').and_return(response)
      end

      it 'uses the pipeline option' do
        expect(DummyImportingModel.import(pipeline: 'my-pipeline')).to eq(0)
      end
    end
  end
end