This repository has been archived by the owner on Jun 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
/
test_associations.rb
215 lines (160 loc) · 8.29 KB
/
test_associations.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
require 'test_helper'
class AssociationsTest < ParanoidBaseTest
def test_removal_with_associations
# This test shows that the current implementation doesn't handle
# assciation deletion correctly (when hard deleting via parent-object)
paranoid_company_1 = ParanoidDestroyCompany.create! :name => "ParanoidDestroyCompany #1"
paranoid_company_2 = ParanoidDeleteCompany.create! :name => "ParanoidDestroyCompany #1"
paranoid_company_1.paranoid_products.create! :name => "ParanoidProduct #1"
paranoid_company_2.paranoid_products.create! :name => "ParanoidProduct #2"
assert_equal 1, ParanoidDestroyCompany.count
assert_equal 1, ParanoidDeleteCompany.count
assert_equal 2, ParanoidProduct.count
ParanoidDestroyCompany.first.destroy
assert_equal 0, ParanoidDestroyCompany.count
assert_equal 1, ParanoidProduct.count
assert_equal 1, ParanoidDestroyCompany.with_deleted.count
assert_equal 2, ParanoidProduct.with_deleted.count
ParanoidDestroyCompany.with_deleted.first.destroy!
assert_equal 0, ParanoidDestroyCompany.count
assert_equal 1, ParanoidProduct.count
assert_equal 0, ParanoidDestroyCompany.with_deleted.count
assert_equal 1, ParanoidProduct.with_deleted.count
ParanoidDeleteCompany.with_deleted.first.destroy!
assert_equal 0, ParanoidDeleteCompany.count
assert_equal 0, ParanoidProduct.count
assert_equal 0, ParanoidDeleteCompany.with_deleted.count
assert_equal 0, ParanoidProduct.with_deleted.count
end
def test_belongs_to_with_deleted
paranoid_time = ParanoidTime.first
paranoid_has_many_dependant = paranoid_time.paranoid_has_many_dependants.create(:name => 'dependant!')
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_with_deleted
paranoid_time.destroy
assert_nil paranoid_has_many_dependant.paranoid_time(true)
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_with_deleted(true)
end
def test_belongs_to_polymorphic_with_deleted
paranoid_time = ParanoidTime.first
paranoid_has_many_dependant = ParanoidHasManyDependant.create!(:name => 'dependant!', :paranoid_time_polymorphic_with_deleted => paranoid_time)
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted
paranoid_time.destroy
assert_nil paranoid_has_many_dependant.paranoid_time(true)
assert_equal paranoid_time, paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted(true)
end
def test_belongs_to_nil_polymorphic_with_deleted
paranoid_time = ParanoidTime.first
paranoid_has_many_dependant = ParanoidHasManyDependant.create!(:name => 'dependant!', :paranoid_time_polymorphic_with_deleted => nil)
assert_nil paranoid_has_many_dependant.paranoid_time
assert_nil paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted
paranoid_time.destroy
assert_nil paranoid_has_many_dependant.paranoid_time(true)
assert_nil paranoid_has_many_dependant.paranoid_time_polymorphic_with_deleted(true)
end
def test_belongs_to_options
paranoid_time = ParanoidHasManyDependant.reflections[:paranoid_time]
assert_equal :belongs_to, paranoid_time.macro
assert_nil paranoid_time.options[:with_deleted]
end
def test_belongs_to_with_deleted_options
paranoid_time_with_deleted = ParanoidHasManyDependant.reflections[:paranoid_time_with_deleted]
assert_equal :belongs_to, paranoid_time_with_deleted.macro
assert paranoid_time_with_deleted.options[:with_deleted]
end
def test_belongs_to_polymorphic_with_deleted_options
paranoid_time_polymorphic_with_deleted = ParanoidHasManyDependant.reflections[:paranoid_time_polymorphic_with_deleted]
assert_equal :belongs_to, paranoid_time_polymorphic_with_deleted.macro
assert paranoid_time_polymorphic_with_deleted.options[:with_deleted]
end
def test_only_find_associated_records_when_finding_with_paranoid_deleted
parent = ParanoidBelongsDependant.create
child = ParanoidHasManyDependant.create
parent.paranoid_has_many_dependants << child
unrelated_parent = ParanoidBelongsDependant.create
unrelated_child = ParanoidHasManyDependant.create
unrelated_parent.paranoid_has_many_dependants << unrelated_child
child.destroy
assert_paranoid_deletion(child)
assert_equal [child], parent.paranoid_has_many_dependants.with_deleted.to_a
end
def test_cannot_find_a_paranoid_deleted_many_many_association
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
left.paranoid_many_many_parent_rights.delete(right)
left.reload
assert_equal [], left.paranoid_many_many_children, "Linking objects not deleted"
assert_equal [], left.paranoid_many_many_parent_rights, "Associated objects not unlinked"
assert_equal right, ParanoidManyManyParentRight.find(right.id), "Associated object deleted"
end
def test_cannot_find_a_paranoid_destroyed_many_many_association
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
left.paranoid_many_many_parent_rights.destroy(right)
left.reload
assert_equal [], left.paranoid_many_many_children, "Linking objects not deleted"
assert_equal [], left.paranoid_many_many_parent_rights, "Associated objects not unlinked"
assert_equal right, ParanoidManyManyParentRight.find(right.id), "Associated object deleted"
end
def test_cannot_find_a_has_many_through_object_when_its_linking_object_is_paranoid_destroyed
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
child = left.paranoid_many_many_children.first
child.destroy
left.reload
assert_equal [], left.paranoid_many_many_parent_rights, "Associated objects not deleted"
end
def test_cannot_find_a_paranoid_deleted_model
model = ParanoidBelongsDependant.create
model.destroy
assert_raises ActiveRecord::RecordNotFound do
ParanoidBelongsDependant.find(model.id)
end
end
def test_bidirectional_has_many_through_association_clear_is_paranoid
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
child = left.paranoid_many_many_children.first
assert_equal left, child.paranoid_many_many_parent_left, "Child's left parent is incorrect"
assert_equal right, child.paranoid_many_many_parent_right, "Child's right parent is incorrect"
left.paranoid_many_many_parent_rights.clear
assert_paranoid_deletion(child)
end
def test_bidirectional_has_many_through_association_destroy_is_paranoid
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
child = left.paranoid_many_many_children.first
assert_equal left, child.paranoid_many_many_parent_left, "Child's left parent is incorrect"
assert_equal right, child.paranoid_many_many_parent_right, "Child's right parent is incorrect"
left.paranoid_many_many_parent_rights.destroy(right)
assert_paranoid_deletion(child)
end
def test_bidirectional_has_many_through_association_delete_is_paranoid
left = ParanoidManyManyParentLeft.create
right = ParanoidManyManyParentRight.create
left.paranoid_many_many_parent_rights << right
child = left.paranoid_many_many_children.first
assert_equal left, child.paranoid_many_many_parent_left, "Child's left parent is incorrect"
assert_equal right, child.paranoid_many_many_parent_right, "Child's right parent is incorrect"
left.paranoid_many_many_parent_rights.delete(right)
assert_paranoid_deletion(child)
end
def test_belongs_to_on_normal_model_is_paranoid
not_paranoid = HasOneNotParanoid.create
not_paranoid.paranoid_time = ParanoidTime.create
assert not_paranoid.save
assert_not_nil not_paranoid.paranoid_time
end
def test_double_belongs_to_with_deleted
not_paranoid = DoubleHasOneNotParanoid.create
not_paranoid.paranoid_time = ParanoidTime.create
assert not_paranoid.save
assert_not_nil not_paranoid.paranoid_time
end
end