elasticsearch-persistence 5.0.2 → 6.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. checksums.yaml +5 -5
  2. data/.rspec +2 -0
  3. data/Gemfile +9 -0
  4. data/README.md +206 -338
  5. data/Rakefile +15 -12
  6. data/elasticsearch-persistence.gemspec +6 -7
  7. data/examples/notes/application.rb +3 -4
  8. data/lib/elasticsearch/persistence.rb +2 -110
  9. data/lib/elasticsearch/persistence/repository.rb +212 -53
  10. data/lib/elasticsearch/persistence/repository/dsl.rb +94 -0
  11. data/lib/elasticsearch/persistence/repository/find.rb +27 -10
  12. data/lib/elasticsearch/persistence/repository/response/results.rb +21 -8
  13. data/lib/elasticsearch/persistence/repository/search.rb +30 -18
  14. data/lib/elasticsearch/persistence/repository/serialize.rb +65 -7
  15. data/lib/elasticsearch/persistence/repository/store.rb +38 -44
  16. data/lib/elasticsearch/persistence/version.rb +1 -1
  17. data/spec/repository/find_spec.rb +179 -0
  18. data/spec/repository/response/results_spec.rb +128 -0
  19. data/spec/repository/search_spec.rb +181 -0
  20. data/spec/repository/serialize_spec.rb +53 -0
  21. data/spec/repository/store_spec.rb +327 -0
  22. data/spec/repository_spec.rb +723 -0
  23. data/spec/spec_helper.rb +32 -0
  24. metadata +26 -104
  25. data/examples/music/album.rb +0 -54
  26. data/examples/music/artist.rb +0 -70
  27. data/examples/music/artists/_form.html.erb +0 -8
  28. data/examples/music/artists/artists_controller.rb +0 -67
  29. data/examples/music/artists/artists_controller_test.rb +0 -53
  30. data/examples/music/artists/index.html.erb +0 -60
  31. data/examples/music/artists/show.html.erb +0 -54
  32. data/examples/music/assets/application.css +0 -257
  33. data/examples/music/assets/autocomplete.css +0 -48
  34. data/examples/music/assets/blank_artist.png +0 -0
  35. data/examples/music/assets/blank_cover.png +0 -0
  36. data/examples/music/assets/form.css +0 -113
  37. data/examples/music/index_manager.rb +0 -73
  38. data/examples/music/search/index.html.erb +0 -95
  39. data/examples/music/search/search_controller.rb +0 -41
  40. data/examples/music/search/search_controller_test.rb +0 -12
  41. data/examples/music/search/search_helper.rb +0 -15
  42. data/examples/music/suggester.rb +0 -69
  43. data/examples/music/template.rb +0 -430
  44. data/examples/music/vendor/assets/jquery-ui-1.10.4.custom.min.css +0 -7
  45. data/examples/music/vendor/assets/jquery-ui-1.10.4.custom.min.js +0 -6
  46. data/examples/music/vendor/assets/stylesheets/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  47. data/lib/elasticsearch/persistence/client.rb +0 -51
  48. data/lib/elasticsearch/persistence/model.rb +0 -135
  49. data/lib/elasticsearch/persistence/model/base.rb +0 -87
  50. data/lib/elasticsearch/persistence/model/errors.rb +0 -8
  51. data/lib/elasticsearch/persistence/model/find.rb +0 -180
  52. data/lib/elasticsearch/persistence/model/rails.rb +0 -47
  53. data/lib/elasticsearch/persistence/model/store.rb +0 -254
  54. data/lib/elasticsearch/persistence/model/utils.rb +0 -0
  55. data/lib/elasticsearch/persistence/repository/class.rb +0 -71
  56. data/lib/elasticsearch/persistence/repository/naming.rb +0 -115
  57. data/lib/rails/generators/elasticsearch/model/model_generator.rb +0 -21
  58. data/lib/rails/generators/elasticsearch/model/templates/model.rb.tt +0 -9
  59. data/lib/rails/generators/elasticsearch_generator.rb +0 -2
  60. data/test/integration/model/model_basic_test.rb +0 -233
  61. data/test/integration/repository/custom_class_test.rb +0 -85
  62. data/test/integration/repository/customized_class_test.rb +0 -82
  63. data/test/integration/repository/default_class_test.rb +0 -116
  64. data/test/integration/repository/virtus_model_test.rb +0 -118
  65. data/test/test_helper.rb +0 -55
  66. data/test/unit/model_base_test.rb +0 -72
  67. data/test/unit/model_find_test.rb +0 -153
  68. data/test/unit/model_gateway_test.rb +0 -101
  69. data/test/unit/model_rails_test.rb +0 -112
  70. data/test/unit/model_store_test.rb +0 -576
  71. data/test/unit/persistence_test.rb +0 -32
  72. data/test/unit/repository_class_test.rb +0 -51
  73. data/test/unit/repository_client_test.rb +0 -32
  74. data/test/unit/repository_find_test.rb +0 -388
  75. data/test/unit/repository_indexing_test.rb +0 -37
  76. data/test/unit/repository_module_test.rb +0 -146
  77. data/test/unit/repository_naming_test.rb +0 -146
  78. data/test/unit/repository_response_results_test.rb +0 -98
  79. data/test/unit/repository_search_test.rb +0 -117
  80. data/test/unit/repository_serialize_test.rb +0 -57
  81. data/test/unit/repository_store_test.rb +0 -303
