/
association_matcher_spec.rb
364 lines (316 loc) · 11.3 KB
/
association_matcher_spec.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
require 'spec_helper'
describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
context "belong_to" do
before do
@matcher = belong_to(:parent)
end
it "should accept a good association with the default foreign key" do
define_model :parent
define_model :child, :parent_id => :integer do
belongs_to :parent
end
Child.new.should @matcher
end
it "should reject a nonexistent association" do
define_model :child
Child.new.should_not @matcher
end
it "should reject an association of the wrong type" do
define_model :parent, :child_id => :integer
child_class = define_model :child do
has_one :parent
end
Child.new.should_not @matcher
end
it "should reject an association that has a nonexistent foreign key" do
define_model :parent
define_model :child do
belongs_to :parent
end
Child.new.should_not @matcher
end
it "should accept an association with an existing custom foreign key" do
define_model :parent
define_model :child, :guardian_id => :integer do
belongs_to :parent, :foreign_key => 'guardian_id'
end
Child.new.should @matcher
end
it "should accept a polymorphic association" do
define_model :child, :parent_type => :string,
:parent_id => :integer do
belongs_to :parent, :polymorphic => true
end
Child.new.should @matcher
end
it "should accept an association with a valid :dependent option" do
define_model :parent
define_model :child, :parent_id => :integer do
belongs_to :parent, :dependent => :destroy
end
Child.new.should @matcher.dependent(:destroy)
end
it "should reject an association with a bad :dependent option" do
define_model :parent
define_model :child, :parent_id => :integer do
belongs_to :parent
end
Child.new.should_not @matcher.dependent(:destroy)
end
it "should accept an association with a valid :conditions option" do
define_model :parent, :adopter => :boolean
define_model :child, :parent_id => :integer do
belongs_to :parent, :conditions => { :adopter => true }
end
Child.new.should @matcher.conditions(:adopter => true)
end
it "should reject an association with a bad :conditions option" do
define_model :parent, :adopter => :boolean
define_model :child, :parent_id => :integer do
belongs_to :parent
end
Child.new.should_not @matcher.conditions(:adopter => true)
end
end
context "have_many" do
before do
@matcher = have_many(:children)
end
it "should accept a valid association without any options" do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children
end
Parent.new.should @matcher
end
it "should accept a valid association with a :through option" do
define_model :child
define_model :conception, :child_id => :integer,
:parent_id => :integer do
belongs_to :child
end
define_model :parent do
has_many :conceptions
has_many :children, :through => :conceptions
end
Parent.new.should @matcher
end
it "should accept a valid association with an :as option" do
define_model :child, :guardian_type => :string,
:guardian_id => :integer
define_model :parent do
has_many :children, :as => :guardian
end
Parent.new.should @matcher
end
it "should reject an association that has a nonexistent foreign key" do
define_model :child
define_model :parent do
has_many :children
end
Parent.new.should_not @matcher
end
it "should reject an association with a bad :as option" do
define_model :child, :caretaker_type => :string,
:caretaker_id => :integer
define_model :parent do
has_many :children, :as => :guardian
end
Parent.new.should_not @matcher
end
it "should reject an association that has a bad :through option" do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children
end
@matcher.through(:conceptions).matches?(Parent.new).should be_false
@matcher.failure_message.should =~ /does not have any relationship to conceptions/
end
it "should reject an association that has the wrong :through option" do
define_model :child
define_model :conception, :child_id => :integer,
:parent_id => :integer do
belongs_to :child
end
define_model :parent do
has_many :conceptions
has_many :relationships
has_many :children, :through => :conceptions
end
@matcher.through(:relationships).matches?(Parent.new).should be_false
@matcher.failure_message.should =~ /through relationships, but got it through conceptions/
end
it "should accept an association with a valid :dependent option" do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children, :dependent => :destroy
end
Parent.new.should @matcher.dependent(:destroy)
end
it "should reject an association with a bad :dependent option" do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children
end
Parent.new.should_not @matcher.dependent(:destroy)
end
it "should accept an association with a valid :order option" do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children, :order => :id
end
Parent.new.should @matcher.order(:id)
end
it "should reject an association with a bad :order option" do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children
end
Parent.new.should_not @matcher.order(:id)
end
it "should accept an association with a valid :conditions option" do
define_model :child, :parent_id => :integer, :adopted => :boolean
define_model :parent do
has_many :children, :conditions => { :adopted => true }
end
Parent.new.should @matcher.conditions({ :adopted => true })
end
it "should reject an association with a bad :conditions option" do
define_model :child, :parent_id => :integer, :adopted => :boolean
define_model :parent do
has_many :children
end
Parent.new.should_not @matcher.conditions({ :adopted => true })
end
end
context "have_one" do
before do
@matcher = have_one(:detail)
end
it "should accept a valid association without any options" do
define_model :detail, :person_id => :integer
define_model :person do
has_one :detail
end
Person.new.should @matcher
end
it "should accept a valid association with an :as option" do
define_model :detail, :detailable_id => :integer,
:detailable_type => :string
define_model :person do
has_one :detail, :as => :detailable
end
Person.new.should @matcher
end
it "should reject an association that has a nonexistent foreign key" do
define_model :detail
define_model :person do
has_one :detail
end
Person.new.should_not @matcher
end
it "should reject an association with a bad :as option" do
define_model :detail, :detailable_id => :integer,
:detailable_type => :string
define_model :person do
has_one :detail, :as => :describable
end
Person.new.should_not @matcher
end
it "should accept an association with a valid :dependent option" do
define_model :detail, :person_id => :integer
define_model :person do
has_one :detail, :dependent => :destroy
end
Person.new.should @matcher.dependent(:destroy)
end
it "should reject an association with a bad :dependent option" do
define_model :detail, :person_id => :integer
define_model :person do
has_one :detail
end
Person.new.should_not @matcher.dependent(:destroy)
end
it "should accept an association with a valid :order option" do
define_model :detail, :person_id => :integer
define_model :person do
has_one :detail, :order => :id
end
Person.new.should @matcher.order(:id)
end
it "should reject an association with a bad :order option" do
define_model :detail, :person_id => :integer
define_model :person do
has_one :detail
end
Person.new.should_not @matcher.order(:id)
end
it "should accept an association with a valid :conditions option" do
define_model :detail, :person_id => :integer, :disabled => :boolean
define_model :person do
has_one :detail, :conditions => { :disabled => true}
end
Person.new.should @matcher.conditions(:disabled => true)
end
it "should reject an association with a bad :conditions option" do
define_model :detail, :person_id => :integer, :disabled => :boolean
define_model :person do
has_one :detail
end
Person.new.should_not @matcher.conditions(:disabled => true)
end
end
context "have_and_belong_to_many" do
before do
@matcher = have_and_belong_to_many(:relatives)
end
it "should accept a valid association" do
define_model :relatives
define_model :person do
has_and_belongs_to_many :relatives
end
define_model :people_relative, :person_id => :integer,
:relative_id => :integer
Person.new.should @matcher
end
it "should reject a nonexistent association" do
define_model :relatives
define_model :person
define_model :people_relative, :person_id => :integer,
:relative_id => :integer
Person.new.should_not @matcher
end
it "should reject an association with a nonexistent join table" do
define_model :relatives
define_model :person do
has_and_belongs_to_many :relatives
end
Person.new.should_not @matcher
end
it "should reject an association of the wrong type" do
define_model :relatives, :person_id => :integer
define_model :person do
has_many :relatives
end
Person.new.should_not @matcher
end
it "should accept an association with a valid :conditions option" do
define_model :relatives, :adopted => :boolean
define_model :person do
has_and_belongs_to_many :relatives, :conditions => { :adopted => true }
end
define_model :people_relative, :person_id => :integer,
:relative_id => :integer
Person.new.should @matcher.conditions(:adopted => true)
end
it "should reject an association with a bad :conditions option" do
define_model :relatives, :adopted => :boolean
define_model :person do
has_and_belongs_to_many :relatives
end
define_model :people_relative, :person_id => :integer,
:relative_id => :integer
Person.new.should_not @matcher.conditions(:adopted => true)
end
end
end