ibm_db 2.5.26-universal-darwin-14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES +233 -0
  3. data/LICENSE +18 -0
  4. data/MANIFEST +14 -0
  5. data/ParameterizedQueries README +39 -0
  6. data/README +225 -0
  7. data/ext/Makefile.nt32 +181 -0
  8. data/ext/Makefile.nt32.191 +212 -0
  9. data/ext/extconf.rb +261 -0
  10. data/ext/ibm_db.c +11793 -0
  11. data/ext/ruby_ibm_db.h +240 -0
  12. data/ext/ruby_ibm_db_cli.c +845 -0
  13. data/ext/ruby_ibm_db_cli.h +489 -0
  14. data/init.rb +42 -0
  15. data/lib/IBM_DB.rb +19 -0
  16. data/lib/active_record/connection_adapters/ibm_db_adapter.rb +3289 -0
  17. data/lib/active_record/connection_adapters/ibm_db_pstmt.rb +1965 -0
  18. data/lib/active_record/connection_adapters/ibmdb_adapter.rb +2 -0
  19. data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -0
  20. data/lib/linux/rb18x/ibm_db.bundle +0 -0
  21. data/lib/linux/rb19x/ibm_db.bundle +0 -0
  22. data/lib/linux/rb20x/ibm_db.bundle +0 -0
  23. data/lib/linux/rb21x/ibm_db.bundle +0 -0
  24. data/test/cases/adapter_test.rb +207 -0
  25. data/test/cases/associations/belongs_to_associations_test.rb +711 -0
  26. data/test/cases/associations/cascaded_eager_loading_test.rb +181 -0
  27. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +851 -0
  28. data/test/cases/associations/join_model_test.rb +743 -0
  29. data/test/cases/attribute_methods_test.rb +822 -0
  30. data/test/cases/base_test.rb +2133 -0
  31. data/test/cases/calculations_test.rb +482 -0
  32. data/test/cases/migration_test.rb +2408 -0
  33. data/test/cases/persistence_test.rb +642 -0
  34. data/test/cases/query_cache_test.rb +257 -0
  35. data/test/cases/relations_test.rb +1182 -0
  36. data/test/cases/schema_dumper_test.rb +256 -0
  37. data/test/cases/transaction_callbacks_test.rb +300 -0
  38. data/test/cases/validations/uniqueness_validation_test.rb +299 -0
  39. data/test/cases/xml_serialization_test.rb +408 -0
  40. data/test/config.yml +154 -0
  41. data/test/connections/native_ibm_db/connection.rb +44 -0
  42. data/test/ibm_db_test.rb +25 -0
  43. data/test/models/warehouse_thing.rb +5 -0
  44. data/test/schema/i5/ibm_db_specific_schema.rb +137 -0
  45. data/test/schema/ids/ibm_db_specific_schema.rb +140 -0
  46. data/test/schema/luw/ibm_db_specific_schema.rb +137 -0
  47. data/test/schema/schema.rb +751 -0
  48. data/test/schema/zOS/ibm_db_specific_schema.rb +208 -0
  49. metadata +114 -0
