mongoid 8.1.13 → 8.1.14

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.
@@ -83,6 +83,30 @@ 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.saved_change_to_attribute('title')).to eq([ nil, 'sir' ])
90
+ expect(person.saved_change_to_attribute?(:title)).to be true
91
+ expect(person.title_previously_changed?).to be true
92
+ end
93
+
94
+ it 'reports the saved change for an aliased readonly field' do
95
+ expect(person.previous_changes['at']).to eq([ nil, Time.at(42) ])
96
+ expect(person.saved_change_to_attribute?(:aliased_timestamp)).to be true
97
+ end
98
+
99
+ it 'does not persist a later change to the readonly field' do
100
+ person.title = 'mr'
101
+ person.score = 1
102
+ person.save!
103
+
104
+ expect(person.previous_changes).not_to have_key('title')
105
+ expect(person.previous_changes).to have_key('score')
106
+
107
+ expect(person.reload.title).to eq('sir')
108
+ expect(person.reload.score).to eq(1)
109
+ end
86
110
  end
87
111
 
88
112
  context "when updating an existing readonly field" do
@@ -233,6 +257,371 @@ describe Mongoid::Attributes::Readonly do
233
257
  end
234
258
  end
235
259
 
260
+ context 'when modifying a readonly field in place' do
261
+ let(:attributes) do
262
+ %i[map aliases array]
263
+ end
264
+
265
+ let(:person) do
266
+ Person.create!(map: { 'enabled' => false }, aliases: [ 'original' ], array: [ 'original' ], title: 'old')
267
+ end
268
+
269
+ it 'does not persist changes to a readonly hash' do
270
+ person.map['enabled'] = true
271
+ person.save!
272
+
273
+ expect(person.reload.map).to eq('enabled' => false)
274
+ end
275
+
276
+ it 'does not mark the document as changed' do
277
+ person.map['enabled'] = true
278
+
279
+ expect(person).not_to be_changed
280
+ end
281
+
282
+ it 'does not mark an aliased readonly field as changed' do
283
+ person.array << 'added'
284
+
285
+ expect(person).not_to be_changed
286
+ end
287
+
288
+ it 'allows in-place changes before the document is created' do
289
+ new_person = Person.new(map: { 'enabled' => false })
290
+ new_person.map['enabled'] = true
291
+ new_person.save!
292
+
293
+ expect(new_person.reload.map).to eq('enabled' => true)
294
+ end
295
+
296
+ it 'does not report the ignored change as saved' do
297
+ person.map['enabled'] = true
298
+ person.title = 'new'
299
+ person.save!
300
+
301
+ expect(person.previous_changes).not_to have_key('map')
302
+ expect(person.previous_changes).to have_key('title')
303
+ end
304
+
305
+ it 'does not update timestamps for an ignored change' do
306
+ model = test_model do
307
+ include Mongoid::Timestamps
308
+
309
+ field :settings, type: Hash
310
+ attr_readonly :settings
311
+ end
312
+ document = Timecop.freeze(Time.utc(2020, 1, 1)) do
313
+ model.create!(settings: { 'enabled' => false })
314
+ end
315
+
316
+ document.settings['enabled'] = true
317
+ Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
318
+
319
+ expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
320
+ end
321
+
322
+ it 'does not persist changes made through attributes' do
323
+ person.map
324
+ person.attributes['map']['enabled'] = true
325
+ person.save!
326
+
327
+ expect(person.reload.map).to eq('enabled' => false)
328
+ end
329
+
330
+ it 'resets an in-place change to a readonly hash' do
331
+ person.map['enabled'] = true
332
+ person.reset_map!
333
+
334
+ expect(person.map).to eq('enabled' => false)
335
+ expect(person).not_to be_changed
336
+ end
337
+
338
+ context 'when a tracked field becomes readonly' do
339
+ let(:model) do
340
+ test_model do
341
+ include Mongoid::Timestamps
342
+
343
+ field :settings, type: Hash
344
+ field :title
345
+ end
346
+ end
347
+
348
+ let(:document) do
349
+ Timecop.freeze(Time.utc(2020, 1, 1)) do
350
+ model.create!(settings: { 'enabled' => false }, title: 'old')
351
+ end
352
+ end
353
+
354
+ before do
355
+ document.settings['enabled'] = true
356
+ model.attr_readonly :settings
357
+ end
358
+
359
+ it 'does not persist the ignored change' do
360
+ document.save!
361
+
362
+ expect(document.reload.settings).to eq('enabled' => false)
363
+ end
364
+
365
+ it 'does not report the ignored change as dirty' do
366
+ expect(document).not_to be_changed
367
+ expect(document.changes).not_to have_key('settings')
368
+ end
369
+
370
+ it 'does not report the readonly field as changed' do
371
+ expect(document.settings_changed?).to be false
372
+ end
373
+
374
+ it 'does not report the ignored change as saved' do
375
+ document.title = 'new'
376
+ document.save!
377
+
378
+ expect(document.previous_changes).not_to have_key('settings')
379
+ expect(document.saved_change_to_attribute?(:settings)).to be false
380
+ expect(document.previous_changes).to have_key('title')
381
+ end
382
+
383
+ it 'does not update timestamps for the ignored change' do
384
+ Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
385
+
386
+ expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
387
+ end
388
+ end
389
+
390
+ it 'does not persist changes to a readonly array' do
391
+ person.aliases << 'added'
392
+ person.save!
393
+
394
+ expect(person.reload.aliases).to eq([ 'original' ])
395
+ end
396
+
397
+ it 'does not persist changes to an aliased readonly field' do
398
+ person.array << 'added'
399
+ person.save!
400
+
401
+ expect(person.reload.array).to eq([ 'original' ])
402
+ end
403
+
404
+ it 'persists changes to other fields' do
405
+ person.map['enabled'] = true
406
+ person.title = 'new'
407
+ person.save!
408
+
409
+ expect(person.reload.map).to eq('enabled' => false)
410
+ expect(person.title).to eq('new')
411
+ end
412
+
413
+ it 'still allows atomic set on a readonly field' do
414
+ person.set('map.enabled' => true)
415
+
416
+ expect(person.reload.map).to eq('enabled' => true)
417
+ end
418
+
419
+ it 'still allows atomic unset on a readonly field' do
420
+ person.unset('map.enabled')
421
+
422
+ expect(person.reload.map).to eq({})
423
+ end
424
+
425
+ context 'when an atomic array update fails' do
426
+ it 'restores the readonly array in memory and in the database' do
427
+ expect do
428
+ person.atomically do
429
+ person.push(aliases: 'added')
430
+ raise 'abort'
431
+ end
432
+ end.to raise_error('abort')
433
+
434
+ expect(Person.collection.find(_id: person.id).first['aliases']).to eq([ 'original' ])
435
+ expect(person.aliases).to eq([ 'original' ])
436
+ end
437
+
438
+ it 'still allows a successful atomic update' do
439
+ person.atomically { person.push(aliases: 'added') }
440
+
441
+ expect(person.reload.aliases).to eq(%w[original added])
442
+ end
443
+ end
444
+ end
445
+
446
+ context 'when an embedded document has a readonly hash' do
447
+ let(:attributes) do
448
+ :title
449
+ end
450
+
451
+ let(:person) do
452
+ Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
453
+ end
454
+
455
+ around do |example|
456
+ original = Address.readonly_attributes
457
+ Address.attr_readonly :map
458
+ example.run
459
+ ensure
460
+ Address.readonly_attributes = original
461
+ end
462
+
463
+ it 'allows the initial value to be saved' do
464
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
465
+ end
466
+
467
+ it 'ignores in-place changes when saving the parent' do
468
+ person.addresses.first.map['enabled'] = true
469
+ person.save!
470
+
471
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
472
+ end
473
+
474
+ it 'ignores in-place changes when saving the embedded document' do
475
+ address = person.addresses.first
476
+ address.map['enabled'] = true
477
+ address.save!
478
+
479
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
480
+ end
481
+
482
+ it 'allows an initial value when adding a new embedded document' do
483
+ person.addresses.build(street: 'Second', map: { 'enabled' => true })
484
+ person.save!
485
+
486
+ expect(person.reload.addresses.last.map).to eq('enabled' => true)
487
+ end
488
+
489
+ it 'reports the initial value as a saved change' do
490
+ address = person.addresses.first
491
+
492
+ expect(address.previous_changes['map']).to eq([ nil, { 'enabled' => false } ])
493
+ expect(address.saved_change_to_attribute?(:map)).to be true
494
+ end
495
+ end
496
+
497
+ context 'when a persisted root document is converted with becomes' do
498
+ let(:attributes) do
499
+ :title
500
+ end
501
+
502
+ let(:model) do
503
+ test_model do
504
+ field :settings, type: Hash
505
+ attr_readonly :settings
506
+ end
507
+ end
508
+
509
+ let(:original) do
510
+ model.create!(settings: { 'enabled' => false })
511
+ end
512
+
513
+ it 'does not persist an in-place change to the readonly field of the became root document' do
514
+ became = original.becomes(model)
515
+ became.settings['enabled'] = true
516
+ became.save!
517
+
518
+ expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
519
+ end
520
+ end
521
+
522
+ context 'when a persisted document is converted to another class with becomes' do
523
+ let(:attributes) do
524
+ :title
525
+ end
526
+
527
+ let(:model) do
528
+ test_model do
529
+ field :settings, type: Hash
530
+ attr_readonly :settings
531
+ end
532
+ end
533
+
534
+ let(:subclass) do
535
+ Class.new(model)
536
+ end
537
+
538
+ let(:original) do
539
+ model.create!(settings: { 'enabled' => false })
540
+ end
541
+
542
+ it 'does not persist an in-place change to the readonly field of the became document' do
543
+ became = original.becomes(subclass)
544
+ became.settings['enabled'] = true
545
+ became.save!
546
+
547
+ expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
548
+ end
549
+ end
550
+
551
+ context 'when a persisted document with embedded documents is converted with becomes' do
552
+ let(:attributes) do
553
+ :title
554
+ end
555
+
556
+ let(:person) do
557
+ Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
558
+ end
559
+
560
+ around do |example|
561
+ original = Address.readonly_attributes
562
+ Address.attr_readonly :map
563
+ example.run
564
+ ensure
565
+ Address.readonly_attributes = original
566
+ end
567
+
568
+ it 'does not persist an in-place change to the readonly field of a became embedded document' do
569
+ became = person.becomes(Person)
570
+ became.addresses.first.map['enabled'] = true
571
+ became.save!
572
+
573
+ stored = Person.collection.find(_id: person.id).first['addresses'].first['map']
574
+ expect(stored).to eq('enabled' => false)
575
+ end
576
+ end
577
+
578
+ context 'when a new document is converted with becomes' do
579
+ let(:attributes) do
580
+ :title
581
+ end
582
+
583
+ let(:model) do
584
+ test_model do
585
+ field :settings, type: Hash
586
+ attr_readonly :settings
587
+ end
588
+ end
589
+
590
+ it 'reports the readonly value as a saved change when the became document is created' do
591
+ became = model.new(settings: { 'enabled' => false }).becomes(model)
592
+ became.save!
593
+
594
+ expect(became.previous_changes['settings']).to eq([ nil, { 'enabled' => false } ])
595
+ expect(model.collection.find(_id: became.id).first['settings']).to eq('enabled' => false)
596
+ end
597
+ end
598
+
599
+ context 'when a field is named like an internal dirty tracking method' do
600
+ let(:attributes) do
601
+ :title
602
+ end
603
+
604
+ let(:model) do
605
+ test_model do
606
+ field :name, type: String
607
+ field :readonly_change_ignored, type: String
608
+ end
609
+ end
610
+
611
+ it 'tracks changes and creates the document' do
612
+ document = model.new(name: 'original', readonly_change_ignored: 'value')
613
+
614
+ expect(document.name_changed?).to be true
615
+
616
+ document.save!
617
+
618
+ expect(document.reload.name).to eq('original')
619
+ expect(document.readonly_change_ignored).to eq('value')
620
+ expect(document.readonly_change_ignored?).to be true
621
+ expect(document).not_to be_changed
622
+ end
623
+ end
624
+
236
625
  context 'when a foreign_key is defined as readonly' do
