db2 2.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGES +181 -0
  3. data/LICENSE +18 -0
  4. data/MANIFEST +14 -0
  5. data/ParameterizedQueries README +39 -0
  6. data/README +282 -0
  7. data/ext/Makefile.nt32 +181 -0
  8. data/ext/extconf.rb +66 -0
  9. data/ext/ibm_db.c +11166 -0
  10. data/ext/ruby_ibm_db.h +236 -0
  11. data/ext/ruby_ibm_db_cli.c +738 -0
  12. data/ext/ruby_ibm_db_cli.h +431 -0
  13. data/init.rb +42 -0
  14. data/lib/IBM_DB.rb +2 -0
  15. data/lib/active_record/connection_adapters/ibm_db_adapter.rb +2558 -0
  16. data/lib/active_record/connection_adapters/ibm_db_pstmt.rb +1965 -0
  17. data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -0
  18. data/test/cases/adapter_test.rb +202 -0
  19. data/test/cases/associations/belongs_to_associations_test.rb +486 -0
  20. data/test/cases/associations/cascaded_eager_loading_test.rb +183 -0
  21. data/test/cases/associations/eager_test.rb +862 -0
  22. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +917 -0
  23. data/test/cases/associations/has_many_through_associations_test.rb +461 -0
  24. data/test/cases/associations/join_model_test.rb +793 -0
  25. data/test/cases/attribute_methods_test.rb +621 -0
  26. data/test/cases/base_test.rb +1486 -0
  27. data/test/cases/calculations_test.rb +362 -0
  28. data/test/cases/finder_test.rb +1088 -0
  29. data/test/cases/fixtures_test.rb +684 -0
  30. data/test/cases/migration_test.rb +2014 -0
  31. data/test/cases/schema_dumper_test.rb +232 -0
  32. data/test/cases/validations/uniqueness_validation_test.rb +283 -0
  33. data/test/connections/native_ibm_db/connection.rb +42 -0
  34. data/test/ibm_db_test.rb +25 -0
  35. data/test/models/warehouse_thing.rb +5 -0
  36. data/test/schema/i5/ibm_db_specific_schema.rb +135 -0
  37. data/test/schema/ids/ibm_db_specific_schema.rb +138 -0
  38. data/test/schema/luw/ibm_db_specific_schema.rb +135 -0
  39. data/test/schema/schema.rb +647 -0
  40. data/test/schema/zOS/ibm_db_specific_schema.rb +206 -0
  41. metadata +105 -0
