elasticsearch-model 5.1.0 → 6.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -3
  3. data/Gemfile +5 -0
  4. data/README.md +18 -11
  5. data/Rakefile +27 -36
  6. data/elasticsearch-model.gemspec +6 -6
  7. data/examples/activerecord_custom_analyzer.rb +135 -0
  8. data/examples/activerecord_mapping_completion.rb +3 -18
  9. data/examples/datamapper_article.rb +11 -1
  10. data/gemfiles/3.0.gemfile +6 -1
  11. data/gemfiles/4.0.gemfile +8 -1
  12. data/gemfiles/5.0.gemfile +7 -1
  13. data/gemfiles/6.0.gemfile +18 -0
  14. data/lib/elasticsearch/model/adapters/active_record.rb +8 -24
  15. data/lib/elasticsearch/model/adapters/mongoid.rb +10 -3
  16. data/lib/elasticsearch/model/importing.rb +1 -1
  17. data/lib/elasticsearch/model/indexing.rb +6 -4
  18. data/lib/elasticsearch/model/naming.rb +9 -2
  19. data/lib/elasticsearch/model/response/aggregations.rb +1 -1
  20. data/lib/elasticsearch/model/response/base.rb +3 -2
  21. data/lib/elasticsearch/model/response/pagination/kaminari.rb +109 -0
  22. data/lib/elasticsearch/model/response/pagination/will_paginate.rb +95 -0
  23. data/lib/elasticsearch/model/response/pagination.rb +2 -192
  24. data/lib/elasticsearch/model/response/result.rb +1 -1
  25. data/lib/elasticsearch/model/response/suggestions.rb +1 -1
  26. data/lib/elasticsearch/model/response.rb +11 -10
  27. data/lib/elasticsearch/model/version.rb +1 -1
  28. data/lib/elasticsearch/model.rb +15 -8
  29. data/spec/elasticsearch/model/adapter_spec.rb +119 -0
  30. data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +334 -0
  31. data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +340 -0
  32. data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +18 -0
  33. data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +187 -0
  34. data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +110 -0
  35. data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +38 -0
  36. data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +315 -0
  37. data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +75 -0
  38. data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +61 -0
  39. data/spec/elasticsearch/model/adapters/active_record_spec.rb +207 -0
  40. data/spec/elasticsearch/model/adapters/default_spec.rb +41 -0
  41. data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +267 -0
  42. data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +66 -0
  43. data/spec/elasticsearch/model/adapters/mongoid_spec.rb +235 -0
  44. data/spec/elasticsearch/model/adapters/multiple_spec.rb +125 -0
  45. data/spec/elasticsearch/model/callbacks_spec.rb +33 -0
  46. data/spec/elasticsearch/model/client_spec.rb +66 -0
  47. data/spec/elasticsearch/model/hash_wrapper_spec.rb +12 -0
  48. data/spec/elasticsearch/model/importing_spec.rb +214 -0
  49. data/spec/elasticsearch/model/indexing_spec.rb +918 -0
  50. data/spec/elasticsearch/model/module_spec.rb +101 -0
  51. data/spec/elasticsearch/model/multimodel_spec.rb +55 -0
  52. data/spec/elasticsearch/model/naming_inheritance_spec.rb +184 -0
  53. data/spec/elasticsearch/model/naming_spec.rb +186 -0
  54. data/spec/elasticsearch/model/proxy_spec.rb +107 -0
  55. data/spec/elasticsearch/model/response/aggregations_spec.rb +66 -0
  56. data/spec/elasticsearch/model/response/base_spec.rb +90 -0
  57. data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +410 -0
  58. data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +262 -0
  59. data/spec/elasticsearch/model/response/records_spec.rb +118 -0
  60. data/spec/elasticsearch/model/response/response_spec.rb +131 -0
  61. data/spec/elasticsearch/model/response/result_spec.rb +122 -0
  62. data/spec/elasticsearch/model/response/results_spec.rb +56 -0
  63. data/spec/elasticsearch/model/searching_search_request_spec.rb +112 -0
  64. data/spec/elasticsearch/model/searching_spec.rb +49 -0
  65. data/spec/elasticsearch/model/serializing_spec.rb +22 -0
  66. data/spec/spec_helper.rb +161 -0
  67. data/spec/support/app/answer.rb +33 -0
  68. data/spec/support/app/article.rb +22 -0
  69. data/spec/support/app/article_for_pagination.rb +12 -0
  70. data/spec/support/app/article_with_custom_serialization.rb +13 -0
  71. data/spec/support/app/article_with_dynamic_index_name.rb +15 -0
  72. data/spec/support/app/author.rb +9 -0
  73. data/spec/support/app/authorship.rb +4 -0
  74. data/spec/support/app/category.rb +3 -0
  75. data/spec/support/app/comment.rb +3 -0
  76. data/spec/support/app/episode.rb +11 -0
  77. data/spec/support/app/image.rb +19 -0
  78. data/spec/support/app/import_article.rb +12 -0
  79. data/spec/support/app/mongoid_article.rb +21 -0
  80. data/spec/support/app/namespaced_book.rb +10 -0
  81. data/spec/support/app/parent_and_child_searchable.rb +24 -0
  82. data/spec/support/app/post.rb +14 -0
  83. data/spec/support/app/question.rb +27 -0
  84. data/spec/support/app/searchable.rb +48 -0
  85. data/spec/support/app/series.rb +11 -0
  86. data/spec/support/app.rb +21 -0
  87. data/spec/support/model.json +1 -0
  88. data/{test → spec}/support/model.yml +0 -0
  89. metadata +134 -89
  90. data/test/integration/active_record_associations_parent_child_test.rb +0 -147
  91. data/test/integration/active_record_associations_test.rb +0 -339
  92. data/test/integration/active_record_basic_test.rb +0 -251
  93. data/test/integration/active_record_custom_serialization_test.rb +0 -67
  94. data/test/integration/active_record_import_test.rb +0 -115
  95. data/test/integration/active_record_namespaced_model_test.rb +0 -54
  96. data/test/integration/active_record_pagination_test.rb +0 -149
  97. data/test/integration/dynamic_index_name_test.rb +0 -52
  98. data/test/integration/mongoid_basic_test.rb +0 -176
  99. data/test/integration/multiple_models_test.rb +0 -176
  100. data/test/support/model.json +0 -1
  101. data/test/test_helper.rb +0 -92
  102. data/test/unit/adapter_active_record_test.rb +0 -157
  103. data/test/unit/adapter_default_test.rb +0 -41
  104. data/test/unit/adapter_mongoid_test.rb +0 -104
  105. data/test/unit/adapter_multiple_test.rb +0 -106
  106. data/test/unit/adapter_test.rb +0 -69
  107. data/test/unit/callbacks_test.rb +0 -31
  108. data/test/unit/client_test.rb +0 -27
  109. data/test/unit/hash_wrapper_test.rb +0 -13
  110. data/test/unit/importing_test.rb +0 -203
  111. data/test/unit/indexing_test.rb +0 -687
  112. data/test/unit/module_test.rb +0 -68
  113. data/test/unit/multimodel_test.rb +0 -38
  114. data/test/unit/naming_inheritance_test.rb +0 -94
  115. data/test/unit/naming_test.rb +0 -103
  116. data/test/unit/proxy_test.rb +0 -98
  117. data/test/unit/response_aggregations_test.rb +0 -46
  118. data/test/unit/response_base_test.rb +0 -40
  119. data/test/unit/response_pagination_kaminari_test.rb +0 -433
  120. data/test/unit/response_pagination_will_paginate_test.rb +0 -398
  121. data/test/unit/response_records_test.rb +0 -91
  122. data/test/unit/response_result_test.rb +0 -90
  123. data/test/unit/response_results_test.rb +0 -31
  124. data/test/unit/response_test.rb +0 -104
  125. data/test/unit/searching_search_request_test.rb +0 -78
  126. data/test/unit/searching_test.rb +0 -41
  127. data/test/unit/serializing_test.rb +0 -17
