forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_store_test.rb
514 lines (404 loc) · 15.5 KB
/
model_store_test.rb
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
require 'test_helper'
require 'active_model'
require 'virtus'
require 'elasticsearch/persistence/model/base'
require 'elasticsearch/persistence/model/errors'
require 'elasticsearch/persistence/model/store'
class Elasticsearch::Persistence::ModelStoreTest < Test::Unit::TestCase
context "The model store module," do
class DummyStoreModel
include ActiveModel::Naming
include ActiveModel::Conversion
include ActiveModel::Serialization
include ActiveModel::Serializers::JSON
include ActiveModel::Validations
include Virtus.model
include Elasticsearch::Persistence::Model::Base::InstanceMethods
extend Elasticsearch::Persistence::Model::Store::ClassMethods
include Elasticsearch::Persistence::Model::Store::InstanceMethods
extend ActiveModel::Callbacks
define_model_callbacks :create, :save, :update, :destroy
define_model_callbacks :find, :touch, only: :after
attribute :title, String
attribute :count, Integer, default: 0
attribute :created_at, DateTime, default: lambda { |o,a| Time.now.utc }
attribute :updated_at, DateTime, default: lambda { |o,a| Time.now.utc }
end
setup do
@shoulda_subject = DummyStoreModel.new title: 'Test'
@gateway = stub
DummyStoreModel.stubs(:gateway).returns(@gateway)
end
teardown do
Elasticsearch::Persistence::ModelStoreTest.__send__ :remove_const, :DummyStoreModelWithCallback \
rescue NameError; nil
end
should "be new_record" do
assert subject.new_record?
end
context "when creating," do
should "save the object and return it" do
DummyStoreModel.any_instance.expects(:save).returns({'_id' => 'X'})
assert_instance_of DummyStoreModel, DummyStoreModel.create(title: 'Test')
end
should "execute the callbacks" do
DummyStoreModelWithCallback = Class.new(DummyStoreModel)
@gateway.expects(:save).returns({'_id' => 'X'})
DummyStoreModelWithCallback.after_create { $stderr.puts "CREATED" }
DummyStoreModelWithCallback.after_save { $stderr.puts "SAVED" }
$stderr.expects(:puts).with('CREATED')
$stderr.expects(:puts).with('SAVED')
DummyStoreModelWithCallback.create name: 'test'
end
end
context "when saving," do
should "save the model" do
@gateway
.expects(:save)
.with do |object, options|
assert_equal subject, object
assert_equal nil, options[:id]
true
end
.returns({'_id' => 'abc123'})
assert ! subject.persisted?
assert subject.save
assert subject.persisted?
end
should "save the model and set the ID" do
@gateway
.expects(:save)
.returns({'_id' => 'abc123'})
assert_nil subject.id
subject.save
assert_equal 'abc123', subject.id
end
should "save the model and update the timestamp" do
now = Time.parse('2014-01-01T00:00:00Z')
Time.expects(:now).returns(now).at_least_once
@gateway
.expects(:save)
.returns({'_id' => 'abc123'})
subject.save
assert_equal Time.parse('2014-01-01T00:00:00Z'), subject.updated_at
end
should "pass the options to gateway" do
@gateway
.expects(:save)
.with do |object, options|
assert_equal 'ABC', options[:routing]
true
end
.returns({'_id' => 'abc123'})
assert subject.save routing: 'ABC'
end
should "return the response" do
@gateway
.expects(:save)
.returns('FOOBAR')
assert_equal 'FOOBAR', subject.save
end
should "execute the callbacks" do
@gateway.expects(:save).returns({'_id' => 'abc'})
DummyStoreModelWithCallback = Class.new(DummyStoreModel)
DummyStoreModelWithCallback.after_save { $stderr.puts "SAVED" }
$stderr.expects(:puts).with('SAVED')
d = DummyStoreModelWithCallback.new name: 'Test'
d.save
end
should "save the model to its own index" do
@gateway.expects(:save)
.with do |model, options|
assert_equal 'my_custom_index', options[:index]
assert_equal 'my_custom_type', options[:type]
true
end
.returns({'_id' => 'abc'})
d = DummyStoreModel.new name: 'Test'
d.instance_variable_set(:@_index, 'my_custom_index')
d.instance_variable_set(:@_type, 'my_custom_type')
d.save
end
should "set the meta attributes from response" do
@gateway.expects(:save)
.with do |model, options|
assert_equal 'my_custom_index', options[:index]
assert_equal 'my_custom_type', options[:type]
true
end
.returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})
d = DummyStoreModel.new name: 'Test'
d.instance_variable_set(:@_index, 'my_custom_index')
d.instance_variable_set(:@_type, 'my_custom_type')
d.save
assert_equal 'foo', d._index
assert_equal 'bar', d._type
assert_equal '100', d._version
end
end
context "when destroying," do
should "remove the model from Elasticsearch" do
subject.expects(:persisted?).returns(true)
subject.expects(:id).returns('abc123')
subject.expects(:freeze).returns(subject)
@gateway
.expects(:delete)
.with('abc123', {})
.returns({'_id' => 'abc123', 'version' => 2})
assert subject.destroy
assert subject.destroyed?
end
should "pass the options to gateway" do
subject.expects(:persisted?).returns(true)
subject.expects(:freeze).returns(subject)
@gateway
.expects(:delete)
.with do |object, options|
assert_equal 'ABC', options[:routing]
true
end
.returns({'_id' => 'abc123'})
assert subject.destroy routing: 'ABC'
end
should "return the response" do
subject.expects(:persisted?).returns(true)
subject.expects(:freeze).returns(subject)
@gateway
.expects(:delete)
.returns('FOOBAR')
assert_equal 'FOOBAR', subject.destroy
end
should "execute the callbacks" do
@gateway.expects(:delete).returns({'_id' => 'abc'})
DummyStoreModelWithCallback = Class.new(DummyStoreModel)
DummyStoreModelWithCallback.after_destroy { $stderr.puts "DELETED" }
$stderr.expects(:puts).with('DELETED')
d = DummyStoreModelWithCallback.new name: 'Test'
d.expects(:persisted?).returns(true)
d.expects(:freeze).returns(d)
d.destroy
end
should "remove the model from its own index" do
@gateway.expects(:delete)
.with do |model, options|
assert_equal 'my_custom_index', options[:index]
assert_equal 'my_custom_type', options[:type]
true
end
.returns({'_id' => 'abc'})
d = DummyStoreModel.new name: 'Test'
d.instance_variable_set(:@_index, 'my_custom_index')
d.instance_variable_set(:@_type, 'my_custom_type')
d.expects(:persisted?).returns(true)
d.expects(:freeze).returns(d)
d.destroy
end
end
context "when updating," do
should "update the document with partial attributes" do
subject.expects(:persisted?).returns(true)
subject.expects(:id).returns('abc123').at_least_once
@gateway
.expects(:update)
.with do |id, options|
assert_equal 'abc123', id
assert_equal 'UPDATED', options[:doc][:title]
true
end
.returns({'_id' => 'abc123', 'version' => 2})
assert subject.update title: 'UPDATED'
assert_equal 'UPDATED', subject.title
end
should "allow to update the document with a custom script" do
subject.expects(:persisted?).returns(true)
subject.expects(:id).returns('abc123').at_least_once
@gateway
.expects(:update)
.with do |id, options|
assert_equal 'abc123', id
assert_equal 'EXEC', options[:script]
true
end
.returns({'_id' => 'abc123', 'version' => 2})
assert subject.update( {}, { script: 'EXEC' } )
end
should "pass the options to gateway" do
subject.expects(:persisted?).returns(true)
@gateway
.expects(:update)
.with do |object, options|
assert_equal 'ABC', options[:routing]
true
end
.returns({'_id' => 'abc123'})
assert subject.update( { title: 'UPDATED' }, { routing: 'ABC' } )
end
should "return the response" do
subject.expects(:persisted?).returns(true)
@gateway
.expects(:update)
.returns('FOOBAR')
assert_equal 'FOOBAR', subject.update
end
should "execute the callbacks" do
@gateway.expects(:update).returns({'_id' => 'abc'})
DummyStoreModelWithCallback = Class.new(DummyStoreModel)
DummyStoreModelWithCallback.after_update { $stderr.puts "UPDATED" }
$stderr.expects(:puts).with('UPDATED')
d = DummyStoreModelWithCallback.new name: 'Test'
d.expects(:persisted?).returns(true)
d.update name: 'Update'
end
should "update the model in its own index" do
@gateway.expects(:update)
.with do |model, options|
assert_equal 'my_custom_index', options[:index]
assert_equal 'my_custom_type', options[:type]
true
end
.returns({'_id' => 'abc'})
d = DummyStoreModel.new name: 'Test'
d.instance_variable_set(:@_index, 'my_custom_index')
d.instance_variable_set(:@_type, 'my_custom_type')
d.expects(:persisted?).returns(true)
d.update name: 'Update'
end
should "set the meta attributes from response" do
@gateway.expects(:update)
.with do |model, options|
assert_equal 'my_custom_index', options[:index]
assert_equal 'my_custom_type', options[:type]
true
end
.returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})
d = DummyStoreModel.new name: 'Test'
d.instance_variable_set(:@_index, 'my_custom_index')
d.instance_variable_set(:@_type, 'my_custom_type')
d.expects(:persisted?).returns(true)
d.update name: 'Update'
assert_equal 'foo', d._index
assert_equal 'bar', d._type
assert_equal '100', d._version
end
end
context "when incrementing," do
should "increment the attribute" do
subject.expects(:persisted?).returns(true)
@gateway
.expects(:update)
.with do |id, options|
assert_equal 'ctx._source.count += 1', options[:script]
true
end
.returns({'_id' => 'abc123', 'version' => 2})
assert subject.increment :count
assert_equal 1, subject.count
end
should "set the meta attributes from response" do
subject.expects(:persisted?).returns(true)
@gateway.expects(:update)
.with do |model, options|
assert_equal 'my_custom_index', options[:index]
assert_equal 'my_custom_type', options[:type]
true
end
.returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})
subject.instance_variable_set(:@_index, 'my_custom_index')
subject.instance_variable_set(:@_type, 'my_custom_type')
subject.increment :count
assert_equal 'foo', subject._index
assert_equal 'bar', subject._type
assert_equal '100', subject._version
end
end
context "when decrement," do
should "decrement the attribute" do
subject.expects(:persisted?).returns(true)
@gateway
.expects(:update)
.with do |id, options|
assert_equal 'ctx._source.count = ctx._source.count - 1', options[:script]
true
end
.returns({'_id' => 'abc123', 'version' => 2})
assert subject.decrement :count
assert_equal -1, subject.count
end
should "set the meta attributes from response" do
subject.expects(:persisted?).returns(true)
@gateway.expects(:update)
.with do |model, options|
assert_equal 'my_custom_index', options[:index]
assert_equal 'my_custom_type', options[:type]
true
end
.returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})
subject.instance_variable_set(:@_index, 'my_custom_index')
subject.instance_variable_set(:@_type, 'my_custom_type')
subject.decrement :count
assert_equal 'foo', subject._index
assert_equal 'bar', subject._type
assert_equal '100', subject._version
end
end
context "when touching," do
should "raise exception when touching not existing attribute" do
subject.expects(:persisted?).returns(true)
assert_raise(ArgumentError) { subject.touch :foobar }
end
should "update updated_at by default" do
subject.expects(:persisted?).returns(true)
now = Time.parse('2014-01-01T00:00:00Z')
Time.expects(:now).returns(now).at_least_once
@gateway
.expects(:update)
.with do |id, options|
assert_equal '2014-01-01T00:00:00Z', options[:doc][:updated_at]
true
end
.returns({'_id' => 'abc123', 'version' => 2})
subject.touch
assert_equal Time.parse('2014-01-01T00:00:00Z'), subject.updated_at
end
should "update a custom attribute by default" do
subject.expects(:persisted?).returns(true)
now = Time.parse('2014-01-01T00:00:00Z')
Time.expects(:now).returns(now).at_least_once
@gateway
.expects(:update)
.with do |id, options|
assert_equal '2014-01-01T00:00:00Z', options[:doc][:created_at]
true
end
.returns({'_id' => 'abc123', 'version' => 2})
subject.touch :created_at
assert_equal Time.parse('2014-01-01T00:00:00Z'), subject.created_at
end
should "execute the callbacks" do
@gateway.expects(:update).returns({'_id' => 'abc'})
DummyStoreModelWithCallback = Class.new(DummyStoreModel)
DummyStoreModelWithCallback.after_touch { $stderr.puts "TOUCHED" }
$stderr.expects(:puts).with('TOUCHED')
d = DummyStoreModelWithCallback.new name: 'Test'
d.expects(:persisted?).returns(true)
d.touch
end
should "set the meta attributes from response" do
subject.expects(:persisted?).returns(true)
@gateway.expects(:update)
.with do |model, options|
assert_equal 'my_custom_index', options[:index]
assert_equal 'my_custom_type', options[:type]
true
end
.returns({'_id' => 'abc', '_index' => 'foo', '_type' => 'bar', '_version' => '100'})
subject.instance_variable_set(:@_index, 'my_custom_index')
subject.instance_variable_set(:@_type, 'my_custom_type')
subject.touch
assert_equal 'foo', subject._index
assert_equal 'bar', subject._type
assert_equal '100', subject._version
end
end
end
end