acts_as_paranoid 0.7.3 → 0.8.0

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.
@@ -1,354 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "test_helper"
4
-
5
- class AssociationsTest < ParanoidBaseTest
6
- def test_removal_with_destroy_associations
7
- paranoid_company = ParanoidDestroyCompany.create! name: "ParanoidDestroyCompany #1"
8
- paranoid_company.paranoid_products.create! name: "ParanoidProduct #1"
9
-
10
- assert_equal 1, ParanoidDestroyCompany.count
11
- assert_equal 1, ParanoidProduct.count
12
-
13
- ParanoidDestroyCompany.first.destroy
14
- assert_equal 0, ParanoidDestroyCompany.count
15
- assert_equal 0, ParanoidProduct.count
16
- assert_equal 1, ParanoidDestroyCompany.with_deleted.count
17
- assert_equal 1, ParanoidProduct.with_deleted.count
18
-
19
- ParanoidDestroyCompany.with_deleted.first.destroy
20
- assert_equal 0, ParanoidDestroyCompany.count
21
- assert_equal 0, ParanoidProduct.count
22
- assert_equal 0, ParanoidDestroyCompany.with_deleted.count
23
- assert_equal 0, ParanoidProduct.with_deleted.count
24
- end
25
-
26
- def test_removal_with_delete_all_associations
27
- paranoid_company = ParanoidDeleteCompany.create! name: "ParanoidDestroyCompany #1"
28
- paranoid_company.paranoid_products.create! name: "ParanoidProduct #2"
29
-
30
- assert_equal 1, ParanoidDeleteCompany.count
31
- assert_equal 1, ParanoidProduct.count
32
-
33
- ParanoidDeleteCompany.first.destroy
34
- assert_equal 0, ParanoidDeleteCompany.count
35
- assert_equal 0, ParanoidProduct.count
36
- assert_equal 1, ParanoidDeleteCompany.with_deleted.count
37
- assert_equal 1, ParanoidProduct.with_deleted.count
38
-
39
- ParanoidDeleteCompany.with_deleted.first.destroy
40
- assert_equal 0, ParanoidDeleteCompany.count
41
- assert_equal 0, ParanoidProduct.count
42
- assert_equal 0, ParanoidDeleteCompany.with_deleted.count
43
- assert_equal 0, ParanoidProduct.with_deleted.count
44
- end
45
-
46
- def test_belongs_to_with_scope_option
47
- paranoid_has_many_dependant = ParanoidHasManyDependant.new
48
-
49
- expected_includes_values = ParanoidTime.includes(:not_paranoid).includes_values
50
- includes_values = paranoid_has_many_dependant
51
- .association(:paranoid_time_with_scope).scope.includes_values
52
-
53
- assert_equal expected_includes_values, includes_values
54
-
55
- paranoid_time = ParanoidTime.create(name: "not-hello")
56
- paranoid_has_many_dependant.paranoid_time = paranoid_time
57
- paranoid_has_many_dependant.save!
58
-
59
- assert_nil paranoid_has_many_dependant.paranoid_time_with_scope
60
-
61
- paranoid_time.update(name: "hello")
62
-
63
- paranoid_has_many_dependant.reload
64
-
65
- assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_with_scope
66
-
67
- paranoid_time.destroy
68
-
69
- paranoid_has_many_dependant.reload
70
-
71
- assert_nil paranoid_has_many_dependant.paranoid_time_with_scope
72
- end
73
-
74
- def test_belongs_to_with_scope_and_deleted_option
75
- paranoid_has_many_dependant = ParanoidHasManyDependant.new
76
- includes_values = ParanoidTime.includes(:not_paranoid).includes_values
77
-
78
- assert_equal includes_values, paranoid_has_many_dependant
79
- .association(:paranoid_time_with_scope_with_deleted).scope.includes_values
80
-
81
- paranoid_time = ParanoidTime.create(name: "not-hello")
82
- paranoid_has_many_dependant.paranoid_time = paranoid_time
83
- paranoid_has_many_dependant.save!
84
-
85
- assert_nil paranoid_has_many_dependant.paranoid_time_with_scope_with_deleted
86
-
87
- paranoid_time.update(name: "hello")
88
- paranoid_has_many_dependant.reload
89
-
90
- assert_equal paranoid_time, paranoid_has_many_dependant
91
- .paranoid_time_with_scope_with_deleted
92
-
93
- paranoid_time.destroy
94
- paranoid_has_many_dependant.reload
95
-
96
- assert_equal paranoid_time, paranoid_has_many_dependant
97
- .paranoid_time_with_scope_with_deleted
98
- end
99
-
100
- def test_belongs_to_with_deleted
101
- paranoid_time = ParanoidTime.first
102
- paranoid_has_many_dependant = paranoid_time.paranoid_has_many_dependants
103
- .create(name: "dependant!")
104
-
105
- assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time
106
- assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_with_deleted
107
-
108
- paranoid_time.destroy
109
- paranoid_has_many_dependant.reload
110
-
111
- assert_nil paranoid_has_many_dependant.paranoid_time
112
- assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_with_deleted
113
- end
114
-
115
- def test_belongs_to_polymorphic_with_deleted
116
- paranoid_time = ParanoidTime.first
117
- paranoid_has_many_dependant = ParanoidHasManyDependant
118
- .create!(name: "dependant!", paranoid_time_polymorphic_with_deleted: paranoid_time)
119
-
120
- assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time
121
- assert_equal paranoid_time, paranoid_has_many_dependant
122
- .paranoid_time_polymorphic_with_deleted
123
-
124
- paranoid_time.destroy
125
-
126
- assert_nil paranoid_has_many_dependant.reload.paranoid_time
127
- assert_equal paranoid_time, paranoid_has_many_dependant
128
- .reload.paranoid_time_polymorphic_with_deleted
129
- end
130
-
131
- def test_belongs_to_nil_polymorphic_with_deleted
132
- paranoid_time = ParanoidTime.first
133
- paranoid_has_many_dependant =
134
- ParanoidHasManyDependant.create!(name: "dependant!",
135
- paranoid_time_polymorphic_with_deleted: nil)
136
-
137
- assert_nil paranoid_has_many_dependant.paranoid_time
138
- assert_nil paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted
139
-
140
- paranoid_time.destroy
141
-
142
- assert_nil paranoid_has_many_dependant.reload.paranoid_time
143
- assert_nil paranoid_has_many_dependant.reload.paranoid_time_polymorphic_with_deleted
144
- end
145
-
146
- def test_belongs_to_options
147
- paranoid_time = ParanoidHasManyDependant.reflections
148
- .with_indifferent_access[:paranoid_time]
149
- assert_equal :belongs_to, paranoid_time.macro
150
- assert_nil paranoid_time.options[:with_deleted]
151
- end
152
-
153
- def test_belongs_to_with_deleted_options
154
- paranoid_time_with_deleted =
155
- ParanoidHasManyDependant.reflections
156
- .with_indifferent_access[:paranoid_time_with_deleted]
157
- assert_equal :belongs_to, paranoid_time_with_deleted.macro
158
- assert paranoid_time_with_deleted.options[:with_deleted]
159
- end
160
-
161
- def test_belongs_to_polymorphic_with_deleted_options
162
- paranoid_time_polymorphic_with_deleted = ParanoidHasManyDependant.reflections
163
- .with_indifferent_access[:paranoid_time_polymorphic_with_deleted]
164
- assert_equal :belongs_to, paranoid_time_polymorphic_with_deleted.macro
165
- assert paranoid_time_polymorphic_with_deleted.options[:with_deleted]
166
- end
167
-
168
- def test_only_find_associated_records_when_finding_with_paranoid_deleted
169
- parent = ParanoidBelongsDependant.create
170
- child = ParanoidHasManyDependant.create
171
- parent.paranoid_has_many_dependants << child
172
-
173
- unrelated_parent = ParanoidBelongsDependant.create
174
- unrelated_child = ParanoidHasManyDependant.create
175
- unrelated_parent.paranoid_has_many_dependants << unrelated_child
176
-
177
- child.destroy
178
- assert_paranoid_deletion(child)
179
-
180
- parent.reload
181
-
182
- assert_equal [], parent.paranoid_has_many_dependants.to_a
183
- assert_equal [child], parent.paranoid_has_many_dependants.with_deleted.to_a
184
- end
185
-
186
- def test_join_with_model_with_deleted
187
- obj = ParanoidHasManyDependant.create(paranoid_time: ParanoidTime.create)
188
- assert_not_nil obj.paranoid_time
189
- assert_not_nil obj.paranoid_time_with_deleted
190
-
191
- obj.paranoid_time.destroy
192
- obj.reload
193
-
194
- assert_nil obj.paranoid_time
195
- assert_not_nil obj.paranoid_time_with_deleted
196
-
197
- # Note that obj is destroyed because of dependent: :destroy in ParanoidTime
198
- assert obj.destroyed?
199
-
200
- assert_empty ParanoidHasManyDependant.with_deleted.joins(:paranoid_time)
201
- assert_equal [obj],
202
- ParanoidHasManyDependant.with_deleted.joins(:paranoid_time_with_deleted)
203
- end
204
-
205
- def test_includes_with_deleted
206
- paranoid_time = ParanoidTime.first
207
- paranoid_time.paranoid_has_many_dependants.create(name: "dependant!")
208
-
209
- paranoid_time.destroy
210
-
211
- ParanoidHasManyDependant.with_deleted
212
- .includes(:paranoid_time_with_deleted).each do |hasmany|
213
- assert_not_nil hasmany.paranoid_time_with_deleted
214
- end
215
- end
216
-
217
- def test_includes_with_deleted_with_polymorphic_parent
218
- not_paranoid_parent = NotParanoidHasManyAsParent.create(name: "not paranoid parent")
219
- paranoid_parent = ParanoidHasManyAsParent.create(name: "paranoid parent")
220
- ParanoidBelongsToPolymorphic.create(name: "belongs_to", parent: not_paranoid_parent)
221
- ParanoidBelongsToPolymorphic.create(name: "belongs_to", parent: paranoid_parent)
222
-
223
- paranoid_parent.destroy
224
-
225
- ParanoidBelongsToPolymorphic.with_deleted.includes(:parent).each do |hasmany|
226
- assert_not_nil hasmany.parent
227
- end
228
- end
229
-
230
- def test_cannot_find_a_paranoid_deleted_many_many_association
231
- left = ParanoidManyManyParentLeft.create
232
- right = ParanoidManyManyParentRight.create
233
- left.paranoid_many_many_parent_rights << right
234
-
235
- left.paranoid_many_many_parent_rights.delete(right)
236
-
237
- left.reload
238
-
239
- assert_equal [], left.paranoid_many_many_children, "Linking objects not deleted"
240
- assert_equal [], left.paranoid_many_many_parent_rights,
241
- "Associated objects not unlinked"
242
- assert_equal right, ParanoidManyManyParentRight.find(right.id),
243
- "Associated object deleted"
244
- end
245
-
246
- def test_cannot_find_a_paranoid_destroyed_many_many_association
247
- left = ParanoidManyManyParentLeft.create
248
- right = ParanoidManyManyParentRight.create
249
- left.paranoid_many_many_parent_rights << right
250
-
251
- left.paranoid_many_many_parent_rights.destroy(right)
252
-
253
- left.reload
254
-
255
- assert_equal [], left.paranoid_many_many_children, "Linking objects not deleted"
256
- assert_equal [], left.paranoid_many_many_parent_rights,
257
- "Associated objects not unlinked"
258
- assert_equal right, ParanoidManyManyParentRight.find(right.id),
259
- "Associated object deleted"
260
- end
261
-
262
- def test_cannot_find_a_has_many_through_object_when_its_linking_object_is_soft_destroyed
263
- left = ParanoidManyManyParentLeft.create
264
- right = ParanoidManyManyParentRight.create
265
- left.paranoid_many_many_parent_rights << right
266
-
267
- child = left.paranoid_many_many_children.first
268
-
269
- child.destroy
270
-
271
- left.reload
272
-
273
- assert_equal [], left.paranoid_many_many_parent_rights, "Associated objects not deleted"
274
- end
275
-
276
- def test_cannot_find_a_paranoid_deleted_model
277
- model = ParanoidBelongsDependant.create
278
- model.destroy
279
-
280
- assert_raises ActiveRecord::RecordNotFound do
281
- ParanoidBelongsDependant.find(model.id)
282
- end
283
- end
284
-
285
- def test_bidirectional_has_many_through_association_clear_is_paranoid
286
- left = ParanoidManyManyParentLeft.create
287
- right = ParanoidManyManyParentRight.create
288
- left.paranoid_many_many_parent_rights << right
289
-
290
- child = left.paranoid_many_many_children.first
291
- assert_equal left, child.paranoid_many_many_parent_left,
292
- "Child's left parent is incorrect"
293
- assert_equal right, child.paranoid_many_many_parent_right,
294
- "Child's right parent is incorrect"
295
-
296
- left.paranoid_many_many_parent_rights.clear
297
-
298
- assert_paranoid_deletion(child)
299
- end
300
-
301
- def test_bidirectional_has_many_through_association_destroy_is_paranoid
302
- left = ParanoidManyManyParentLeft.create
303
- right = ParanoidManyManyParentRight.create
304
- left.paranoid_many_many_parent_rights << right
305
-
306
- child = left.paranoid_many_many_children.first
307
- assert_equal left, child.paranoid_many_many_parent_left,
308
- "Child's left parent is incorrect"
309
- assert_equal right, child.paranoid_many_many_parent_right,
310
- "Child's right parent is incorrect"
311
-
312
- left.paranoid_many_many_parent_rights.destroy(right)
313
-
314
- assert_paranoid_deletion(child)
315
- end
316
-
317
- def test_bidirectional_has_many_through_association_delete_is_paranoid
318
- left = ParanoidManyManyParentLeft.create
319
- right = ParanoidManyManyParentRight.create
320
- left.paranoid_many_many_parent_rights << right
321
-
322
- child = left.paranoid_many_many_children.first
323
- assert_equal left, child.paranoid_many_many_parent_left,
324
- "Child's left parent is incorrect"
325
- assert_equal right, child.paranoid_many_many_parent_right,
326
- "Child's right parent is incorrect"
327
-
328
- left.paranoid_many_many_parent_rights.delete(right)
329
-
330
- assert_paranoid_deletion(child)
331
- end
332
-
333
- def test_belongs_to_on_normal_model_is_paranoid
334
- not_paranoid = HasOneNotParanoid.create
335
- not_paranoid.paranoid_time = ParanoidTime.create
336
-
337
- assert not_paranoid.save
338
- assert_not_nil not_paranoid.paranoid_time
339
- end
340
-
341
- def test_double_belongs_to_with_deleted
342
- not_paranoid = DoubleHasOneNotParanoid.create
343
- not_paranoid.paranoid_time = ParanoidTime.create
344
-
345
- assert not_paranoid.save
346
- assert_not_nil not_paranoid.paranoid_time
347
- end
348
-
349
- def test_mass_assignment_of_paranoid_column_disabled
350
- assert_raises ActiveRecord::RecordNotSaved do
351
- ParanoidTime.create! name: "Foo", deleted_at: Time.now
352
- end
353
- end
354
- end