237
626
 
238
627
  let(:attributes) do
@@ -1506,6 +1506,24 @@ describe Mongoid::Changeable do
1506
1506
  expect(person.title).to be_nil
1507
1507
  end
1508
1508
  end
1509
+
1510
+ context 'when a resizable attribute was read but its value has not changed' do
1511
+ let(:person) do
1512
+ Person.create!(map: { 'enabled' => false })
1513
+ end
1514
+
1515
+ it 'keeps the current value object so held references stay attached' do
1516
+ map = person.map
1517
+ person.reset_map!
1518
+
1519
+ expect(person.attributes['map']).to equal(map)
1520
+
1521
+ map['enabled'] = true
1522
+ person.save!
1523
+
1524
+ expect(person.reload.map).to eq('enabled' => true)
1525
+ end
1526
+ end
1509
1527
  end
1510
1528
 
1511
1529
  describe "#reset_attribute_to_default!" do
@@ -306,6 +306,13 @@ describe Mongoid::Config do
306
306
  end
307
307
  end
308
308
 
309
+ # Config.reset does not invoke the option's on_change callback, so use the
310
+ # setter to restore any global state the callback modifies (e.g. the BSON
311
+ # registry for allow_bson5_decimal128).
312
+ after do
313
+ Mongoid.send("#{option}=", default)
314
+ end
315
+
309
316
  context 'when the value is false' do
