public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Search Repo:
Split associations_test.rb into multiple files based on association type. 
[Pratik]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9233 
5ecf4fe2-1ee6-0310-87b1-e25e094e27de
lifo (author)
Sat Apr 05 20:01:09 -0700 2008
commit  5b822aca4b6524c9147224764ec0f20a728ba2a1
tree    1834d978c369f899bfa9ffba92c9f35353587eb8
parent  7ddc8f2e1bc9c818b622373a8c85bd679533cefd
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
0
@@ -1 +1,381 @@
0
+require "cases/helper"
0
+require 'models/developer'
0
+require 'models/project'
0
+require 'models/company'
0
+require 'models/topic'
0
+require 'models/reply'
0
+require 'models/computer'
0
+require 'models/customer'
0
+require 'models/order'
0
+require 'models/post'
0
+require 'models/author'
0
+require 'models/tag'
0
+require 'models/tagging'
0
+require 'models/comment'
0
+
0
+class BelongsToAssociationsTest < ActiveRecord::TestCase
0
+ fixtures :accounts, :companies, :developers, :projects, :topics,
0
+ :developers_projects, :computers, :authors, :posts, :tags, :taggings, :comments
0
+
0
+ def test_belongs_to
0
+ Client.find(3).firm.name
0
+ assert_equal companies(:first_firm).name, Client.find(3).firm.name
0
+ assert !Client.find(3).firm.nil?, "Microsoft should have a firm"
0
+ end
0
+
0
+ def test_proxy_assignment
0
+ account = Account.find(1)
0
+ assert_nothing_raised { account.firm = account.firm }
0
+ end
0
+
0
+ def test_triple_equality
0
+ assert Client.find(3).firm === Firm
0
+ assert Firm === Client.find(3).firm
0
+ end
0
+
0
+ def test_type_mismatch
0
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 }
0
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) }
0
+ end
0
+
0
+ def test_natural_assignment
0
+ apple = Firm.create("name" => "Apple")
0
+ citibank = Account.create("credit_limit" => 10)
0
+ citibank.firm = apple
0
+ assert_equal apple.id, citibank.firm_id
0
+ end
0
+
0
+ def test_no_unexpected_aliasing
0
+ first_firm = companies(:first_firm)
0
+ another_firm = companies(:another_firm)
0
+
0
+ citibank = Account.create("credit_limit" => 10)
0
+ citibank.firm = first_firm
0
+ original_proxy = citibank.firm
0
+ citibank.firm = another_firm
0
+
0
+ assert_equal first_firm.object_id, original_proxy.object_id
0
+ assert_equal another_firm.object_id, citibank.firm.object_id
0
+ end
0
+
0
+ def test_creating_the_belonging_object
0
+ citibank = Account.create("credit_limit" => 10)
0
+ apple = citibank.create_firm("name" => "Apple")
0
+ assert_equal apple, citibank.firm
0
+ citibank.save
0
+ citibank.reload
0
+ assert_equal apple, citibank.firm
0
+ end
0
+
0
+ def test_building_the_belonging_object
0
+ citibank = Account.create("credit_limit" => 10)
0
+ apple = citibank.build_firm("name" => "Apple")
0
+ citibank.save
0
+ assert_equal apple.id, citibank.firm_id
0
+ end
0
+
0
+ def test_natural_assignment_to_nil
0
+ client = Client.find(3)
0
+ client.firm = nil
0
+ client.save
0
+ assert_nil client.firm(true)
0
+ assert_nil client.client_of
0
+ end
0
+
0
+ def test_with_different_class_name
0
+ assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
0
+ assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm"
0
+ end
0
+
0
+ def test_with_condition
0
+ assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
0
+ assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
0
+ end
0
+
0
+ def test_belongs_to_counter
0
+ debate = Topic.create("title" => "debate")
0
+ assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
0
+
0
+ trash = debate.replies.create("title" => "blah!", "content" => "world around!")
0
+ assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
0
+
0
+ trash.destroy
0
+ assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
0
+ end
0
+
0
+ def test_belongs_to_counter_with_assigning_nil
0
+ p = Post.find(1)
0
+ c = Comment.find(1)
0
+
0
+ assert_equal p.id, c.post_id
0
+ assert_equal 2, Post.find(p.id).comments.size
0
+
0
+ c.post = nil
0
+
0
+ assert_equal 1, Post.find(p.id).comments.size
0
+ end
0
+
0
+ def test_belongs_to_counter_with_reassigning
0
+ t1 = Topic.create("title" => "t1")
0
+ t2 = Topic.create("title" => "t2")
0
+ r1 = Reply.new("title" => "r1", "content" => "r1")
0
+ r1.topic = t1
0
+
0
+ assert r1.save
0
+ assert_equal 1, Topic.find(t1.id).replies.size
0
+ assert_equal 0, Topic.find(t2.id).replies.size
0
+
0
+ r1.topic = Topic.find(t2.id)
0
+
0
+ assert r1.save
0
+ assert_equal 0, Topic.find(t1.id).replies.size
0
+ assert_equal 1, Topic.find(t2.id).replies.size
0
+
0
+ r1.topic = nil
0
+
0
+ assert_equal 0, Topic.find(t1.id).replies.size
0
+ assert_equal 0, Topic.find(t2.id).replies.size
0
+
0
+ r1.topic = t1
0
+
0
+ assert_equal 1, Topic.find(t1.id).replies.size
0
+ assert_equal 0, Topic.find(t2.id).replies.size
0
+
0
+ r1.destroy
0
+
0
+ assert_equal 0, Topic.find(t1.id).replies.size
0
+ assert_equal 0, Topic.find(t2.id).replies.size
0
+ end
0
+
0
+ def test_belongs_to_counter_after_save
0
+ topic = Topic.create!(:title => "monday night")
0
+ topic.replies.create!(:title => "re: monday night", :content => "football")
0
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
0
+
0
+ topic.save!
0
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
0
+ end
0
+
0
+ def test_belongs_to_counter_after_update_attributes
0
+ topic = Topic.create!(:title => "37s")
0
+ topic.replies.create!(:title => "re: 37s", :content => "rails")
0
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
0
+
0
+ topic.update_attributes(:title => "37signals")
0
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
0
+ end
0
+
0
+ def test_belongs_to_counter_after_save
0
+ topic = Topic.create("title" => "monday night")
0
+ topic.replies.create("title" => "re: monday night", "content" => "football")
0
+ assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
0
+
0
+ topic.save
0
+ assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
0
+ end
0
+
0
+ def test_belongs_to_counter_after_update_attributes
0
+ topic = Topic.create("title" => "37s")
0
+ topic.replies.create("title" => "re: 37s", "content" => "rails")
0
+ assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
0
+
0
+ topic.update_attributes("title" => "37signals")
0
+ assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
0
+ end
0
+
0
+ def test_assignment_before_parent_saved
0
+ client = Client.find(:first)
0
+ apple = Firm.new("name" => "Apple")
0
+ client.firm = apple
0
+ assert_equal apple, client.firm
0
+ assert apple.new_record?
0
+ assert client.save
0
+ assert apple.save
0
+ assert !apple.new_record?
0
+ assert_equal apple, client.firm
0
+ assert_equal apple, client.firm(true)
0
+ end
0
+
0
+ def test_assignment_before_child_saved
0
+ final_cut = Client.new("name" => "Final Cut")
0
+ firm = Firm.find(1)
0
+ final_cut.firm = firm
0
+ assert final_cut.new_record?
0
+ assert final_cut.save
0
+ assert !final_cut.new_record?
0
+ assert !firm.new_record?
0
+ assert_equal firm, final_cut.firm
0
+ assert_equal firm, final_cut.firm(true)
0
+ end
0
+
0
+ def test_assignment_before_either_saved
0
+ final_cut = Client.new("name" => "Final Cut")
0
+ apple = Firm.new("name" => "Apple")
0
+ final_cut.firm = apple
0
+ assert final_cut.new_record?
0
+ assert apple.new_record?
0
+ assert final_cut.save
0
+ assert !final_cut.new_record?
0
+ assert !apple.new_record?
0
+ assert_equal apple, final_cut.firm
0
+ assert_equal apple, final_cut.firm(true)
0
+ end
0
+
0
+ def test_new_record_with_foreign_key_but_no_object
0
+ c = Client.new("firm_id" => 1)
0
+ assert_equal Firm.find(:first), c.firm_with_basic_id
0
+ end
0
+
0
+ def test_forgetting_the_load_when_foreign_key_enters_late
0
+ c = Client.new
0
+ assert_nil c.firm_with_basic_id
0
+
0
+ c.firm_id = 1
0
+ assert_equal Firm.find(:first), c.firm_with_basic_id
0
+ end
0
+
0
+ def test_field_name_same_as_foreign_key
0
+ computer = Computer.find(1)
0
+ assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
0
+ end
0
+
0
+ def test_counter_cache
0
+ topic = Topic.create :title => "Zoom-zoom-zoom"
0
+ assert_equal 0, topic[:replies_count]
0
+
0
+ reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
0
+ reply.topic = topic
0
+
0
+ assert_equal 1, topic.reload[:replies_count]
0
+ assert_equal 1, topic.replies.size
0
+
0
+ topic[:replies_count] = 15
0
+ assert_equal 15, topic.replies.size
0
+ end
0
+
0
+ def test_custom_counter_cache
0
+ reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
0
+ assert_equal 0, reply[:replies_count]
0
+
0
+ silly = SillyReply.create(:title => "gaga", :content => "boo-boo")
0
+ silly.reply = reply
0
+
0
+ assert_equal 1, reply.reload[:replies_count]
0
+ assert_equal 1, reply.replies.size
0
+
0
+ reply[:replies_count] = 17
0
+ assert_equal 17, reply.replies.size
0
+ end
0
+
0
+ def test_store_two_association_with_one_save
0
+ num_orders = Order.count
0
+ num_customers = Customer.count
0
+ order = Order.new
0
+
0
+ customer1 = order.billing = Customer.new
0
+ customer2 = order.shipping = Customer.new
0
+ assert order.save
0
+ assert_equal customer1, order.billing
0
+ assert_equal customer2, order.shipping
0
+
0
+ order.reload
0
+
0
+ assert_equal customer1, order.billing
0
+ assert_equal customer2, order.shipping
0
+
0
+ assert_equal num_orders +1, Order.count
0
+ assert_equal num_customers +2, Customer.count
0
+ end
0
+
0
+
0
+ def test_store_association_in_two_relations_with_one_save
0
+ num_orders = Order.count
0
+ num_customers = Customer.count
0
+ order = Order.new
0
+
0
+ customer = order.billing = order.shipping = Customer.new
0
+ assert order.save
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+
0
+ order.reload
0
+
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+
0
+ assert_equal num_orders +1, Order.count
0
+ assert_equal num_customers +1, Customer.count
0
+ end
0
+
0
+ def test_store_association_in_two_relations_with_one_save_in_existing_object
0
+ num_orders = Order.count
0
+ num_customers = Customer.count
0
+ order = Order.create
0
+
0
+ customer = order.billing = order.shipping = Customer.new
0
+ assert order.save
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+
0
+ order.reload
0
+
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+
0
+ assert_equal num_orders +1, Order.count
0
+ assert_equal num_customers +1, Customer.count
0
+ end
0
+
0
+ def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
0
+ num_orders = Order.count
0
+ num_customers = Customer.count
0
+ order = Order.create
0
+
0
+ customer = order.billing = order.shipping = Customer.new
0
+ assert order.save
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+
0
+ order.reload
0
+
0
+ customer = order.billing = order.shipping = Customer.new
0
+
0
+ assert order.save
0
+ order.reload
0
+
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+
0
+ assert_equal num_orders +1, Order.count
0
+ assert_equal num_customers +2, Customer.count
0
+ end
0
+
0
+
0
+ def test_association_assignment_sticks
0
+ post = Post.find(:first)
0
+
0
+ author1, author2 = Author.find(:all, :limit => 2)
0
+ assert_not_nil author1
0
+ assert_not_nil author2
0
+
0
+ # make sure the association is loaded
0
+ post.author
0
+
0
+ # set the association by id, directly
0
+ post.author_id = author2.id
0
+
0
+ # save and reload
0
+ post.save!
0
+ post.reload
0
+
0
+ # the author id of the post should be the id we set
0
+ assert_equal post.author_id, author2.id
0
+ end
0
+
0
+ def test_cant_save_readonly_association
0
+ assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! }
0
+ assert companies(:first_client).readonly_firm.readonly?
0
+ end
0
+
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
0
@@ -1 +1,654 @@
0
+require "cases/helper"
0
+require 'models/developer'
0
+require 'models/project'
0
+require 'models/company'
0
+require 'models/topic'
0
+require 'models/reply'
0
+require 'models/computer'
0
+require 'models/customer'
0
+require 'models/order'
0
+require 'models/categorization'
0
+require 'models/category'
0
+require 'models/post'
0
+require 'models/author'
0
+require 'models/comment'
0
+require 'models/tag'
0
+require 'models/tagging'
0
+require 'models/person'
0
+require 'models/reader'
0
+require 'models/parrot'
0
+require 'models/pirate'
0
+require 'models/treasure'
0
+require 'models/price_estimate'
0
+require 'models/club'
0
+require 'models/member'
0
+require 'models/membership'
0
+require 'models/sponsor'
0
+
0
+class ProjectWithAfterCreateHook < ActiveRecord::Base
0
+ set_table_name 'projects'
0
+ has_and_belongs_to_many :developers,
0
+ :class_name => "DeveloperForProjectWithAfterCreateHook",
0
+ :join_table => "developers_projects",
0
+ :foreign_key => "project_id",
0
+ :association_foreign_key => "developer_id"
0
+
0
+ after_create :add_david
0
+
0
+ def add_david
0
+ david = DeveloperForProjectWithAfterCreateHook.find_by_name('David')
0
+ david.projects << self
0
+ end
0
+end
0
+
0
+class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
0
+ set_table_name 'developers'
0
+ has_and_belongs_to_many :projects,
0
+ :class_name => "ProjectWithAfterCreateHook",
0
+ :join_table => "developers_projects",
0
+ :association_foreign_key => "project_id",
0
+ :foreign_key => "developer_id"
0
+end
0
+
0
+
0
+class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
0
+ fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects,
0
+ :parrots, :pirates, :treasures, :price_estimates
0
+
0
+ def test_has_and_belongs_to_many
0
+ david = Developer.find(1)
0
+
0
+ assert !david.projects.empty?
0
+ assert_equal 2, david.projects.size
0
+
0
+ active_record = Project.find(1)
0
+ assert !active_record.developers.empty?
0
+ assert_equal 3, active_record.developers.size
0
+ assert active_record.developers.include?(david)
0
+ end
0
+
0
+ def test_triple_equality
0
+ assert !(Array === Developer.find(1).projects)
0
+ assert Developer.find(1).projects === Array
0
+ end
0
+
0
+ def test_adding_single
0
+ jamis = Developer.find(2)
0
+ jamis.projects.reload # causing the collection to load
0
+ action_controller = Project.find(2)
0
+ assert_equal 1, jamis.projects.size
0
+ assert_equal 1, action_controller.developers.size
0
+
0
+ jamis.projects << action_controller
0
+
0
+ assert_equal 2, jamis.projects.size
0
+ assert_equal 2, jamis.projects(true).size
0
+ assert_equal 2, action_controller.developers(true).size
0
+ end
0
+
0
+ def test_adding_type_mismatch
0
+ jamis = Developer.find(2)
0
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { jamis.projects << nil }
0
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { jamis.projects << 1 }
0
+ end
0
+
0
+ def test_adding_from_the_project
0
+ jamis = Developer.find(2)
0
+ action_controller = Project.find(2)
0
+ action_controller.developers.reload
0
+ assert_equal 1, jamis.projects.size
0
+ assert_equal 1, action_controller.developers.size
0
+
0
+ action_controller.developers << jamis
0
+
0
+ assert_equal 2, jamis.projects(true).size
0
+ assert_equal 2, action_controller.developers.size
0
+ assert_equal 2, action_controller.developers(true).size
0
+ end
0
+
0
+ def test_adding_from_the_project_fixed_timestamp
0
+ jamis = Developer.find(2)
0
+ action_controller = Project.find(2)
0
+ action_controller.developers.reload
0
+ assert_equal 1, jamis.projects.size
0
+ assert_equal 1, action_controller.developers.size
0
+ updated_at = jamis.updated_at
0
+
0
+ action_controller.developers << jamis
0
+
0
+ assert_equal updated_at, jamis.updated_at
0
+ assert_equal 2, jamis.projects(true).size
0
+ assert_equal 2, action_controller.developers.size
0
+ assert_equal 2, action_controller.developers(true).size
0
+ end
0
+
0
+ def test_adding_multiple
0
+ aredridel = Developer.new("name" => "Aredridel")
0
+ aredridel.save
0
+ aredridel.projects.reload
0
+ aredridel.projects.push(Project.find(1), Project.find(2))
0
+ assert_equal 2, aredridel.projects.size
0
+ assert_equal 2, aredridel.projects(true).size
0
+ end
0
+
0
+ def test_adding_a_collection
0
+ aredridel = Developer.new("name" => "Aredridel")
0
+ aredridel.save
0
+ aredridel.projects.reload
0
+ aredridel.projects.concat([Project.find(1), Project.find(2)])
0
+ assert_equal 2, aredridel.projects.size
0
+ assert_equal 2, aredridel.projects(true).size
0
+ end
0
+
0
+ def test_adding_uses_default_values_on_join_table
0
+ ac = projects(:action_controller)
0
+ assert !developers(:jamis).projects.include?(ac)
0
+ developers(:jamis).projects << ac
0
+
0
+ assert developers(:jamis, :reload).projects.include?(ac)
0
+ project = developers(:jamis).projects.detect { |p| p == ac }
0
+ assert_equal 1, project.access_level.to_i
0
+ end
0
+
0
+ def test_habtm_attribute_access_and_respond_to
0
+ project = developers(:jamis).projects[0]
0
+ assert project.has_attribute?("name")
0
+ assert project.has_attribute?("joined_on")
0
+ assert project.has_attribute?("access_level")
0
+ assert project.respond_to?("name")
0
+ assert project.respond_to?("name=")
0
+ assert project.respond_to?("name?")
0
+ assert project.respond_to?("joined_on")
0
+ # given that the 'join attribute' won't be persisted, I don't
0
+ # think we should define the mutators
0
+ #assert project.respond_to?("joined_on=")
0
+ assert project.respond_to?("joined_on?")
0
+ assert project.respond_to?("access_level")
0
+ #assert project.respond_to?("access_level=")
0