@@ -1,112 +0,0 @@
1
- require 'test_helper'
2
-
3
- require 'elasticsearch/persistence/model'
4
- require 'elasticsearch/persistence/model/rails'
5
-
6
- require 'rails'
7
- require 'action_controller/railtie'
8
- require 'action_view/railtie'
9
-
10
- class ::MyRailsModel
11
- include Elasticsearch::Persistence::Model
12
- include Elasticsearch::Persistence::Model::Rails
13
-
14
- attribute :name, String, mapping: { analyzer: 'english' }
15
- attribute :published_at, DateTime
16
- attribute :published_on, Date
17
- end
18
-
19
- class Application < Rails::Application
20
- config.eager_load = false
21
- config.root = File.dirname(File.expand_path('../../../tmp', __FILE__))
22
- config.logger = Logger.new($stderr)
23
-
24
- routes.append do
25
- resources :my_rails_models
26
- end
27
- end
28
-
29
- class ApplicationController < ActionController::Base
30
- include Application.routes.url_helpers
31
- include ActionController::UrlFor
32
- end
33
- ApplicationController.default_url_options = { host: 'localhost' }
34
- ApplicationController._routes.append { resources :my_rails_models }
35
-
36
- class MyRailsModelController < ApplicationController; end
37
-
38
- Application.initialize!
39
-
40
- class Elasticsearch::Persistence::ModelRailsTest < Test::Unit::TestCase
41
- context "The model in a Rails application" do
42
-
43
- should "generate proper URLs and paths" do
44
- model = MyRailsModel.new name: 'Test'
45
- model.stubs(:id).returns(1)
46
- model.stubs(:persisted?).returns(true)
47
-
48
- controller = MyRailsModelController.new
49
- controller.request = ActionDispatch::Request.new({})
50
-
51
- assert_equal 'http://localhost/my_rails_models/1', controller.url_for(model)
52
- assert_equal '/my_rails_models/1/edit', controller.edit_my_rails_model_path(model)
53
- end
54
-
55
- should "generate a link" do
56
- class MyView; include ActionView::Helpers::UrlHelper; end
57
-
58
- model = MyRailsModel.new name: 'Test'
59
- view = MyView.new
60
- view.expects(:url_for).with(model).returns('foo/bar')
61
-
62
- assert_equal '<a href="foo/bar">Show</a>', view.link_to('Show', model)
63
- end
64
-
65
- should "parse DateTime from Rails forms" do
66
- params = { "published_at(1i)"=>"2014",
67
- "published_at(2i)"=>"1",
68
- "published_at(3i)"=>"1",
69
- "published_at(4i)"=>"12",
70
- "published_at(5i)"=>"00"
71
- }
72
-
73
- assert_equal '2014-1-1- 12:00:',
74
- Elasticsearch::Persistence::Model::Rails.__convert_rails_dates(params)['published_at']
75
-
76
- m = MyRailsModel.new params
77
- assert_equal "2014-01-01T12:00:00+00:00", m.published_at.iso8601
78
- end
79
-
80
- should "parse Date from Rails forms" do
81
- params = { "published_on(1i)"=>"2014",
82
- "published_on(2i)"=>"1",
83
- "published_on(3i)"=>"1"
84
- }
85
-
86
- assert_equal '2014-1-1-',
87
- Elasticsearch::Persistence::Model::Rails.__convert_rails_dates(params)['published_on']
88
-
89
-
90
- m = MyRailsModel.new params
91
- assert_equal "2014-01-01", m.published_on.iso8601
92
- end
93
-
94
- context "when updating," do
95
- should "pass the options to gateway" do
96
- model = MyRailsModel.new name: 'Test'
97
- model.stubs(:persisted?).returns(true)
98
-
99
- model.class.gateway
100
- .expects(:update)
101
- .with do |object, options|
102
- assert_equal 'ABC', options[:routing]
103
- true
104
- end
105
- .returns({'_id' => 'abc123'})
106
-
107
- assert model.update( { title: 'UPDATED' }, { routing: 'ABC' } )
108
- end
109
- end
110
-
111
- end
112
- end
@@ -1,576 +0,0 @@
1
- require 'test_helper'
2
-
3
- require 'active_model'
4
- require 'virtus'
5
-
6
- require 'elasticsearch/persistence/model/base'
7
- require 'elasticsearch/persistence/model/errors'
8
- require 'elasticsearch/persistence/model/store'
9
-
10
- class Elasticsearch::Persistence::ModelStoreTest < Test::Unit::TestCase
11
- context "The model store module," do
12
-
13
- class DummyStoreModel
14
- include ActiveModel::Naming
15
- include ActiveModel::Conversion
16
- include ActiveModel::Serialization
17
- include ActiveModel::Serializers::JSON
18
- include ActiveModel::Validations
19
-
20
- include Virtus.model
21
-
22
- include Elasticsearch::Persistence::Model::Base::InstanceMethods
23
- extend Elasticsearch::Persistence::Model::Store::ClassMethods
24
- include Elasticsearch::Persistence::Model::Store::InstanceMethods
25
-
26
- extend ActiveModel::Callbacks
27
- define_model_callbacks :create, :save, :update, :destroy
28
- define_model_callbacks :find, :touch, only: :after
29
-
30
- attribute :title, String
31
- attribute :count, Integer, default: 0
32
- attribute :created_at, DateTime, default: lambda { |o,a| Time.now.utc }
33
- attribute :updated_at, DateTime, default: lambda { |o,a| Time.now.utc }
34
- end
35
-
36
- setup do
37
- @shoulda_subject = DummyStoreModel.new title: 'Test'
38
- @gateway = stub
39
- DummyStoreModel.stubs(:gateway).returns(@gateway)
40
- end
41
-
42
- teardown do
43
- Elasticsearch::Persistence::ModelStoreTest.__send__ :remove_const, :DummyStoreModelWithCallback \
44
- rescue NameError; nil
45
- end
46
-
47
- should "be new_record" do
48
- assert subject.new_record?
49
- end
50
-
51
- context "when creating," do
52
- should "save the object and return it" do
53
- DummyStoreModel.any_instance.expects(:save).returns({'_id' => 'X'})
54
-
55
- assert_instance_of DummyStoreModel, DummyStoreModel.create(title: 'Test')
56
- end
57
-
58
- should "execute the callbacks" do
59
- DummyStoreModelWithCallback = Class.new(DummyStoreModel)
60
- @gateway.expects(:save).returns({'_id' => 'X'})
61
-
62
- DummyStoreModelWithCallback.after_create { $stderr.puts "CREATED" }
63
- DummyStoreModelWithCallback.after_save { $stderr.puts "SAVED" }
64
-
65
- $stderr.expects(:puts).with('CREATED')
66
- $stderr.expects(:puts).with('SAVED')
67
-
68
- DummyStoreModelWithCallback.create name: 'test'
69
- end
70
- end
71
-
72
- context "when saving," do
73
- should "save the model" do
74
- @gateway
75
- .expects(:save)
76
- .with do |object, options|
77
- assert_equal subject, object
78
- assert_equal nil, options[:id]
79
- true
80
- end
81
- .returns({'_id' => 'abc123'})
82
-
83
- assert subject.save
84
- end
85
-
86
- should "save the model and set the ID" do
87
- @gateway
88
- .expects(:save)
89
- .returns({'_id' => 'abc123'})
90
-
91
- assert_nil subject.id
92
-
93
- subject.save
94
- assert_equal 'abc123', subject.id
95
- end
96
-
97
- should "save the model and update the timestamp" do
98
- now = Time.parse('2014-01-01T00:00:00Z')
99
- Time.expects(:now).returns(now).at_least_once
100
- @gateway
101
- .expects(:save)
102
- .returns({'_id' => 'abc123'})
103
-
104
- subject.save
105
- assert_equal Time.parse('2014-01-01T00:00:00Z'), subject.updated_at
106
- end
107
-
108
- should "not save an invalid model" do
109
- @gateway
110
- .expects(:save)
111
- .never
112
-
113
- subject.instance_eval do
114
- def valid?; false; end;
115
- end
116
-
117
- assert ! subject.save
118
- assert ! subject.persisted?
119
- end
120
-
121
- should "skip the validation with the :validate option" do
122
- @gateway
123
- .expects(:save)
124
- .with do |object, options|
125
- assert_equal subject, object
126
- assert_equal nil, options[:id]
127
- true
128
- end
129
- .returns({'_id' => 'abc123'})
130
-
131
- subject.instance_eval do
132
- def valid?; false; end;
133
- end
134
-
135
- assert subject.save validate: false
136
- assert subject.persisted?
137
- end
138
-
139
- should "pass the options to gateway" do
140
- @gateway
141
- .expects(:save)
142
- .with do |object, options|
143
- assert_equal 'ABC', options[:routing]
144
- true
145
- end
146
- .returns({'_id' => 'abc123'})
147
-
148
- assert subject.save routing: 'ABC'
149
- end
150
-
151
- should "return the response" do
152
- @gateway
153
- .expects(:save)
154
- .returns('FOOBAR')
155
-
156
- assert_equal 'FOOBAR', subject.save
157
- end
158
-
159
- should "execute the callbacks" do
160
- @gateway.expects(:save).returns({'_id' => 'abc'})
161
- DummyStoreModelWithCallback = Class.new(DummyStoreModel)
162
-
163
- DummyStoreModelWithCallback.after_save { $stderr.puts "SAVED" }
164
-
165
- $stderr.expects(:puts).with('SAVED')
166
- d = DummyStoreModelWithCallback.new name: 'Test'
167
- d.save
168
- end
169
-
170
- should "save the model to its own index" do
171
- @gateway.expects(:save)
172
- .with do |model, options|
173
- assert_equal 'my_custom_index', options[:index]
174
- assert_equal 'my_custom_type', options[:type]
175
- true
176
- end
177
- .returns({'_id' => 'abc'})
178
-
179
- d = DummyStoreModel.new name: 'Test'
180
- d.instance_variable_set(:@_index, 'my_custom_index')
181
- d.instance_variable_set(:@_type, 'my_custom_type')
182
- d.save
183
- end
184
-
185
- should "set the meta attributes from response" do
186
- @gateway.expects(:save)
187
- .with do |model, options|
188
- assert_equal 'my_custom_index', options[:index]
189
- assert_equal 'my_custom_type', options[:type]
190
- true
191
- end
192
- .returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})
193
-
194
- d = DummyStoreModel.new name: 'Test'
195
- d.instance_variable_set(:@_index, 'my_custom_index')
196
- d.instance_variable_set(:@_type, 'my_custom_type')
197
- d.save
198
-
199
- assert_equal 'foo', d._index
200
- assert_equal 'bar', d._type
201
- assert_equal '100', d._version
202
- end
203
- end
204
-
205
- context "when destroying," do
206
- should "remove the model from Elasticsearch" do
207
- subject.expects(:persisted?).returns(true)
208
- subject.expects(:id).returns('abc123')
209
- subject.expects(:freeze).returns(subject)
210
-
211
- @gateway
212
- .expects(:delete)
213
- .with('abc123', {})
214
- .returns({'_id' => 'abc123', 'version' => 2})
215
-
216
- assert subject.destroy
217
- assert subject.destroyed?
218
- end
219
-
220
- should "pass the options to gateway" do
221
- subject.expects(:persisted?).returns(true)
222
- subject.expects(:freeze).returns(subject)
223
-
224
- @gateway
225
- .expects(:delete)
226
- .with do |object, options|
227
- assert_equal 'ABC', options[:routing]
228
- true
229
- end
230
- .returns({'_id' => 'abc123'})
231
-
232
- assert subject.destroy routing: 'ABC'
233
- end
234
-
235
- should "return the response" do
236
- subject.expects(:persisted?).returns(true)
237
- subject.expects(:freeze).returns(subject)
238
-
239
- @gateway
240
- .expects(:delete)
241
- .returns('FOOBAR')
242
-
243
- assert_equal 'FOOBAR', subject.destroy
244
- end
245
-
246
- should "execute the callbacks" do
247
- @gateway.expects(:delete).returns({'_id' => 'abc'})
248
- DummyStoreModelWithCallback = Class.new(DummyStoreModel)
249
-
250
- DummyStoreModelWithCallback.after_destroy { $stderr.puts "DELETED" }
251
-
252
- $stderr.expects(:puts).with('DELETED')
253
- d = DummyStoreModelWithCallback.new name: 'Test'
254
- d.expects(:persisted?).returns(true)
255
- d.expects(:freeze).returns(d)
256
-
257
- d.destroy
258
- end
259
-
260
- should "remove the model from its own index" do
261
- @gateway.expects(:delete)
262
- .with do |model, options|
263
- assert_equal 'my_custom_index', options[:index]
264
- assert_equal 'my_custom_type', options[:type]
265
- true
266
- end
267
- .returns({'_id' => 'abc'})
268
-
269
- d = DummyStoreModel.new name: 'Test'
270
- d.instance_variable_set(:@_index, 'my_custom_index')
271
- d.instance_variable_set(:@_type, 'my_custom_type')
272
- d.expects(:persisted?).returns(true)
273
- d.expects(:freeze).returns(d)
274
-
275
- d.destroy
276
- end
277
- end
278
-
279
- context "when updating," do
280
- should "update the document with partial attributes" do
281
- subject.expects(:persisted?).returns(true)
282
- subject.expects(:id).returns('abc123').at_least_once
283
-
284
- @gateway
285
- .expects(:update)
286
- .with do |id, options|
287
- assert_equal 'abc123', id
288
- assert_equal 'UPDATED', options[:doc][:title]
289
- true
290
- end
291
- .returns({'_id' => 'abc123', 'version' => 2})
292
-
293
- assert subject.update title: 'UPDATED'
294
-
295
- assert_equal 'UPDATED', subject.title
296
- end
297
-
298
- should "allow to update the document with a custom script" do
299
- subject.expects(:persisted?).returns(true)
300
- subject.expects(:id).returns('abc123').at_least_once
301
-
302
- @gateway
303
- .expects(:update)
304
- .with do |id, options|
305
- assert_equal 'abc123', id
306
- assert_equal 'EXEC', options[:script]
307
- true
308
- end
309
- .returns({'_id' => 'abc123', 'version' => 2})
310
-
311
- assert subject.update( {}, { script: 'EXEC' } )
312
- end
313
-
314
- should "not update an invalid model" do
315
- @gateway
316
- .expects(:update)
317
- .never
318
-
319
- subject.instance_eval do
320
- def valid?; false; end;
321
- end
322
-
323
- assert ! subject.update(title: 'INVALID')
324
- end
325
-
326
- should "skip the validation with the :validate option" do
327
- subject.expects(:persisted?).returns(true).at_least_once
328
- subject.expects(:id).returns('abc123').at_least_once
329
-
330
- @gateway
331
- .expects(:update)
332
- .with do |object, options|
333
- assert_equal 'abc123', object
334
- assert_equal nil, options[:id]
335
- assert_equal 'INVALID', options[:doc][:title]
336
- true
337
- end
338
- .returns({'_id' => 'abc123'})
339
-
340
- subject.instance_eval do
341
- def valid?; false; end;
342
- end
343
-
344
- assert subject.update( { title: 'INVALID' }, { validate: false } )
345
- assert subject.persisted?
346
- end
347
-
348
- should "pass the options to gateway" do
349
- subject.expects(:persisted?).returns(true)
350
-
351
- @gateway
352
- .expects(:update)
353
- .with do |object, options|
354
- assert_equal 'ABC', options[:routing]
355
- true
356
- end
357
- .returns({'_id' => 'abc123'})
358
-
359
- assert subject.update( { title: 'UPDATED' }, { routing: 'ABC' } )
360
- end
361
-
362
- should "return the response" do
363
- subject.expects(:persisted?).returns(true)
364
-
365
- @gateway
366
- .expects(:update)
367
- .returns('FOOBAR')
368
-
369
- assert_equal 'FOOBAR', subject.update
370
- end
371
-
372
- should "execute the callbacks" do
373
- @gateway.expects(:update).returns({'_id' => 'abc'})
374
- DummyStoreModelWithCallback = Class.new(DummyStoreModel)
375
-
376
- DummyStoreModelWithCallback.after_update { $stderr.puts "UPDATED" }
377
-
378
- $stderr.expects(:puts).with('UPDATED')
379
- d = DummyStoreModelWithCallback.new name: 'Test'
380
- d.expects(:persisted?).returns(true)
381
- d.update name: 'Update'
382
- end
383
-
384
- should "update the model in its own index" do
385
- @gateway.expects(:update)
386
- .with do |model, options|
387
- assert_equal 'my_custom_index', options[:index]
388
- assert_equal 'my_custom_type', options[:type]
389
- true
390
- end
391
- .returns({'_id' => 'abc'})
392
-
393
- d = DummyStoreModel.new name: 'Test'
394
- d.instance_variable_set(:@_index, 'my_custom_index')
395
- d.instance_variable_set(:@_type, 'my_custom_type')
396
- d.expects(:persisted?).returns(true)
397
-
398
- d.update name: 'Update'
399
- end
400
-
401
- should "set the meta attributes from response" do
402
- @gateway.expects(:update)
403
- .with do |model, options|
404
- assert_equal 'my_custom_index', options[:index]
405
- assert_equal 'my_custom_type', options[:type]
406
- true
407
- end
408
- .returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})
409
-
410
- d = DummyStoreModel.new name: 'Test'
411
- d.instance_variable_set(:@_index, 'my_custom_index')
412
- d.instance_variable_set(:@_type, 'my_custom_type')
413
- d.expects(:persisted?).returns(true)
414
-
415
- d.update name: 'Update'
416
-
417
- assert_equal 'foo', d._index
418
- assert_equal 'bar', d._type
419
- assert_equal '100', d._version
420
- end
421
- end
422
-
423
- context "when incrementing," do
424
- should "increment the attribute" do
425
- subject.expects(:persisted?).returns(true)
426
-
427
- @gateway
428
- .expects(:update)
429
- .with do |id, options|
430
- assert_equal 'ctx._source.count += 1', options[:script]
431
- true
432
- end
433
- .returns({'_id' => 'abc123', 'version' => 2})
434
-
435
- assert subject.increment :count
436
-
437
- assert_equal 1, subject.count
438
- end
439
-
440
- should "set the meta attributes from response" do
441
- subject.expects(:persisted?).returns(true)
442
-
443
- @gateway.expects(:update)
444
- .with do |model, options|
445
- assert_equal 'my_custom_index', options[:index]
446
- assert_equal 'my_custom_type', options[:type]
447
- true
448
- end
449
- .returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})
450
-
451
- subject.instance_variable_set(:@_index, 'my_custom_index')
452
- subject.instance_variable_set(:@_type, 'my_custom_type')
453
-
454
- subject.increment :count
455
-
456
- assert_equal 'foo', subject._index
457
- assert_equal 'bar', subject._type
458
- assert_equal '100', subject._version
459
- end
460
- end
461
-
462
- context "when decrement," do
463
- should "decrement the attribute" do
464
- subject.expects(:persisted?).returns(true)
465
-
466
- @gateway
467
- .expects(:update)
468
- .with do |id, options|
469
- assert_equal 'ctx._source.count = ctx._source.count - 1', options[:script]
470
- true
471
- end
472
- .returns({'_id' => 'abc123', 'version' => 2})
473
-
474
- assert subject.decrement :count
475
-
476
- assert_equal -1, subject.count
477
- end
478
-
479
- should "set the meta attributes from response" do
480
- subject.expects(:persisted?).returns(true)
481
-
482
- @gateway.expects(:update)
483
- .with do |model, options|
484
- assert_equal 'my_custom_index', options[:index]
485
- assert_equal 'my_custom_type', options[:type]
486
- true
487
- end
488
- .returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})
489
-
490
- subject.instance_variable_set(:@_index, 'my_custom_index')
491
- subject.instance_variable_set(:@_type, 'my_custom_type')
492
-
493
- subject.decrement :count
494
-
495
- assert_equal 'foo', subject._index
496
- assert_equal 'bar', subject._type
497
- assert_equal '100', subject._version
498
- end
499
- end
500
-
501
- context "when touching," do
502
- should "raise exception when touching not existing attribute" do
503
- subject.expects(:persisted?).returns(true)
504
- assert_raise(ArgumentError) { subject.touch :foobar }
505
- end
506
-
507
- should "update updated_at by default" do
508
- subject.expects(:persisted?).returns(true)
509
- now = Time.parse('2014-01-01T00:00:00Z')
510
- Time.expects(:now).returns(now).at_least_once
511
-
512
- @gateway
513
- .expects(:update)
514
- .with do |id, options|
515
- assert_equal '2014-01-01T00:00:00Z', options[:doc][:updated_at]
516
- true
517
- end
518
- .returns({'_id' => 'abc123', 'version' => 2})
519
-
520
- subject.touch
521
- assert_equal Time.parse('2014-01-01T00:00:00Z'), subject.updated_at
522
- end
523
-
524
- should "update a custom attribute by default" do
525
- subject.expects(:persisted?).returns(true)
526
- now = Time.parse('2014-01-01T00:00:00Z')
527
- Time.expects(:now).returns(now).at_least_once
528
-
529
- @gateway
530
- .expects(:update)
531
- .with do |id, options|
532
- assert_equal '2014-01-01T00:00:00Z', options[:doc][:created_at]
533
- true
534
- end
535
- .returns({'_id' => 'abc123', 'version' => 2})
536
-
537
- subject.touch :created_at
538
- assert_equal Time.parse('2014-01-01T00:00:00Z'), subject.created_at
539
- end
540
-
541
- should "execute the callbacks" do
542
- @gateway.expects(:update).returns({'_id' => 'abc'})
543
- DummyStoreModelWithCallback = Class.new(DummyStoreModel)
544
-
545
- DummyStoreModelWithCallback.after_touch { $stderr.puts "TOUCHED" }
546
-
547
- $stderr.expects(:puts).with('TOUCHED')
548
- d = DummyStoreModelWithCallback.new name: 'Test'
549
- d.expects(:persisted?).returns(true)
550
- d.touch
551
- end
552
-
553
- should "set the meta attributes from response" do
554
- subject.expects(:persisted?).returns(true)
555
-
556
- @gateway.expects(:update)
557
- .with do |model, options|
558
- assert_equal 'my_custom_index', options[:index]
559
- assert_equal 'my_custom_type', options[:type]
560
- true
561
- end
562
- .returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})
563
-
564
- subject.instance_variable_set(:@_index, 'my_custom_index')
565
- subject.instance_variable_set(:@_type, 'my_custom_type')
566
-
567
- subject.touch
568
-
569
- assert_equal 'foo', subject._index
570
- assert_equal 'bar', subject._type
571
- assert_equal '100', subject._version
572
- end
573
- end
574
-
575
- end
576
- end