310
317
 
311
318
  let(:conf) do
@@ -468,6 +468,31 @@ describe Mongoid::Contextual::Memory do
468
468
  }.to raise_exception(Mongoid::Errors::InMemoryCollationNotSupported)
469
469
  end
470
470
  end
471
+
472
+ context "when the field name is a method name" do
473
+ let!(:person) do
474
+ Person.create!(ssn: "secret-ssn")
475
+ end
476
+
477
+ let!(:address) do
478
+ person.addresses.create!(street: "hobrecht")
479
+ end
480
+
481
+ let(:criteria) do
482
+ Address.all.tap do |crit|
483
+ crit.documents = [ address ]
484
+ end
485
+ end
486
+
487
+ let(:context) do
488
+ described_class.new(criteria)
489
+ end
490
+
491
+ it "does not call the method" do
492
+ expect(address).not_to receive(:destroy)
493
+ expect(context.distinct(:destroy)).to eq([ nil ])
494
+ end
495
+ end
471
496
  end
472
497
 
473
498
  context "when legacy_pluck_distinct is false" do
@@ -53,10 +53,17 @@ describe Mongoid::Contextual::None do
53
53
  end
54
54
  end
55
55
 
56
- describe "#pluck" do
56
+ describe '#destroy' do
57
+ it 'returns zero and destroys nothing' do
58
+ expect(context.destroy).to eq(0)
59
+ expect(context.destroy_all).to eq(0)
60
+ expect(Band.where(name: 'Depeche Mode').count).to eq(1)
61
+ end
62
+ end
57
63
 
