mongoid 7.6.1 → 7.6.3
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.
- checksums.yaml +4 -4
- data/lib/config/locales/en.yml +26 -0
- data/lib/mongoid/association/depending.rb +8 -4
- data/lib/mongoid/association/nested/many.rb +38 -3
- data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
- data/lib/mongoid/association/referenced/has_many/proxy.rb +105 -4
- data/lib/mongoid/attributes/readonly.rb +33 -3
- data/lib/mongoid/changeable.rb +22 -4
- data/lib/mongoid/config.rb +62 -0
- data/lib/mongoid/contextual/aggregable/memory.rb +8 -2
- data/lib/mongoid/contextual/memory.rb +19 -8
- data/lib/mongoid/contextual/none.rb +51 -0
- data/lib/mongoid/criteria/queryable/mergeable.rb +12 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +124 -10
- data/lib/mongoid/criteria/scopable.rb +7 -1
- data/lib/mongoid/criteria.rb +45 -1
- data/lib/mongoid/document.rb +10 -0
- data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
- data/lib/mongoid/errors.rb +1 -0
- data/lib/mongoid/field_readable.rb +70 -0
- data/lib/mongoid/matchable.rb +6 -1
- data/lib/mongoid/matcher/eq_impl_with_regexp.rb +3 -5
- data/lib/mongoid/matcher/regex.rb +8 -9
- data/lib/mongoid/matcher/regexp_budget.rb +389 -0
- data/lib/mongoid/matcher.rb +1 -0
- data/lib/mongoid/reloadable.rb +1 -0
- data/lib/mongoid/threaded.rb +3 -0
- data/lib/mongoid/version.rb +1 -1
- data/lib/mongoid/warnings.rb +1 -0
- data/spec/integration/app_spec.rb +8 -0
- data/spec/integration/matcher_operator_data/regex.yml +21 -0
- data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
- data/spec/integration/query_operator_guard_spec.rb +94 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +96 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +228 -0
- data/spec/mongoid/attributes/nested_spec.rb +202 -8
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +286 -0
- data/spec/mongoid/attributes/readonly_spec.rb +387 -0
- data/spec/mongoid/changeable_spec.rb +18 -0
- data/spec/mongoid/contextual/aggregable/memory_spec.rb +98 -0
- data/spec/mongoid/contextual/memory_spec.rb +196 -0
- data/spec/mongoid/contextual/none_spec.rb +18 -0
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +29 -0
- data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +200 -0
- data/spec/mongoid/criteria_spec.rb +19 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +576 -0
- data/spec/mongoid/scopable_spec.rb +116 -2
- data/spec/shared/lib/mrss/cluster_config.rb +5 -40
- data/spec/shared/lib/mrss/constraints.rb +7 -46
- data/spec/shared/lib/mrss/docker_runner.rb +10 -21
- data/spec/shared/lib/mrss/eg_config_utils.rb +0 -31
- data/spec/shared/lib/mrss/release/candidate.rb +18 -12
- data/spec/shared/lib/mrss/server_version_registry.rb +1 -5
- data/spec/shared/share/Dockerfile.erb +6 -8
- data/spec/shared/shlib/server.sh +0 -50
- data/spec/shared/shlib/set_env.sh +4 -10
- data/spec/spec_helper.rb +8 -0
- data/spec/support/models/delete_all_account.rb +10 -0
- data/spec/support/models/delete_all_item.rb +9 -0
- data/spec/support/models/delete_all_scoped_item.rb +11 -0
- data/spec/support/models/delete_all_tag.rb +7 -0
- metadata +1817 -1781
|
@@ -83,6 +83,28 @@ describe Mongoid::Attributes::Readonly do
|
|
|
83
83
|
it "persists the third readonly value" do
|
|
84
84
|
expect(person.reload.aliased_timestamp).to eq(Time.at(42))
|
|
85
85
|
end
|
|
86
|
+
|
|
87
|
+
it 'reports the readonly value as a saved change' do
|
|
88
|
+
expect(person.previous_changes['title']).to eq([ nil, 'sir' ])
|
|
89
|
+
expect(person.title_previously_changed?).to be true
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
it 'reports the saved change for an aliased readonly field' do
|
|
93
|
+
expect(person.previous_changes['at']).to eq([ nil, Time.at(42) ])
|
|
94
|
+
expect(person.aliased_timestamp_previously_changed?).to be true
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'does not persist a later change to the readonly field' do
|
|
98
|
+
person.title = 'mr'
|
|
99
|
+
person.score = 1
|
|
100
|
+
person.save!
|
|
101
|
+
|
|
102
|
+
expect(person.previous_changes).not_to have_key('title')
|
|
103
|
+
expect(person.previous_changes).to have_key('score')
|
|
104
|
+
|
|
105
|
+
expect(person.reload.title).to eq('sir')
|
|
106
|
+
expect(person.reload.score).to eq(1)
|
|
107
|
+
end
|
|
86
108
|
end
|
|
87
109
|
|
|
88
110
|
context "when updating an existing readonly field" do
|
|
@@ -233,6 +255,371 @@ describe Mongoid::Attributes::Readonly do
|
|
|
233
255
|
end
|
|
234
256
|
end
|
|
235
257
|
|
|
258
|
+
context 'when modifying a readonly field in place' do
|
|
259
|
+
let(:attributes) do
|
|
260
|
+
%i[map aliases array]
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
let(:person) do
|
|
264
|
+
Person.create!(map: { 'enabled' => false }, aliases: [ 'original' ], array: [ 'original' ], title: 'old')
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
it 'does not persist changes to a readonly hash' do
|
|
268
|
+
person.map['enabled'] = true
|
|
269
|
+
person.save!
|
|
270
|
+
|
|
271
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
it 'does not mark the document as changed' do
|
|
275
|
+
person.map['enabled'] = true
|
|
276
|
+
|
|
277
|
+
expect(person).not_to be_changed
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
it 'does not mark an aliased readonly field as changed' do
|
|
281
|
+
person.array << 'added'
|
|
282
|
+
|
|
283
|
+
expect(person).not_to be_changed
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
it 'allows in-place changes before the document is created' do
|
|
287
|
+
new_person = Person.new(map: { 'enabled' => false })
|
|
288
|
+
new_person.map['enabled'] = true
|
|
289
|
+
new_person.save!
|
|
290
|
+
|
|
291
|
+
expect(new_person.reload.map).to eq('enabled' => true)
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
it 'does not report the ignored change as saved' do
|
|
295
|
+
person.map['enabled'] = true
|
|
296
|
+
person.title = 'new'
|
|
297
|
+
person.save!
|
|
298
|
+
|
|
299
|
+
expect(person.previous_changes).not_to have_key('map')
|
|
300
|
+
expect(person.previous_changes).to have_key('title')
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
it 'does not update timestamps for an ignored change' do
|
|
304
|
+
model = test_model do
|
|
305
|
+
include Mongoid::Timestamps
|
|
306
|
+
|
|
307
|
+
field :settings, type: Hash
|
|
308
|
+
attr_readonly :settings
|
|
309
|
+
end
|
|
310
|
+
document = Timecop.freeze(Time.utc(2020, 1, 1)) do
|
|
311
|
+
model.create!(settings: { 'enabled' => false })
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
document.settings['enabled'] = true
|
|
315
|
+
Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
|
|
316
|
+
|
|
317
|
+
expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
it 'does not persist changes made through attributes' do
|
|
321
|
+
person.map
|
|
322
|
+
person.attributes['map']['enabled'] = true
|
|
323
|
+
person.save!
|
|
324
|
+
|
|
325
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
it 'resets an in-place change to a readonly hash' do
|
|
329
|
+
person.map['enabled'] = true
|
|
330
|
+
person.reset_map!
|
|
331
|
+
|
|
332
|
+
expect(person.map).to eq('enabled' => false)
|
|
333
|
+
expect(person).not_to be_changed
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
context 'when a tracked field becomes readonly' do
|
|
337
|
+
let(:model) do
|
|
338
|
+
test_model do
|
|
339
|
+
include Mongoid::Timestamps
|
|
340
|
+
|
|
341
|
+
field :settings, type: Hash
|
|
342
|
+
field :title
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
let(:document) do
|
|
347
|
+
Timecop.freeze(Time.utc(2020, 1, 1)) do
|
|
348
|
+
model.create!(settings: { 'enabled' => false }, title: 'old')
|
|
349
|
+
end
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
before do
|
|
353
|
+
document.settings['enabled'] = true
|
|
354
|
+
model.attr_readonly :settings
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
it 'does not persist the ignored change' do
|
|
358
|
+
document.save!
|
|
359
|
+
|
|
360
|
+
expect(document.reload.settings).to eq('enabled' => false)
|
|
361
|
+
end
|
|
362
|
+
|
|
363
|
+
it 'does not report the ignored change as dirty' do
|
|
364
|
+
expect(document).not_to be_changed
|
|
365
|
+
expect(document.changes).not_to have_key('settings')
|
|
366
|
+
end
|
|
367
|
+
|
|
368
|
+
it 'does not report the readonly field as changed' do
|
|
369
|
+
expect(document.settings_changed?).to be false
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
it 'does not report the ignored change as saved' do
|
|
373
|
+
document.title = 'new'
|
|
374
|
+
document.save!
|
|
375
|
+
|
|
376
|
+
expect(document.previous_changes).not_to have_key('settings')
|
|
377
|
+
expect(document.settings_previously_changed?).to be false
|
|
378
|
+
expect(document.previous_changes).to have_key('title')
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
it 'does not update timestamps for the ignored change' do
|
|
382
|
+
Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
|
|
383
|
+
|
|
384
|
+
expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
|
|
385
|
+
end
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
it 'does not persist changes to a readonly array' do
|
|
389
|
+
person.aliases << 'added'
|
|
390
|
+
person.save!
|
|
391
|
+
|
|
392
|
+
expect(person.reload.aliases).to eq([ 'original' ])
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
it 'does not persist changes to an aliased readonly field' do
|
|
396
|
+
person.array << 'added'
|
|
397
|
+
person.save!
|
|
398
|
+
|
|
399
|
+
expect(person.reload.array).to eq([ 'original' ])
|
|
400
|
+
end
|
|
401
|
+
|
|
402
|
+
it 'persists changes to other fields' do
|
|
403
|
+
person.map['enabled'] = true
|
|
404
|
+
person.title = 'new'
|
|
405
|
+
person.save!
|
|
406
|
+
|
|
407
|
+
expect(person.reload.map).to eq('enabled' => false)
|
|
408
|
+
expect(person.title).to eq('new')
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
it 'still allows atomic set on a readonly field' do
|
|
412
|
+
person.set('map.enabled' => true)
|
|
413
|
+
|
|
414
|
+
expect(person.reload.map).to eq('enabled' => true)
|
|
415
|
+
end
|
|
416
|
+
|
|
417
|
+
it 'still allows atomic unset on a readonly field' do
|
|
418
|
+
person.unset('map.enabled')
|
|
419
|
+
|
|
420
|
+
expect(person.reload.map).to eq({})
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
context 'when an atomic array update fails' do
|
|
424
|
+
it 'restores the readonly array in memory and in the database' do
|
|
425
|
+
expect do
|
|
426
|
+
person.atomically do
|
|
427
|
+
person.push(aliases: 'added')
|
|
428
|
+
raise 'abort'
|
|
429
|
+
end
|
|
430
|
+
end.to raise_error('abort')
|
|
431
|
+
|
|
432
|
+
expect(Person.collection.find(_id: person.id).first['aliases']).to eq([ 'original' ])
|
|
433
|
+
expect(person.aliases).to eq([ 'original' ])
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
it 'still allows a successful atomic update' do
|
|
437
|
+
person.atomically { person.push(aliases: 'added') }
|
|
438
|
+
|
|
439
|
+
expect(person.reload.aliases).to eq(%w[original added])
|
|
440
|
+
end
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
context 'when an embedded document has a readonly hash' do
|
|
445
|
+
let(:attributes) do
|
|
446
|
+
:title
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
let(:person) do
|
|
450
|
+
Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
around do |example|
|
|
454
|
+
original = Address.readonly_attributes
|
|
455
|
+
Address.attr_readonly :map
|
|
456
|
+
example.run
|
|
457
|
+
ensure
|
|
458
|
+
Address.readonly_attributes = original
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
it 'allows the initial value to be saved' do
|
|
462
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
it 'ignores in-place changes when saving the parent' do
|
|
466
|
+
person.addresses.first.map['enabled'] = true
|
|
467
|
+
person.save!
|
|
468
|
+
|
|
469
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
it 'ignores in-place changes when saving the embedded document' do
|
|
473
|
+
address = person.addresses.first
|
|
474
|
+
address.map['enabled'] = true
|
|
475
|
+
address.save!
|
|
476
|
+
|
|
477
|
+
expect(person.reload.addresses.first.map).to eq('enabled' => false)
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
it 'allows an initial value when adding a new embedded document' do
|
|
481
|
+
person.addresses.build(street: 'Second', map: { 'enabled' => true })
|
|
482
|
+
person.save!
|
|
483
|
+
|
|
484
|
+
expect(person.reload.addresses.last.map).to eq('enabled' => true)
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
it 'reports the initial value as a saved change' do
|
|
488
|
+
address = person.addresses.first
|
|
489
|
+
|
|
490
|
+
expect(address.previous_changes['map']).to eq([ nil, { 'enabled' => false } ])
|
|
491
|
+
expect(address.map_previously_changed?).to be true
|
|
492
|
+
end
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
context 'when a persisted root document is converted with becomes' do
|
|
496
|
+
let(:attributes) do
|
|
497
|
+
:title
|
|
498
|
+
end
|
|
499
|
+
|
|
500
|
+
let(:model) do
|
|
501
|
+
test_model do
|
|
502
|
+
field :settings, type: Hash
|
|
503
|
+
attr_readonly :settings
|
|
504
|
+
end
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
let(:original) do
|
|
508
|
+
model.create!(settings: { 'enabled' => false })
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
it 'does not persist an in-place change to the readonly field of the became root document' do
|
|
512
|
+
became = original.becomes(model)
|
|
513
|
+
became.settings['enabled'] = true
|
|
514
|
+
became.save!
|
|
515
|
+
|
|
516
|
+
expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
|
|
517
|
+
end
|
|
518
|
+
end
|
|
519
|
+
|
|
520
|
+
context 'when a persisted document is converted to another class with becomes' do
|
|
521
|
+
let(:attributes) do
|
|
522
|
+
:title
|
|
523
|
+
end
|
|
524
|
+
|
|
525
|
+
let(:model) do
|
|
526
|
+
test_model do
|
|
527
|
+
field :settings, type: Hash
|
|
528
|
+
attr_readonly :settings
|
|
529
|
+
end
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
let(:subclass) do
|
|
533
|
+
Class.new(model)
|
|
534
|
+
end
|
|
535
|
+
|
|
536
|
+
let(:original) do
|
|
537
|
+
model.create!(settings: { 'enabled' => false })
|
|
538
|
+
end
|
|
539
|
+
|
|
540
|
+
it 'does not persist an in-place change to the readonly field of the became document' do
|
|
541
|
+
became = original.becomes(subclass)
|
|
542
|
+
became.settings['enabled'] = true
|
|
543
|
+
became.save!
|
|
544
|
+
|
|
545
|
+
expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
|
|
546
|
+
end
|
|
547
|
+
end
|
|
548
|
+
|
|
549
|
+
context 'when a persisted document with embedded documents is converted with becomes' do
|
|
550
|
+
let(:attributes) do
|
|
551
|
+
:title
|
|
552
|
+
end
|
|
553
|
+
|
|
554
|
+
let(:person) do
|
|
555
|
+
Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
around do |example|
|
|
559
|
+
original = Address.readonly_attributes
|
|
560
|
+
Address.attr_readonly :map
|
|
561
|
+
example.run
|
|
562
|
+
ensure
|
|
563
|
+
Address.readonly_attributes = original
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
it 'does not persist an in-place change to the readonly field of a became embedded document' do
|
|
567
|
+
became = person.becomes(Person)
|
|
568
|
+
became.addresses.first.map['enabled'] = true
|
|
569
|
+
became.save!
|
|
570
|
+
|
|
571
|
+
stored = Person.collection.find(_id: person.id).first['addresses'].first['map']
|
|
572
|
+
expect(stored).to eq('enabled' => false)
|
|
573
|
+
end
|
|
574
|
+
end
|
|
575
|
+
|
|
576
|
+
context 'when a new document is converted with becomes' do
|
|
577
|
+
let(:attributes) do
|
|
578
|
+
:title
|
|
579
|
+
end
|
|
580
|
+
|
|
581
|
+
let(:model) do
|
|
582
|
+
test_model do
|
|
583
|
+
field :settings, type: Hash
|
|
584
|
+
attr_readonly :settings
|
|
585
|
+
end
|
|
586
|
+
end
|
|
587
|
+
|
|
588
|
+
it 'reports the readonly value as a saved change when the became document is created' do
|
|
589
|
+
became = model.new(settings: { 'enabled' => false }).becomes(model)
|
|
590
|
+
became.save!
|
|
591
|
+
|
|
592
|
+
expect(became.previous_changes['settings']).to eq([ nil, { 'enabled' => false } ])
|
|
593
|
+
expect(model.collection.find(_id: became.id).first['settings']).to eq('enabled' => false)
|
|
594
|
+
end
|
|
595
|
+
end
|
|
596
|
+
|
|
597
|
+
context 'when a field is named like an internal dirty tracking method' do
|
|
598
|
+
let(:attributes) do
|
|
599
|
+
:title
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
let(:model) do
|
|
603
|
+
test_model do
|
|
604
|
+
field :name, type: String
|
|
605
|
+
field :readonly_change_ignored, type: String
|
|
606
|
+
end
|
|
607
|
+
end
|
|
608
|
+
|
|
609
|
+
it 'tracks changes and creates the document' do
|
|
610
|
+
document = model.new(name: 'original', readonly_change_ignored: 'value')
|
|
611
|
+
|
|
612
|
+
expect(document.name_changed?).to be true
|
|
613
|
+
|
|
614
|
+
document.save!
|
|
615
|
+
|
|
616
|
+
expect(document.reload.name).to eq('original')
|
|
617
|
+
expect(document.readonly_change_ignored).to eq('value')
|
|
618
|
+
expect(document.readonly_change_ignored?).to be true
|
|
619
|
+
expect(document).not_to be_changed
|
|
620
|
+
end
|
|
621
|
+
end
|
|
622
|
+
|
|
236
623
|
context 'when a foreign_key is defined as readonly' do
|
|
237
624
|
|
|
238
625
|
let(:attributes) do
|
|
@@ -1235,6 +1235,24 @@ describe Mongoid::Changeable do
|
|
|
1235
1235
|
expect(person.title).to be_nil
|
|
1236
1236
|
end
|
|
1237
1237
|
end
|
|
1238
|
+
|
|
1239
|
+
context 'when a resizable attribute was read but its value has not changed' do
|
|
1240
|
+
let(:person) do
|
|
1241
|
+
Person.create!(map: { 'enabled' => false })
|
|
1242
|
+
end
|
|
1243
|
+
|
|
1244
|
+
it 'keeps the current value object so held references stay attached' do
|
|
1245
|
+
map = person.map
|
|
1246
|
+
person.reset_map!
|
|
1247
|
+
|
|
1248
|
+
expect(person.attributes['map']).to equal(map)
|
|
1249
|
+
|
|
1250
|
+
map['enabled'] = true
|
|
1251
|
+
person.save!
|
|
1252
|
+
|
|
1253
|
+
expect(person.reload.map).to eq('enabled' => true)
|
|
1254
|
+
end
|
|
1255
|
+
end
|
|
1238
1256
|
end
|
|
1239
1257
|
|
|
1240
1258
|
describe "#reset_attribute_to_default!" do
|
|
@@ -4,6 +4,10 @@ require "spec_helper"
|
|
|
4
4
|
|
|
5
5
|
describe Mongoid::Contextual::Aggregable::Memory do
|
|
6
6
|
|
|
7
|
+
let(:context) do
|
|
8
|
+
Mongoid::Contextual::Memory.new(criteria)
|
|
9
|
+
end
|
|
10
|
+
|
|
7
11
|
describe "#aggregates" do
|
|
8
12
|
let(:context) do
|
|
9
13
|
Mongoid::Contextual::Memory.new(criteria)
|
|
@@ -332,4 +336,98 @@ describe Mongoid::Contextual::Aggregable::Memory do
|
|
|
332
336
|
end
|
|
333
337
|
end
|
|
334
338
|
end
|
|
339
|
+
|
|
340
|
+
context "when the field name is a method name" do
|
|
341
|
+
|
|
342
|
+
let!(:depeche) do
|
|
343
|
+
Band.create!(name: "Depeche Mode", likes: 1000)
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
let(:criteria) do
|
|
347
|
+
Band.all.tap do |crit|
|
|
348
|
+
crit.documents = [ depeche ]
|
|
349
|
+
end
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
it "does not call the method for sum" do
|
|
353
|
+
expect(depeche).not_to receive(:delete)
|
|
354
|
+
expect(context.sum(:delete)).to eq(0)
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
it "does not call the method for min" do
|
|
358
|
+
expect(depeche).not_to receive(:delete)
|
|
359
|
+
expect(context.min(:delete)).to be_nil
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
it "does not call the method for max" do
|
|
363
|
+
expect(depeche).not_to receive(:delete)
|
|
364
|
+
expect(context.max(:delete)).to be_nil
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
it "does not call the method for avg" do
|
|
368
|
+
expect(depeche).not_to receive(:delete)
|
|
369
|
+
expect(context.avg(:delete)).to eq(0.0)
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
it "does not call the method for aggregates" do
|
|
373
|
+
expect(depeche).not_to receive(:delete)
|
|
374
|
+
expect(context.aggregates(:delete))
|
|
375
|
+
.to eq("count" => 0, "avg" => 0.0, "max" => nil, "min" => nil, "sum" => 0)
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
it "does not call a private method for avg" do
|
|
379
|
+
expect(depeche).not_to receive(:exit)
|
|
380
|
+
expect(context.avg(:exit)).to eq(0.0)
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
context "when the field is not defined" do
|
|
385
|
+
|
|
386
|
+
let!(:depeche) do
|
|
387
|
+
Band.create!(name: "Depeche Mode", likes: 1000)
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
let(:criteria) do
|
|
391
|
+
Band.all.tap do |crit|
|
|
392
|
+
crit.documents = [ depeche ]
|
|
393
|
+
end
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
it "returns zero for sum" do
|
|
397
|
+
expect(context.sum(:not_a_field)).to eq(0)
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
it "returns nil for min" do
|
|
401
|
+
expect(context.min(:not_a_field)).to be_nil
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
it "returns nil for max" do
|
|
405
|
+
expect(context.max(:not_a_field)).to be_nil
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
it "returns zero for avg" do
|
|
409
|
+
expect(context.avg(:not_a_field)).to eq(0.0)
|
|
410
|
+
end
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
context "when the field is the foreign key of a many to many association" do
|
|
414
|
+
|
|
415
|
+
let!(:preference) do
|
|
416
|
+
Preference.create!(name: "nature")
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
let!(:person) do
|
|
420
|
+
Person.new(preferences: [ preference ])
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
let(:criteria) do
|
|
424
|
+
Person.all.tap do |crit|
|
|
425
|
+
crit.documents = [ person ]
|
|
426
|
+
end
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
it "aggregates the stored ids" do
|
|
430
|
+
expect(context.max(:preference_ids)).to eq([ preference.id ])
|
|
431
|
+
end
|
|
432
|
+
end
|
|
335
433
|
end
|