forked from spark-solutions/paranoia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparanoia_test.rb
1373 lines (1106 loc) · 44 KB
/
paranoia_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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'bundler/setup'
require 'active_record'
require 'minitest/autorun'
require 'paranoia'
test_framework = defined?(MiniTest::Test) ? MiniTest::Test : MiniTest::Unit::TestCase
if ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks=)
ActiveRecord::Base.raise_in_transactional_callbacks = true
end
def connect!
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', database: ':memory:'
end
def setup!
connect!
{
'parent_model_with_counter_cache_columns' => 'related_models_count INTEGER DEFAULT 0',
'parent_models' => 'deleted_at DATETIME',
'paranoid_models' => 'parent_model_id INTEGER, deleted_at DATETIME',
'paranoid_model_with_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER',
'paranoid_model_with_build_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_and_build_id INTEGER, name VARCHAR(32)',
'paranoid_model_with_anthor_class_name_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, paranoid_model_with_has_one_id INTEGER',
'paranoid_model_with_foreign_key_belongs' => 'parent_model_id INTEGER, deleted_at DATETIME, has_one_foreign_key_id INTEGER',
'paranoid_model_with_timestamps' => 'parent_model_id INTEGER, created_at DATETIME, updated_at DATETIME, deleted_at DATETIME',
'not_paranoid_model_with_belongs' => 'parent_model_id INTEGER, paranoid_model_with_has_one_id INTEGER',
'not_paranoid_model_with_belongs_and_assocation_not_soft_destroyed_validator' => 'parent_model_id INTEGER, paranoid_model_with_has_one_id INTEGER',
'paranoid_model_with_has_one_and_builds' => 'parent_model_id INTEGER, color VARCHAR(32), deleted_at DATETIME, has_one_foreign_key_id INTEGER',
'featureful_models' => 'deleted_at DATETIME, name VARCHAR(32)',
'plain_models' => 'deleted_at DATETIME',
'callback_models' => 'deleted_at DATETIME',
'fail_callback_models' => 'deleted_at DATETIME',
'related_models' => 'parent_model_id INTEGER, parent_model_with_counter_cache_column_id INTEGER, deleted_at DATETIME',
'asplode_models' => 'parent_model_id INTEGER, deleted_at DATETIME',
'employers' => 'name VARCHAR(32), deleted_at DATETIME',
'employees' => 'deleted_at DATETIME',
'jobs' => 'employer_id INTEGER NOT NULL, employee_id INTEGER NOT NULL, deleted_at DATETIME',
'custom_column_models' => 'destroyed_at DATETIME',
'custom_sentinel_models' => 'deleted_at DATETIME NOT NULL',
'non_paranoid_models' => 'parent_model_id INTEGER',
'polymorphic_models' => 'parent_id INTEGER, parent_type STRING, deleted_at DATETIME',
'namespaced_paranoid_has_ones' => 'deleted_at DATETIME, paranoid_belongs_tos_id INTEGER',
'namespaced_paranoid_belongs_tos' => 'deleted_at DATETIME, paranoid_has_one_id INTEGER',
'unparanoid_unique_models' => 'name VARCHAR(32), paranoid_with_unparanoids_id INTEGER',
'active_column_models' => 'deleted_at DATETIME, active BOOLEAN',
'active_column_model_with_uniqueness_validations' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN',
'paranoid_model_with_belongs_to_active_column_model_with_has_many_relationships' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN, active_column_model_with_has_many_relationship_id INTEGER',
'active_column_model_with_has_many_relationships' => 'name VARCHAR(32), deleted_at DATETIME, active BOOLEAN',
'without_default_scope_models' => 'deleted_at DATETIME'
}.each do |table_name, columns_as_sql_string|
ActiveRecord::Base.connection.execute "CREATE TABLE #{table_name} (id INTEGER NOT NULL PRIMARY KEY, #{columns_as_sql_string})"
end
end
class WithDifferentConnection < ActiveRecord::Base
establish_connection adapter: 'sqlite3', database: ':memory:'
connection.execute 'CREATE TABLE with_different_connections (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME)'
acts_as_paranoid
end
setup!
class ParanoiaTest < test_framework
def setup
connection = ActiveRecord::Base.connection
cleaner = ->(source) {
ActiveRecord::Base.connection.execute "DELETE FROM #{source}"
}
if ActiveRecord::VERSION::MAJOR < 5
connection.tables.each(&cleaner)
else
connection.data_sources.each(&cleaner)
end
end
def test_plain_model_class_is_not_paranoid
assert_equal false, PlainModel.paranoid?
end
def test_paranoid_model_class_is_paranoid
assert_equal true, ParanoidModel.paranoid?
end
def test_plain_models_are_not_paranoid
assert_equal false, PlainModel.new.paranoid?
end
def test_paranoid_models_are_paranoid
assert_equal true, ParanoidModel.new.paranoid?
end
def test_paranoid_models_to_param
model = ParanoidModel.new
model.save
to_param = model.to_param
model.destroy
assert model.to_param
assert_equal to_param, model.to_param
end
def test_destroy_behavior_for_plain_models
model = PlainModel.new
assert_equal 0, model.class.count
model.save!
assert_equal 1, model.class.count
model.destroy
assert_equal true, model.deleted_at.nil?
assert_equal 0, model.class.count
assert_equal 0, model.class.unscoped.count
end
# Anti-regression test for #81, which would've introduced a bug to break this test.
def test_destroy_behavior_for_plain_models_callbacks
model = CallbackModel.new
model.save
model.remove_called_variables # clear called callback flags
model.destroy
assert_nil model.instance_variable_get(:@update_callback_called)
assert_nil model.instance_variable_get(:@save_callback_called)
assert_nil model.instance_variable_get(:@validate_called)
assert model.instance_variable_get(:@destroy_callback_called)
assert model.instance_variable_get(:@after_destroy_callback_called)
assert model.instance_variable_get(:@after_commit_callback_called)
end
def test_delete_behavior_for_plain_models_callbacks
model = CallbackModel.new
model.save
model.remove_called_variables # clear called callback flags
model.delete
assert_nil model.instance_variable_get(:@update_callback_called)
assert_nil model.instance_variable_get(:@save_callback_called)
assert_nil model.instance_variable_get(:@validate_called)
assert_nil model.instance_variable_get(:@destroy_callback_called)
assert_nil model.instance_variable_get(:@after_destroy_callback_called)
assert_nil model.instance_variable_get(:@after_commit_callback_called)
end
def test_delete_in_transaction_behavior_for_plain_models_callbacks
model = CallbackModel.new
model.save
model.remove_called_variables # clear called callback flags
CallbackModel.transaction do
model.delete
end
assert_nil model.instance_variable_get(:@update_callback_called)
assert_nil model.instance_variable_get(:@save_callback_called)
assert_nil model.instance_variable_get(:@validate_called)
assert_nil model.instance_variable_get(:@destroy_callback_called)
assert_nil model.instance_variable_get(:@after_destroy_callback_called)
assert model.instance_variable_get(:@after_commit_callback_called)
end
def test_destroy_behavior_for_paranoid_models
model = ParanoidModel.new
assert_equal 0, model.class.count
model.save!
assert_equal 1, model.class.count
model.destroy
assert_equal false, model.deleted_at.nil?
assert_equal 0, model.class.count
assert_equal 1, model.class.unscoped.count
end
def test_update_columns_on_paranoia_destroyed
record = ParentModel.create
record.destroy
assert record.update_columns deleted_at: Time.now
end
def test_scoping_behavior_for_paranoid_models
parent1 = ParentModel.create
parent2 = ParentModel.create
p1 = ParanoidModel.create(:parent_model => parent1)
p2 = ParanoidModel.create(:parent_model => parent2)
p1.destroy
p2.destroy
assert_equal 0, parent1.paranoid_models.count
assert_equal 1, parent1.paranoid_models.only_deleted.count
assert_equal 2, ParanoidModel.only_deleted.joins(:parent_model).count
assert_equal 1, parent1.paranoid_models.deleted.count
assert_equal 0, parent1.paranoid_models.without_deleted.count
p3 = ParanoidModel.create(:parent_model => parent1)
assert_equal 2, parent1.paranoid_models.with_deleted.count
assert_equal 1, parent1.paranoid_models.without_deleted.count
assert_equal [p1,p3], parent1.paranoid_models.with_deleted
end
def test_only_deleted_with_joins
c1 = ActiveColumnModelWithHasManyRelationship.create(name: 'Jacky')
c2 = ActiveColumnModelWithHasManyRelationship.create(name: 'Thomas')
p1 = ParanoidModelWithBelongsToActiveColumnModelWithHasManyRelationship.create(name: 'Hello', active_column_model_with_has_many_relationship: c1)
c1.destroy
assert_equal 1, ActiveColumnModelWithHasManyRelationship.count
assert_equal 1, ActiveColumnModelWithHasManyRelationship.only_deleted.count
assert_equal 1, ActiveColumnModelWithHasManyRelationship.only_deleted.joins(:paranoid_model_with_belongs_to_active_column_model_with_has_many_relationships).count
end
def test_destroy_behavior_for_custom_column_models
model = CustomColumnModel.new
assert_equal 0, model.class.count
model.save!
assert_nil model.destroyed_at
assert_equal 1, model.class.count
model.destroy
assert_equal false, model.destroyed_at.nil?
assert model.paranoia_destroyed?
assert_equal 0, model.class.count
assert_equal 1, model.class.unscoped.count
assert_equal 1, model.class.only_deleted.count
assert_equal 1, model.class.deleted.count
end
def test_default_sentinel_value
assert_nil ParanoidModel.paranoia_sentinel_value
end
def test_without_default_scope_option
model = WithoutDefaultScopeModel.create
model.destroy
assert_equal 1, model.class.count
assert_equal 1, model.class.only_deleted.count
assert_equal 0, model.class.where(deleted_at: nil).count
end
def test_active_column_model
model = ActiveColumnModel.new
assert_equal 0, model.class.count
model.save!
assert_nil model.deleted_at
assert_equal true, model.active
assert_equal 1, model.class.count
model.destroy
assert_equal false, model.deleted_at.nil?
assert_nil model.active
assert model.paranoia_destroyed?
assert_equal 0, model.class.count
assert_equal 1, model.class.unscoped.count
assert_equal 1, model.class.only_deleted.count
assert_equal 1, model.class.deleted.count
end
def test_active_column_model_with_uniqueness_validation_only_checks_non_deleted_records
a = ActiveColumnModelWithUniquenessValidation.create!(name: "A")
a.destroy
b = ActiveColumnModelWithUniquenessValidation.new(name: "A")
assert b.valid?
end
def test_active_column_model_with_uniqueness_validation_still_works_on_non_deleted_records
a = ActiveColumnModelWithUniquenessValidation.create!(name: "A")
b = ActiveColumnModelWithUniquenessValidation.new(name: "A")
refute b.valid?
end
def test_sentinel_value_for_custom_sentinel_models
model = CustomSentinelModel.new
assert_equal 0, model.class.count
model.save!
assert_equal DateTime.new(0), model.deleted_at
assert_equal 1, model.class.count
model.destroy
assert DateTime.new(0) != model.deleted_at
assert model.paranoia_destroyed?
assert_equal 0, model.class.count
assert_equal 1, model.class.unscoped.count
assert_equal 1, model.class.only_deleted.count
assert_equal 1, model.class.deleted.count
model.restore
assert_equal DateTime.new(0), model.deleted_at
assert !model.destroyed?
assert_equal 1, model.class.count
assert_equal 1, model.class.unscoped.count
assert_equal 0, model.class.only_deleted.count
assert_equal 0, model.class.deleted.count
end
def test_destroy_behavior_for_featureful_paranoid_models
model = get_featureful_model
assert_equal 0, model.class.count
model.save!
assert_equal 1, model.class.count
model.destroy
assert_equal false, model.deleted_at.nil?
assert_equal 0, model.class.count
assert_equal 1, model.class.unscoped.count
end
def test_destroy_behavior_for_has_one_with_build_and_validation_error
model = ParanoidModelWithHasOneAndBuild.create
model.destroy
end
# Regression test for #24
def test_chaining_for_paranoid_models
scope = FeaturefulModel.where(:name => "foo").only_deleted
assert_equal({'name' => "foo"}, scope.where_values_hash)
end
def test_only_destroyed_scope_for_paranoid_models
model = ParanoidModel.new
model.save
model.destroy
model2 = ParanoidModel.new
model2.save
assert_equal model, ParanoidModel.only_deleted.last
assert_equal false, ParanoidModel.only_deleted.include?(model2)
end
def test_default_scope_for_has_many_relationships
parent = ParentModel.create
assert_equal 0, parent.related_models.count
child = parent.related_models.create
assert_equal 1, parent.related_models.count
child.destroy
assert_equal false, child.deleted_at.nil?
assert_equal 0, parent.related_models.count
assert_equal 1, parent.related_models.unscoped.count
end
def test_default_scope_for_has_many_through_relationships
employer = Employer.create
employee = Employee.create
assert_equal 0, employer.jobs.count
assert_equal 0, employer.employees.count
assert_equal 0, employee.jobs.count
assert_equal 0, employee.employers.count
job = Job.create :employer => employer, :employee => employee
assert_equal 1, employer.jobs.count
assert_equal 1, employer.employees.count
assert_equal 1, employee.jobs.count
assert_equal 1, employee.employers.count
employee2 = Employee.create
job2 = Job.create :employer => employer, :employee => employee2
employee2.destroy
assert_equal 2, employer.jobs.count
assert_equal 1, employer.employees.count
job.destroy
assert_equal 1, employer.jobs.count
assert_equal 0, employer.employees.count
assert_equal 0, employee.jobs.count
assert_equal 0, employee.employers.count
end
def test_delete_behavior_for_callbacks
model = CallbackModel.new
model.save
model.delete
assert_nil model.instance_variable_get(:@destroy_callback_called)
end
def test_destroy_behavior_for_callbacks
model = CallbackModel.new
model.save
model.destroy
assert model.instance_variable_get(:@destroy_callback_called)
end
def test_destroy_on_readonly_record
# Just to demonstrate the AR behaviour
model = NonParanoidModel.create!
model.readonly!
assert_raises ActiveRecord::ReadOnlyRecord do
model.destroy
end
# Mirrors behaviour above
model = ParanoidModel.create!
model.readonly!
assert_raises ActiveRecord::ReadOnlyRecord do
model.destroy
end
end
def test_destroy_on_really_destroyed_record
model = ParanoidModel.create!
model.really_destroy!
assert model.really_destroyed?
assert model.paranoia_destroyed?
model.destroy
assert model.really_destroyed?
assert model.paranoia_destroyed?
end
def test_destroy_on_unsaved_record
# Just to demonstrate the AR behaviour
model = NonParanoidModel.new
model.destroy!
assert model.destroyed?
model.destroy!
assert model.destroyed?
# Mirrors behaviour above
model = ParanoidModel.new
model.destroy!
assert model.paranoia_destroyed?
model.destroy!
assert model.paranoia_destroyed?
end
def test_restore
model = ParanoidModel.new
model.save
id = model.id
model.destroy
assert model.paranoia_destroyed?
model = ParanoidModel.only_deleted.find(id)
model.restore!
model.reload
assert_equal false, model.paranoia_destroyed?
end
def test_restore_on_object_return_self
model = ParanoidModel.create
model.destroy
assert_equal model.class, model.restore.class
end
# Regression test for #92
def test_destroy_twice
model = ParanoidModel.new
model.save
model.destroy
model.destroy
assert_equal 1, ParanoidModel.unscoped.where(id: model.id).count
end
# Regression test for #92
def test_destroy_bang_twice
model = ParanoidModel.new
model.save!
model.destroy!
model.destroy!
assert_equal 1, ParanoidModel.unscoped.where(id: model.id).count
end
def test_destroy_return_value_on_success
model = ParanoidModel.create
return_value = model.destroy
assert_equal(return_value, model)
end
def test_destroy_return_value_on_failure
model = FailCallbackModel.create
return_value = model.destroy
assert_equal(return_value, false)
end
def test_restore_behavior_for_callbacks
model = CallbackModel.new
model.save
id = model.id
model.destroy
assert model.paranoia_destroyed?
model = CallbackModel.only_deleted.find(id)
model.restore!
model.reload
assert model.instance_variable_get(:@restore_callback_called)
end
def test_really_destroy
model = ParanoidModel.new
model.save
model.really_destroy!
refute ParanoidModel.unscoped.exists?(model.id)
end
def test_real_destroy_dependent_destroy
parent = ParentModel.create
child1 = parent.very_related_models.create
child2 = parent.non_paranoid_models.create
child3 = parent.create_non_paranoid_model
parent.really_destroy!
refute RelatedModel.unscoped.exists?(child1.id)
refute NonParanoidModel.unscoped.exists?(child2.id)
refute NonParanoidModel.unscoped.exists?(child3.id)
end
def test_real_destroy_dependent_destroy_after_normal_destroy
parent = ParentModel.create
child = parent.very_related_models.create
parent.destroy
parent.really_destroy!
refute RelatedModel.unscoped.exists?(child.id)
end
def test_real_destroy_dependent_destroy_after_normal_destroy_does_not_delete_other_children
parent_1 = ParentModel.create
child_1 = parent_1.very_related_models.create
parent_2 = ParentModel.create
child_2 = parent_2.very_related_models.create
parent_1.destroy
parent_1.really_destroy!
assert RelatedModel.unscoped.exists?(child_2.id)
end
def test_really_destroy_behavior_for_callbacks
model = CallbackModel.new
model.save
model.really_destroy!
assert model.instance_variable_get(:@real_destroy_callback_called)
end
def test_really_destroy_behavior_for_active_column_model
model = ActiveColumnModel.new
model.save
model.really_destroy!
refute ParanoidModel.unscoped.exists?(model.id)
end
def test_really_delete
model = ParanoidModel.new
model.save
model.really_delete
refute ParanoidModel.unscoped.exists?(model.id)
end
def test_multiple_restore
a = ParanoidModel.new
a.save
a_id = a.id
a.destroy
b = ParanoidModel.new
b.save
b_id = b.id
b.destroy
c = ParanoidModel.new
c.save
c_id = c.id
c.destroy
ParanoidModel.restore([a_id, c_id])
a.reload
b.reload
c.reload
refute a.paranoia_destroyed?
assert b.paranoia_destroyed?
refute c.paranoia_destroyed?
end
def test_restore_with_associations_using_recovery_window
parent = ParentModel.create
first_child = parent.very_related_models.create
second_child = parent.very_related_models.create
parent.destroy
second_child.update(deleted_at: parent.deleted_at + 11.minutes)
parent.restore!(:recursive => true)
assert_equal true, parent.deleted_at.nil?
assert_equal true, first_child.reload.deleted_at.nil?
assert_equal true, second_child.reload.deleted_at.nil?
parent.destroy
second_child.update(deleted_at: parent.deleted_at + 11.minutes)
parent.restore(:recursive => true, :recovery_window => 10.minutes)
assert_equal true, parent.deleted_at.nil?
assert_equal true, first_child.reload.deleted_at.nil?
assert_equal false, second_child.reload.deleted_at.nil?
second_child.restore
parent.destroy
first_child.update(deleted_at: parent.deleted_at - 11.minutes)
second_child.update(deleted_at: parent.deleted_at - 9.minutes)
ParentModel.restore(parent.id, :recursive => true, :recovery_window => 10.minutes)
assert_equal true, parent.reload.deleted_at.nil?
assert_equal false, first_child.reload.deleted_at.nil?
assert_equal true, second_child.reload.deleted_at.nil?
end
def test_restore_with_associations
parent = ParentModel.create
first_child = parent.very_related_models.create
second_child = parent.non_paranoid_models.create
parent.destroy
assert_equal false, parent.deleted_at.nil?
assert_equal false, first_child.reload.deleted_at.nil?
assert_equal true, second_child.destroyed?
parent.restore!
assert_equal true, parent.deleted_at.nil?
assert_equal false, first_child.reload.deleted_at.nil?
assert_equal true, second_child.destroyed?
parent.destroy
parent.restore(:recursive => true)
assert_equal true, parent.deleted_at.nil?
assert_equal true, first_child.reload.deleted_at.nil?
assert_equal true, second_child.destroyed?
parent.destroy
ParentModel.restore(parent.id, :recursive => true)
assert_equal true, parent.reload.deleted_at.nil?
assert_equal true, first_child.reload.deleted_at.nil?
assert_equal true, second_child.destroyed?
end
# regression tests for #118
def test_restore_with_has_one_association
# setup and destroy test objects
hasOne = ParanoidModelWithHasOne.create
belongsTo = ParanoidModelWithBelong.create
anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
foreignKey = ParanoidModelWithForeignKeyBelong.create
notParanoidModel = NotParanoidModelWithBelong.create
hasOne.paranoid_model_with_belong = belongsTo
hasOne.class_name_belong = anthorClassName
hasOne.paranoid_model_with_foreign_key_belong = foreignKey
hasOne.not_paranoid_model_with_belong = notParanoidModel
hasOne.save!
hasOne.destroy
assert_equal false, hasOne.deleted_at.nil?
assert_equal false, belongsTo.deleted_at.nil?
# Does it restore has_one associations?
hasOne.restore(:recursive => true)
hasOne.save!
assert_equal true, hasOne.reload.deleted_at.nil?
assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
assert_equal true, notParanoidModel.destroyed?
assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
end
def test_new_restore_with_has_one_association
# setup and destroy test objects
hasOne = ParanoidModelWithHasOne.create
belongsTo = ParanoidModelWithBelong.create
anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
foreignKey = ParanoidModelWithForeignKeyBelong.create
notParanoidModel = NotParanoidModelWithBelong.create
hasOne.paranoid_model_with_belong = belongsTo
hasOne.class_name_belong = anthorClassName
hasOne.paranoid_model_with_foreign_key_belong = foreignKey
hasOne.not_paranoid_model_with_belong = notParanoidModel
hasOne.save!
hasOne.destroy
assert_equal false, hasOne.deleted_at.nil?
assert_equal false, belongsTo.deleted_at.nil?
# Does it restore has_one associations?
newHasOne = ParanoidModelWithHasOne.with_deleted.find(hasOne.id)
newHasOne.restore(:recursive => true)
newHasOne.save!
assert_equal true, hasOne.reload.deleted_at.nil?
assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
assert_equal true, notParanoidModel.destroyed?
assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
end
def test_model_restore_with_has_one_association
# setup and destroy test objects
hasOne = ParanoidModelWithHasOne.create
belongsTo = ParanoidModelWithBelong.create
anthorClassName = ParanoidModelWithAnthorClassNameBelong.create
foreignKey = ParanoidModelWithForeignKeyBelong.create
notParanoidModel = NotParanoidModelWithBelong.create
hasOne.paranoid_model_with_belong = belongsTo
hasOne.class_name_belong = anthorClassName
hasOne.paranoid_model_with_foreign_key_belong = foreignKey
hasOne.not_paranoid_model_with_belong = notParanoidModel
hasOne.save!
hasOne.destroy
assert_equal false, hasOne.deleted_at.nil?
assert_equal false, belongsTo.deleted_at.nil?
# Does it restore has_one associations?
ParanoidModelWithHasOne.restore(hasOne.id, :recursive => true)
hasOne.save!
assert_equal true, hasOne.reload.deleted_at.nil?
assert_equal true, belongsTo.reload.deleted_at.nil?, "#{belongsTo.deleted_at}"
assert_equal true, notParanoidModel.destroyed?
assert ParanoidModelWithBelong.with_deleted.reload.count != 0, "There should be a record"
assert ParanoidModelWithAnthorClassNameBelong.with_deleted.reload.count != 0, "There should be an other record"
assert ParanoidModelWithForeignKeyBelong.with_deleted.reload.count != 0, "There should be a foreign_key record"
end
def test_restore_with_nil_has_one_association
# setup and destroy test object
hasOne = ParanoidModelWithHasOne.create
hasOne.destroy
assert_equal false, hasOne.reload.deleted_at.nil?
# Does it raise NoMethodException on restore of nil
hasOne.restore(:recursive => true)
assert hasOne.reload.deleted_at.nil?
end
def test_restore_with_module_scoped_has_one_association
# setup and destroy test object
hasOne = Namespaced::ParanoidHasOne.create
hasOne.destroy
assert_equal false, hasOne.reload.deleted_at.nil?
# Does it raise "uninitialized constant ParanoidBelongsTo"
# on restore of ParanoidHasOne?
hasOne.restore(:recursive => true)
assert hasOne.reload.deleted_at.nil?
end
# covers #185
def test_restoring_recursive_has_one_restores_correct_object
hasOnes = 2.times.map { ParanoidModelWithHasOne.create }
belongsTos = 2.times.map { ParanoidModelWithBelong.create }
hasOnes[0].update paranoid_model_with_belong: belongsTos[0]
hasOnes[1].update paranoid_model_with_belong: belongsTos[1]
hasOnes.each(&:destroy)
ParanoidModelWithHasOne.restore(hasOnes[1].id, :recursive => true)
hasOnes.each(&:reload)
belongsTos.each(&:reload)
# without #185, belongsTos[0] will be restored instead of belongsTos[1]
refute_nil hasOnes[0].deleted_at
refute_nil belongsTos[0].deleted_at
assert_nil hasOnes[1].deleted_at
assert_nil belongsTos[1].deleted_at
end
# covers #131
def test_has_one_really_destroy_with_nil
model = ParanoidModelWithHasOne.create
model.really_destroy!
refute ParanoidModelWithBelong.unscoped.exists?(model.id)
end
def test_has_one_really_destroy_with_record
model = ParanoidModelWithHasOne.create { |record| record.build_paranoid_model_with_belong }
model.really_destroy!
refute ParanoidModelWithBelong.unscoped.exists?(model.id)
end
def test_observers_notified
a = ParanoidModelWithObservers.create
a.destroy
a.restore!
assert a.observers_notified.select {|args| args == [:before_restore, a]}
assert a.observers_notified.select {|args| args == [:after_restore, a]}
end
def test_observers_not_notified_if_not_supported
a = ParanoidModelWithObservers.create
a.destroy
a.restore!
# essentially, we're just ensuring that this doesn't crash
end
def test_validates_uniqueness_only_checks_non_deleted_records
a = Employer.create!(name: "A")
a.destroy
b = Employer.new(name: "A")
assert b.valid?
end
def test_validates_uniqueness_still_works_on_non_deleted_records
a = Employer.create!(name: "A")
b = Employer.new(name: "A")
refute b.valid?
end
def test_updated_at_modification_on_destroy
paranoid_model = ParanoidModelWithTimestamp.create(:parent_model => ParentModel.create, :updated_at => 1.day.ago)
assert paranoid_model.updated_at < 10.minutes.ago
paranoid_model.destroy
assert paranoid_model.updated_at > 10.minutes.ago
end
def test_updated_at_modification_on_restore
parent1 = ParentModel.create
pt1 = ParanoidModelWithTimestamp.create(:parent_model => parent1)
ParanoidModelWithTimestamp.record_timestamps = false
pt1.update_columns(created_at: 20.years.ago, updated_at: 10.years.ago, deleted_at: 10.years.ago)
ParanoidModelWithTimestamp.record_timestamps = true
assert pt1.updated_at < 10.minutes.ago
refute pt1.deleted_at.nil?
pt1.restore!
assert pt1.deleted_at.nil?
assert pt1.updated_at > 10.minutes.ago
end
def test_i_am_the_destroyer
expected = %Q{
Sharon: "There should be a method called I_AM_THE_DESTROYER!"
Ryan: "What should this method do?"
Sharon: "It should fix all the spelling errors on the page!"
}
assert_output expected do
ParanoidModel.I_AM_THE_DESTROYER!
end
end
def test_destroy_fails_if_callback_raises_exception
parent = AsplodeModel.create
assert_raises(StandardError) { parent.destroy }
#transaction should be rolled back, so parent NOT deleted
refute parent.destroyed?, 'Parent record was destroyed, even though AR callback threw exception'
end
def test_destroy_fails_if_association_callback_raises_exception
parent = ParentModel.create
children = []
3.times { children << parent.asplode_models.create }
assert_raises(StandardError) { parent.destroy }
#transaction should be rolled back, so parent and children NOT deleted
refute parent.destroyed?, 'Parent record was destroyed, even though AR callback threw exception'
refute children.any?(&:destroyed?), 'Child record was destroyed, even though AR callback threw exception'
end
def test_restore_model_with_different_connection
ActiveRecord::Base.remove_connection # Disconnect the main connection
a = WithDifferentConnection.create
a.destroy!
a.restore!
# This test passes if no exception is raised
ensure
setup! # Reconnect the main connection
end
def test_restore_clear_association_cache_if_associations_present
parent = ParentModel.create
3.times { parent.very_related_models.create }
parent.destroy
assert_equal 0, parent.very_related_models.count
assert_equal 0, parent.very_related_models.size
parent.restore(recursive: true)
assert_equal 3, parent.very_related_models.count
assert_equal 3, parent.very_related_models.size
end
def test_model_without_db_connection
ActiveRecord::Base.remove_connection
NoConnectionModel.class_eval{ acts_as_paranoid }
ensure
setup!
end
def test_restore_recursive_on_polymorphic_has_one_association
parent = ParentModel.create
polymorphic = PolymorphicModel.create(parent: parent)
parent.destroy
assert_equal 0, polymorphic.class.count
parent.restore(recursive: true)
assert_equal 1, polymorphic.class.count
end
# Ensure that we're checking parent_type when restoring
def test_missing_restore_recursive_on_polymorphic_has_one_association
parent = ParentModel.create
polymorphic = PolymorphicModel.create(parent_id: parent.id, parent_type: 'ParanoidModel')
parent.destroy
polymorphic.destroy
assert_equal 0, polymorphic.class.count
parent.restore(recursive: true)
assert_equal 0, polymorphic.class.count
end
def test_counter_cache_column_update_on_destroy#_and_restore_and_really_destroy
parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
related_model = parent_model_with_counter_cache_column.related_models.create
assert_equal 1, parent_model_with_counter_cache_column.reload.related_models_count
related_model.destroy
assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count
end
def test_callbacks_for_counter_cache_column_update_on_destroy
parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
related_model = parent_model_with_counter_cache_column.related_models.create
assert_nil related_model.instance_variable_get(:@after_destroy_callback_called)
assert_nil related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
related_model.destroy
assert related_model.instance_variable_get(:@after_destroy_callback_called)
# assert related_model.instance_variable_get(:@after_commit_on_destroy_callback_called)
end
def test_uniqueness_for_unparanoid_associated
parent_model = ParanoidWithUnparanoids.create
related = parent_model.unparanoid_unique_models.create
# will raise exception if model is not checked for paranoia
related.valid?
end
def test_assocation_not_soft_destroyed_validator
notParanoidModel = NotParanoidModelWithBelongsAndAssocationNotSoftDestroyedValidator.create
parentModel = ParentModel.create
assert notParanoidModel.valid?
notParanoidModel.parent_model = parentModel
assert notParanoidModel.valid?
parentModel.destroy
assert !notParanoidModel.valid?
assert notParanoidModel.errors.full_messages.include? "Parent model has been soft-deleted"
end
# TODO: find a fix for Rails 4.1
if ActiveRecord::VERSION::STRING !~ /\A4\.1/
def test_counter_cache_column_update_on_really_destroy
parent_model_with_counter_cache_column = ParentModelWithCounterCacheColumn.create
related_model = parent_model_with_counter_cache_column.related_models.create
assert_equal 1, parent_model_with_counter_cache_column.reload.related_models_count
related_model.really_destroy!
assert_equal 0, parent_model_with_counter_cache_column.reload.related_models_count