mongoid_paranoia 0.4.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,887 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Mongoid::Paranoia do
4
- context 'configuring the paranoid_field setting' do
5
- before do
6
- Mongoid::Paranoia.configure do |c|
7
- c.paranoid_field = :myFieldName
8
- end
9
- end
10
-
11
- describe '.configure' do
12
- before do
13
- class ParanoidConfigured
14
- include Mongoid::Document
15
- include Mongoid::Paranoia
16
- end
17
- end
18
-
19
- it 'allows custom setting of the paranoid_field' do
20
- paranoid_configured = ParanoidConfigured.new
21
- expect(paranoid_configured.attribute_names).to include('myFieldName')
22
- end
23
-
24
- after(:each) do
25
- Mongoid::Paranoia.reset
26
- end
27
- end
28
-
29
- describe '.reset' do
30
- before do
31
- Mongoid::Paranoia.reset
32
-
33
- # the configuration gets set at include time
34
- # so you need to reset before defining a new class
35
- class ParanoidConfiguredReset
36
- include Mongoid::Document
37
- include Mongoid::Paranoia
38
- end
39
- end
40
-
41
- it 'restores the paranoid_field to the default setting' do
42
- paranoid_configured = ParanoidConfiguredReset.new
43
- expect(paranoid_configured.attribute_names).to include('deleted_at')
44
- end
45
- end
46
- end
47
-
48
- describe ".scoped" do
49
-
50
- it "returns a scoped criteria" do
51
- expect(ParanoidPost.scoped.selector).to eq({ "deleted_at" => nil })
52
- end
53
- end
54
-
55
- describe ".deleted" do
56
-
57
- context "when called on a root document" do
58
-
59
- let(:post) do
60
- ParanoidPost.create(title: "testing")
61
- end
62
-
63
- before do
64
- post.destroy
65
- end
66
-
67
- let(:deleted) do
68
- ParanoidPost.deleted
69
- end
70
-
71
- it "returns the deleted documents" do
72
- expect(deleted).to eq([ post ])
73
- end
74
- end
75
-
76
- context "when called on an embedded document" do
77
-
78
- let(:person) do
79
- Person.create
80
- end
81
-
82
- let(:phone) do
83
- person.paranoid_phones.create
84
- end
85
-
86
- before do
87
- phone.destroy
88
- person.reload
89
- end
90
-
91
- it "returns the deleted documents" do
92
- expect(person.paranoid_phones.deleted.to_a).to eq([ phone ])
93
- end
94
-
95
- it "returns the correct count" do
96
- expect(person.paranoid_phones.deleted.count).to eq(1)
97
- end
98
- end
99
- end
100
-
101
- describe "#destroy!" do
102
-
103
- context "when the document is a root" do
104
-
105
- let(:post) do
106
- ParanoidPost.create(title: "testing")
107
- end
108
-
109
- before do
110
- post.destroy!
111
- end
112
-
113
- let(:raw) do
114
- ParanoidPost.collection.find(_id: post.id).first
115
- end
116
-
117
- it "hard deletes the document" do
118
- expect(raw).to be_nil
119
- end
120
-
121
- it "executes the before destroy callbacks" do
122
- expect(post.before_destroy_called).to be_truthy
123
- end
124
-
125
- it "executes the after destroy callbacks" do
126
- expect(post.after_destroy_called).to be_truthy
127
- end
128
-
129
- it "executes the before remove callbacks" do
130
- expect(post.before_remove_called).to be_truthy
131
- end
132
-
133
- it "executes the after remove callbacks" do
134
- expect(post.after_remove_called).to be_truthy
135
- end
136
- end
137
-
138
- context "when the document is embedded" do
139
-
140
- let(:person) do
141
- Person.create
142
- end
143
-
144
- let(:phone) do
145
- person.paranoid_phones.create(number: "911")
146
- end
147
-
148
- before do
149
- phone.destroy!
150
- end
151
-
152
- let(:raw) do
153
- Person.collection.find(_id: person.id).first
154
- end
155
-
156
- it "hard deletes the document" do
157
- expect(raw["paranoid_phones"]).to be_empty
158
- end
159
-
160
- it "executes the before destroy callbacks" do
161
- expect(phone.before_destroy_called).to be_truthy
162
- end
163
-
164
- it "executes the after destroy callbacks" do
165
- expect(phone.after_destroy_called).to be_truthy
166
- end
167
- end
168
-
169
- context "when the document has a dependent relation" do
170
-
171
- let(:post) do
172
- ParanoidPost.create(title: "test")
173
- end
174
-
175
- let!(:author) do
176
- post.authors.create(name: "poe")
177
- end
178
-
179
- before do
180
- post.destroy!
181
- end
182
-
183
- it "cascades the dependent option" do
184
- expect {
185
- author.reload
186
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
187
- end
188
- end
189
- end
190
-
191
- describe "#destroy" do
192
-
193
- context "when the document is a root" do
194
-
195
- let(:post) do
196
- ParanoidPost.create(title: "testing")
197
- end
198
-
199
- before do
200
- post.destroy
201
- end
202
-
203
- let(:raw) do
204
- ParanoidPost.collection.find(_id: post.id).first
205
- end
206
-
207
- it "soft deletes the document" do
208
- expect(raw["deleted_at"]).to be_within(1).of(Time.now)
209
- end
210
-
211
- it "is still marked as persisted" do
212
- expect(post.persisted?).to eq(true)
213
- end
214
-
215
- it "does not return the document in a find" do
216
- expect {
217
- ParanoidPost.find(post.id)
218
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
219
- end
220
-
221
- it "executes the before destroy callbacks" do
222
- expect(post.before_destroy_called).to be_truthy
223
- end
224
-
225
- it "executes the after destroy callbacks" do
226
- expect(post.after_destroy_called).to be_truthy
227
- end
228
-
229
- it "does not execute the before remove callbacks" do
230
- expect(post.before_remove_called).to be_falsey
231
- end
232
-
233
- it "does not execute the after remove callbacks" do
234
- expect(post.after_remove_called).to be_falsey
235
- end
236
- end
237
-
238
- context "when the document is embedded" do
239
-
240
- let(:person) do
241
- Person.create
242
- end
243
-
244
- let(:phone) do
245
- person.paranoid_phones.create(number: "911")
246
- end
247
-
248
- before do
249
- phone.destroy
250
- end
251
-
252
- let(:raw) do
253
- Person.collection.find(_id: person.id).first
254
- end
255
-
256
- it "soft deletes the document" do
257
- expect(raw["paranoid_phones"].first["deleted_at"]).to be_within(1).of(Time.now)
258
- end
259
-
260
- it "does not return the document in a find" do
261
- expect {
262
- person.paranoid_phones.find(phone.id)
263
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
264
- end
265
-
266
- it "does not include the document in the relation" do
267
- expect(person.paranoid_phones.scoped).to be_empty
268
- end
269
-
270
- it "executes the before destroy callbacks" do
271
- expect(phone.before_destroy_called).to be_truthy
272
- end
273
-
274
- it "executes the after destroy callbacks" do
275
- expect(phone.after_destroy_called).to be_truthy
276
- end
277
- end
278
-
279
- context "when the document has a dependent: :delete relation" do
280
-
281
- let(:post) do
282
- ParanoidPost.create(title: "test")
283
- end
284
-
285
- let!(:author) do
286
- post.authors.create(name: "poe")
287
- end
288
-
289
- before do
290
- post.destroy
291
- end
292
-
293
- it "cascades the dependent option" do
294
- expect {
295
- author.reload
296
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
297
- end
298
- end
299
-
300
- context "when the document has a dependent: :restrict relation" do
301
-
302
- let(:post) do
303
- ParanoidPost.create(title: "test")
304
- end
305
-
306
- let!(:title) do
307
- post.titles.create
308
- end
309
-
310
- before do
311
- begin
312
- post.destroy
313
- rescue Mongoid::Errors::DeleteRestriction
314
- end
315
- end
316
-
317
- it "does not destroy the document" do
318
- expect(post).not_to be_destroyed
319
- end
320
- end
321
- end
322
-
323
- describe "#destroyed?" do
324
-
325
- context "when the document is a root" do
326
-
327
- let(:post) do
328
- ParanoidPost.create(title: "testing")
329
- end
330
-
331
- context "when the document is hard deleted" do
332
-
333
- before do
334
- post.destroy!
335
- end
336
-
337
- it "returns true" do
338
- expect(post).to be_destroyed
339
- end
340
- end
341
-
342
- context "when the document is soft deleted" do
343
-
344
- before do
345
- post.destroy
346
- end
347
-
348
- it "returns true" do
349
- expect(post).to be_destroyed
350
- end
351
-
352
- it "returns true for deleted scope document" do
353
- expect(ParanoidPost.deleted.last).to be_destroyed
354
- end
355
- end
356
- end
357
-
358
- context "when the document is embedded" do
359
-
360
- let(:person) do
361
- Person.create
362
- end
363
-
364
- let(:phone) do
365
- person.paranoid_phones.create(number: "911")
366
- end
367
-
368
- context "when the document is hard deleted" do
369
-
370
- before do
371
- phone.destroy!
372
- end
373
-
374
- it "returns true" do
375
- expect(phone).to be_destroyed
376
- end
377
- end
378
-
379
- context "when the document is soft deleted" do
380
-
381
- before do
382
- phone.destroy
383
- end
384
-
385
- it "returns true" do
386
- expect(phone).to be_destroyed
387
- end
388
- end
389
- end
390
- end
391
-
392
- describe "#deleted?" do
393
-
394
- context "when the document is a root" do
395
-
396
- let(:post) do
397
- ParanoidPost.create(title: "testing")
398
- end
399
-
400
- context "when the document is hard deleted" do
401
-
402
- before do
403
- post.destroy!
404
- end
405
-
406
- it "returns true" do
407
- expect(post).to be_deleted
408
- end
409
- end
410
-
411
- context "when the document is soft deleted" do
412
-
413
- before do
414
- post.destroy
415
- end
416
-
417
- it "returns true" do
418
- expect(post).to be_deleted
419
- end
420
- end
421
- end
422
-
423
- context "when the document is embedded" do
424
-
425
- let(:person) do
426
- Person.create
427
- end
428
-
429
- let(:phone) do
430
- person.paranoid_phones.create(number: "911")
431
- end
432
-
433
- context "when the document is hard deleted" do
434
-
435
- before do
436
- phone.destroy!
437
- end
438
-
439
- it "returns true" do
440
- expect(phone).to be_deleted
441
- end
442
- end
443
-
444
- context "when the document is soft deleted" do
445
-
446
- before do
447
- phone.destroy
448
- end
449
-
450
- it "returns true" do
451
- expect(phone).to be_deleted
452
- end
453
- end
454
-
455
- context "when the document has non-dependent relation" do
456
- let(:post) do
457
- ParanoidPost.create(title: "test")
458
- end
459
-
460
- let!(:tag) do
461
- post.tags.create(text: "tagie")
462
- end
463
-
464
- before do
465
- post.delete
466
- end
467
-
468
- it "doesn't cascades the dependent option" do
469
- expect(tag.reload).to eq(tag)
470
- end
471
-
472
- end
473
- end
474
- end
475
-
476
- describe "#delete!" do
477
-
478
- context "when the document is a root" do
479
-
480
- let(:post) do
481
- ParanoidPost.create(title: "testing")
482
- end
483
-
484
- before do
485
- post.delete!
486
- end
487
-
488
- let(:raw) do
489
- ParanoidPost.collection.find(_id: post.id).first
490
- end
491
-
492
- it "hard deletes the document" do
493
- expect(raw).to be_nil
494
- end
495
- end
496
-
497
- context "when the document is embedded" do
498
-
499
- let(:person) do
500
- Person.create
501
- end
502
-
503
- let(:phone) do
504
- person.paranoid_phones.create(number: "911")
505
- end
506
-
507
- before do
508
- phone.delete!
509
- end
510
-
511
- let(:raw) do
512
- Person.collection.find(_id: person.id).first
513
- end
514
-
515
- it "hard deletes the document" do
516
- expect(raw["paranoid_phones"]).to be_empty
517
- end
518
- end
519
-
520
- context "when the document has a dependent relation" do
521
-
522
- let(:post) do
523
- ParanoidPost.create(title: "test")
524
- end
525
-
526
- let!(:author) do
527
- post.authors.create(name: "poe")
528
- end
529
-
530
- before do
531
- post.delete!
532
- end
533
-
534
- it "cascades the dependent option" do
535
- expect {
536
- author.reload
537
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
538
- end
539
- end
540
- end
541
-
542
- describe "#delete" do
543
-
544
- context "when the document is a root" do
545
-
546
- let(:post) do
547
- ParanoidPost.create(title: "testing")
548
- end
549
-
550
- before do
551
- post.delete
552
- end
553
-
554
- let(:raw) do
555
- ParanoidPost.collection.find(_id: post.id).first
556
- end
557
-
558
- it "soft deletes the document" do
559
- expect(raw["deleted_at"]).to be_within(1).of(Time.now)
560
- end
561
-
562
- it "does not return the document in a find" do
563
- expect {
564
- ParanoidPost.find(post.id)
565
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
566
- end
567
- end
568
-
569
- context "when the document is embedded" do
570
-
571
- let(:person) do
572
- Person.create
573
- end
574
-
575
- let(:phone) do
576
- person.paranoid_phones.create(number: "911")
577
- end
578
-
579
- before do
580
- phone.delete
581
- end
582
-
583
- let(:raw) do
584
- Person.collection.find(_id: person.id).first
585
- end
586
-
587
- it "soft deletes the document" do
588
- expect(raw["paranoid_phones"].first["deleted_at"]).to be_within(1).of(Time.now)
589
- end
590
-
591
- it "does not return the document in a find" do
592
- expect {
593
- person.paranoid_phones.find(phone.id)
594
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
595
- end
596
-
597
- it "does not include the document in the relation" do
598
- expect(person.paranoid_phones.scoped).to be_empty
599
- end
600
- end
601
-
602
- context "when the document has a dependent relation" do
603
-
604
- let(:post) do
605
- ParanoidPost.create(title: "test")
606
- end
607
-
608
- let!(:author) do
609
- post.authors.create(name: "poe")
610
- end
611
-
612
- before do
613
- post.delete
614
- end
615
-
616
- it "cascades the dependent option" do
617
- expect {
618
- author.reload
619
- }.to raise_error(Mongoid::Errors::DocumentNotFound)
620
- end
621
- end
622
-
623
- context "when the document has a dependent: :restrict relation" do
624
-
625
- let(:post) do
626
- ParanoidPost.create(title: "test")
627
- end
628
-
629
- let!(:title) do
630
- post.titles.create
631
- end
632
-
633
- before do
634
- begin
635
- post.delete
636
- rescue Mongoid::Errors::DeleteRestriction
637
- end
638
- end
639
-
640
- it "does not destroy the document" do
641
- expect(post).not_to be_destroyed
642
- end
643
- end
644
- end
645
-
646
- describe "#remove" do
647
-
648
- let(:post) do
649
- ParanoidPost.new
650
- end
651
-
652
- let!(:time) do
653
- Time.now
654
- end
655
-
656
- before do
657
- post.remove
658
- end
659
-
660
- it "sets the deleted flag" do
661
- expect(post).to be_destroyed
662
- end
663
- end
664
-
665
- describe "#restore" do
666
-
667
- context "when the document is a root" do
668
-
669
- let(:post) do
670
- ParanoidPost.create(title: "testing")
671
- end
672
-
673
- before do
674
- post.delete
675
- post.restore
676
- end
677
-
678
- it "removes the deleted at time" do
679
- expect(post.deleted_at).to be_nil
680
- end
681
-
682
- it "persists the change" do
683
- expect(post.reload.deleted_at).to be_nil
684
- end
685
-
686
- it "marks document again as persisted" do
687
- expect(post.persisted?).to be_truthy
688
- end
689
-
690
- context "will run callback" do
691
-
692
- it "before restore" do
693
- expect(post.before_restore_called).to be_truthy
694
- end
695
-
696
- it "after restore" do
697
- expect(post.after_restore_called).to be_truthy
698
- end
699
-
700
- it "around restore" do
701
- expect(post.around_before_restore_called).to be_truthy
702
- expect(post.around_after_restore_called).to be_truthy
703
- end
704
- end
705
- end
706
-
707
- context "when the document is embedded" do
708
-
709
- let(:person) do
710
- Person.create
711
- end
712
-
713
- let(:phone) do
714
- person.paranoid_phones.create(number: "911")
715
- end
716
-
717
- before do
718
- phone.delete
719
- phone.restore
720
- end
721
-
722
- it "removes the deleted at time" do
723
- expect(phone.deleted_at).to be_nil
724
- end
725
-
726
- it "persists the change" do
727
- expect(person.reload.paranoid_phones.first.deleted_at).to be_nil
728
- end
729
- end
730
- end
731
-
732
- describe "#restore_relations" do
733
-
734
- subject { ParaBase.create }
735
-
736
- let!(:para_has_one) { subject.para_has_one = ParaHasOne.create }
737
- let!(:para_has_many) { 2.times.map { subject.para_has_many.create } }
738
- let!(:para_habtm) { 3.times.map { subject.para_habtm.create } }
739
- let!(:para_belongs_to) { subject.para_belongs_to = ParaBelongsTo.create }
740
- let!(:para_embeds_one) { subject.para_embeds_one = ParaEmbedsOne.new }
741
- let!(:para_embeds_many) { 2.times.map { subject.para_embeds_many.build } }
742
-
743
- let!(:norm_has_one) { subject.norm_has_one = NormHasOne.create }
744
- let!(:norm_has_many) { 2.times.map { subject.norm_has_many.create } }
745
- let!(:norm_habtm) { 3.times.map { subject.norm_habtm.create } }
746
- let!(:norm_belongs_to) { subject.norm_belongs_to = NormBelongsTo.create }
747
- let!(:norm_embeds_one) { subject.norm_embeds_one = NormEmbedsOne.new }
748
- let!(:norm_embeds_many) { 2.times.map { subject.norm_embeds_many.build } }
749
-
750
- let(:prepare) do
751
- subject.destroy
752
- subject.restore
753
- end
754
-
755
- context "restores paranoid associations" do
756
- before { prepare }
757
-
758
- it { expect{ subject.restore_relations}.to change{ ParaHasOne.count }.by(1) }
759
- it { expect{ subject.restore_relations}.to change{ ParaHasMany.count }.by(2) }
760
- it { expect{ subject.restore_relations}.to change{ ParaHabtm.count }.by(3) }
761
- it { expect{ subject.restore_relations}.to change{ ParaBelongsTo.count }.by(1) }
762
- end
763
-
764
- context "does not affect embedded paranoid documents" do
765
- before { prepare }
766
-
767
- it { expect{ subject.restore_relations}.to_not change{ subject.para_embeds_one } }
768
- it { expect{ subject.restore_relations}.to_not change{ subject.para_embeds_many.count } }
769
- end
770
-
771
- context "does not affect non-paranoid documents" do
772
- before { prepare }
773
-
774
- it { expect{ subject.restore_relations}.to_not change{ NormHasOne.count } }
775
- it { expect{ subject.restore_relations}.to_not change{ NormHasMany.count } }
776
- it { expect{ subject.restore_relations}.to_not change{ NormHabtm.count } }
777
- it { expect{ subject.restore_relations}.to_not change{ NormBelongsTo.count } }
778
- it { expect{ subject.restore_relations}.to_not change{ subject.norm_embeds_one } }
779
- it { expect{ subject.restore_relations}.to_not change{ subject.norm_embeds_many.count } }
780
- end
781
-
782
- context "recursion" do
783
-
784
- let!(:para_habtm_norm_has_one) { subject.para_habtm.first.norm_has_one = NormHasOne.create } # not restored
785
- let!(:para_habtm_para_has_one) { subject.para_habtm.first.para_has_one = ParaHasOne.create } # restored
786
- let!(:para_habtm_norm_has_many) { 2.times.map { subject.para_habtm.first.norm_has_many = NormHasMany.create } } # not restored
787
- let!(:para_habtm_para_has_many) { 3.times.map { subject.para_habtm.second.para_has_many = ParaHasMany.create } } # restored
788
-
789
- # Untestable due to infinite recursion condition in #destroy
790
- # let!(:para_habtm_norm_habtm) { 3.times.map { subject.para_habtm.second.norm_habtm.create } } # not restored
791
- # let!(:para_habtm_recursive) { 2.times.map { subject.para_habtm.first.recursive.create } } # restored
792
-
793
- before do
794
- subject.destroy
795
- subject.restore
796
- end
797
-
798
- it { expect{ subject.restore_relations}.to change { ParaHasOne.count }.by(2) }
799
- it { expect{ subject.restore_relations}.to change { ParaHasMany.count }.by(3) }
800
-
801
- # Untestable due to infinite recursion condition in #destroy
802
- # it { expect{ ParaHabtm.unscoped.each(&:restore)}.to change { ParaHabtm.count }.by(5) }
803
-
804
- it { expect{ subject.restore_relations}.to_not change { NormHasOne.count } }
805
- it { expect{ subject.restore_relations}.to_not change { NormHasMany.count } }
806
- it { expect{ subject.restore_relations}.to_not change { NormHabtm.count } }
807
- end
808
- end
809
-
810
- describe ".scoped" do
811
-
812
- let(:scoped) do
813
- ParanoidPost.scoped
814
- end
815
-
816
- it "returns a scoped criteria" do
817
- expect(scoped.selector).to eq({ "deleted_at" => nil })
818
- end
819
- end
820
-
821
- describe "#set" do
822
-
823
- let!(:post) do
824
- ParanoidPost.create
825
- end
826
-
827
- let(:time) do
828
- 20.days.ago
829
- end
830
-
831
- let!(:set) do
832
- post.set(:deleted_at => time)
833
- end
834
-
835
- it "persists the change" do
836
- expect(post.reload.deleted_at).to be_within(1).of(time)
837
- end
838
- end
839
-
840
- describe ".unscoped" do
841
-
842
- let(:unscoped) do
843
- ParanoidPost.unscoped
844
- end
845
-
846
- it "returns an unscoped criteria" do
847
- expect(unscoped.selector).to eq({})
848
- end
849
- end
850
-
851
- describe "#to_param" do
852
-
853
- let(:post) do
854
- ParanoidPost.new(title: "testing")
855
- end
856
-
857
- context "when the document is new" do
858
-
859
- it "still returns nil" do
860
- expect(post.to_param).to be_nil
861
- end
862
- end
863
-
864
- context "when the document is not deleted" do
865
-
866
- before do
867
- post.save
868
- end
869
-
870
- it "returns the id as a string" do
871
- expect(post.to_param).to eq(post.id.to_s)
872
- end
873
- end
874
-
875
- context "when the document is deleted" do
876
-
877
- before do
878
- post.save
879
- post.delete
880
- end
881
-
882
- it "returns the id as a string" do
883
- expect(post.to_param).to eq(post.id.to_s)
884
- end
885
- end
886
- end
887
- end