elasticsearch-model 0.1.9 → 7.1.1

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