@@ -1,687 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::IndexingTest < Test::Unit::TestCase
4
- context "Indexing module: " do
5
- class ::DummyIndexingModel
6
- extend ActiveModel::Naming
7
- extend Elasticsearch::Model::Naming::ClassMethods
8
- extend Elasticsearch::Model::Indexing::ClassMethods
9
-
10
- def self.foo
11
- 'bar'
12
- end
13
- end
14
-
15
- class NotFound < Exception; end
16
-
17
- context "Settings class" do
18
- should "be convertible to hash" do
19
- hash = { foo: 'bar' }
20
- settings = Elasticsearch::Model::Indexing::Settings.new hash
21
- assert_equal hash, settings.to_hash
22
- assert_equal settings.to_hash, settings.as_json
23
- end
24
- end
25
-
26
- context "Settings method" do
27
- should "initialize the index settings" do
28
- assert_instance_of Elasticsearch::Model::Indexing::Settings, DummyIndexingModel.settings
29
- end
30
-
31
- should "update and return the index settings from a hash" do
32
- DummyIndexingModel.settings foo: 'boo'
33
- DummyIndexingModel.settings bar: 'bam'
34
-
35
- assert_equal( {foo: 'boo', bar: 'bam'}, DummyIndexingModel.settings.to_hash)
36
- end
37
-
38
- should "update and return the index settings from a yml file" do
39
- DummyIndexingModel.settings File.open("test/support/model.yml")
40
- DummyIndexingModel.settings bar: 'bam'
41
-
42
- assert_equal( {foo: 'boo', bar: 'bam', 'baz' => 'qux'}, DummyIndexingModel.settings.to_hash)
43
- end
44
-
45
- should "update and return the index settings from a json file" do
46
- DummyIndexingModel.settings File.open("test/support/model.json")
47
- DummyIndexingModel.settings bar: 'bam'
48
-
49
- assert_equal( {foo: 'boo', bar: 'bam', 'baz' => 'qux'}, DummyIndexingModel.settings.to_hash)
50
- end
51
-
52
- should "evaluate the block" do
53
- DummyIndexingModel.expects(:foo)
54
-
55
- DummyIndexingModel.settings do
56
- foo
57
- end
58
- end
59
- end
60
-
61
- context "Mappings class" do
62
- should "initialize the index mappings" do
63
- assert_instance_of Elasticsearch::Model::Indexing::Mappings, DummyIndexingModel.mappings
64
- end
65
-
66
- should "raise an exception when not passed type" do
67
- assert_raise ArgumentError do
68
- Elasticsearch::Model::Indexing::Mappings.new
69
- end
70
- end
71
-
72
- should "be convertible to hash" do
73
- mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype, { foo: 'bar' }
74
- assert_equal( { :mytype => { foo: 'bar', :properties => {} } }, mappings.to_hash )
75
- assert_equal mappings.to_hash, mappings.as_json
76
- end
77
-
78
- should "define properties" do
79
- mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
80
- assert_respond_to mappings, :indexes
81
-
82
- mappings.indexes :foo, { type: 'boolean', include_in_all: false }
83
- assert_equal 'boolean', mappings.to_hash[:mytype][:properties][:foo][:type]
84
- end
85
-
86
- should "define type as 'text' by default" do
87
- mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
88
-
89
- mappings.indexes :bar
90
- assert_equal 'text', mappings.to_hash[:mytype][:properties][:bar][:type]
91
- end
92
-
93
- should "define multiple fields" do
94
- mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
95
-
96
- mappings.indexes :my_field, type: 'text' do
97
- indexes :raw, type: 'keyword'
98
- end
99
-
100
- assert_equal 'text', mappings.to_hash[:mytype][:properties][:my_field][:type]
101
- assert_equal 'keyword', mappings.to_hash[:mytype][:properties][:my_field][:fields][:raw][:type]
102
- assert_nil mappings.to_hash[:mytype][:properties][:my_field][:properties]
103
- end
104
-
105
- should "define embedded properties" do
106
- mappings = Elasticsearch::Model::Indexing::Mappings.new :mytype
107
-
108
- mappings.indexes :foo do
109
- indexes :bar
110
- end
111
-
112
- mappings.indexes :foo_object, type: 'object' do
113
- indexes :bar
114
- end
115
-
116
- mappings.indexes :foo_nested, type: 'nested' do
117
- indexes :bar
118
- end
119
-
120
- mappings.indexes :foo_nested_as_symbol, type: :nested do
121
- indexes :bar
122
- end
123
-
124
- # Object is the default when `type` is missing and there's a block passed
125
- #
126
- assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo][:type]
127
- assert_equal 'text', mappings.to_hash[:mytype][:properties][:foo][:properties][:bar][:type]
128
- assert_nil mappings.to_hash[:mytype][:properties][:foo][:fields]
129
-
130
- assert_equal 'object', mappings.to_hash[:mytype][:properties][:foo_object][:type]
131
- assert_equal 'text', mappings.to_hash[:mytype][:properties][:foo_object][:properties][:bar][:type]
132
- assert_nil mappings.to_hash[:mytype][:properties][:foo_object][:fields]
133
-
134
- assert_equal 'nested', mappings.to_hash[:mytype][:properties][:foo_nested][:type]
135
- assert_equal 'text', mappings.to_hash[:mytype][:properties][:foo_nested][:properties][:bar][:type]
136
- assert_nil mappings.to_hash[:mytype][:properties][:foo_nested][:fields]
137
-
138
- assert_equal :nested, mappings.to_hash[:mytype][:properties][:foo_nested_as_symbol][:type]
139
- assert_not_nil mappings.to_hash[:mytype][:properties][:foo_nested_as_symbol][:properties]
140
- assert_nil mappings.to_hash[:mytype][:properties][:foo_nested_as_symbol][:fields]
141
- end
142
- end
143
-
144
- context "Mappings method" do
145
- should "initialize the index mappings" do
146
- assert_instance_of Elasticsearch::Model::Indexing::Mappings, DummyIndexingModel.mappings
147
- end
148
-
149
- should "update and return the index mappings" do
150
- DummyIndexingModel.mappings foo: 'boo'
151
- DummyIndexingModel.mappings bar: 'bam'
152
- assert_equal( { dummy_indexing_model: { foo: "boo", bar: "bam", properties: {} } },
153
- DummyIndexingModel.mappings.to_hash )
154
- end
155
-
156
- should "evaluate the block" do
157
- DummyIndexingModel.mappings.expects(:indexes).with(:foo).returns(true)
158
-
159
- DummyIndexingModel.mappings do
160
- indexes :foo
161
- end
162
- end
163
- end
164
-
165
- context "Instance methods" do
166
- class ::DummyIndexingModelWithCallbacks
167
- extend Elasticsearch::Model::Indexing::ClassMethods
168
- include Elasticsearch::Model::Indexing::InstanceMethods
169
-
170
- def self.before_save(&block)
171
- (@callbacks ||= {})[block.hash] = block
172
- end
173
-
174
- def changes_to_save
175
- {:foo => ['One', 'Two']}
176
- end
177
- end
178
-
179
- class ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson
180
- extend Elasticsearch::Model::Indexing::ClassMethods
181
- include Elasticsearch::Model::Indexing::InstanceMethods
182
-
183
- def self.before_save(&block)
184
- (@callbacks ||= {})[block.hash] = block
185
- end
186
-
187
- def changes_to_save
188
- {:foo => ['A', 'B'], :bar => ['C', 'D']}
189
- end
190
-
191
- def as_indexed_json(options={})
192
- { :foo => 'B' }
193
- end
194
- end
195
-
196
- class ::DummyIndexingModelWithOldDirty
197
- extend Elasticsearch::Model::Indexing::ClassMethods
198
- include Elasticsearch::Model::Indexing::InstanceMethods
199
-
200
- def self.before_save(&block)
201
- (@callbacks ||= {})[block.hash] = block
202
- end
203
-
204
- def changes
205
- {:foo => ['One', 'Two']}
206
- end
207
- end
208
-
209
- should "register before_save callback when included" do
210
- ::DummyIndexingModelWithCallbacks.expects(:before_save).returns(true)
211
- ::DummyIndexingModelWithCallbacks.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
212
- end
213
-
214
- should "set the @__changed_model_attributes variable before save" do
215
- instance = ::DummyIndexingModelWithCallbacks.new
216
- instance.expects(:instance_variable_set).with do |name, value|
217
- assert_equal :@__changed_model_attributes, name
218
- assert_equal({foo: 'Two'}, value)
219
- true
220
- end
221
-
222
- ::DummyIndexingModelWithCallbacks.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
223
-
224
- ::DummyIndexingModelWithCallbacks.instance_variable_get(:@callbacks).each do |n,b|
225
- instance.instance_eval(&b)
226
- end
227
- end
228
-
229
- # https://github.com/elastic/elasticsearch-rails/issues/714
230
- # https://github.com/rails/rails/pull/25337#issuecomment-225166796
231
- should "set the @__changed_model_attributes variable before save for old ActiveModel::Dirty" do
232
- instance = ::DummyIndexingModelWithOldDirty.new
233
- instance.expects(:instance_variable_set).with do |name, value|
234
- assert_equal :@__changed_model_attributes, name
235
- assert_equal({foo: 'Two'}, value)
236
- true
237
- end
238
-
239
- ::DummyIndexingModelWithOldDirty.__send__ :include, Elasticsearch::Model::Indexing::InstanceMethods
240
-
241
- ::DummyIndexingModelWithOldDirty.instance_variable_get(:@callbacks).each do |n,b|
242
- instance.instance_eval(&b)
243
- end
244
- end
245
-
246
- should "have the index_document method" do
247
- client = mock('client')
248
- instance = ::DummyIndexingModelWithCallbacks.new
249
-
250
- client.expects(:index).with do |payload|
251
- assert_equal 'foo', payload[:index]
252
- assert_equal 'bar', payload[:type]
253
- assert_equal '1', payload[:id]
254
- assert_equal 'JSON', payload[:body]
255
- true
256
- end
257
-
258
- instance.expects(:client).returns(client)
259
- instance.expects(:as_indexed_json).returns('JSON')
260
- instance.expects(:index_name).returns('foo')
261
- instance.expects(:document_type).returns('bar')
262
- instance.expects(:id).returns('1')
263
-
264
- instance.index_document
265
- end
266
-
267
- should "pass extra options to the index_document method to client.index" do
268
- client = mock('client')
269
- instance = ::DummyIndexingModelWithCallbacks.new
270
-
271
- client.expects(:index).with do |payload|
272
- assert_equal 'A', payload[:parent]
273
- true
274
- end
275
-
276
- instance.expects(:client).returns(client)
277
- instance.expects(:as_indexed_json).returns('JSON')
278
- instance.expects(:index_name).returns('foo')
279
- instance.expects(:document_type).returns('bar')
280
- instance.expects(:id).returns('1')
281
-
282
- instance.index_document(parent: 'A')
283
- end
284
-
285
- should "have the delete_document method" do
286
- client = mock('client')
287
- instance = ::DummyIndexingModelWithCallbacks.new
288
-
289
- client.expects(:delete).with do |payload|
290
- assert_equal 'foo', payload[:index]
291
- assert_equal 'bar', payload[:type]
292
- assert_equal '1', payload[:id]
293
- true
294
- end
295
-
296
- instance.expects(:client).returns(client)
297
- instance.expects(:index_name).returns('foo')
298
- instance.expects(:document_type).returns('bar')
299
- instance.expects(:id).returns('1')
300
-
301
- instance.delete_document()
302
- end
303
-
304
- should "pass extra options to the delete_document method to client.delete" do
305
- client = mock('client')
306
- instance = ::DummyIndexingModelWithCallbacks.new
307
-
308
- client.expects(:delete).with do |payload|
309
- assert_equal 'A', payload[:parent]
310
- true
311
- end
312
-
313
- instance.expects(:client).returns(client)
314
- instance.expects(:id).returns('1')
315
- instance.expects(:index_name).returns('foo')
316
- instance.expects(:document_type).returns('bar')
317
-
318
- instance.delete_document(parent: 'A')
319
- end
320
-
321
- should "update the document by re-indexing when no changes are present" do
322
- client = mock('client')
323
- instance = ::DummyIndexingModelWithCallbacks.new
324
-
325
- # Reset the fake `changes`
326
- instance.instance_variable_set(:@__changed_model_attributes, nil)
327
-
328
- instance.expects(:index_document)
329
- instance.update_document
330
- end
331
-
332
- should "update the document by partial update when changes are present" do
333
- client = mock('client')
334
- instance = ::DummyIndexingModelWithCallbacks.new
335
-
336
- # Set the fake `changes` hash
337
- instance.instance_variable_set(:@__changed_model_attributes, {foo: 'bar'})
338
-
339
- client.expects(:update).with do |payload|
340
- assert_equal 'foo', payload[:index]
341
- assert_equal 'bar', payload[:type]
342
- assert_equal '1', payload[:id]
343
- assert_equal({foo: 'bar'}, payload[:body][:doc])
344
- true
345
- end
346
-
347
- instance.expects(:client).returns(client)
348
- instance.expects(:index_name).returns('foo')
349
- instance.expects(:document_type).returns('bar')
350
- instance.expects(:id).returns('1')
351
-
352
- instance.update_document
353
- end
354
-
355
- should "exclude attributes not contained in custom as_indexed_json during partial update" do
356
- client = mock('client')
357
- instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new
358
-
359
- # Set the fake `changes` hash
360
- instance.instance_variable_set(:@__changed_model_attributes, {'foo' => 'B', 'bar' => 'D' })
361
-
362
- client.expects(:update).with do |payload|
363
- assert_equal({:foo => 'B'}, payload[:body][:doc])
364
- true
365
- end
366
-
367
- instance.expects(:client).returns(client)
368
- instance.expects(:index_name).returns('foo')
369
- instance.expects(:document_type).returns('bar')
370
- instance.expects(:id).returns('1')
371
-
372
- instance.update_document
373
- end
374
-
375
- should "get attributes from as_indexed_json during partial update" do
376
- client = mock('client')
377
- instance = ::DummyIndexingModelWithCallbacksAndCustomAsIndexedJson.new
378
-
379
- instance.instance_variable_set(:@__changed_model_attributes, { 'foo' => { 'bar' => 'BAR'} })
380
- # Overload as_indexed_json
381
- instance.expects(:as_indexed_json).returns({ 'foo' => 'BAR' })
382
-
383
- client.expects(:update).with do |payload|
384
- assert_equal({'foo' => 'BAR'}, payload[:body][:doc])
385
- true
386
- end
387
-
388
- instance.expects(:client).returns(client)
389
- instance.expects(:index_name).returns('foo')
390
- instance.expects(:document_type).returns('bar')
391
- instance.expects(:id).returns('1')
392
-
393
- instance.update_document
394
- end
395
-
396
- should "update only the specific attributes" do
397
- client = mock('client')
398
- instance = ::DummyIndexingModelWithCallbacks.new
399
-
400
- # Set the fake `changes` hash
401
- instance.instance_variable_set(:@__changed_model_attributes, {author: 'john'})
402
-
403
- client.expects(:update).with do |payload|
404
- assert_equal 'foo', payload[:index]
405
- assert_equal 'bar', payload[:type]
406
- assert_equal '1', payload[:id]
407
- assert_equal({title: 'green'}, payload[:body][:doc])
408
- true
409
- end
410
-
411
- instance.expects(:client).returns(client)
412
- instance.expects(:index_name).returns('foo')
413
- instance.expects(:document_type).returns('bar')
414
- instance.expects(:id).returns('1')
415
-
416
- instance.update_document_attributes title: "green"
417
- end
418
-
419
- should "pass options to the update_document_attributes method" do
420
- client = mock('client')
421
- instance = ::DummyIndexingModelWithCallbacks.new
422
-
423
- client.expects(:update).with do |payload|
424
- assert_equal 'foo', payload[:index]
425
- assert_equal 'bar', payload[:type]
426
- assert_equal '1', payload[:id]
427
- assert_equal({title: 'green'}, payload[:body][:doc])
428
- assert_equal true, payload[:refresh]
429
- true
430
- end
431
-
432
- instance.expects(:client).returns(client)
433
- instance.expects(:index_name).returns('foo')
434
- instance.expects(:document_type).returns('bar')
435
- instance.expects(:id).returns('1')
436
-
437
- instance.update_document_attributes( { title: "green" }, { refresh: true } )
438
- end
439
- end
440
-
441
- context "Checking for index existence" do
442
- context "when the index exists" do
443
- should "return true" do
444
- indices = mock('indices', exists: true)
445
- client = stub('client', indices: indices)
446
-
447
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
448
-
449
- assert_equal true, DummyIndexingModelForRecreate.index_exists?
450
- end
451
- end
452
-
453
- context "when the index does not exists" do
454
- should "return false" do
455
- indices = mock('indices', exists: false)
456
- client = stub('client', indices: indices)
457
-
458
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
459
-
460
- assert_equal false, DummyIndexingModelForRecreate.index_exists?
461
- end
462
- end
463
-
464
- context "when the indices API raises an error" do
465
- should "return false" do
466
- client = stub('client')
467
- client.expects(:indices).raises(StandardError)
468
-
469
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
470
-
471
- assert_equal false, DummyIndexingModelForRecreate.index_exists?
472
- end
473
- end
474
-
475
- context "the indices.exists API raises an error" do
476
- should "return false" do
477
- indices = stub('indices')
478
- client = stub('client')
479
- client.expects(:indices).returns(indices)
480
-
481
- indices.expects(:exists).raises(StandardError)
482
-
483
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
484
-
485
- assert_equal false, DummyIndexingModelForRecreate.index_exists?
486
- end
487
- end
488
- end
489
-
490
- context "Re-creating the index" do
491
- class ::DummyIndexingModelForRecreate
492
- extend ActiveModel::Naming
493
- extend Elasticsearch::Model::Naming::ClassMethods
494
- extend Elasticsearch::Model::Indexing::ClassMethods
495
-
496
- settings index: { number_of_shards: 1 } do
497
- mappings do
498
- indexes :foo, analyzer: 'keyword'
499
- end
500
- end
501
- end
502
-
503
- should "delete the index without raising exception when the index is not found" do
504
- client = stub('client')
505
- indices = stub('indices')
506
- client.stubs(:indices).returns(indices)
507
-
508
- indices.expects(:delete).returns({}).then.raises(NotFound).at_least_once
509
-
510
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
511
-
512
- assert_nothing_raised { DummyIndexingModelForRecreate.delete_index! force: true }
513
- end
514
-
515
- should "raise an exception without the force option" do
516
- client = stub('client')
517
- indices = stub('indices')
518
- client.stubs(:indices).returns(indices)
519
-
520
- indices.expects(:delete).raises(NotFound)
521
-
522
- DummyIndexingModelForRecreate.expects(:client).returns(client)
523
-
524
- assert_raise(NotFound) { DummyIndexingModelForRecreate.delete_index! }
525
- end
526
-
527
- should "raise a regular exception when deleting the index" do
528
- client = stub('client')
529
-
530
- indices = stub('indices')
531
- indices.expects(:delete).raises(Exception)
532
- client.stubs(:indices).returns(indices)
533
-
534
- DummyIndexingModelForRecreate.expects(:client).returns(client)
535
-
536
- assert_raise(Exception) { DummyIndexingModelForRecreate.delete_index! force: true }
537
- end
538
-
539
- should "create the index with correct settings and mappings when it doesn't exist" do
540
- client = stub('client')
541
- indices = stub('indices')
542
- client.stubs(:indices).returns(indices)
543
-
544
- indices.expects(:create).with do |payload|
545
- assert_equal 'dummy_indexing_model_for_recreates', payload[:index]
546
- assert_equal 1, payload[:body][:settings][:index][:number_of_shards]
547
- assert_equal 'keyword', payload[:body][:mappings][:dummy_indexing_model_for_recreate][:properties][:foo][:analyzer]
548
- true
549
- end.returns({})
550
-
551
- DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
552
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
553
-
554
- assert_nothing_raised { DummyIndexingModelForRecreate.create_index! }
555
- end
556
-
557
- should "get the index settings and mappings from options" do
558
- client = stub('client')
559
- indices = stub('indices')
560
- client.stubs(:indices).returns(indices)
561
-
562
- indices.expects(:create).with do |payload|
563
- assert_equal 'foobar', payload[:index]
564
- assert_equal 3, payload[:body][:settings][:index][:number_of_shards]
565
- assert_equal 'bar', payload[:body][:mappings][:foobar][:properties][:foo][:analyzer]
566
- true
567
- end.returns({})
568
-
569
- DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
570
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
571
-
572
- DummyIndexingModelForRecreate.create_index! \
573
- index: 'foobar',
574
- settings: { index: { number_of_shards: 3 } },
575
- mappings: { foobar: { properties: { foo: { analyzer: 'bar' } } } }
576
- end
577
-
578
- should "not create the index when it exists" do
579
- client = stub('client')
580
- indices = stub('indices')
581
- client.stubs(:indices).returns(indices)
582
-
583
- indices.expects(:create).never
584
-
585
- DummyIndexingModelForRecreate.expects(:index_exists?).returns(true)
586
- DummyIndexingModelForRecreate.expects(:client).returns(client).never
587
-
588
- assert_nothing_raised { DummyIndexingModelForRecreate.create_index! }
589
- end
590
-
591
- should "raise exception during index creation" do
592
- client = stub('client')
593
- indices = stub('indices')
594
- client.stubs(:indices).returns(indices)
595
-
596
- indices.expects(:delete).returns({})
597
- indices.expects(:create).raises(Exception).at_least_once
598
-
599
- DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
600
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
601
-
602
- assert_raise(Exception) { DummyIndexingModelForRecreate.create_index! force: true }
603
- end
604
-
605
- should "delete the index first with the force option" do
606
- client = stub('client')
607
- indices = stub('indices')
608
- client.stubs(:indices).returns(indices)
609
-
610
- indices.expects(:delete).returns({})
611
- indices.expects(:create).returns({}).at_least_once
612
-
613
- DummyIndexingModelForRecreate.expects(:index_exists?).returns(false)
614
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
615
-
616
- assert_nothing_raised do
617
- DummyIndexingModelForRecreate.create_index! force: true
618
- end
619
- end
620
-
621
- should "refresh the index without raising exception with the force option" do
622
- client = stub('client')
623
- indices = stub('indices')
624
- client.stubs(:indices).returns(indices)
625
-
626
- indices.expects(:refresh).returns({}).then.raises(NotFound).at_least_once
627
-
628
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
629
-
630
- assert_nothing_raised { DummyIndexingModelForRecreate.refresh_index! force: true }
631
- end
632
-
633
- should "raise a regular exception when refreshing the index" do
634
- client = stub('client')
635
- indices = stub('indices')
636
- client.stubs(:indices).returns(indices)
637
-
638
- indices.expects(:refresh).returns({}).then.raises(Exception).at_least_once
639
-
640
- DummyIndexingModelForRecreate.expects(:client).returns(client).at_least_once
641
-
642
- assert_nothing_raised { DummyIndexingModelForRecreate.refresh_index! force: true }
643
- end
644
-
645
- context "with a custom index name" do
646
- setup do
647
- @client = stub('client')
648
- @indices = stub('indices')
649
- @client.stubs(:indices).returns(@indices)
650
- DummyIndexingModelForRecreate.expects(:client).returns(@client).at_least_once
651
- end
652
-
653
- should "create the custom index" do
654
- @indices.expects(:create).with do |arguments|
655
- assert_equal 'custom-foo', arguments[:index]
656
- true
657
- end
658
- DummyIndexingModelForRecreate.expects(:index_exists?).with do |arguments|
659
- assert_equal 'custom-foo', arguments[:index]
660
- true
661
- end
662
-
663
- DummyIndexingModelForRecreate.create_index! index: 'custom-foo'
664
- end
665
-
666
- should "delete the custom index" do
667
- @indices.expects(:delete).with do |arguments|
668
- assert_equal 'custom-foo', arguments[:index]
669
- true
670
- end
671
-
672
- DummyIndexingModelForRecreate.delete_index! index: 'custom-foo'
673
- end
674
-
675
- should "refresh the custom index" do
676
- @indices.expects(:refresh).with do |arguments|
677
- assert_equal 'custom-foo', arguments[:index]
678
- true
679
- end
680
-
681
- DummyIndexingModelForRecreate.refresh_index! index: 'custom-foo'
682
- end
683
- end
684
- end
685
-
686
- end
687
- end