@@ -0,0 +1,711 @@
1
+ require "cases/helper"
2
+ require 'models/developer'
3
+ require 'models/project'
4
+ require 'models/company'
5
+ require 'models/topic'
6
+ require 'models/reply'
7
+ require 'models/computer'
8
+ require 'models/post'
9
+ require 'models/author'
10
+ require 'models/tag'
11
+ require 'models/tagging'
12
+ require 'models/comment'
13
+ require 'models/sponsor'
14
+ require 'models/member'
15
+ require 'models/essay'
16
+ require 'models/toy'
17
+
18
+ class BelongsToAssociationsTest < ActiveRecord::TestCase
19
+ fixtures :accounts, :companies, :developers, :projects, :topics,
20
+ :developers_projects, :computers, :authors, :author_addresses,
21
+ :posts, :tags, :taggings, :comments, :sponsors, :members
22
+
23
+ def test_belongs_to
24
+ Client.find(3).firm.name
25
+ assert_equal companies(:first_firm).name, Client.find(3).firm.name
26
+ assert_not_nil Client.find(3).firm, "Microsoft should have a firm"
27
+ end
28
+
29
+ def test_belongs_to_with_primary_key
30
+ client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
31
+ assert_equal companies(:first_firm).name, client.firm_with_primary_key.name
32
+ end
33
+
34
+ def test_belongs_to_with_primary_key_joins_on_correct_column
35
+ sql = Client.joins(:firm_with_primary_key).to_sql
36
+ if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
37
+ assert_no_match(/`firm_with_primary_keys_companies`\.`id`/, sql)
38
+ assert_match(/`firm_with_primary_keys_companies`\.`name`/, sql)
39
+ elsif current_adapter?(:OracleAdapter)
40
+ # on Oracle aliases are truncated to 30 characters and are quoted in uppercase
41
+ assert_no_match(/"firm_with_primary_keys_compani"\."id"/i, sql)
42
+ assert_match(/"firm_with_primary_keys_compani"\."name"/i, sql)
43
+ elsif current_adapter?(:IBM_DBAdapter)
44
+ # Quoting of column names is not necessary for IBM_DB
45
+ assert_no_match(/firm_with_primary_keys_companies\.id/i, sql)
46
+ assert_match(/firm_with_primary_keys_companies\.name/i, sql)
47
+ else
48
+ assert_no_match(/"firm_with_primary_keys_companies"\."id"/, sql)
49
+ assert_match(/"firm_with_primary_keys_companies"\."name"/, sql)
50
+ end
51
+ end
52
+
53
+ def test_proxy_assignment
54
+ account = Account.find(1)
55
+ assert_nothing_raised { account.firm = account.firm }
56
+ end
57
+
58
+ def test_type_mismatch
59
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 }
60
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) }
61
+ end
62
+
63
+ def test_natural_assignment
64
+ apple = Firm.create("name" => "Apple")
65
+ citibank = Account.create("credit_limit" => 10)
66
+ citibank.firm = apple
67
+ assert_equal apple.id, citibank.firm_id
68
+ end
69
+
70
+ def test_natural_assignment_with_primary_key
71
+ apple = Firm.create("name" => "Apple")
72
+ citibank = Client.create("name" => "Primary key client")
73
+ citibank.firm_with_primary_key = apple
74
+ assert_equal apple.name, citibank.firm_name
75
+ end
76
+
77
+ def test_eager_loading_with_primary_key
78
+ Firm.create("name" => "Apple")
79
+ Client.create("name" => "Citibank", :firm_name => "Apple")
80
+ citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key)
81
+ assert citibank_result.association_cache.key?(:firm_with_primary_key)
82
+ end
83
+
84
+ def test_eager_loading_with_primary_key_as_symbol
85
+ Firm.create("name" => "Apple")
86
+ Client.create("name" => "Citibank", :firm_name => "Apple")
87
+ citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key_symbols)
88
+ assert citibank_result.association_cache.key?(:firm_with_primary_key_symbols)
89
+ end
90
+
91
+ def test_creating_the_belonging_object
92
+ citibank = Account.create("credit_limit" => 10)
93
+ apple = citibank.create_firm("name" => "Apple")
94
+ assert_equal apple, citibank.firm
95
+ citibank.save
96
+ citibank.reload
97
+ assert_equal apple, citibank.firm
98
+ end
99
+
100
+ def test_creating_the_belonging_object_with_primary_key
101
+ client = Client.create(:name => "Primary key client")
102
+ apple = client.create_firm_with_primary_key("name" => "Apple")
103
+ assert_equal apple, client.firm_with_primary_key
104
+ client.save
105
+ client.reload
106
+ assert_equal apple, client.firm_with_primary_key
107
+ end
108
+
109
+ def test_building_the_belonging_object
110
+ citibank = Account.create("credit_limit" => 10)
111
+ apple = citibank.build_firm("name" => "Apple")
112
+ citibank.save
113
+ assert_equal apple.id, citibank.firm_id
114
+ end
115
+
116
+ def test_building_the_belonging_object_with_primary_key
117
+ client = Client.create(:name => "Primary key client")
118
+ apple = client.build_firm_with_primary_key("name" => "Apple")
119
+ client.save
120
+ assert_equal apple.name, client.firm_name
121
+ end
122
+
123
+ def test_create!
124
+ client = Client.create!(:name => "Jimmy")
125
+ account = client.create_account!(:credit_limit => 10)
126
+ assert_equal account, client.account
127
+ assert account.persisted?
128
+ client.save
129
+ client.reload
130
+ assert_equal account, client.account
131
+ end
132
+
133
+ def test_failing_create!
134
+ client = Client.create!(:name => "Jimmy")
135
+ assert_raise(ActiveRecord::RecordInvalid) { client.create_account! }
136
+ assert_not_nil client.account
137
+ assert client.account.new_record?
138
+ end
139
+
140
+ def test_natural_assignment_to_nil
141
+ client = Client.find(3)
142
+ client.firm = nil
143
+ client.save
144
+ assert_nil client.firm(true)
145
+ assert_nil client.client_of
146
+ end
147
+
148
+ def test_natural_assignment_to_nil_with_primary_key
149
+ client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
150
+ client.firm_with_primary_key = nil
151
+ client.save
152
+ assert_nil client.firm_with_primary_key(true)
153
+ assert_nil client.client_of
154
+ end
155
+
156
+ def test_with_different_class_name
157
+ assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
158
+ assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm"
159
+ end
160
+
161
+ def test_with_condition
162
+ assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
163
+ assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
164
+ end
165
+
166
+ def test_polymorphic_association_class
167
+ sponsor = Sponsor.new
168
+ assert_nil sponsor.association(:sponsorable).send(:klass)
169
+
170
+ sponsor.sponsorable_type = '' # the column doesn't have to be declared NOT NULL
171
+ assert_nil sponsor.association(:sponsorable).send(:klass)
172
+
173
+ sponsor.sponsorable = Member.new :name => "Bert"
174
+ assert_equal Member, sponsor.association(:sponsorable).send(:klass)
175
+ end
176
+
177
+ def test_with_polymorphic_and_condition
178
+ sponsor = Sponsor.create
179
+ member = Member.create :name => "Bert"
180
+ sponsor.sponsorable = member
181
+
182
+ assert_equal member, sponsor.sponsorable
183
+ assert_nil sponsor.sponsorable_with_conditions
184
+ end
185
+
186
+ def test_with_select
187
+ assert_equal Company.find(2).firm_with_select.attributes.size, 1
188
+ assert_equal Company.find(2, :include => :firm_with_select ).firm_with_select.attributes.size, 1
189
+ end
190
+
191
+ def test_belongs_to_counter
192
+ debate = Topic.create("title" => "debate")
193
+ assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
194
+
195
+ trash = debate.replies.create("title" => "blah!", "content" => "world around!")
196
+ assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
197
+
198
+ trash.destroy
199
+ assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
200
+ end
201
+
202
+ def test_belongs_to_counter_with_assigning_nil
203
+ p = Post.find(1)
204
+ c = Comment.find(1)
205
+
206
+ assert_equal p.id, c.post_id
207
+ assert_equal 2, Post.find(p.id).comments.size
208
+
209
+ c.post = nil
210
+
211
+ assert_equal 1, Post.find(p.id).comments.size
212
+ end
213
+
214
+ def test_belongs_to_with_primary_key_counter
215
+ debate = Topic.create("title" => "debate")
216
+ debate2 = Topic.create("title" => "debate2")
217
+ reply = Reply.create("title" => "blah!", "content" => "world around!", "parent_title" => "debate")
218
+
219
+ assert_equal 1, debate.reload.replies_count
220
+ assert_equal 0, debate2.reload.replies_count
221
+
222
+ reply.topic_with_primary_key = debate2
223
+
224
+ assert_equal 0, debate.reload.replies_count
225
+ assert_equal 1, debate2.reload.replies_count
226
+
227
+ reply.topic_with_primary_key = nil
228
+
229
+ assert_equal 0, debate.reload.replies_count
230
+ assert_equal 0, debate2.reload.replies_count
231
+ end
232
+
233
+ def test_belongs_to_counter_with_reassigning
234
+ t1 = Topic.create("title" => "t1")
235
+ t2 = Topic.create("title" => "t2")
236
+ r1 = Reply.new("title" => "r1", "content" => "r1")
237
+ r1.topic = t1
238
+
239
+ assert r1.save
240
+ assert_equal 1, Topic.find(t1.id).replies.size
241
+ assert_equal 0, Topic.find(t2.id).replies.size
242
+
243
+ r1.topic = Topic.find(t2.id)
244
+
245
+ assert_no_queries do
246
+ r1.topic = t2
247
+ end
248
+
249
+ assert r1.save
250
+ assert_equal 0, Topic.find(t1.id).replies.size
251
+ assert_equal 1, Topic.find(t2.id).replies.size
252
+
253
+ r1.topic = nil
254
+
255
+ assert_equal 0, Topic.find(t1.id).replies.size
256
+ assert_equal 0, Topic.find(t2.id).replies.size
257
+
258
+ r1.topic = t1
259
+
260
+ assert_equal 1, Topic.find(t1.id).replies.size
261
+ assert_equal 0, Topic.find(t2.id).replies.size
262
+
263
+ r1.destroy
264
+
265
+ assert_equal 0, Topic.find(t1.id).replies.size
266
+ assert_equal 0, Topic.find(t2.id).replies.size
267
+ end
268
+
269
+ def test_belongs_to_reassign_with_namespaced_models_and_counters
270
+ t1 = Web::Topic.create("title" => "t1")
271
+ t2 = Web::Topic.create("title" => "t2")
272
+ r1 = Web::Reply.new("title" => "r1", "content" => "r1")
273
+ r1.topic = t1
274
+
275
+ assert r1.save
276
+ assert_equal 1, Web::Topic.find(t1.id).replies.size
277
+ assert_equal 0, Web::Topic.find(t2.id).replies.size
278
+
279
+ r1.topic = Web::Topic.find(t2.id)
280
+
281
+ assert r1.save
282
+ assert_equal 0, Web::Topic.find(t1.id).replies.size
283
+ assert_equal 1, Web::Topic.find(t2.id).replies.size
284
+ end
285
+
286
+ def test_belongs_to_counter_after_save
287
+ topic = Topic.create!(:title => "monday night")
288
+ topic.replies.create!(:title => "re: monday night", :content => "football")
289
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
290
+
291
+ topic.save!
292
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
293
+ end
294
+
295
+ def test_belongs_to_counter_after_update_attributes
296
+ topic = Topic.create!(:title => "37s")
297
+ topic.replies.create!(:title => "re: 37s", :content => "rails")
298
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
299
+
300
+ topic.update_attributes(:title => "37signals")
301
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
302
+ end
303
+
304
+ def test_belongs_to_counter_when_update_column
305
+ topic = Topic.create!(:title => "37s")
306
+ topic.replies.create!(:title => "re: 37s", :content => "rails")
307
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
308
+
309
+ topic.update_column(:content, "rails is wonderfull")
310
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
311
+ end
312
+
313
+ def test_assignment_before_child_saved
314
+ final_cut = Client.new("name" => "Final Cut")
315
+ firm = Firm.find(1)
316
+ final_cut.firm = firm
317
+ assert !final_cut.persisted?
318
+ assert final_cut.save
319
+ assert final_cut.persisted?
320
+ assert firm.persisted?
321
+ assert_equal firm, final_cut.firm
322
+ assert_equal firm, final_cut.firm(true)
323
+ end
324
+
325
+ def test_assignment_before_child_saved_with_primary_key
326
+ final_cut = Client.new("name" => "Final Cut")
327
+ firm = Firm.find(1)
328
+ final_cut.firm_with_primary_key = firm
329
+ assert !final_cut.persisted?
330
+ assert final_cut.save
331
+ assert final_cut.persisted?
332
+ assert firm.persisted?
333
+ assert_equal firm, final_cut.firm_with_primary_key
334
+ assert_equal firm, final_cut.firm_with_primary_key(true)
335
+ end
336
+
337
+ def test_new_record_with_foreign_key_but_no_object
338
+ c = Client.new("firm_id" => 1)
339
+ # sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
340
+ assert_equal Firm.find(:first, :order => "id"), c.firm_with_basic_id
341
+ end
342
+
343
+ def test_setting_foreign_key_after_nil_target_loaded
344
+ client = Client.new
345
+ client.firm_with_basic_id
346
+ client.firm_id = 1
347
+
348
+ assert_equal companies(:first_firm), client.firm_with_basic_id
349
+ end
350
+
351
+ def test_polymorphic_setting_foreign_key_after_nil_target_loaded
352
+ sponsor = Sponsor.new
353
+ sponsor.sponsorable
354
+ sponsor.sponsorable_id = 1
355
+ sponsor.sponsorable_type = "Member"
356
+
357
+ assert_equal members(:groucho), sponsor.sponsorable
358
+ end
359
+
360
+ def test_dont_find_target_when_foreign_key_is_null
361
+ tagging = taggings(:thinking_general)
362
+ queries = assert_sql { tagging.super_tag }
363
+ assert_equal 0, queries.length
364
+ end
365
+
366
+ def test_field_name_same_as_foreign_key
367
+ computer = Computer.find(1)
368
+ assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
369
+ end
370
+
371
+ def test_counter_cache
372
+ topic = Topic.create :title => "Zoom-zoom-zoom"
373
+ assert_equal 0, topic[:replies_count]
374
+
375
+ reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
376
+ reply.topic = topic
377
+
378
+ assert_equal 1, topic.reload[:replies_count]
379
+ assert_equal 1, topic.replies.size
380
+
381
+ topic[:replies_count] = 15
382
+ assert_equal 15, topic.replies.size
383
+ end
384
+
385
+ def test_custom_counter_cache
386
+ reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
387
+ assert_equal 0, reply[:replies_count]
388
+
389
+ silly = SillyReply.create(:title => "gaga", :content => "boo-boo")
390
+ silly.reply = reply
391
+
392
+ assert_equal 1, reply.reload[:replies_count]
393
+ assert_equal 1, reply.replies.size
394
+
395
+ reply[:replies_count] = 17
396
+ assert_equal 17, reply.replies.size
397
+ end
398
+
399
+ def test_association_assignment_sticks
400
+ post = Post.find(:first)
401
+
402
+ author1, author2 = Author.find(:all, :limit => 2)
403
+ assert_not_nil author1
404
+ assert_not_nil author2
405
+
406
+ # make sure the association is loaded
407
+ post.author
408
+
409
+ # set the association by id, directly
410
+ post.author_id = author2.id
411
+
412
+ # save and reload
413
+ post.save!
414
+ post.reload
415
+
416
+ # the author id of the post should be the id we set
417
+ assert_equal post.author_id, author2.id
418
+ end
419
+
420
+ def test_cant_save_readonly_association
421
+ assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! }
422
+ assert companies(:first_client).readonly_firm.readonly?
423
+ end
424
+
425
+ def test_polymorphic_assignment_foreign_type_field_updating
426
+ # should update when assigning a saved record
427
+ sponsor = Sponsor.new
428
+ member = Member.create
429
+ sponsor.sponsorable = member
430
+ assert_equal "Member", sponsor.sponsorable_type
431
+
432
+ # should update when assigning a new record
433
+ sponsor = Sponsor.new
434
+ member = Member.new
435
+ sponsor.sponsorable = member
436
+ assert_equal "Member", sponsor.sponsorable_type
437
+ end
438
+
439
+ def test_polymorphic_assignment_with_primary_key_foreign_type_field_updating
440
+ # should update when assigning a saved record
441
+ essay = Essay.new
442
+ writer = Author.create(:name => "David")
443
+ essay.writer = writer
444
+ assert_equal "Author", essay.writer_type
445
+
446
+ # should update when assigning a new record
447
+ essay = Essay.new
448
+ writer = Author.new
449
+ essay.writer = writer
450
+ assert_equal "Author", essay.writer_type
451
+ end
452
+
453
+ def test_polymorphic_assignment_updates_foreign_id_field_for_new_and_saved_records
454
+ sponsor = Sponsor.new
455
+ saved_member = Member.create
456
+ new_member = Member.new
457
+
458
+ sponsor.sponsorable = saved_member
459
+ assert_equal saved_member.id, sponsor.sponsorable_id
460
+
461
+ sponsor.sponsorable = new_member
462
+ assert_nil sponsor.sponsorable_id
463
+ end
464
+
465
+ def test_assignment_updates_foreign_id_field_for_new_and_saved_records
466
+ client = Client.new
467
+ saved_firm = Firm.create :name => "Saved"
468
+ new_firm = Firm.new
469
+
470
+ client.firm = saved_firm
471
+ assert_equal saved_firm.id, client.client_of
472
+
473
+ client.firm = new_firm
474
+ assert_nil client.client_of
475
+ end
476
+
477
+ def test_polymorphic_assignment_with_primary_key_updates_foreign_id_field_for_new_and_saved_records
478
+ essay = Essay.new
479
+ saved_writer = Author.create(:name => "David")
480
+ new_writer = Author.new
481
+
482
+ essay.writer = saved_writer
483
+ assert_equal saved_writer.name, essay.writer_id
484
+
485
+ essay.writer = new_writer
486
+ assert_nil essay.writer_id
487
+ end
488
+
489
+ def test_belongs_to_proxy_should_not_respond_to_private_methods
490
+ assert_raise(NoMethodError) { companies(:first_firm).private_method }
491
+ assert_raise(NoMethodError) { companies(:second_client).firm.private_method }
492
+ end
493
+
494
+ def test_belongs_to_proxy_should_respond_to_private_methods_via_send
495
+ companies(:first_firm).send(:private_method)
496
+ companies(:second_client).firm.send(:private_method)
497
+ end
498
+
499
+ def test_save_of_record_with_loaded_belongs_to
500
+ @account = companies(:first_firm).account
501
+
502
+ assert_nothing_raised do
503
+ Account.find(@account.id).save!
504
+ Account.find(@account.id, :include => :firm).save!
505
+ end
506
+
507
+ @account.firm.delete
508
+
509
+ assert_nothing_raised do
510
+ Account.find(@account.id).save!
511
+ Account.find(@account.id, :include => :firm).save!
512
+ end
513
+ end
514
+
515
+ def test_dependent_delete_and_destroy_with_belongs_to
516
+ author_address = author_addresses(:david_address)
517
+ author_address_extra = author_addresses(:david_address_extra)
518
+ assert_equal [], AuthorAddress.destroyed_author_address_ids
519
+
520
+ assert_difference "AuthorAddress.count", -2 do
521
+ authors(:david).destroy
522
+ end
523
+
524
+ assert_equal [], AuthorAddress.find_all_by_id([author_address.id, author_address_extra.id])
525
+ assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids
526
+ end
527
+
528
+ def test_invalid_belongs_to_dependent_option_nullify_raises_exception
529
+ assert_raise ArgumentError do
530
+ Author.belongs_to :special_author_address, :dependent => :nullify
531
+ end
532
+ end
533
+
534
+ def test_invalid_belongs_to_dependent_option_restrict_raises_exception
535
+ assert_raise ArgumentError do
536
+ Author.belongs_to :special_author_address, :dependent => :restrict
537
+ end
538
+ end
539
+
540
+ def test_attributes_are_being_set_when_initialized_from_belongs_to_association_with_where_clause
541
+ new_firm = accounts(:signals37).build_firm(:name => 'Apple')
542
+ assert_equal new_firm.name, "Apple"
543
+ end
544
+
545
+ def test_reassigning_the_parent_id_updates_the_object
546
+ client = companies(:second_client)
547
+
548
+ client.firm
549
+ client.firm_with_condition
550
+ firm_proxy = client.send(:association_instance_get, :firm)
551
+ firm_with_condition_proxy = client.send(:association_instance_get, :firm_with_condition)
552
+
553
+ assert !firm_proxy.stale_target?
554
+ assert !firm_with_condition_proxy.stale_target?
555
+ assert_equal companies(:first_firm), client.firm
556
+ assert_equal companies(:first_firm), client.firm_with_condition
557
+
558
+ client.client_of = companies(:another_firm).id
559
+
560
+ assert firm_proxy.stale_target?
561
+ assert firm_with_condition_proxy.stale_target?
562
+ assert_equal companies(:another_firm), client.firm
563
+ assert_equal companies(:another_firm), client.firm_with_condition
564
+ end
565
+
566
+ def test_polymorphic_reassignment_of_associated_id_updates_the_object
567
+ sponsor = sponsors(:moustache_club_sponsor_for_groucho)
568
+
569
+ sponsor.sponsorable
570
+ proxy = sponsor.send(:association_instance_get, :sponsorable)
571
+
572
+ assert !proxy.stale_target?
573
+ assert_equal members(:groucho), sponsor.sponsorable
574
+
575
+ sponsor.sponsorable_id = members(:some_other_guy).id
576
+
577
+ assert proxy.stale_target?
578
+ assert_equal members(:some_other_guy), sponsor.sponsorable
579
+ end
580
+
581
+ def test_polymorphic_reassignment_of_associated_type_updates_the_object
582
+ sponsor = sponsors(:moustache_club_sponsor_for_groucho)
583
+
584
+ sponsor.sponsorable
585
+ proxy = sponsor.send(:association_instance_get, :sponsorable)
586
+
587
+ assert !proxy.stale_target?
588
+ assert_equal members(:groucho), sponsor.sponsorable
589
+
590
+ sponsor.sponsorable_type = 'Firm'
591
+
592
+ assert proxy.stale_target?
593
+ assert_equal companies(:first_firm), sponsor.sponsorable
594
+ end
595
+
596
+ def test_reloading_association_with_key_change
597
+ client = companies(:second_client)
598
+ firm = client.association(:firm)
599
+
600
+ client.firm = companies(:another_firm)
601
+ firm.reload
602
+ assert_equal companies(:another_firm), firm.target
603
+
604
+ client.client_of = companies(:first_firm).id
605
+ firm.reload
606
+ assert_equal companies(:first_firm), firm.target
607
+ end
608
+
609
+ def test_polymorphic_counter_cache
610
+ tagging = taggings(:welcome_general)
611
+ post = posts(:welcome)
612
+ comment = comments(:greetings)
613
+
614
+ assert_difference lambda { post.reload.taggings_count }, -1 do
615
+ assert_difference 'comment.reload.taggings_count', +1 do
616
+ tagging.taggable = comment
617
+ end
618
+ end
619
+ end
620
+
621
+ def test_polymorphic_with_custom_foreign_type
622
+ sponsor = sponsors(:moustache_club_sponsor_for_groucho)
623
+ groucho = members(:groucho)
624
+ other = members(:some_other_guy)
625
+
626
+ assert_equal groucho, sponsor.sponsorable
627
+ assert_equal groucho, sponsor.thing
628
+
629
+ sponsor.thing = other
630
+
631
+ assert_equal other, sponsor.sponsorable
632
+ assert_equal other, sponsor.thing
633
+
634
+ sponsor.sponsorable = groucho
635
+
636
+ assert_equal groucho, sponsor.sponsorable
637
+ assert_equal groucho, sponsor.thing
638
+ end
639
+
640
+ def test_build_with_conditions
641
+ client = companies(:second_client)
642
+ firm = client.build_bob_firm
643
+
644
+ assert_equal "Bob", firm.name
645
+ end
646
+
647
+ def test_create_with_conditions
648
+ client = companies(:second_client)
649
+ firm = client.create_bob_firm
650
+
651
+ assert_equal "Bob", firm.name
652
+ end
653
+
654
+ def test_create_bang_with_conditions
655
+ client = companies(:second_client)
656
+ firm = client.create_bob_firm!
657
+
658
+ assert_equal "Bob", firm.name
659
+ end
660
+
661
+ def test_build_with_block
662
+ client = Client.create(:name => 'Client Company')
663
+
664
+ firm = client.build_firm{ |f| f.name = 'Agency Company' }
665
+ assert_equal 'Agency Company', firm.name
666
+ end
667
+
668
+ def test_create_with_block
669
+ client = Client.create(:name => 'Client Company')
670
+
671
+ firm = client.create_firm{ |f| f.name = 'Agency Company' }
672
+ assert_equal 'Agency Company', firm.name
673
+ end
674
+
675
+ def test_create_bang_with_block
676
+ client = Client.create(:name => 'Client Company')
677
+
678
+ firm = client.create_firm!{ |f| f.name = 'Agency Company' }
679
+ assert_equal 'Agency Company', firm.name
680
+ end
681
+
682
+ def test_should_set_foreign_key_on_create_association
683
+ client = Client.create! :name => "fuu"
684
+
685
+ firm = client.create_firm :name => "baa"
686
+ assert_equal firm.id, client.client_of
687
+ end
688
+
689
+ def test_should_set_foreign_key_on_create_association!
690
+ client = Client.create! :name => "fuu"
691
+
692
+ firm = client.create_firm! :name => "baa"
693
+ assert_equal firm.id, client.client_of
694
+ end
695
+
696
+ def test_self_referential_belongs_to_with_counter_cache_assigning_nil
697
+ comment = Comment.create! :post => posts(:thinking), :body => "fuu"
698
+ comment.parent = nil
699
+ comment.save!
700
+
701
+ assert_equal nil, comment.reload.parent
702
+ assert_equal 0, comments(:greetings).reload.children_count
703
+ end
704
+
705
+ def test_polymorphic_with_custom_primary_key
706
+ toy = Toy.create!
707
+ sponsor = Sponsor.create!(:sponsorable => toy)
708
+
709
+ assert_equal toy, sponsor.reload.sponsorable
710
+ end
711
+ end