Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 259 lines (225 sloc) 7.859 kB
39041bf @jferris Moved ActiveRecord matcher tests into their own directory and added f…
jferris authored
1 require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
291c332 @jferris Reorganized matcher tests and files
jferris authored
2
3 class AssociationMatcherTest < Test::Unit::TestCase # :nodoc:
4
5 context "belong_to" do
6 setup do
7 @matcher = belong_to(:parent)
8 end
9
10 should "accept a good association with the default foreign key" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
11 define_model :parent
12 define_model :child, :parent_id => :integer do
291c332 @jferris Reorganized matcher tests and files
jferris authored
13 belongs_to :parent
14 end
15 assert_accepts @matcher, Child.new
16 end
17
18 should "reject a nonexistent association" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
19 define_model :child
291c332 @jferris Reorganized matcher tests and files
jferris authored
20 assert_rejects @matcher, Child.new
21 end
22
23 should "reject an association of the wrong type" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
24 define_model :parent, :child_id => :integer
25 child_class = define_model :child do
291c332 @jferris Reorganized matcher tests and files
jferris authored
26 has_one :parent
27 end
28 assert_rejects @matcher, Child.new
29 end
30
31 should "reject an association that has a nonexistent foreign key" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
32 define_model :parent
33 define_model :child do
291c332 @jferris Reorganized matcher tests and files
jferris authored
34 belongs_to :parent
35 end
36 assert_rejects @matcher, Child.new
37 end
38
39 should "accept an association with an existing custom foreign key" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
40 define_model :parent
41 define_model :child, :guardian_id => :integer do
291c332 @jferris Reorganized matcher tests and files
jferris authored
42 belongs_to :parent, :foreign_key => 'guardian_id'
43 end
44 assert_accepts @matcher, Child.new
45 end
46
47 should "accept a polymorphic association" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
48 define_model :child, :parent_type => :string,
291c332 @jferris Reorganized matcher tests and files
jferris authored
49 :parent_id => :integer do
50 belongs_to :parent, :polymorphic => true
51 end
52 assert_accepts @matcher, Child.new
53 end
54
55 should "accept an association with a valid :dependent option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
56 define_model :parent
57 define_model :child, :parent_id => :integer do
291c332 @jferris Reorganized matcher tests and files
jferris authored
58 belongs_to :parent, :dependent => :destroy
59 end
60 assert_accepts @matcher.dependent(:destroy), Child.new
61 end
62
63 should "reject an association with a bad :dependent option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
64 define_model :parent
65 define_model :child, :parent_id => :integer do
291c332 @jferris Reorganized matcher tests and files
jferris authored
66 belongs_to :parent
67 end
68 assert_rejects @matcher.dependent(:destroy), Child.new
69 end
70 end
71
72 context "have_many" do
73 setup do
74 @matcher = have_many(:children)
75 end
76
77 should "accept a valid association without any options" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
78 define_model :child, :parent_id => :integer
79 define_model :parent do
291c332 @jferris Reorganized matcher tests and files
jferris authored
80 has_many :children
81 end
82 assert_accepts @matcher, Parent.new
83 end
84
85 should "accept a valid association with a :through option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
86 define_model :child
87 define_model :conception, :child_id => :integer,
291c332 @jferris Reorganized matcher tests and files
jferris authored
88 :parent_id => :integer do
89 belongs_to :child
90 end
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
91 define_model :parent do
291c332 @jferris Reorganized matcher tests and files
jferris authored
92 has_many :conceptions
93 has_many :children, :through => :conceptions
94 end
95 assert_accepts @matcher, Parent.new
96 end
97
98 should "accept a valid association with an :as option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
99 define_model :child, :guardian_type => :string,
291c332 @jferris Reorganized matcher tests and files
jferris authored
100 :guardian_id => :integer
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
101 define_model :parent do
291c332 @jferris Reorganized matcher tests and files
jferris authored
102 has_many :children, :as => :guardian
103 end
104 assert_accepts @matcher, Parent.new
105 end
106
107 should "reject an association that has a nonexistent foreign key" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
108 define_model :child
109 define_model :parent do
291c332 @jferris Reorganized matcher tests and files
jferris authored
110 has_many :children
111 end
112 assert_rejects @matcher, Parent.new
113 end
114
115 should "reject an association with a bad :as option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
116 define_model :child, :caretaker_type => :string,
291c332 @jferris Reorganized matcher tests and files
jferris authored
117 :caretaker_id => :integer
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
118 define_model :parent do
291c332 @jferris Reorganized matcher tests and files
jferris authored
119 has_many :children, :as => :guardian
120 end
121 assert_rejects @matcher, Parent.new
122 end
123
124 should "reject an association that has a bad :through option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
125 define_model :child, :parent_id => :integer
126 define_model :parent do
291c332 @jferris Reorganized matcher tests and files
jferris authored
127 has_many :children
128 end
129 assert_rejects @matcher.through(:conceptions), Parent.new
130 end
131
132 should "reject an association that has the wrong :through option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
133 define_model :child
134 define_model :conception, :child_id => :integer,
291c332 @jferris Reorganized matcher tests and files
jferris authored
135 :parent_id => :integer do
136 belongs_to :child
137 end
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
138 define_model :parent do
291c332 @jferris Reorganized matcher tests and files
jferris authored
139 has_many :conceptions
140 has_many :children, :through => :conceptions
141 end
142 assert_rejects @matcher.through(:relationships), Parent.new
143 end
144
145 should "accept an association with a valid :dependent option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
146 define_model :child, :parent_id => :integer
147 define_model :parent do
291c332 @jferris Reorganized matcher tests and files
jferris authored
148 has_many :children, :dependent => :destroy
149 end
150 assert_accepts @matcher.dependent(:destroy), Parent.new
151 end
152
153 should "reject an association with a bad :dependent option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
154 define_model :child, :parent_id => :integer
155 define_model :parent do
291c332 @jferris Reorganized matcher tests and files
jferris authored
156 has_many :children
157 end
158 assert_rejects @matcher.dependent(:destroy), Parent.new
159 end
160 end
161
162 context "have_one" do
163 setup do
164 @matcher = have_one(:profile)
165 end
166
167 should "accept a valid association without any options" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
168 define_model :profile, :person_id => :integer
169 define_model :person do
291c332 @jferris Reorganized matcher tests and files
jferris authored
170 has_one :profile
171 end
172 assert_accepts @matcher, Person.new
173 end
174
175 should "accept a valid association with an :as option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
176 define_model :profile, :profilable_id => :integer,
291c332 @jferris Reorganized matcher tests and files
jferris authored
177 :profilable_type => :string
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
178 define_model :person do
291c332 @jferris Reorganized matcher tests and files
jferris authored
179 has_one :profile, :as => :profilable
180 end
181 assert_accepts @matcher, Person.new
182 end
183
184 should "reject an association that has a nonexistent foreign key" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
185 define_model :profile
186 define_model :person do
291c332 @jferris Reorganized matcher tests and files
jferris authored
187 has_one :profile
188 end
189 assert_rejects @matcher, Person.new
190 end
191
192 should "reject an association with a bad :as option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
193 define_model :profile, :profilable_id => :integer,
291c332 @jferris Reorganized matcher tests and files
jferris authored
194 :profilable_type => :string
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
195 define_model :person do
291c332 @jferris Reorganized matcher tests and files
jferris authored
196 has_one :profile, :as => :describable
197 end
198 assert_rejects @matcher, Person.new
199 end
200
201 should "accept an association with a valid :dependent option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
202 define_model :profile, :person_id => :integer
203 define_model :person do
291c332 @jferris Reorganized matcher tests and files
jferris authored
204 has_one :profile, :dependent => :destroy
205 end
206 assert_accepts @matcher.dependent(:destroy), Person.new
207 end
208
209 should "reject an association with a bad :dependent option" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
210 define_model :profile, :person_id => :integer
211 define_model :person do
291c332 @jferris Reorganized matcher tests and files
jferris authored
212 has_one :profile
213 end
214 assert_rejects @matcher.dependent(:destroy), Person.new
215 end
216 end
217
218 context "have_and_belong_to_many" do
219 setup do
220 @matcher = have_and_belong_to_many(:relatives)
221 end
222
223 should "accept a valid association" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
224 define_model :relatives
225 define_model :person do
291c332 @jferris Reorganized matcher tests and files
jferris authored
226 has_and_belongs_to_many :relatives
227 end
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
228 define_model :people_relative, :person_id => :integer,
291c332 @jferris Reorganized matcher tests and files
jferris authored
229 :relative_id => :integer
230 assert_accepts @matcher, Person.new
231 end
232
233 should "reject a nonexistent association" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
234 define_model :relatives
235 define_model :person
236 define_model :people_relative, :person_id => :integer,
291c332 @jferris Reorganized matcher tests and files
jferris authored
237 :relative_id => :integer
238 assert_rejects @matcher, Person.new
239 end
240
241 should "reject an association with a nonexistent join table" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
242 define_model :relatives
243 define_model :person do
291c332 @jferris Reorganized matcher tests and files
jferris authored
244 has_and_belongs_to_many :relatives
245 end
246 assert_rejects @matcher, Person.new
247 end
248
249 should "reject an association of the wrong type" do
6463037 @jferris Refactored build_model_class so that the migrate/constant steps can b…
jferris authored
250 define_model :relatives, :person_id => :integer
251 define_model :person do
291c332 @jferris Reorganized matcher tests and files
jferris authored
252 has_many :relatives
253 end
254 assert_rejects @matcher, Person.new
255 end
256 end
257
258 end
Something went wrong with that request. Please try again.