58
- context "when plucking one field" do
59
- it "returns an empty array" do
64
+ describe '#pluck' do
65
+ context 'when plucking one field' do
66
+ it 'returns an empty array' do
60
67
  expect(context.pluck(:id)).to eq([])
61
68
  end
62
69
  end
@@ -80,8 +87,18 @@ describe Mongoid::Contextual::None do
80
87
  end
81
88
  end
82
89
 
83
- describe "#first" do
84
- it "returns nil" do
90
+ describe '#update' do
91
+ it 'returns false without attributes and nil with them, updating nothing' do
92
+ expect(context.update).to be(false)
93
+ expect(context.update(name: 'The Cure')).to be_nil
94
+ expect(context.update_all).to be(false)
95
+ expect(context.update_all(name: 'The Cure')).to be_nil
96
+ expect(Band.where(name: 'Depeche Mode').first.name).to eq('Depeche Mode')
97
+ end
98
+ end
99
+
100
+ describe '#first' do
101
+ it 'returns nil' do
85
102
  expect(context.first).to be_nil
86
103
  end
87
104
 
@@ -1507,6 +1507,23 @@ describe Mongoid::Criteria do
1507
1507
  it "returns the same criteria" do
1508
1508
  expect(merged).to equal(criteria)
1509
1509
  end
1510
+
1511
+ context 'when merging a none criteria into a criteria with a cached context' do
1512
+ let(:criteria) { Band.where(name: 'Depeche Mode') }
1513
+ let(:mergeable) { Band.none }
1514
+
1515
+ before do
1516
+ Band.create!(name: 'Depeche Mode')
1517
+ end
1518
+
1519
+ it 'does not use the cached context' do
1520
+ expect(criteria.count).to eq(1)
1521
+
1522
+ merged
1523
+
1524
+ expect(criteria.count).to eq(0)
1525
+ end
1526
+ end
1510
1527
  end
1511
1528
 
1512
1529
  describe "#min" do
@@ -817,6 +817,20 @@ describe Mongoid::Findable do
817
817
  expect(criteria.size).to eq(0)
818
818
  end
819
819
  end
820
+
821
+ context 'when none is called on a criteria with a cached context' do
822
+ let(:criteria) do
823
+ Band.where(name: 'Depeche Mode')
824
+ end
825
+
826
+ it 'does not use the cached context' do
827
+ expect(criteria.count).to eq(1)
828
+
829
+ criteria.none
830
+
831
+ expect(criteria.count).to eq(0)
832
+ end
833
+ end
820
834
  end
821
835
 
822
836
  describe ".pluck" do
@@ -32,6 +32,12 @@ describe Mongoid::Matcher::RegexpBudget do
32
32
  # exercising one has to hand .open something to bound.
33
33
  let(:regexp_selector) { { 'name' => /\Aabc\z/ } }
34
34
 
35
+ describe 'REGEXP_BUDGET_KEY' do
36
+ it 'is namespaced so a foreign thread-local cannot collide with it' do
37
+ expect(Mongoid::Threaded::REGEXP_BUDGET_KEY).to start_with('[mongoid]:')
38
+ end
39
+ end
40
+
35
41
  describe '.open' do
36
42
  context 'when no limit is configured' do
37
43
  config_override :in_memory_regexp_time_limit, nil