acts_as_paranoid 0.4.1 → 0.4.5

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_core.rb ADDED
@@ -0,0 +1,387 @@
1
+ require 'test_helper'
2
+
3
+ class ParanoidTest < ParanoidBaseTest
4
+ def test_paranoid?
5
+ assert !NotParanoid.paranoid?
6
+ assert_raise(NoMethodError) { NotParanoid.delete_all! }
7
+ assert_raise(NoMethodError) { NotParanoid.first.destroy! }
8
+ assert_raise(NoMethodError) { NotParanoid.with_deleted }
9
+ assert_raise(NoMethodError) { NotParanoid.only_deleted }
10
+
11
+ assert ParanoidTime.paranoid?
12
+ end
13
+
14
+ def test_scope_inclusion_with_time_column_type
15
+ assert ParanoidTime.respond_to?(:deleted_inside_time_window)
16
+ assert ParanoidTime.respond_to?(:deleted_before_time)
17
+ assert ParanoidTime.respond_to?(:deleted_after_time)
18
+
19
+ assert !ParanoidBoolean.respond_to?(:deleted_inside_time_window)
20
+ assert !ParanoidBoolean.respond_to?(:deleted_before_time)
21
+ assert !ParanoidBoolean.respond_to?(:deleted_after_time)
22
+ end
23
+
24
+ def test_fake_removal
25
+ assert_equal 3, ParanoidTime.count
26
+ assert_equal 3, ParanoidBoolean.count
27
+ assert_equal 1, ParanoidString.count
28
+
29
+ ParanoidTime.first.destroy
30
+ ParanoidBoolean.delete_all("name = 'paranoid' OR name = 'really paranoid'")
31
+ ParanoidString.first.destroy
32
+ assert_equal 2, ParanoidTime.count
33
+ assert_equal 1, ParanoidBoolean.count
34
+ assert_equal 0, ParanoidString.count
35
+ assert_equal 1, ParanoidTime.only_deleted.count
36
+ assert_equal 2, ParanoidBoolean.only_deleted.count
37
+ assert_equal 1, ParanoidString.only_deleted.count
38
+ assert_equal 3, ParanoidTime.with_deleted.count
39
+ assert_equal 3, ParanoidBoolean.with_deleted.count
40
+ assert_equal 1, ParanoidString.with_deleted.count
41
+ end
42
+
43
+ def test_real_removal
44
+ ParanoidTime.first.destroy!
45
+ ParanoidBoolean.delete_all!("name = 'extremely paranoid' OR name = 'really paranoid'")
46
+ ParanoidString.first.destroy!
47
+ assert_equal 2, ParanoidTime.count
48
+ assert_equal 1, ParanoidBoolean.count
49
+ assert_equal 0, ParanoidString.count
50
+ assert_equal 2, ParanoidTime.with_deleted.count
51
+ assert_equal 1, ParanoidBoolean.with_deleted.count
52
+ assert_equal 0, ParanoidString.with_deleted.count
53
+ assert_equal 0, ParanoidTime.only_deleted.count
54
+ assert_equal 0, ParanoidBoolean.only_deleted.count
55
+ assert_equal 0, ParanoidString.only_deleted.count
56
+
57
+ ParanoidTime.first.destroy
58
+ ParanoidTime.only_deleted.first.destroy
59
+ assert_equal 0, ParanoidTime.only_deleted.count
60
+
61
+ ParanoidTime.delete_all!
62
+ assert_empty ParanoidTime.all
63
+ assert_empty ParanoidTime.with_deleted.all
64
+ end
65
+
66
+ def test_non_persisted_destroy
67
+ pt = ParanoidTime.new
68
+ assert_nil pt.paranoid_value
69
+ pt.destroy
70
+ assert_not_nil pt.paranoid_value
71
+ end
72
+
73
+ def test_non_persisted_destroy!
74
+ pt = ParanoidTime.new
75
+ assert_nil pt.paranoid_value
76
+ pt.destroy!
77
+ assert_not_nil pt.paranoid_value
78
+ end
79
+
80
+ def test_recovery
81
+ assert_equal 3, ParanoidBoolean.count
82
+ ParanoidBoolean.first.destroy
83
+ assert_equal 2, ParanoidBoolean.count
84
+ ParanoidBoolean.only_deleted.first.recover
85
+ assert_equal 3, ParanoidBoolean.count
86
+
87
+ assert_equal 1, ParanoidString.count
88
+ ParanoidString.first.destroy
89
+ assert_equal 0, ParanoidString.count
90
+ ParanoidString.with_deleted.first.recover
91
+ assert_equal 1, ParanoidString.count
92
+ end
93
+
94
+ def setup_recursive_tests
95
+ @paranoid_time_object = ParanoidTime.first
96
+
97
+ # Create one extra ParanoidHasManyDependant record so that we can validate
98
+ # the correct dependants are recovered.
99
+ ParanoidTime.where('id <> ?', @paranoid_time_object.id).first.paranoid_has_many_dependants.create(:name => "should not be recovered").destroy
100
+
101
+ @paranoid_boolean_count = ParanoidBoolean.count
102
+
103
+ assert_equal 0, ParanoidHasManyDependant.count
104
+ assert_equal 0, ParanoidBelongsDependant.count
105
+ assert_equal 1, NotParanoid.count
106
+
107
+ (1..3).each do |i|
108
+ has_many_object = @paranoid_time_object.paranoid_has_many_dependants.create(:name => "has_many_#{i}")
109
+ has_many_object.create_paranoid_belongs_dependant(:name => "belongs_to_#{i}")
110
+ has_many_object.save
111
+
112
+ paranoid_boolean = @paranoid_time_object.paranoid_booleans.create(:name => "boolean_#{i}")
113
+ paranoid_boolean.create_paranoid_has_one_dependant(:name => "has_one_#{i}")
114
+ paranoid_boolean.save
115
+
116
+ @paranoid_time_object.not_paranoids.create(:name => "not_paranoid_a#{i}")
117
+
118
+ end
119
+
120
+ @paranoid_time_object.create_not_paranoid(:name => "not_paranoid_belongs_to")
121
+ @paranoid_time_object.create_has_one_not_paranoid(:name => "has_one_not_paranoid")
122
+
123
+ assert_equal 3, ParanoidTime.count
124
+ assert_equal 3, ParanoidHasManyDependant.count
125
+ assert_equal 3, ParanoidBelongsDependant.count
126
+ assert_equal @paranoid_boolean_count + 3, ParanoidBoolean.count
127
+ assert_equal 3, ParanoidHasOneDependant.count
128
+ assert_equal 5, NotParanoid.count
129
+ assert_equal 1, HasOneNotParanoid.count
130
+ end
131
+
132
+ def test_recursive_fake_removal
133
+ setup_recursive_tests
134
+
135
+ @paranoid_time_object.destroy
136
+
137
+ assert_equal 2, ParanoidTime.count
138
+ assert_equal 0, ParanoidHasManyDependant.count
139
+ assert_equal 0, ParanoidBelongsDependant.count
140
+ assert_equal @paranoid_boolean_count, ParanoidBoolean.count
141
+ assert_equal 0, ParanoidHasOneDependant.count
142
+ assert_equal 1, NotParanoid.count
143
+ assert_equal 0, HasOneNotParanoid.count
144
+ end
145
+
146
+ def test_recursive_real_removal
147
+ setup_recursive_tests
148
+
149
+ @paranoid_time_object.destroy!
150
+
151
+ assert_equal 0, ParanoidTime.only_deleted.count
152
+ assert_equal 1, ParanoidHasManyDependant.only_deleted.count
153
+ assert_equal 0, ParanoidBelongsDependant.only_deleted.count
154
+ assert_equal 0, ParanoidBoolean.only_deleted.count
155
+ assert_equal 0, ParanoidHasOneDependant.only_deleted.count
156
+ assert_equal 1, NotParanoid.count
157
+ assert_equal 0, HasOneNotParanoid.count
158
+ end
159
+
160
+ def test_recursive_recovery
161
+ setup_recursive_tests
162
+
163
+ @paranoid_time_object.destroy
164
+ @paranoid_time_object.reload
165
+
166
+ @paranoid_time_object.recover(:recursive => true)
167
+
168
+ assert_equal 3, ParanoidTime.count
169
+ assert_equal 3, ParanoidHasManyDependant.count
170
+ assert_equal 3, ParanoidBelongsDependant.count
171
+ assert_equal @paranoid_boolean_count + 3, ParanoidBoolean.count
172
+ assert_equal 3, ParanoidHasOneDependant.count
173
+ assert_equal 1, NotParanoid.count
174
+ assert_equal 0, HasOneNotParanoid.count
175
+ end
176
+
177
+ def test_recursive_recovery_dependant_window
178
+ setup_recursive_tests
179
+
180
+ @paranoid_time_object.destroy
181
+ @paranoid_time_object.reload
182
+
183
+ # Stop the following from recovering:
184
+ # - ParanoidHasManyDependant and its ParanoidBelongsDependant
185
+ # - A single ParanoidBelongsDependant, but not its parent
186
+ dependants = @paranoid_time_object.paranoid_has_many_dependants.with_deleted
187
+ dependants.first.update_attribute(:deleted_at, 2.days.ago)
188
+ ParanoidBelongsDependant.with_deleted.where(:id => dependants.last.paranoid_belongs_dependant_id).first.update_attribute(:deleted_at, 1.hour.ago)
189
+
190
+ @paranoid_time_object.recover(:recursive => true)
191
+
192
+ assert_equal 3, ParanoidTime.count
193
+ assert_equal 2, ParanoidHasManyDependant.count
194
+ assert_equal 1, ParanoidBelongsDependant.count
195
+ assert_equal @paranoid_boolean_count + 3, ParanoidBoolean.count
196
+ assert_equal 3, ParanoidHasOneDependant.count
197
+ assert_equal 1, NotParanoid.count
198
+ assert_equal 0, HasOneNotParanoid.count
199
+ end
200
+
201
+ def test_recursive_recovery_for_belongs_to_polymorphic
202
+ child_1 = ParanoidAndroid.create
203
+ section_1 = ParanoidSection.create(:paranoid_thing => child_1)
204
+
205
+ child_2 = ParanoidHuman.create(:gender => 'male')
206
+ section_2 = ParanoidSection.create(:paranoid_thing => child_2)
207
+
208
+ assert_equal section_1.paranoid_thing, child_1
209
+ assert_equal section_1.paranoid_thing.class, ParanoidAndroid
210
+ assert_equal section_2.paranoid_thing, child_2
211
+ assert_equal section_2.paranoid_thing.class, ParanoidHuman
212
+
213
+ parent = ParanoidTime.create(:name => "paranoid_parent")
214
+ parent.paranoid_sections << section_1
215
+ parent.paranoid_sections << section_2
216
+
217
+ assert_equal 4, ParanoidTime.count
218
+ assert_equal 2, ParanoidSection.count
219
+ assert_equal 1, ParanoidAndroid.count
220
+ assert_equal 1, ParanoidHuman.count
221
+
222
+ parent.destroy
223
+
224
+ assert_equal 3, ParanoidTime.count
225
+ assert_equal 0, ParanoidSection.count
226
+ assert_equal 0, ParanoidAndroid.count
227
+ assert_equal 0, ParanoidHuman.count
228
+
229
+ parent.reload
230
+ parent.recover
231
+
232
+ assert_equal 4, ParanoidTime.count
233
+ assert_equal 2, ParanoidSection.count
234
+ assert_equal 1, ParanoidAndroid.count
235
+ assert_equal 1, ParanoidHuman.count
236
+ end
237
+
238
+ def test_non_recursive_recovery
239
+ setup_recursive_tests
240
+
241
+ @paranoid_time_object.destroy
242
+ @paranoid_time_object.reload
243
+
244
+ @paranoid_time_object.recover(:recursive => false)
245
+
246
+ assert_equal 3, ParanoidTime.count
247
+ assert_equal 0, ParanoidHasManyDependant.count
248
+ assert_equal 0, ParanoidBelongsDependant.count
249
+ assert_equal @paranoid_boolean_count, ParanoidBoolean.count
250
+ assert_equal 0, ParanoidHasOneDependant.count
251
+ assert_equal 1, NotParanoid.count
252
+ assert_equal 0, HasOneNotParanoid.count
253
+ end
254
+
255
+ def test_deleted?
256
+ ParanoidTime.first.destroy
257
+ assert ParanoidTime.with_deleted.first.deleted?
258
+
259
+ ParanoidString.first.destroy
260
+ assert ParanoidString.with_deleted.first.deleted?
261
+ end
262
+
263
+ def test_paranoid_destroy_callbacks
264
+ @paranoid_with_callback = ParanoidWithCallback.first
265
+ ParanoidWithCallback.transaction do
266
+ @paranoid_with_callback.destroy
267
+ end
268
+
269
+ assert @paranoid_with_callback.called_before_destroy
270
+ assert @paranoid_with_callback.called_after_destroy
271
+ assert @paranoid_with_callback.called_after_commit_on_destroy
272
+ end
273
+
274
+ def test_hard_destroy_callbacks
275
+ @paranoid_with_callback = ParanoidWithCallback.first
276
+
277
+ ParanoidWithCallback.transaction do
278
+ @paranoid_with_callback.destroy!
279
+ end
280
+
281
+ assert @paranoid_with_callback.called_before_destroy
282
+ assert @paranoid_with_callback.called_after_destroy
283
+ assert @paranoid_with_callback.called_after_commit_on_destroy
284
+ end
285
+
286
+ def test_recovery_callbacks
287
+ @paranoid_with_callback = ParanoidWithCallback.first
288
+
289
+ ParanoidWithCallback.transaction do
290
+ @paranoid_with_callback.destroy
291
+
292
+ assert_nil @paranoid_with_callback.called_before_recover
293
+ assert_nil @paranoid_with_callback.called_after_recover
294
+
295
+ @paranoid_with_callback.recover
296
+ end
297
+
298
+ assert @paranoid_with_callback.called_before_recover
299
+ assert @paranoid_with_callback.called_after_recover
300
+ end
301
+
302
+ def test_delete_by_multiple_id_is_paranoid
303
+ model_a = ParanoidBelongsDependant.create
304
+ model_b = ParanoidBelongsDependant.create
305
+ ParanoidBelongsDependant.delete([model_a.id, model_b.id])
306
+
307
+ assert_paranoid_deletion(model_a)
308
+ assert_paranoid_deletion(model_b)
309
+ end
310
+
311
+ def test_destroy_by_multiple_id_is_paranoid
312
+ model_a = ParanoidBelongsDependant.create
313
+ model_b = ParanoidBelongsDependant.create
314
+ ParanoidBelongsDependant.destroy([model_a.id, model_b.id])
315
+
316
+ assert_paranoid_deletion(model_a)
317
+ assert_paranoid_deletion(model_b)
318
+ end
319
+
320
+ def test_delete_by_single_id_is_paranoid
321
+ model = ParanoidBelongsDependant.create
322
+ ParanoidBelongsDependant.delete(model.id)
323
+
324
+ assert_paranoid_deletion(model)
325
+ end
326
+
327
+ def test_destroy_by_single_id_is_paranoid
328
+ model = ParanoidBelongsDependant.create
329
+ ParanoidBelongsDependant.destroy(model.id)
330
+
331
+ assert_paranoid_deletion(model)
332
+ end
333
+
334
+ def test_instance_delete_is_paranoid
335
+ model = ParanoidBelongsDependant.create
336
+ model.delete
337
+
338
+ assert_paranoid_deletion(model)
339
+ end
340
+
341
+ def test_instance_destroy_is_paranoid
342
+ model = ParanoidBelongsDependant.create
343
+ model.destroy
344
+
345
+ assert_paranoid_deletion(model)
346
+ end
347
+
348
+ # Test string type columns that don't have a nil value when not deleted (Y/N for example)
349
+ def test_string_type_with_no_nil_value_before_destroy
350
+ ps = ParanoidString.create!(:deleted => 'not dead')
351
+ assert_equal 1, ParanoidString.where(:id => ps).count
352
+ end
353
+
354
+ def test_string_type_with_no_nil_value_after_destroy
355
+ ps = ParanoidString.create!(:deleted => 'not dead')
356
+ ps.destroy
357
+ assert_equal 0, ParanoidString.where(:id => ps).count
358
+ end
359
+
360
+ def test_string_type_with_no_nil_value_before_destroy_with_deleted
361
+ ps = ParanoidString.create!(:deleted => 'not dead')
362
+ assert_equal 1, ParanoidString.with_deleted.where(:id => ps).count
363
+ end
364
+
365
+ def test_string_type_with_no_nil_value_after_destroy_with_deleted
366
+ ps = ParanoidString.create!(:deleted => 'not dead')
367
+ ps.destroy
368
+ assert_equal 1, ParanoidString.with_deleted.where(:id => ps).count
369
+ end
370
+
371
+ def test_string_type_with_no_nil_value_before_destroy_only_deleted
372
+ ps = ParanoidString.create!(:deleted => 'not dead')
373
+ assert_equal 0, ParanoidString.only_deleted.where(:id => ps).count
374
+ end
375
+
376
+ def test_string_type_with_no_nil_value_after_destroy_only_deleted
377
+ ps = ParanoidString.create!(:deleted => 'not dead')
378
+ ps.destroy
379
+ assert_equal 1, ParanoidString.only_deleted.where(:id => ps).count
380
+ end
381
+
382
+ def test_string_type_with_no_nil_value_after_destroyed_twice
383
+ ps = ParanoidString.create!(:deleted => 'not dead')
384
+ 2.times { ps.destroy }
385
+ assert_equal 0, ParanoidString.with_deleted.where(:id => ps).count
386
+ end
387
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class MultipleDefaultScopesTest < ParanoidBaseTest
4
+ def setup
5
+ setup_db
6
+
7
+ # Naturally, the default scope for humans is male. Sexism++
8
+ ParanoidHuman.create! :gender => 'male'
9
+ ParanoidHuman.create! :gender => 'male'
10
+ ParanoidHuman.create! :gender => 'male'
11
+ ParanoidHuman.create! :gender => 'female'
12
+
13
+ assert_equal 3, ParanoidHuman.count
14
+ assert_equal 4, ParanoidHuman.unscoped.count
15
+ end
16
+
17
+ def test_fake_removal_with_multiple_default_scope
18
+ ParanoidHuman.first.destroy
19
+ assert_equal 2, ParanoidHuman.count
20
+ assert_equal 3, ParanoidHuman.with_deleted.count
21
+ assert_equal 1, ParanoidHuman.only_deleted.count
22
+ assert_equal 4, ParanoidHuman.unscoped.count
23
+
24
+ ParanoidHuman.destroy_all
25
+ assert_equal 0, ParanoidHuman.count
26
+ assert_equal 3, ParanoidHuman.with_deleted.count
27
+ assert_equal 3, ParanoidHuman.with_deleted.count
28
+ assert_equal 4, ParanoidHuman.unscoped.count
29
+ end
30
+
31
+ def test_real_removal_with_multiple_default_scope
32
+ # two-step
33
+ ParanoidHuman.first.destroy
34
+ ParanoidHuman.only_deleted.first.destroy
35
+ assert_equal 2, ParanoidHuman.count
36
+ assert_equal 2, ParanoidHuman.with_deleted.count
37
+ assert_equal 0, ParanoidHuman.only_deleted.count
38
+ assert_equal 3, ParanoidHuman.unscoped.count
39
+
40
+ ParanoidHuman.first.destroy!
41
+ assert_equal 1, ParanoidHuman.count
42
+ assert_equal 1, ParanoidHuman.with_deleted.count
43
+ assert_equal 0, ParanoidHuman.only_deleted.count
44
+ assert_equal 2, ParanoidHuman.unscoped.count
45
+
46
+ ParanoidHuman.delete_all!
47
+ assert_equal 0, ParanoidHuman.count
48
+ assert_equal 0, ParanoidHuman.with_deleted.count
49
+ assert_equal 0, ParanoidHuman.only_deleted.count
50
+ assert_equal 1, ParanoidHuman.unscoped.count
51
+ end
52
+ end