@@ -0,0 +1,486 @@
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
+
17
+ class BelongsToAssociationsTest < ActiveRecord::TestCase
18
+ fixtures :accounts, :companies, :developers, :projects, :topics,
19
+ :developers_projects, :computers, :authors, :author_addresses,
20
+ :posts, :tags, :taggings, :comments
21
+
22
+ def test_belongs_to
23
+ Client.find(3).firm.name
24
+ assert_equal companies(:first_firm).name, Client.find(3).firm.name
25
+ assert_not_nil Client.find(3).firm, "Microsoft should have a firm"
26
+ end
27
+
28
+ def test_belongs_to_with_primary_key
29
+ client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
30
+ assert_equal companies(:first_firm).name, client.firm_with_primary_key.name
31
+ end
32
+
33
+ def test_belongs_to_with_primary_key_joins_on_correct_column
34
+ sql = Client.joins(:firm_with_primary_key).to_sql
35
+ if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
36
+ assert_no_match(/`firm_with_primary_keys_companies`\.`id`/, sql)
37
+ assert_match(/`firm_with_primary_keys_companies`\.`name`/, sql)
38
+ elsif current_adapter?(:OracleAdapter)
39
+ # on Oracle aliases are truncated to 30 characters and are quoted in uppercase
40
+ assert_no_match(/"firm_with_primary_keys_compani"\."id"/i, sql)
41
+ assert_match(/"firm_with_primary_keys_compani"\."name"/i, sql)
42
+ elsif current_adapter?(:IBM_DBAdapter)
43
+ # Quoting of column names is not necessary for IBM_DB
44
+ assert_no_match(/firm_with_primary_keys_companies\.id/i, sql)
45
+ assert_match(/firm_with_primary_keys_companies\.name/i, sql)
46
+ else
47
+ assert_no_match(/"firm_with_primary_keys_companies"\."id"/, sql)
48
+ assert_match(/"firm_with_primary_keys_companies"\."name"/, sql)
49
+ end
50
+ end
51
+
52
+ def test_proxy_assignment
53
+ account = Account.find(1)
54
+ assert_nothing_raised { account.firm = account.firm }
55
+ end
56
+
57
+ def test_triple_equality
58
+ assert Client.find(3).firm === Firm
59
+ assert Firm === Client.find(3).firm
60
+ end
61
+
62
+ def test_type_mismatch
63
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 }
64
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) }
65
+ end
66
+
67
+ def test_natural_assignment
68
+ apple = Firm.create("name" => "Apple")
69
+ citibank = Account.create("credit_limit" => 10)
70
+ citibank.firm = apple
71
+ assert_equal apple.id, citibank.firm_id
72
+ end
73
+
74
+ def test_natural_assignment_with_primary_key
75
+ apple = Firm.create("name" => "Apple")
76
+ citibank = Client.create("name" => "Primary key client")
77
+ citibank.firm_with_primary_key = apple
78
+ assert_equal apple.name, citibank.firm_name
79
+ end
80
+
81
+ def test_eager_loading_with_primary_key
82
+ apple = Firm.create("name" => "Apple")
83
+ citibank = Client.create("name" => "Citibank", :firm_name => "Apple")
84
+ citibank_result = Client.find(:first, :conditions => {:name => "Citibank"}, :include => :firm_with_primary_key)
85
+ assert_not_nil citibank_result.instance_variable_get("@firm_with_primary_key")
86
+ end
87
+
88
+ def test_no_unexpected_aliasing
89
+ first_firm = companies(:first_firm)
90
+ another_firm = companies(:another_firm)
91
+
92
+ citibank = Account.create("credit_limit" => 10)
93
+ citibank.firm = first_firm
94
+ original_proxy = citibank.firm
95
+ citibank.firm = another_firm
96
+
97
+ assert_equal first_firm.object_id, original_proxy.target.object_id
98
+ assert_equal another_firm.object_id, citibank.firm.target.object_id
99
+ end
100
+
101
+ def test_creating_the_belonging_object
102
+ citibank = Account.create("credit_limit" => 10)
103
+ apple = citibank.create_firm("name" => "Apple")
104
+ assert_equal apple, citibank.firm
105
+ citibank.save
106
+ citibank.reload
107
+ assert_equal apple, citibank.firm
108
+ end
109
+
110
+ def test_creating_the_belonging_object_with_primary_key
111
+ client = Client.create(:name => "Primary key client")
112
+ apple = client.create_firm_with_primary_key("name" => "Apple")
113
+ assert_equal apple, client.firm_with_primary_key
114
+ client.save
115
+ client.reload
116
+ assert_equal apple, client.firm_with_primary_key
117
+ end
118
+
119
+ def test_building_the_belonging_object
120
+ citibank = Account.create("credit_limit" => 10)
121
+ apple = citibank.build_firm("name" => "Apple")
122
+ citibank.save
123
+ assert_equal apple.id, citibank.firm_id
124
+ end
125
+
126
+ def test_building_the_belonging_object_with_primary_key
127
+ client = Client.create(:name => "Primary key client")
128
+ apple = client.build_firm_with_primary_key("name" => "Apple")
129
+ client.save
130
+ assert_equal apple.name, client.firm_name
131
+ end
132
+
133
+ def test_natural_assignment_to_nil
134
+ client = Client.find(3)
135
+ client.firm = nil
136
+ client.save
137
+ assert_nil client.firm(true)
138
+ assert_nil client.client_of
139
+ end
140
+
141
+ def test_natural_assignment_to_nil_with_primary_key
142
+ client = Client.create(:name => "Primary key client", :firm_name => companies(:first_firm).name)
143
+ client.firm_with_primary_key = nil
144
+ client.save
145
+ assert_nil client.firm_with_primary_key(true)
146
+ assert_nil client.client_of
147
+ end
148
+
149
+ def test_with_different_class_name
150
+ assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
151
+ assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm"
152
+ end
153
+
154
+ def test_with_condition
155
+ assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
156
+ assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
157
+ end
158
+
159
+ def test_with_select
160
+ assert_equal Company.find(2).firm_with_select.attributes.size, 1
161
+ assert_equal Company.find(2, :include => :firm_with_select ).firm_with_select.attributes.size, 1
162
+ end
163
+
164
+ def test_belongs_to_counter
165
+ debate = Topic.create("title" => "debate")
166
+ assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
167
+
168
+ trash = debate.replies.create("title" => "blah!", "content" => "world around!")
169
+ assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
170
+
171
+ trash.destroy
172
+ assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
173
+ end
174
+
175
+ def test_belongs_to_with_primary_key_counter
176
+ debate = Topic.create("title" => "debate")
177
+ assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
178
+
179
+ trash = debate.replies_with_primary_key.create("title" => "blah!", "content" => "world around!")
180
+ assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
181
+
182
+ trash.destroy
183
+ assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
184
+ end
185
+
186
+ def test_belongs_to_counter_with_assigning_nil
187
+ p = Post.find(1)
188
+ c = Comment.find(1)
189
+
190
+ assert_equal p.id, c.post_id
191
+ assert_equal 2, Post.find(p.id).comments.size
192
+
193
+ c.post = nil
194
+
195
+ assert_equal 1, Post.find(p.id).comments.size
196
+ end
197
+
198
+ def test_belongs_to_with_primary_key_counter_with_assigning_nil
199
+ debate = Topic.create("title" => "debate")
200
+ reply = Reply.create("title" => "blah!", "content" => "world around!", "parent_title" => "debate")
201
+
202
+ assert_equal debate.title, reply.parent_title
203
+ assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count")
204
+
205
+ reply.topic_with_primary_key = nil
206
+
207
+ assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count")
208
+ end
209
+
210
+ def test_belongs_to_counter_with_reassigning
211
+ t1 = Topic.create("title" => "t1")
212
+ t2 = Topic.create("title" => "t2")
213
+ r1 = Reply.new("title" => "r1", "content" => "r1")
214
+ r1.topic = t1
215
+
216
+ assert r1.save
217
+ assert_equal 1, Topic.find(t1.id).replies.size
218
+ assert_equal 0, Topic.find(t2.id).replies.size
219
+
220
+ r1.topic = Topic.find(t2.id)
221
+
222
+ assert_no_queries do
223
+ r1.topic = t2
224
+ end
225
+
226
+ assert r1.save
227
+ assert_equal 0, Topic.find(t1.id).replies.size
228
+ assert_equal 1, Topic.find(t2.id).replies.size
229
+
230
+ r1.topic = nil
231
+
232
+ assert_equal 0, Topic.find(t1.id).replies.size
233
+ assert_equal 0, Topic.find(t2.id).replies.size
234
+
235
+ r1.topic = t1
236
+
237
+ assert_equal 1, Topic.find(t1.id).replies.size
238
+ assert_equal 0, Topic.find(t2.id).replies.size
239
+
240
+ r1.destroy
241
+
242
+ assert_equal 0, Topic.find(t1.id).replies.size
243
+ assert_equal 0, Topic.find(t2.id).replies.size
244
+ end
245
+
246
+ def test_belongs_to_reassign_with_namespaced_models_and_counters
247
+ t1 = Web::Topic.create("title" => "t1")
248
+ t2 = Web::Topic.create("title" => "t2")
249
+ r1 = Web::Reply.new("title" => "r1", "content" => "r1")
250
+ r1.topic = t1
251
+
252
+ assert r1.save
253
+ assert_equal 1, Web::Topic.find(t1.id).replies.size
254
+ assert_equal 0, Web::Topic.find(t2.id).replies.size
255
+
256
+ r1.topic = Web::Topic.find(t2.id)
257
+
258
+ assert r1.save
259
+ assert_equal 0, Web::Topic.find(t1.id).replies.size
260
+ assert_equal 1, Web::Topic.find(t2.id).replies.size
261
+ end
262
+
263
+ def test_belongs_to_counter_after_save
264
+ topic = Topic.create!(:title => "monday night")
265
+ topic.replies.create!(:title => "re: monday night", :content => "football")
266
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
267
+
268
+ topic.save!
269
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
270
+ end
271
+
272
+ def test_belongs_to_counter_after_update_attributes
273
+ topic = Topic.create!(:title => "37s")
274
+ topic.replies.create!(:title => "re: 37s", :content => "rails")
275
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
276
+
277
+ topic.update_attributes(:title => "37signals")
278
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
279
+ end
280
+
281
+ def test_assignment_before_child_saved
282
+ final_cut = Client.new("name" => "Final Cut")
283
+ firm = Firm.find(1)
284
+ final_cut.firm = firm
285
+ assert !final_cut.persisted?
286
+ assert final_cut.save
287
+ assert final_cut.persisted?
288
+ assert firm.persisted?
289
+ assert_equal firm, final_cut.firm
290
+ assert_equal firm, final_cut.firm(true)
291
+ end
292
+
293
+ def test_assignment_before_child_saved_with_primary_key
294
+ final_cut = Client.new("name" => "Final Cut")
295
+ firm = Firm.find(1)
296
+ final_cut.firm_with_primary_key = firm
297
+ assert !final_cut.persisted?
298
+ assert final_cut.save
299
+ assert final_cut.persisted?
300
+ assert firm.persisted?
301
+ assert_equal firm, final_cut.firm_with_primary_key
302
+ assert_equal firm, final_cut.firm_with_primary_key(true)
303
+ end
304
+
305
+ def test_new_record_with_foreign_key_but_no_object
306
+ c = Client.new("firm_id" => 1)
307
+ # sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
308
+ assert_equal Firm.find(:first, :order => "id"), c.firm_with_basic_id
309
+ end
310
+
311
+ def test_forgetting_the_load_when_foreign_key_enters_late
312
+ c = Client.new
313
+ assert_nil c.firm_with_basic_id
314
+
315
+ c.firm_id = 1
316
+ # sometimes tests on Oracle fail if ORDER BY is not provided therefore add always :order with :first
317
+ assert_equal Firm.find(:first, :order => "id"), c.firm_with_basic_id
318
+ end
319
+
320
+ def test_field_name_same_as_foreign_key
321
+ computer = Computer.find(1)
322
+ assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
323
+ end
324
+
325
+ def test_counter_cache
326
+ topic = Topic.create :title => "Zoom-zoom-zoom"
327
+ assert_equal 0, topic[:replies_count]
328
+
329
+ reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
330
+ reply.topic = topic
331
+
332
+ assert_equal 1, topic.reload[:replies_count]
333
+ assert_equal 1, topic.replies.size
334
+
335
+ topic[:replies_count] = 15
336
+ assert_equal 15, topic.replies.size
337
+ end
338
+
339
+ def test_custom_counter_cache
340
+ reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
341
+ assert_equal 0, reply[:replies_count]
342
+
343
+ silly = SillyReply.create(:title => "gaga", :content => "boo-boo")
344
+ silly.reply = reply
345
+
346
+ assert_equal 1, reply.reload[:replies_count]
347
+ assert_equal 1, reply.replies.size
348
+
349
+ reply[:replies_count] = 17
350
+ assert_equal 17, reply.replies.size
351
+ end
352
+
353
+ def test_association_assignment_sticks
354
+ post = Post.find(:first)
355
+
356
+ author1, author2 = Author.find(:all, :limit => 2)
357
+ assert_not_nil author1
358
+ assert_not_nil author2
359
+
360
+ # make sure the association is loaded
361
+ post.author
362
+
363
+ # set the association by id, directly
364
+ post.author_id = author2.id
365
+
366
+ # save and reload
367
+ post.save!
368
+ post.reload
369
+
370
+ # the author id of the post should be the id we set
371
+ assert_equal post.author_id, author2.id
372
+ end
373
+
374
+ def test_cant_save_readonly_association
375
+ assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! }
376
+ assert companies(:first_client).readonly_firm.readonly?
377
+ end
378
+
379
+ def test_polymorphic_assignment_foreign_type_field_updating
380
+ # should update when assigning a saved record
381
+ sponsor = Sponsor.new
382
+ member = Member.create
383
+ sponsor.sponsorable = member
384
+ assert_equal "Member", sponsor.sponsorable_type
385
+
386
+ # should update when assigning a new record
387
+ sponsor = Sponsor.new
388
+ member = Member.new
389
+ sponsor.sponsorable = member
390
+ assert_equal "Member", sponsor.sponsorable_type
391
+ end
392
+
393
+ def test_polymorphic_assignment_with_primary_key_foreign_type_field_updating
394
+ # should update when assigning a saved record
395
+ essay = Essay.new
396
+ writer = Author.create(:name => "David")
397
+ essay.writer = writer
398
+ assert_equal "Author", essay.writer_type
399
+
400
+ # should update when assigning a new record
401
+ essay = Essay.new
402
+ writer = Author.new
403
+ essay.writer = writer
404
+ assert_equal "Author", essay.writer_type
405
+ end
406
+
407
+ def test_polymorphic_assignment_updates_foreign_id_field_for_new_and_saved_records
408
+ sponsor = Sponsor.new
409
+ saved_member = Member.create
410
+ new_member = Member.new
411
+
412
+ sponsor.sponsorable = saved_member
413
+ assert_equal saved_member.id, sponsor.sponsorable_id
414
+
415
+ sponsor.sponsorable = new_member
416
+ assert_nil sponsor.sponsorable_id
417
+ end
418
+
419
+ def test_polymorphic_assignment_with_primary_key_updates_foreign_id_field_for_new_and_saved_records
420
+ essay = Essay.new
421
+ saved_writer = Author.create(:name => "David")
422
+ new_writer = Author.new
423
+
424
+ essay.writer = saved_writer
425
+ assert_equal saved_writer.name, essay.writer_id
426
+
427
+ essay.writer = new_writer
428
+ assert_nil essay.writer_id
429
+ end
430
+
431
+ def test_belongs_to_proxy_should_not_respond_to_private_methods
432
+ assert_raise(NoMethodError) { companies(:first_firm).private_method }
433
+ assert_raise(NoMethodError) { companies(:second_client).firm.private_method }
434
+ end
435
+
436
+ def test_belongs_to_proxy_should_respond_to_private_methods_via_send
437
+ companies(:first_firm).send(:private_method)
438
+ companies(:second_client).firm.send(:private_method)
439
+ end
440
+
441
+ def test_save_of_record_with_loaded_belongs_to
442
+ @account = companies(:first_firm).account
443
+
444
+ assert_nothing_raised do
445
+ Account.find(@account.id).save!
446
+ Account.find(@account.id, :include => :firm).save!
447
+ end
448
+
449
+ @account.firm.delete
450
+
451
+ assert_nothing_raised do
452
+ Account.find(@account.id).save!
453
+ Account.find(@account.id, :include => :firm).save!
454
+ end
455
+ end
456
+
457
+ def test_dependent_delete_and_destroy_with_belongs_to
458
+ author_address = author_addresses(:david_address)
459
+ author_address_extra = author_addresses(:david_address_extra)
460
+ assert_equal [], AuthorAddress.destroyed_author_address_ids
461
+
462
+ assert_difference "AuthorAddress.count", -2 do
463
+ authors(:david).destroy
464
+ end
465
+
466
+ assert_equal [], AuthorAddress.find_all_by_id([author_address.id, author_address_extra.id])
467
+ assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids
468
+ end
469
+
470
+ def test_invalid_belongs_to_dependent_option_nullify_raises_exception
471
+ assert_raise ArgumentError do
472
+ Author.belongs_to :special_author_address, :dependent => :nullify
473
+ end
474
+ end
475
+
476
+ def test_invalid_belongs_to_dependent_option_restrict_raises_exception
477
+ assert_raise ArgumentError do
478
+ Author.belongs_to :special_author_address, :dependent => :restrict
479
+ end
480
+ end
481
+
482
+ def test_attributes_are_being_set_when_initialized_from_belongs_to_association_with_where_clause
483
+ new_firm = accounts(:signals37).build_firm(:name => 'Apple')
484
+ assert_equal new_firm.name, "Apple"
485
+ end
486
+ end