ibm_db 2.5.5 → 2.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGES +5 -0
- data/README +4 -2
- data/ext/ibm_db.c +144 -12
- data/ext/ruby_ibm_db_cli.h +1 -0
- data/lib/IBM_DB.rb +1 -1
- data/lib/active_record/connection_adapters/ibm_db_pstmt.rb +62 -1
- data/test/cases/adapter_test.rb +41 -19
- data/test/cases/associations/belongs_to_associations_test.rb +486 -0
- data/test/cases/associations/cascaded_eager_loading_test.rb +53 -3
- data/test/cases/associations/eager_test.rb +42 -22
- data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +114 -71
- data/test/cases/associations/has_many_through_associations_test.rb +214 -34
- data/test/cases/associations/join_model_test.rb +26 -34
- data/test/cases/attribute_methods_test.rb +387 -78
- data/test/cases/base_test.rb +555 -1183
- data/test/cases/calculations_test.rb +55 -43
- data/test/cases/finder_test.rb +218 -222
- data/test/cases/fixtures_test.rb +44 -20
- data/test/cases/migration_test.rb +638 -242
- data/test/cases/schema_dumper_test.rb +38 -3
- data/test/cases/validations/uniqueness_validation_test.rb +283 -0
- data/test/schema/i5/ibm_db_specific_schema.rb +1 -0
- data/test/schema/ids/ibm_db_specific_schema.rb +1 -0
- data/test/schema/luw/ibm_db_specific_schema.rb +2 -1
- data/test/schema/schema.rb +157 -10
- data/test/schema/zOS/ibm_db_specific_schema.rb +1 -0
- metadata +5 -5
- data/test/cases/helper.rb +0 -75
- data/test/cases/validations_test.rb +0 -1604
@@ -2,14 +2,16 @@ require "cases/helper"
|
|
2
2
|
require 'models/post'
|
3
3
|
require 'models/comment'
|
4
4
|
require 'models/author'
|
5
|
-
require 'models/category'
|
6
5
|
require 'models/categorization'
|
6
|
+
require 'models/category'
|
7
7
|
require 'models/company'
|
8
8
|
require 'models/topic'
|
9
9
|
require 'models/reply'
|
10
|
+
require 'models/person'
|
10
11
|
|
11
12
|
class CascadedEagerLoadingTest < ActiveRecord::TestCase
|
12
|
-
fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments,
|
13
|
+
fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments,
|
14
|
+
:categorizations, :people, :categories
|
13
15
|
|
14
16
|
def test_eager_association_loading_with_cascaded_two_levels
|
15
17
|
authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id")
|
@@ -29,6 +31,54 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
|
|
29
31
|
assert_equal 2, authors[1].categorizations.size
|
30
32
|
end
|
31
33
|
|
34
|
+
def test_eager_association_loading_with_hmt_does_not_table_name_collide_when_joining_associations
|
35
|
+
assert_nothing_raised do
|
36
|
+
Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).all
|
37
|
+
end
|
38
|
+
authors = Author.joins(:posts).eager_load(:comments).where(:posts => {:taggings_count => 1}).all
|
39
|
+
assert_equal 1, assert_no_queries { authors.size }
|
40
|
+
assert_equal 9, assert_no_queries { authors[0].comments.size }
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_eager_association_loading_grafts_stashed_associations_to_correct_parent
|
44
|
+
assert_nothing_raised do
|
45
|
+
Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').order('people.id').all
|
46
|
+
end
|
47
|
+
assert_equal people(:michael), Person.eager_load(:primary_contact => :primary_contact).where('primary_contacts_people_2.first_name = ?', 'Susan').order('people.id').first
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_cascaded_eager_association_loading_with_join_for_count
|
51
|
+
categories = Category.joins(:categorizations).includes([{:posts=>:comments}, :authors])
|
52
|
+
|
53
|
+
assert_nothing_raised do
|
54
|
+
assert_equal 2, categories.count
|
55
|
+
assert_equal 2, categories.all.uniq.size # Must uniq since instantiating with inner joins will get dupes
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_cascaded_eager_association_loading_with_duplicated_includes
|
60
|
+
categories = Category.includes(:categorizations).includes(:categorizations => :author).where("categorizations.id is not null")
|
61
|
+
assert_nothing_raised do
|
62
|
+
assert_equal 2, categories.count
|
63
|
+
assert_equal 2, categories.all.size
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_cascaded_eager_association_loading_with_twice_includes_edge_cases
|
68
|
+
categories = Category.includes(:categorizations => :author).includes(:categorizations => :post).where("posts.id is not null")
|
69
|
+
assert_nothing_raised do
|
70
|
+
assert_equal 2, categories.count
|
71
|
+
assert_equal 2, categories.all.size
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_eager_association_loading_with_join_for_count
|
76
|
+
authors = Author.joins(:special_posts).includes([:posts, :categorizations])
|
77
|
+
|
78
|
+
assert_nothing_raised { authors.count }
|
79
|
+
assert_queries(3) { authors.all }
|
80
|
+
end
|
81
|
+
|
32
82
|
def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
|
33
83
|
authors = Author.find(:all, :include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id")
|
34
84
|
assert_equal 2, authors.size
|
@@ -106,7 +156,7 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
|
|
106
156
|
authors.first.posts.first.special_comments.first.post.very_special_comment
|
107
157
|
end
|
108
158
|
end
|
109
|
-
|
159
|
+
|
110
160
|
def test_eager_association_loading_where_first_level_returns_nil
|
111
161
|
authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC')
|
112
162
|
assert_equal [authors(:mary), authors(:david)], authors
|
@@ -61,14 +61,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
61
61
|
end
|
62
62
|
|
63
63
|
def test_with_two_tables_in_from_without_getting_double_quoted
|
64
|
-
posts = Post.
|
65
|
-
:select => "posts.*",
|
66
|
-
:from => "authors, posts",
|
67
|
-
:include => :comments,
|
68
|
-
:conditions => "posts.author_id = authors.id",
|
69
|
-
:order => "posts.id"
|
70
|
-
)
|
71
|
-
|
64
|
+
posts = Post.select("posts.*").from("authors, posts").eager_load(:comments).where("posts.author_id = authors.id").order("posts.id").to_a
|
72
65
|
assert_equal 2, posts.first.comments.size
|
73
66
|
end
|
74
67
|
|
@@ -117,7 +110,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
117
110
|
author = assert_queries(3) { Author.find(author_id, :include => {:posts_with_comments => :comments}) } # find the author, then find the posts, then find the comments
|
118
111
|
author.posts_with_comments.each do |post_with_comments|
|
119
112
|
assert_equal post_with_comments.comments.length, post_with_comments.comments.count
|
120
|
-
|
113
|
+
assert_nil post_with_comments.comments.uniq!
|
121
114
|
end
|
122
115
|
end
|
123
116
|
|
@@ -301,13 +294,13 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
301
294
|
subscriber =Subscriber.find(subscribers(:second).id, :include => :subscriptions)
|
302
295
|
assert_equal subscriptions, subscriber.subscriptions.sort_by(&:id)
|
303
296
|
end
|
304
|
-
|
297
|
+
|
305
298
|
def test_eager_load_has_many_through_with_string_keys
|
306
299
|
books = books(:awdr, :rfr)
|
307
300
|
subscriber = Subscriber.find(subscribers(:second).id, :include => :books)
|
308
301
|
assert_equal books, subscriber.books.sort_by(&:id)
|
309
302
|
end
|
310
|
-
|
303
|
+
|
311
304
|
def test_eager_load_belongs_to_with_string_keys
|
312
305
|
subscriber = subscribers(:second)
|
313
306
|
subscription = Subscription.find(subscriptions(:webster_awdr).id, :include => :subscriber)
|
@@ -366,6 +359,18 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
366
359
|
end
|
367
360
|
end
|
368
361
|
|
362
|
+
def test_eager_with_has_many_through_association_with_order
|
363
|
+
author_comments = Author.find(authors(:david).id).comments_desc
|
364
|
+
eager_author_comments = Author.find(authors(:david).id, :include => :comments_desc).comments_desc
|
365
|
+
assert_equal eager_author_comments, author_comments
|
366
|
+
end
|
367
|
+
|
368
|
+
def test_eager_with_has_many_through_with_conditions_join_model_with_include
|
369
|
+
post_tags = Post.find(posts(:welcome).id).misc_tags
|
370
|
+
eager_post_tags = Post.find(1, :include => :misc_tags).misc_tags
|
371
|
+
assert_equal post_tags, eager_post_tags
|
372
|
+
end
|
373
|
+
|
369
374
|
def test_eager_with_has_many_and_limit
|
370
375
|
posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
|
371
376
|
assert_equal 2, posts.size
|
@@ -436,7 +441,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
436
441
|
author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
|
437
442
|
assert_equal author_posts_without_comments.size, author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null')
|
438
443
|
end
|
439
|
-
|
444
|
+
|
440
445
|
def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
|
441
446
|
person = people(:michael)
|
442
447
|
person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
|
@@ -471,7 +476,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
471
476
|
|
472
477
|
def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
|
473
478
|
posts = nil
|
474
|
-
Post.
|
479
|
+
Post.send(:with_scope, :find => {
|
475
480
|
:include => :comments,
|
476
481
|
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
|
477
482
|
}) do
|
@@ -479,7 +484,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
479
484
|
assert_equal 2, posts.size
|
480
485
|
end
|
481
486
|
|
482
|
-
Post.
|
487
|
+
Post.send(:with_scope, :find => {
|
483
488
|
:include => [ :comments, :author ],
|
484
489
|
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
|
485
490
|
}) do
|
@@ -489,7 +494,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
489
494
|
end
|
490
495
|
|
491
496
|
def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
|
492
|
-
Post.
|
497
|
+
Post.send(:with_scope, :find => { :conditions => "1=1" }) do
|
493
498
|
posts = authors(:david).posts.find(:all,
|
494
499
|
:include => :comments,
|
495
500
|
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
|
@@ -508,7 +513,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
508
513
|
|
509
514
|
def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
|
510
515
|
posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2)
|
511
|
-
posts_with_scoped_order = Post.
|
516
|
+
posts_with_scoped_order = Post.send(:with_scope, :find => {:order => 'posts.id DESC'}) do
|
512
517
|
Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
|
513
518
|
end
|
514
519
|
assert_equal posts_with_explicit_order, posts_with_scoped_order
|
@@ -592,7 +597,11 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
592
597
|
assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC, posts.id', :limit => 2, :offset => 1)
|
593
598
|
end
|
594
599
|
end
|
595
|
-
|
600
|
+
|
601
|
+
def test_limited_eager_with_numeric_in_association
|
602
|
+
assert_equal people(:david, :susan), Person.find(:all, :include => [:readers, :primary_contact, :number1_fan], :conditions => "number1_fans_people.first_name like 'M%'", :order => 'people.id', :limit => 2, :offset => 0)
|
603
|
+
end
|
604
|
+
|
596
605
|
def test_preload_with_interpolation
|
597
606
|
assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions
|
598
607
|
end
|
@@ -724,7 +733,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
724
733
|
assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size
|
725
734
|
end
|
726
735
|
end
|
727
|
-
|
736
|
+
|
728
737
|
def test_eager_loading_with_order_on_joined_table_preloads
|
729
738
|
posts = assert_queries(2) do
|
730
739
|
Post.find(:all, :joins => :comments, :include => :author, :order => 'comments.id DESC')
|
@@ -814,13 +823,18 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
814
823
|
end
|
815
824
|
|
816
825
|
def test_include_has_many_using_primary_key
|
817
|
-
expected = Firm.find(1).clients_using_primary_key.sort_by
|
818
|
-
|
826
|
+
expected = Firm.find(1).clients_using_primary_key.sort_by(&:name)
|
827
|
+
# Oracle adapter truncates alias to 30 characters
|
828
|
+
if current_adapter?(:OracleAdapter)
|
829
|
+
firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies'[0,30]+'.name'
|
830
|
+
else
|
831
|
+
firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
|
832
|
+
end
|
819
833
|
assert_no_queries do
|
820
834
|
assert_equal expected, firm.clients_using_primary_key
|
821
835
|
end
|
822
836
|
end
|
823
|
-
|
837
|
+
|
824
838
|
def test_preload_has_one_using_primary_key
|
825
839
|
expected = Firm.find(:first).account_using_primary_key
|
826
840
|
firm = Firm.find :first, :include => :account_using_primary_key
|
@@ -838,5 +852,11 @@ class EagerAssociationTest < ActiveRecord::TestCase
|
|
838
852
|
end
|
839
853
|
end
|
840
854
|
end
|
841
|
-
|
855
|
+
|
856
|
+
def test_preloading_empty_polymorphic_parent
|
857
|
+
t = Tagging.create!(:taggable_type => 'Post', :taggable_id => Post.maximum(:id) + 1, :tag => tags(:general))
|
858
|
+
|
859
|
+
assert_queries(2) { @tagging = Tagging.preload(:taggable).find(t.id) }
|
860
|
+
assert_no_queries { assert ! @tagging.taggable }
|
861
|
+
end
|
842
862
|
end
|
@@ -2,20 +2,14 @@ require "cases/helper"
|
|
2
2
|
require 'models/developer'
|
3
3
|
require 'models/project'
|
4
4
|
require 'models/company'
|
5
|
-
require 'models/topic'
|
6
|
-
require 'models/reply'
|
7
|
-
require 'models/computer'
|
8
5
|
require 'models/customer'
|
9
6
|
require 'models/order'
|
10
7
|
require 'models/categorization'
|
11
8
|
require 'models/category'
|
12
9
|
require 'models/post'
|
13
10
|
require 'models/author'
|
14
|
-
require 'models/comment'
|
15
11
|
require 'models/tag'
|
16
12
|
require 'models/tagging'
|
17
|
-
require 'models/person'
|
18
|
-
require 'models/reader'
|
19
13
|
require 'models/parrot'
|
20
14
|
require 'models/pirate'
|
21
15
|
require 'models/treasure'
|
@@ -24,6 +18,9 @@ require 'models/club'
|
|
24
18
|
require 'models/member'
|
25
19
|
require 'models/membership'
|
26
20
|
require 'models/sponsor'
|
21
|
+
require 'models/country'
|
22
|
+
require 'models/treaty'
|
23
|
+
require 'active_support/core_ext/string/conversions'
|
27
24
|
|
28
25
|
class ProjectWithAfterCreateHook < ActiveRecord::Base
|
29
26
|
set_table_name 'projects'
|
@@ -97,6 +94,60 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
97
94
|
fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects,
|
98
95
|
:parrots, :pirates, :treasures, :price_estimates, :tags, :taggings
|
99
96
|
|
97
|
+
def setup_data_for_habtm_case
|
98
|
+
ActiveRecord::Base.connection.execute('delete from countries_treaties')
|
99
|
+
|
100
|
+
country = Country.new(:name => 'India')
|
101
|
+
country.country_id = 'c1'
|
102
|
+
country.save!
|
103
|
+
|
104
|
+
treaty = Treaty.new(:name => 'peace')
|
105
|
+
treaty.treaty_id = 't1'
|
106
|
+
country.treaties << treaty
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_should_property_quote_string_primary_keys
|
110
|
+
setup_data_for_habtm_case
|
111
|
+
|
112
|
+
con = ActiveRecord::Base.connection
|
113
|
+
sql = 'select * from countries_treaties'
|
114
|
+
record = con.select_rows(sql).last
|
115
|
+
assert_equal 'c1', record[0]
|
116
|
+
assert_equal 't1', record[1]
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_should_record_timestamp_for_join_table
|
120
|
+
setup_data_for_habtm_case
|
121
|
+
|
122
|
+
con = ActiveRecord::Base.connection
|
123
|
+
sql = 'select * from countries_treaties'
|
124
|
+
record = con.select_rows(sql).last
|
125
|
+
assert_not_nil record[2]
|
126
|
+
assert_not_nil record[3]
|
127
|
+
if current_adapter?(:Mysql2Adapter, :OracleAdapter)
|
128
|
+
assert_match %r{\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}}, record[2].to_s(:db)
|
129
|
+
assert_match %r{\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}}, record[3].to_s(:db)
|
130
|
+
else
|
131
|
+
assert_match %r{\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}}, record[2]
|
132
|
+
assert_match %r{\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}}, record[3]
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_should_record_timestamp_for_join_table_only_if_timestamp_should_be_recorded
|
137
|
+
begin
|
138
|
+
Treaty.record_timestamps = false
|
139
|
+
setup_data_for_habtm_case
|
140
|
+
|
141
|
+
con = ActiveRecord::Base.connection
|
142
|
+
sql = 'select * from countries_treaties'
|
143
|
+
record = con.select_rows(sql).last
|
144
|
+
assert_nil record[2]
|
145
|
+
assert_nil record[3]
|
146
|
+
ensure
|
147
|
+
Treaty.record_timestamps = true
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
100
151
|
def test_has_and_belongs_to_many
|
101
152
|
david = Developer.find(1)
|
102
153
|
|
@@ -215,10 +266,10 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
215
266
|
no_of_projects = Project.count
|
216
267
|
aredridel = Developer.new("name" => "Aredridel")
|
217
268
|
aredridel.projects.concat([Project.find(1), p = Project.new("name" => "Projekt")])
|
218
|
-
assert aredridel.
|
219
|
-
assert p.
|
269
|
+
assert !aredridel.persisted?
|
270
|
+
assert !p.persisted?
|
220
271
|
assert aredridel.save
|
221
|
-
assert
|
272
|
+
assert aredridel.persisted?
|
222
273
|
assert_equal no_of_devels+1, Developer.count
|
223
274
|
assert_equal no_of_projects+1, Project.count
|
224
275
|
assert_equal 2, aredridel.projects.size
|
@@ -248,13 +299,13 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
248
299
|
devel = Developer.find(1)
|
249
300
|
proj = assert_no_queries { devel.projects.build("name" => "Projekt") }
|
250
301
|
assert !devel.projects.loaded?
|
251
|
-
|
302
|
+
|
252
303
|
assert_equal devel.projects.last, proj
|
253
304
|
assert devel.projects.loaded?
|
254
|
-
|
255
|
-
assert proj.
|
305
|
+
|
306
|
+
assert !proj.persisted?
|
256
307
|
devel.save
|
257
|
-
assert
|
308
|
+
assert proj.persisted?
|
258
309
|
assert_equal devel.projects.last, proj
|
259
310
|
assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
|
260
311
|
end
|
@@ -264,10 +315,10 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
264
315
|
proj1 = devel.projects.build(:name => "Make bed")
|
265
316
|
proj2 = devel.projects.build(:name => "Lie in it")
|
266
317
|
assert_equal devel.projects.last, proj2
|
267
|
-
assert proj2.
|
318
|
+
assert !proj2.persisted?
|
268
319
|
devel.save
|
269
|
-
assert
|
270
|
-
assert
|
320
|
+
assert devel.persisted?
|
321
|
+
assert proj2.persisted?
|
271
322
|
assert_equal devel.projects.last, proj2
|
272
323
|
assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated
|
273
324
|
end
|
@@ -276,11 +327,11 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
276
327
|
devel = Developer.find(1)
|
277
328
|
proj = devel.projects.create("name" => "Projekt")
|
278
329
|
assert !devel.projects.loaded?
|
279
|
-
|
330
|
+
|
280
331
|
assert_equal devel.projects.last, proj
|
281
332
|
assert !devel.projects.loaded?
|
282
333
|
|
283
|
-
assert
|
334
|
+
assert proj.persisted?
|
284
335
|
assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
|
285
336
|
end
|
286
337
|
|
@@ -322,23 +373,25 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
322
373
|
proj1 = devel.projects.build(:name => "Make bed")
|
323
374
|
proj2 = devel.projects.build(:name => "Lie in it")
|
324
375
|
assert_equal devel.projects.last, proj2
|
325
|
-
assert proj2.
|
376
|
+
assert !proj2.persisted?
|
326
377
|
devel.save
|
327
|
-
assert
|
328
|
-
assert
|
378
|
+
assert devel.persisted?
|
379
|
+
assert proj2.persisted?
|
329
380
|
assert_equal devel.projects.last, proj2
|
330
381
|
assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated
|
331
382
|
end
|
332
383
|
|
333
384
|
def test_creation_respects_hash_condition
|
334
|
-
|
335
|
-
|
385
|
+
# in Oracle '' is saved as null therefore need to save ' ' in not null column
|
386
|
+
post = categories(:general).post_with_conditions.build(:body => ' ')
|
387
|
+
|
336
388
|
assert post.save
|
337
389
|
assert_equal 'Yet Another Testing Title', post.title
|
338
|
-
|
339
|
-
another_post = categories(:general).post_with_conditions.create(:body => '')
|
340
390
|
|
341
|
-
|
391
|
+
# in Oracle '' is saved as null therefore need to save ' ' in not null column
|
392
|
+
another_post = categories(:general).post_with_conditions.create(:body => ' ')
|
393
|
+
|
394
|
+
assert another_post.persisted?
|
342
395
|
assert_equal 'Yet Another Testing Title', another_post.title
|
343
396
|
end
|
344
397
|
|
@@ -346,7 +399,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
346
399
|
dev = developers(:jamis)
|
347
400
|
dev.projects << projects(:active_record)
|
348
401
|
dev.projects << projects(:active_record)
|
349
|
-
|
402
|
+
|
350
403
|
assert_equal 3, dev.projects.size
|
351
404
|
assert_equal 1, dev.projects.uniq.size
|
352
405
|
end
|
@@ -420,7 +473,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
420
473
|
def test_removing_associations_on_destroy
|
421
474
|
david = DeveloperWithBeforeDestroyRaise.find(1)
|
422
475
|
assert !david.projects.empty?
|
423
|
-
|
476
|
+
assert_raise(RuntimeError) { david.destroy }
|
424
477
|
assert david.projects.empty?
|
425
478
|
assert DeveloperWithBeforeDestroyRaise.connection.select_all("SELECT * FROM developers_projects WHERE developer_id = 1").empty?
|
426
479
|
end
|
@@ -500,13 +553,13 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
500
553
|
project.developers.class # force load target
|
501
554
|
|
502
555
|
developer = project.developers.first
|
503
|
-
|
556
|
+
|
504
557
|
assert_no_queries do
|
505
558
|
assert project.developers.loaded?
|
506
559
|
assert project.developers.include?(developer)
|
507
560
|
end
|
508
561
|
end
|
509
|
-
|
562
|
+
|
510
563
|
def test_include_checks_if_record_exists_if_target_not_loaded
|
511
564
|
project = projects(:active_record)
|
512
565
|
developer = project.developers.first
|
@@ -562,16 +615,6 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
562
615
|
assert_equal high_id_jamis, projects(:active_record).developers.find_by_name('Jamis')
|
563
616
|
end
|
564
617
|
|
565
|
-
def test_dynamic_find_order_should_override_association_order
|
566
|
-
# Developers are ordered 'name DESC, id DESC'
|
567
|
-
low_id_jamis = developers(:jamis)
|
568
|
-
middle_id_jamis = developers(:poor_jamis)
|
569
|
-
high_id_jamis = projects(:active_record).developers.create(:name => 'Jamis')
|
570
|
-
|
571
|
-
assert_equal low_id_jamis, projects(:active_record).developers.find(:first, :conditions => "name = 'Jamis'", :order => 'id')
|
572
|
-
assert_equal low_id_jamis, projects(:active_record).developers.find_by_name('Jamis', :order => 'id')
|
573
|
-
end
|
574
|
-
|
575
618
|
def test_dynamic_find_all_should_respect_association_order
|
576
619
|
# Developers are ordered 'name DESC, id DESC'
|
577
620
|
low_id_jamis = developers(:jamis)
|
@@ -582,14 +625,9 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
582
625
|
assert_equal [high_id_jamis, middle_id_jamis, low_id_jamis], projects(:active_record).developers.find_all_by_name('Jamis')
|
583
626
|
end
|
584
627
|
|
585
|
-
def
|
586
|
-
|
587
|
-
|
588
|
-
middle_id_jamis = developers(:poor_jamis)
|
589
|
-
high_id_jamis = projects(:active_record).developers.create(:name => 'Jamis')
|
590
|
-
|
591
|
-
assert_equal [low_id_jamis, middle_id_jamis, high_id_jamis], projects(:active_record).developers.find(:all, :conditions => "name = 'Jamis'", :order => 'id')
|
592
|
-
assert_equal [low_id_jamis, middle_id_jamis, high_id_jamis], projects(:active_record).developers.find_all_by_name('Jamis', :order => 'id')
|
628
|
+
def test_find_should_append_to_association_order
|
629
|
+
ordered_developers = projects(:active_record).developers.order('projects.id')
|
630
|
+
assert_equal ['developers.name desc, developers.id desc', 'projects.id'], ordered_developers.order_values
|
593
631
|
end
|
594
632
|
|
595
633
|
def test_dynamic_find_all_should_respect_association_limit
|
@@ -620,11 +658,10 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
620
658
|
end
|
621
659
|
|
622
660
|
def test_find_in_association_with_options
|
623
|
-
developers = projects(:active_record).developers.
|
661
|
+
developers = projects(:active_record).developers.all
|
624
662
|
assert_equal 3, developers.size
|
625
663
|
|
626
|
-
assert_equal developers(:poor_jamis), projects(:active_record).developers.
|
627
|
-
assert_equal developers(:jamis), projects(:active_record).developers.find(:first, :order => "salary DESC")
|
664
|
+
assert_equal developers(:poor_jamis), projects(:active_record).developers.where("salary < 10000").first
|
628
665
|
end
|
629
666
|
|
630
667
|
def test_replace_with_less
|
@@ -729,8 +766,8 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
729
766
|
end
|
730
767
|
|
731
768
|
def test_find_scoped_grouped
|
732
|
-
assert_equal 4, categories(:general).
|
733
|
-
assert_equal 1, categories(:technology).
|
769
|
+
assert_equal 4, categories(:general).posts_grouped_by_title.size
|
770
|
+
assert_equal 1, categories(:technology).posts_grouped_by_title.size
|
734
771
|
end
|
735
772
|
|
736
773
|
def test_find_scoped_grouped_having
|
@@ -777,23 +814,6 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
777
814
|
assert_equal [projects(:active_record), projects(:action_controller)].map(&:id).sort, developer.project_ids.sort
|
778
815
|
end
|
779
816
|
|
780
|
-
unless current_adapter?(:IBM_DBAdapter)
|
781
|
-
def test_select_limited_ids_list
|
782
|
-
# Set timestamps
|
783
|
-
Developer.transaction do
|
784
|
-
Developer.find(:all, :order => 'id').each_with_index do |record, i|
|
785
|
-
record.update_attributes(:created_at => 5.years.ago + (i * 5.minutes))
|
786
|
-
end
|
787
|
-
end
|
788
|
-
|
789
|
-
join_base = ActiveRecord::Associations::ClassMethods::JoinDependency::JoinBase.new(Project)
|
790
|
-
join_dep = ActiveRecord::Associations::ClassMethods::JoinDependency.new(join_base, :developers, nil)
|
791
|
-
projects = Project.send(:select_limited_ids_list, {:order => 'developers.created_at'}, join_dep)
|
792
|
-
assert !projects.include?("'"), projects
|
793
|
-
assert_equal %w(1 2), projects.scan(/\d/).sort
|
794
|
-
end
|
795
|
-
end
|
796
|
-
|
797
817
|
def test_scoped_find_on_through_association_doesnt_return_read_only_records
|
798
818
|
tag = Post.find(1).tags.find_by_name("General")
|
799
819
|
|
@@ -817,7 +837,7 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
817
837
|
assert_equal developer, project.developers.find(:first)
|
818
838
|
assert_equal project, developer.projects.find(:first)
|
819
839
|
end
|
820
|
-
|
840
|
+
|
821
841
|
def test_self_referential_habtm_without_foreign_key_set_should_raise_exception
|
822
842
|
assert_raise(ActiveRecord::HasAndBelongsToManyAssociationForeignKeyNeeded) {
|
823
843
|
Member.class_eval do
|
@@ -854,6 +874,13 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
854
874
|
assert_equal 1, developer.projects.count
|
855
875
|
end
|
856
876
|
|
877
|
+
unless current_adapter?(:PostgreSQLAdapter)
|
878
|
+
def test_count_with_finder_sql
|
879
|
+
assert_equal 3, projects(:active_record).developers_with_finder_sql.count
|
880
|
+
assert_equal 3, projects(:active_record).developers_with_multiline_finder_sql.count
|
881
|
+
end
|
882
|
+
end
|
883
|
+
|
857
884
|
def test_association_proxy_transaction_method_starts_transaction_in_association_class
|
858
885
|
Post.expects(:transaction)
|
859
886
|
Category.find(:first).posts.transaction do
|
@@ -871,4 +898,20 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
|
|
871
898
|
assert_queries(0) { david.projects.columns; david.projects.columns }
|
872
899
|
end
|
873
900
|
|
901
|
+
def test_attributes_are_being_set_when_initialized_from_habm_association_with_where_clause
|
902
|
+
new_developer = projects(:action_controller).developers.where(:name => "Marcelo").build
|
903
|
+
assert_equal new_developer.name, "Marcelo"
|
904
|
+
end
|
905
|
+
|
906
|
+
def test_attributes_are_being_set_when_initialized_from_habm_association_with_multiple_where_clauses
|
907
|
+
new_developer = projects(:action_controller).developers.where(:name => "Marcelo").where(:salary => 90_000).build
|
908
|
+
assert_equal new_developer.name, "Marcelo"
|
909
|
+
assert_equal new_developer.salary, 90_000
|
910
|
+
end
|
911
|
+
|
912
|
+
def test_include_method_in_has_and_belongs_to_many_association_should_return_true_for_instance_added_with_build
|
913
|
+
project = Project.new
|
914
|
+
developer = project.developers.build
|
915
|
+
assert project.developers.include?(developer)
|
916
|
+
end
|
874
917
|
end
|