ibm_db 1.1.1-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/CHANGES +155 -0
  2. data/LICENSE +18 -0
  3. data/MANIFEST +14 -0
  4. data/README +274 -0
  5. data/ext/Makefile.nt32 +181 -0
  6. data/ext/extconf.rb +58 -0
  7. data/ext/ibm_db.c +6553 -0
  8. data/ext/ruby_ibm_db.h +214 -0
  9. data/init.rb +42 -0
  10. data/lib/IBM_DB.rb +2 -0
  11. data/lib/active_record/connection_adapters/ibm_db_adapter.rb +2218 -0
  12. data/lib/active_record/vendor/db2-i5-zOS.yaml +328 -0
  13. data/lib/mswin32/ibm_db.so +0 -0
  14. data/test/cases/adapter_test.rb +180 -0
  15. data/test/cases/associations/cascaded_eager_loading_test.rb +133 -0
  16. data/test/cases/associations/eager_test.rb +842 -0
  17. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +874 -0
  18. data/test/cases/associations/has_many_through_associations_test.rb +281 -0
  19. data/test/cases/associations/join_model_test.rb +801 -0
  20. data/test/cases/attribute_methods_test.rb +312 -0
  21. data/test/cases/base_test.rb +2114 -0
  22. data/test/cases/calculations_test.rb +346 -0
  23. data/test/cases/finder_test.rb +1092 -0
  24. data/test/cases/fixtures_test.rb +660 -0
  25. data/test/cases/migration_test.rb +1618 -0
  26. data/test/cases/schema_dumper_test.rb +197 -0
  27. data/test/cases/validations_test.rb +1604 -0
  28. data/test/connections/native_ibm_db/connection.rb +40 -0
  29. data/test/ibm_db_test.rb +25 -0
  30. data/test/models/warehouse_thing.rb +5 -0
  31. data/test/schema/i5/ibm_db_specific_schema.rb +134 -0
  32. data/test/schema/ids/ibm_db_specific_schema.rb +137 -0
  33. data/test/schema/luw/ibm_db_specific_schema.rb +134 -0
  34. data/test/schema/schema.rb +499 -0
  35. data/test/schema/zOS/ibm_db_specific_schema.rb +205 -0
  36. metadata +115 -0
@@ -0,0 +1,133 @@
1
+ require "cases/helper"
2
+ require 'models/post'
3
+ require 'models/comment'
4
+ require 'models/author'
5
+ require 'models/category'
6
+ require 'models/categorization'
7
+ require 'models/company'
8
+ require 'models/topic'
9
+ require 'models/reply'
10
+
11
+ class CascadedEagerLoadingTest < ActiveRecord::TestCase
12
+ fixtures :authors, :mixins, :companies, :posts, :topics, :accounts, :comments, :categorizations
13
+
14
+ def test_eager_association_loading_with_cascaded_two_levels
15
+ authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id")
16
+ assert_equal 2, authors.size
17
+ assert_equal 5, authors[0].posts.size
18
+ assert_equal 1, authors[1].posts.size
19
+ assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
20
+ end
21
+
22
+ def test_eager_association_loading_with_cascaded_two_levels_and_one_level
23
+ authors = Author.find(:all, :include=>[{:posts=>:comments}, :categorizations], :order=>"authors.id")
24
+ assert_equal 2, authors.size
25
+ assert_equal 5, authors[0].posts.size
26
+ assert_equal 1, authors[1].posts.size
27
+ assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
28
+ assert_equal 1, authors[0].categorizations.size
29
+ assert_equal 2, authors[1].categorizations.size
30
+ end
31
+
32
+ def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
33
+ authors = Author.find(:all, :include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id")
34
+ assert_equal 2, authors.size
35
+ assert_equal 5, authors[0].posts.size
36
+ assert_equal 1, authors[1].posts.size
37
+ assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
38
+ end
39
+
40
+ def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
41
+ authors = Author.find(:all, :include=>{:posts=>[:comments, :author]}, :order=>"authors.id")
42
+ assert_equal 2, authors.size
43
+ assert_equal 5, authors[0].posts.size
44
+ assert_equal authors(:david).name, authors[0].name
45
+ assert_equal [authors(:david).name], authors[0].posts.collect{|post| post.author.name}.uniq
46
+ end
47
+
48
+ def test_eager_association_loading_with_cascaded_two_levels_with_condition
49
+ authors = Author.find(:all, :include=>{:posts=>:comments}, :conditions=>"authors.id=1", :order=>"authors.id")
50
+ assert_equal 1, authors.size
51
+ assert_equal 5, authors[0].posts.size
52
+ end
53
+
54
+ def test_eager_association_loading_with_cascaded_three_levels_by_ping_pong
55
+ firms = Firm.find(:all, :include=>{:account=>{:firm=>:account}}, :order=>"companies.id")
56
+ assert_equal 2, firms.size
57
+ assert_equal firms.first.account, firms.first.account.firm.account
58
+ assert_equal companies(:first_firm).account, assert_no_queries { firms.first.account.firm.account }
59
+ assert_equal companies(:first_firm).account.firm.account, assert_no_queries { firms.first.account.firm.account }
60
+ end
61
+
62
+ def test_eager_association_loading_with_has_many_sti
63
+ topics = Topic.find(:all, :include => :replies, :order => 'topics.id')
64
+ first, second, = topics(:first).replies.size, topics(:second).replies.size
65
+ assert_no_queries do
66
+ assert_equal first, topics[0].replies.size
67
+ assert_equal second, topics[1].replies.size
68
+ end
69
+ end
70
+
71
+ def test_eager_association_loading_with_has_many_sti_and_subclasses
72
+ silly = SillyReply.new(:title => "gaga", :content => "boo-boo", :parent_id => 1)
73
+ silly.parent_id = 1
74
+ assert silly.save
75
+
76
+ topics = Topic.find(:all, :include => :replies, :order => 'topics.id, replies_topics.id')
77
+ assert_no_queries do
78
+ assert_equal 2, topics[0].replies.size
79
+ assert_equal 0, topics[1].replies.size
80
+ end
81
+ end
82
+
83
+ def test_eager_association_loading_with_belongs_to_sti
84
+ replies = Reply.find(:all, :include => :topic, :order => 'topics.id')
85
+ assert replies.include?(topics(:second))
86
+ assert !replies.include?(topics(:first))
87
+ assert_equal topics(:first), assert_no_queries { replies.first.topic }
88
+ end
89
+
90
+ unless current_adapter?(:IBM_DBAdapter)
91
+ def test_eager_association_loading_with_multiple_stis_and_order
92
+ author = Author.find(:first, :include => { :posts => [ :special_comments , :very_special_comment ] }, :order => 'authors.name, comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4')
93
+ assert_equal authors(:david), author
94
+ assert_no_queries do
95
+ author.posts.first.special_comments
96
+ author.posts.first.very_special_comment
97
+ end
98
+ end
99
+ end
100
+
101
+ def test_eager_association_loading_of_stis_with_multiple_references
102
+ authors = Author.find(:all, :include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4')
103
+ assert_equal [authors(:david)], authors
104
+ assert_no_queries do
105
+ authors.first.posts.first.special_comments.first.post.special_comments
106
+ authors.first.posts.first.special_comments.first.post.very_special_comment
107
+ end
108
+ end
109
+
110
+ def test_eager_association_loading_where_first_level_returns_nil
111
+ authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC')
112
+ assert_equal [authors(:mary), authors(:david)], authors
113
+ assert_no_queries do
114
+ authors[1].post_about_thinking.comments.first
115
+ end
116
+ end
117
+ end
118
+
119
+ require 'models/vertex'
120
+ require 'models/edge'
121
+ class CascadedEagerLoadingTest < ActiveRecord::TestCase
122
+ fixtures :edges, :vertices
123
+
124
+ def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
125
+ source = Vertex.find(:first, :include=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id')
126
+ assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first }
127
+ end
128
+
129
+ def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
130
+ sink = Vertex.find(:first, :include=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC')
131
+ assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first }
132
+ end
133
+ end
@@ -0,0 +1,842 @@
1
+ require "cases/helper"
2
+ require 'models/post'
3
+ require 'models/tagging'
4
+ require 'models/tag'
5
+ require 'models/comment'
6
+ require 'models/author'
7
+ require 'models/category'
8
+ require 'models/company'
9
+ require 'models/person'
10
+ require 'models/reader'
11
+ require 'models/owner'
12
+ require 'models/pet'
13
+ require 'models/reference'
14
+ require 'models/job'
15
+ require 'models/subscriber'
16
+ require 'models/subscription'
17
+ require 'models/book'
18
+ require 'models/developer'
19
+ require 'models/project'
20
+
21
+ class EagerAssociationTest < ActiveRecord::TestCase
22
+ fixtures :posts, :comments, :authors, :author_addresses, :categories, :categories_posts,
23
+ :companies, :accounts, :tags, :taggings, :people, :readers,
24
+ :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
25
+ :developers, :projects, :developers_projects
26
+
27
+ def test_loading_with_one_association
28
+ posts = Post.find(:all, :include => :comments)
29
+ post = posts.find { |p| p.id == 1 }
30
+ assert_equal 2, post.comments.size
31
+ assert post.comments.include?(comments(:greetings))
32
+
33
+ post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
34
+ assert_equal 2, post.comments.size
35
+ assert post.comments.include?(comments(:greetings))
36
+
37
+ posts = Post.find(:all, :include => :last_comment)
38
+ post = posts.find { |p| p.id == 1 }
39
+ assert_equal Post.find(1).last_comment, post.last_comment
40
+ end
41
+
42
+ def test_loading_with_one_association_with_non_preload
43
+ posts = Post.find(:all, :include => :last_comment, :order => 'comments.id DESC')
44
+ post = posts.find { |p| p.id == 1 }
45
+ assert_equal Post.find(1).last_comment, post.last_comment
46
+ end
47
+
48
+ def test_loading_conditions_with_or
49
+ posts = authors(:david).posts.find(:all, :include => :comments, :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'")
50
+ assert_nil posts.detect { |p| p.author_id != authors(:david).id },
51
+ "expected to find only david's posts"
52
+ end
53
+
54
+ def test_with_ordering
55
+ list = Post.find(:all, :include => :comments, :order => "posts.id DESC")
56
+ [:eager_other, :sti_habtm, :sti_post_and_comments, :sti_comments,
57
+ :authorless, :thinking, :welcome
58
+ ].each_with_index do |post, index|
59
+ assert_equal posts(post), list[index]
60
+ end
61
+ end
62
+
63
+ def test_with_two_tables_in_from_without_getting_double_quoted
64
+ posts = Post.find(:all,
65
+ :select => "posts.*",
66
+ :from => "authors, posts",
67
+ :include => :comments,
68
+ :conditions => "posts.author_id = authors.id",
69
+ :order => "posts.id"
70
+ )
71
+
72
+ assert_equal 2, posts.first.comments.size
73
+ end
74
+
75
+ def test_loading_with_multiple_associations
76
+ posts = Post.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id")
77
+ assert_equal 2, posts.first.comments.size
78
+ assert_equal 2, posts.first.categories.size
79
+ assert posts.first.comments.include?(comments(:greetings))
80
+ end
81
+
82
+ def test_duplicate_middle_objects
83
+ comments = Comment.find :all, :conditions => 'post_id = 1', :include => [:post => :author]
84
+ assert_no_queries do
85
+ comments.each {|comment| comment.post.author.name}
86
+ end
87
+ end
88
+
89
+ def test_including_duplicate_objects_from_belongs_to
90
+ popular_post = Post.create!(:title => 'foo', :body => "I like cars!")
91
+ comment = popular_post.comments.create!(:body => "lol")
92
+ popular_post.readers.create!(:person => people(:michael))
93
+ popular_post.readers.create!(:person => people(:david))
94
+
95
+ readers = Reader.find(:all, :conditions => ["post_id = ?", popular_post.id],
96
+ :include => {:post => :comments})
97
+ readers.each do |reader|
98
+ assert_equal [comment], reader.post.comments
99
+ end
100
+ end
101
+
102
+ def test_including_duplicate_objects_from_has_many
103
+ car_post = Post.create!(:title => 'foo', :body => "I like cars!")
104
+ car_post.categories << categories(:general)
105
+ car_post.categories << categories(:technology)
106
+
107
+ comment = car_post.comments.create!(:body => "hmm")
108
+ categories = Category.find(:all, :conditions => ["posts.id=?", car_post.id],
109
+ :include => {:posts => :comments})
110
+ categories.each do |category|
111
+ assert_equal [comment], category.posts[0].comments
112
+ end
113
+ end
114
+
115
+ def test_finding_with_includes_on_has_many_association_with_same_include_includes_only_once
116
+ author_id = authors(:david).id
117
+ 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
+ author.posts_with_comments.each do |post_with_comments|
119
+ assert_equal post_with_comments.comments.length, post_with_comments.comments.count
120
+ assert_equal nil, post_with_comments.comments.uniq!
121
+ end
122
+ end
123
+
124
+ def test_finding_with_includes_on_has_one_assocation_with_same_include_includes_only_once
125
+ author = authors(:david)
126
+ post = author.post_about_thinking_with_last_comment
127
+ last_comment = post.last_comment
128
+ author = assert_queries(3) { Author.find(author.id, :include => {:post_about_thinking_with_last_comment => :last_comment})} # find the author, then find the posts, then find the comments
129
+ assert_no_queries do
130
+ assert_equal post, author.post_about_thinking_with_last_comment
131
+ assert_equal last_comment, author.post_about_thinking_with_last_comment.last_comment
132
+ end
133
+ end
134
+
135
+ def test_finding_with_includes_on_belongs_to_association_with_same_include_includes_only_once
136
+ post = posts(:welcome)
137
+ author = post.author
138
+ author_address = author.author_address
139
+ post = assert_queries(3) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author, then find the address
140
+ assert_no_queries do
141
+ assert_equal author, post.author_with_address
142
+ assert_equal author_address, post.author_with_address.author_address
143
+ end
144
+ end
145
+
146
+ def test_finding_with_includes_on_null_belongs_to_association_with_same_include_includes_only_once
147
+ post = posts(:welcome)
148
+ post.update_attributes!(:author => nil)
149
+ post = assert_queries(1) { Post.find(post.id, :include => {:author_with_address => :author_address}) } # find the post, then find the author which is null so no query for the author or address
150
+ assert_no_queries do
151
+ assert_equal nil, post.author_with_address
152
+ end
153
+ end
154
+
155
+ def test_loading_from_an_association
156
+ posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
157
+ assert_equal 2, posts.first.comments.size
158
+ end
159
+
160
+ def test_loading_from_an_association_that_has_a_hash_of_conditions
161
+ assert_nothing_raised do
162
+ Author.find(:all, :include => :hello_posts_with_hash_conditions)
163
+ end
164
+ assert !Author.find(authors(:david).id, :include => :hello_posts_with_hash_conditions).hello_posts.empty?
165
+ end
166
+
167
+ def test_loading_with_no_associations
168
+ assert_nil Post.find(posts(:authorless).id, :include => :author).author
169
+ end
170
+
171
+ def test_nested_loading_with_no_associations
172
+ assert_nothing_raised do
173
+ Post.find(posts(:authorless).id, :include => {:author => :author_addresss})
174
+ end
175
+ end
176
+
177
+ def test_eager_association_loading_with_belongs_to_and_foreign_keys
178
+ pets = Pet.find(:all, :include => :owner)
179
+ assert_equal 3, pets.length
180
+ end
181
+
182
+ def test_eager_association_loading_with_belongs_to
183
+ comments = Comment.find(:all, :include => :post)
184
+ assert_equal 10, comments.length
185
+ titles = comments.map { |c| c.post.title }
186
+ assert titles.include?(posts(:welcome).title)
187
+ assert titles.include?(posts(:sti_post_and_comments).title)
188
+ end
189
+
190
+ def test_eager_association_loading_with_belongs_to_and_limit
191
+ comments = Comment.find(:all, :include => :post, :limit => 5, :order => 'comments.id')
192
+ assert_equal 5, comments.length
193
+ assert_equal [1,2,3,5,6], comments.collect { |c| c.id }
194
+ end
195
+
196
+ def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
197
+ comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :order => 'comments.id')
198
+ assert_equal 3, comments.length
199
+ assert_equal [5,6,7], comments.collect { |c| c.id }
200
+ end
201
+
202
+ def test_eager_association_loading_with_belongs_to_and_limit_and_offset
203
+ comments = Comment.find(:all, :include => :post, :limit => 3, :offset => 2, :order => 'comments.id')
204
+ assert_equal 3, comments.length
205
+ assert_equal [3,5,6], comments.collect { |c| c.id }
206
+ end
207
+
208
+ def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
209
+ comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :offset => 1, :order => 'comments.id')
210
+ assert_equal 3, comments.length
211
+ assert_equal [6,7,8], comments.collect { |c| c.id }
212
+ end
213
+
214
+ def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
215
+ comments = Comment.find(:all, :include => :post, :conditions => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id')
216
+ assert_equal 3, comments.length
217
+ assert_equal [6,7,8], comments.collect { |c| c.id }
218
+ end
219
+
220
+ def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
221
+ assert_nothing_raised do
222
+ Comment.find(:all, :include => :post, :conditions => ['posts.id = ?',4])
223
+ end
224
+ end
225
+
226
+ def test_eager_association_loading_with_belongs_to_and_conditions_hash
227
+ comments = []
228
+ assert_nothing_raised do
229
+ comments = Comment.find(:all, :include => :post, :conditions => {:posts => {:id => 4}}, :limit => 3, :order => 'comments.id')
230
+ end
231
+ assert_equal 3, comments.length
232
+ assert_equal [5,6,7], comments.collect { |c| c.id }
233
+ assert_no_queries do
234
+ comments.first.post
235
+ end
236
+ end
237
+
238
+ def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
239
+ quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
240
+ assert_nothing_raised do
241
+ Comment.find(:all, :include => :post, :conditions => ["#{quoted_posts_id} = ?",4])
242
+ end
243
+ end
244
+
245
+ def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
246
+ assert_nothing_raised do
247
+ Comment.find(:all, :include => :post, :order => 'posts.id')
248
+ end
249
+ end
250
+
251
+ def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
252
+ quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
253
+ assert_nothing_raised do
254
+ Comment.find(:all, :include => :post, :order => quoted_posts_id)
255
+ end
256
+ end
257
+
258
+ def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
259
+ posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :order => 'posts.id')
260
+ assert_equal 1, posts.length
261
+ assert_equal [1], posts.collect { |p| p.id }
262
+ end
263
+
264
+ def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
265
+ posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id')
266
+ assert_equal 1, posts.length
267
+ assert_equal [2], posts.collect { |p| p.id }
268
+ end
269
+
270
+ def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
271
+ author_favorite = AuthorFavorite.find(:first, :include => :favorite_author)
272
+ assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author }
273
+ end
274
+
275
+ def test_eager_load_belongs_to_quotes_table_and_column_names
276
+ job = Job.find jobs(:unicyclist).id, :include => :ideal_reference
277
+ references(:michael_unicyclist)
278
+ assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference}
279
+ end
280
+
281
+ def test_eager_load_has_one_quotes_table_and_column_names
282
+ michael = Person.find(people(:michael), :include => :favourite_reference)
283
+ references(:michael_unicyclist)
284
+ assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
285
+ end
286
+
287
+ def test_eager_load_has_many_quotes_table_and_column_names
288
+ michael = Person.find(people(:michael), :include => :references)
289
+ references(:michael_magician,:michael_unicyclist)
290
+ assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&:id) }
291
+ end
292
+
293
+ def test_eager_load_has_many_through_quotes_table_and_column_names
294
+ michael = Person.find(people(:michael), :include => :jobs)
295
+ jobs(:magician, :unicyclist)
296
+ assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&:id) }
297
+ end
298
+
299
+ def test_eager_load_has_many_with_string_keys
300
+ subscriptions = subscriptions(:webster_awdr, :webster_rfr)
301
+ subscriber =Subscriber.find(subscribers(:second).id, :include => :subscriptions)
302
+ assert_equal subscriptions, subscriber.subscriptions.sort_by(&:id)
303
+ end
304
+
305
+ def test_eager_load_has_many_through_with_string_keys
306
+ books = books(:awdr, :rfr)
307
+ subscriber = Subscriber.find(subscribers(:second).id, :include => :books)
308
+ assert_equal books, subscriber.books.sort_by(&:id)
309
+ end
310
+
311
+ def test_eager_load_belongs_to_with_string_keys
312
+ subscriber = subscribers(:second)
313
+ subscription = Subscription.find(subscriptions(:webster_awdr).id, :include => :subscriber)
314
+ assert_equal subscriber, subscription.subscriber
315
+ end
316
+
317
+ unless current_adapter?(:IBM_DBAdapter)
318
+ def test_eager_association_loading_with_explicit_join
319
+ posts = Post.find(:all, :include => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id')
320
+ assert_equal 1, posts.length
321
+ end
322
+ end
323
+
324
+ def test_eager_with_has_many_through
325
+ posts_with_comments = people(:michael).posts.find(:all, :include => :comments, :order => 'posts.id')
326
+ posts_with_author = people(:michael).posts.find(:all, :include => :author, :order => 'posts.id')
327
+ posts_with_comments_and_author = people(:michael).posts.find(:all, :include => [ :comments, :author ], :order => 'posts.id')
328
+ assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size }
329
+ assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
330
+ assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
331
+ end
332
+
333
+ def test_eager_with_has_many_through_a_belongs_to_association
334
+ author = authors(:mary)
335
+ post = Post.create!(:author => author, :title => "TITLE", :body => "BODY")
336
+ author.author_favorites.create(:favorite_author_id => 1)
337
+ author.author_favorites.create(:favorite_author_id => 2)
338
+ posts_with_author_favorites = author.posts.find(:all, :include => :author_favorites)
339
+ assert_no_queries { posts_with_author_favorites.first.author_favorites.first.author_id }
340
+ end
341
+
342
+ def test_eager_with_has_many_through_an_sti_join_model
343
+ author = Author.find(:first, :include => :special_post_comments, :order => 'authors.id')
344
+ assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
345
+ end
346
+
347
+ def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
348
+ author = Author.find(:first, :include => :special_nonexistant_post_comments, :order => 'authors.id')
349
+ assert_equal [], author.special_nonexistant_post_comments
350
+ end
351
+
352
+ def test_eager_with_has_many_through_join_model_with_conditions
353
+ assert_equal Author.find(:first, :include => :hello_post_comments,
354
+ :order => 'authors.id').hello_post_comments.sort_by(&:id),
355
+ Author.find(:first, :order => 'authors.id').hello_post_comments.sort_by(&:id)
356
+ end
357
+
358
+ def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
359
+ assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions.first
360
+ end
361
+
362
+ def test_eager_with_has_many_through_join_model_with_include
363
+ author_comments = Author.find(authors(:david).id, :include => :comments_with_include).comments_with_include.to_a
364
+ assert_no_queries do
365
+ author_comments.first.post.title
366
+ end
367
+ end
368
+
369
+ def test_eager_with_has_many_and_limit
370
+ posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
371
+ assert_equal 2, posts.size
372
+ assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
373
+ end
374
+
375
+ def test_eager_with_has_many_and_limit_and_conditions
376
+ if current_adapter?(:OpenBaseAdapter)
377
+ posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "FETCHBLOB(posts.body) = 'hello'", :order => "posts.id")
378
+ else
379
+ posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.body = 'hello'", :order => "posts.id")
380
+ end
381
+ assert_equal 2, posts.size
382
+ assert_equal [4,5], posts.collect { |p| p.id }
383
+ end
384
+
385
+ def test_eager_with_has_many_and_limit_and_conditions_array
386
+ if current_adapter?(:OpenBaseAdapter)
387
+ posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "FETCHBLOB(posts.body) = ?", 'hello' ], :order => "posts.id")
388
+ else
389
+ posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "posts.body = ?", 'hello' ], :order => "posts.id")
390
+ end
391
+ assert_equal 2, posts.size
392
+ assert_equal [4,5], posts.collect { |p| p.id }
393
+ end
394
+
395
+ def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
396
+ posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
397
+ assert_equal 2, posts.size
398
+
399
+ count = Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
400
+ assert_equal count, posts.size
401
+ end
402
+
403
+ def test_eager_with_has_many_and_limit_and_high_offset
404
+ posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
405
+ assert_equal 0, posts.size
406
+ end
407
+
408
+ def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions
409
+ assert_queries(1) do
410
+ posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
411
+ :conditions => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ])
412
+ assert_equal 0, posts.size
413
+ end
414
+ end
415
+
416
+ def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions
417
+ assert_queries(1) do
418
+ posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
419
+ :conditions => { 'authors.name' => 'David', 'comments.body' => 'go crazy' })
420
+ assert_equal 0, posts.size
421
+ end
422
+ end
423
+
424
+ def test_count_eager_with_has_many_and_limit_and_high_offset
425
+ posts = Post.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
426
+ assert_equal 0, posts
427
+ end
428
+
429
+ def test_eager_with_has_many_and_limit_with_no_results
430
+ posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'")
431
+ assert_equal 0, posts.size
432
+ end
433
+
434
+ def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
435
+ author = authors(:david)
436
+ author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
437
+ assert_equal author_posts_without_comments.size, author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null')
438
+ end
439
+
440
+ def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
441
+ person = people(:michael)
442
+ person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
443
+ assert_equal person_posts_without_comments.size, person.posts_with_no_comments.count
444
+ end
445
+
446
+ def test_eager_with_has_and_belongs_to_many_and_limit
447
+ posts = Post.find(:all, :include => :categories, :order => "posts.id", :limit => 3)
448
+ assert_equal 3, posts.size
449
+ assert_equal 2, posts[0].categories.size
450
+ assert_equal 1, posts[1].categories.size
451
+ assert_equal 0, posts[2].categories.size
452
+ assert posts[0].categories.include?(categories(:technology))
453
+ assert posts[1].categories.include?(categories(:general))
454
+ end
455
+
456
+ def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
457
+ posts = authors(:david).posts.find(:all,
458
+ :include => :comments,
459
+ :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
460
+ :limit => 2
461
+ )
462
+ assert_equal 2, posts.size
463
+
464
+ count = Post.count(
465
+ :include => [ :comments, :author ],
466
+ :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
467
+ :limit => 2
468
+ )
469
+ assert_equal count, posts.size
470
+ end
471
+
472
+ def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
473
+ posts = nil
474
+ Post.with_scope(:find => {
475
+ :include => :comments,
476
+ :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
477
+ }) do
478
+ posts = authors(:david).posts.find(:all, :limit => 2)
479
+ assert_equal 2, posts.size
480
+ end
481
+
482
+ Post.with_scope(:find => {
483
+ :include => [ :comments, :author ],
484
+ :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
485
+ }) do
486
+ count = Post.count(:limit => 2)
487
+ assert_equal count, posts.size
488
+ end
489
+ end
490
+
491
+ def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
492
+ Post.with_scope(:find => { :conditions => "1=1" }) do
493
+ posts = authors(:david).posts.find(:all,
494
+ :include => :comments,
495
+ :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
496
+ :limit => 2
497
+ )
498
+ assert_equal 2, posts.size
499
+
500
+ count = Post.count(
501
+ :include => [ :comments, :author ],
502
+ :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
503
+ :limit => 2
504
+ )
505
+ assert_equal count, posts.size
506
+ end
507
+ end
508
+
509
+ def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
510
+ 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.with_scope(:find => {:order => 'posts.id DESC'}) do
512
+ Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
513
+ end
514
+ assert_equal posts_with_explicit_order, posts_with_scoped_order
515
+ end
516
+
517
+ def test_eager_association_loading_with_habtm
518
+ posts = Post.find(:all, :include => :categories, :order => "posts.id")
519
+ assert_equal 2, posts[0].categories.size
520
+ assert_equal 1, posts[1].categories.size
521
+ assert_equal 0, posts[2].categories.size
522
+ assert posts[0].categories.include?(categories(:technology))
523
+ assert posts[1].categories.include?(categories(:general))
524
+ end
525
+
526
+ def test_eager_with_inheritance
527
+ posts = SpecialPost.find(:all, :include => [ :comments ])
528
+ end
529
+
530
+ def test_eager_has_one_with_association_inheritance
531
+ post = Post.find(4, :include => [ :very_special_comment ])
532
+ assert_equal "VerySpecialComment", post.very_special_comment.class.to_s
533
+ end
534
+
535
+ def test_eager_has_many_with_association_inheritance
536
+ post = Post.find(4, :include => [ :special_comments ])
537
+ post.special_comments.each do |special_comment|
538
+ assert_equal "SpecialComment", special_comment.class.to_s
539
+ end
540
+ end
541
+
542
+ def test_eager_habtm_with_association_inheritance
543
+ post = Post.find(6, :include => [ :special_categories ])
544
+ assert_equal 1, post.special_categories.size
545
+ post.special_categories.each do |special_category|
546
+ assert_equal "SpecialCategory", special_category.class.to_s
547
+ end
548
+ end
549
+
550
+ def test_eager_with_has_one_dependent_does_not_destroy_dependent
551
+ assert_not_nil companies(:first_firm).account
552
+ f = Firm.find(:first, :include => :account,
553
+ :conditions => ["companies.name = ?", "37signals"])
554
+ assert_not_nil f.account
555
+ assert_equal companies(:first_firm, :reload).account, f.account
556
+ end
557
+
558
+ def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
559
+ author = authors(:david)
560
+ posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
561
+ assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size
562
+ assert_equal posts_with_no_comments, author.posts_with_no_comments
563
+ end
564
+
565
+ def test_eager_with_invalid_association_reference
566
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
567
+ post = Post.find(6, :include=> :monkeys )
568
+ }
569
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
570
+ post = Post.find(6, :include=>[ :monkeys ])
571
+ }
572
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
573
+ post = Post.find(6, :include=>[ 'monkeys' ])
574
+ }
575
+ assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants") {
576
+ post = Post.find(6, :include=>[ :monkeys, :elephants ])
577
+ }
578
+ end
579
+
580
+ def find_all_ordered(className, include=nil)
581
+ className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
582
+ end
583
+
584
+ unless current_adapter?(:IBM_DBAdapter)
585
+ def test_limited_eager_with_order
586
+ assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title)', :limit => 2, :offset => 1)
587
+ assert_equal posts(:sti_post_and_comments, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title) DESC', :limit => 2, :offset => 1)
588
+ end
589
+
590
+ def test_limited_eager_with_multiple_order_columns
591
+ assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title), posts.id', :limit => 2, :offset => 1)
592
+ 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
+ end
594
+ end
595
+
596
+ def test_preload_with_interpolation
597
+ assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions
598
+ end
599
+
600
+ def test_polymorphic_type_condition
601
+ post = Post.find(posts(:thinking).id, :include => :taggings)
602
+ assert post.taggings.include?(taggings(:thinking_general))
603
+ post = SpecialPost.find(posts(:thinking).id, :include => :taggings)
604
+ assert post.taggings.include?(taggings(:thinking_general))
605
+ end
606
+
607
+ def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
608
+ # Eager includes of has many and habtm associations aren't necessarily sorted in the same way
609
+ def assert_equal_after_sort(item1, item2, item3 = nil)
610
+ assert_equal(item1.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id})
611
+ assert_equal(item3.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id}) if item3
612
+ end
613
+ # Test regular association, association with conditions, association with
614
+ # STI, and association with conditions assured not to be true
615
+ post_types = [:posts, :other_posts, :special_posts]
616
+ # test both has_many and has_and_belongs_to_many
617
+ [Author, Category].each do |className|
618
+ d1 = find_all_ordered(className)
619
+ # test including all post types at once
620
+ d2 = find_all_ordered(className, post_types)
621
+ d1.each_index do |i|
622
+ assert_equal(d1[i], d2[i])
623
+ assert_equal_after_sort(d1[i].posts, d2[i].posts)
624
+ post_types[1..-1].each do |post_type|
625
+ # test including post_types together
626
+ d3 = find_all_ordered(className, [:posts, post_type])
627
+ assert_equal(d1[i], d3[i])
628
+ assert_equal_after_sort(d1[i].posts, d3[i].posts)
629
+ assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
630
+ end
631
+ end
632
+ end
633
+ end
634
+
635
+ def test_eager_with_multiple_associations_with_same_table_has_one
636
+ d1 = find_all_ordered(Firm)
637
+ d2 = find_all_ordered(Firm, :account)
638
+ d1.each_index do |i|
639
+ assert_equal(d1[i], d2[i])
640
+ assert_equal(d1[i].account, d2[i].account)
641
+ end
642
+ end
643
+
644
+ def test_eager_with_multiple_associations_with_same_table_belongs_to
645
+ firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
646
+ d1 = find_all_ordered(Client)
647
+ d2 = find_all_ordered(Client, firm_types)
648
+ d1.each_index do |i|
649
+ assert_equal(d1[i], d2[i])
650
+ firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
651
+ end
652
+ end
653
+ def test_eager_with_valid_association_as_string_not_symbol
654
+ assert_nothing_raised { Post.find(:all, :include => 'comments') }
655
+ end
656
+
657
+ def test_eager_with_floating_point_numbers
658
+ assert_queries(2) do
659
+ # Before changes, the floating point numbers will be interpreted as table names and will cause this to run in one query
660
+ Comment.find :all, :conditions => "123.456 = 123.456", :include => :post
661
+ end
662
+ end
663
+
664
+ def test_preconfigured_includes_with_belongs_to
665
+ author = posts(:welcome).author_with_posts
666
+ assert_no_queries {assert_equal 5, author.posts.size}
667
+ end
668
+
669
+ def test_preconfigured_includes_with_has_one
670
+ comment = posts(:sti_comments).very_special_comment_with_post
671
+ assert_no_queries {assert_equal posts(:sti_comments), comment.post}
672
+ end
673
+
674
+ def test_preconfigured_includes_with_has_many
675
+ posts = authors(:david).posts_with_comments
676
+ one = posts.detect { |p| p.id == 1 }
677
+ assert_no_queries do
678
+ assert_equal 5, posts.size
679
+ assert_equal 2, one.comments.size
680
+ end
681
+ end
682
+
683
+ def test_preconfigured_includes_with_habtm
684
+ posts = authors(:david).posts_with_categories
685
+ one = posts.detect { |p| p.id == 1 }
686
+ assert_no_queries do
687
+ assert_equal 5, posts.size
688
+ assert_equal 2, one.categories.size
689
+ end
690
+ end
691
+
692
+ def test_preconfigured_includes_with_has_many_and_habtm
693
+ posts = authors(:david).posts_with_comments_and_categories
694
+ one = posts.detect { |p| p.id == 1 }
695
+ assert_no_queries do
696
+ assert_equal 5, posts.size
697
+ assert_equal 2, one.comments.size
698
+ assert_equal 2, one.categories.size
699
+ end
700
+ end
701
+
702
+ def test_count_with_include
703
+ if current_adapter?(:SybaseAdapter)
704
+ assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15")
705
+ elsif current_adapter?(:OpenBaseAdapter)
706
+ assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15")
707
+ else
708
+ assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15")
709
+ end
710
+ end
711
+
712
+ def test_load_with_sti_sharing_association
713
+ assert_queries(2) do #should not do 1 query per subclass
714
+ Comment.find :all, :include => :post
715
+ end
716
+ end
717
+
718
+ def test_conditions_on_join_table_with_include_and_limit
719
+ assert_equal 3, Developer.find(:all, :include => 'projects', :conditions => 'developers_projects.access_level = 1', :limit => 5).size
720
+ end
721
+
722
+ unless current_adapter?(:IBM_DBAdapter) #refer db2 ? SQL0214N
723
+ def test_order_on_join_table_with_include_and_limit
724
+ assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size
725
+ end
726
+ end
727
+
728
+ def test_eager_loading_with_order_on_joined_table_preloads
729
+ posts = assert_queries(2) do
730
+ Post.find(:all, :joins => :comments, :include => :author, :order => 'comments.id DESC')
731
+ end
732
+ assert_equal posts(:eager_other), posts[0]
733
+ assert_equal authors(:mary), assert_no_queries { posts[0].author}
734
+ end
735
+
736
+ def test_eager_loading_with_conditions_on_joined_table_preloads
737
+ posts = assert_queries(2) do
738
+ Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
739
+ end
740
+ assert_equal [posts(:welcome)], posts
741
+ assert_equal authors(:david), assert_no_queries { posts[0].author}
742
+
743
+ posts = assert_queries(2) do
744
+ Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
745
+ end
746
+ assert_equal [posts(:welcome)], posts
747
+ assert_equal authors(:david), assert_no_queries { posts[0].author}
748
+
749
+ posts = assert_queries(2) do
750
+ Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id')
751
+ end
752
+ assert_equal posts(:welcome, :thinking), posts
753
+
754
+ posts = assert_queries(2) do
755
+ Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id')
756
+ end
757
+ assert_equal posts(:welcome, :thinking), posts
758
+
759
+ end
760
+
761
+ def test_eager_loading_with_conditions_on_string_joined_table_preloads
762
+ posts = assert_queries(2) do
763
+ Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => "INNER JOIN comments on comments.post_id = posts.id", :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
764
+ end
765
+ assert_equal [posts(:welcome)], posts
766
+ assert_equal authors(:david), assert_no_queries { posts[0].author}
767
+
768
+ posts = assert_queries(2) do
769
+ Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => ["INNER JOIN comments on comments.post_id = posts.id"], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
770
+ end
771
+ assert_equal [posts(:welcome)], posts
772
+ assert_equal authors(:david), assert_no_queries { posts[0].author}
773
+
774
+ end
775
+
776
+ def test_eager_loading_with_select_on_joined_table_preloads
777
+ posts = assert_queries(2) do
778
+ Post.find(:all, :select => 'posts.*, authors.name as author_name', :include => :comments, :joins => :author, :order => 'posts.id')
779
+ end
780
+ assert_equal 'David', posts[0].author_name
781
+ assert_equal posts(:welcome).comments, assert_no_queries { posts[0].comments}
782
+ end
783
+
784
+ def test_eager_loading_with_conditions_on_join_model_preloads
785
+ authors = assert_queries(2) do
786
+ Author.find(:all, :include => :author_address, :joins => :comments, :conditions => "posts.title like 'Welcome%'")
787
+ end
788
+ assert_equal authors(:david), authors[0]
789
+ assert_equal author_addresses(:david_address), authors[0].author_address
790
+ end
791
+
792
+ def test_preload_belongs_to_uses_exclusive_scope
793
+ people = Person.males.find(:all, :include => :primary_contact)
794
+ assert_not_equal people.length, 0
795
+ people.each do |person|
796
+ assert_no_queries {assert_not_nil person.primary_contact}
797
+ assert_equal Person.find(person.id).primary_contact, person.primary_contact
798
+ end
799
+ end
800
+
801
+ def test_preload_has_many_uses_exclusive_scope
802
+ people = Person.males.find :all, :include => :agents
803
+ people.each do |person|
804
+ assert_equal Person.find(person.id).agents, person.agents
805
+ end
806
+ end
807
+
808
+ def test_preload_has_many_using_primary_key
809
+ expected = Firm.find(:first).clients_using_primary_key.to_a
810
+ firm = Firm.find :first, :include => :clients_using_primary_key
811
+ assert_no_queries do
812
+ assert_equal expected, firm.clients_using_primary_key
813
+ end
814
+ end
815
+
816
+ def test_include_has_many_using_primary_key
817
+ expected = Firm.find(1).clients_using_primary_key.sort_by &:name
818
+ firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
819
+ assert_no_queries do
820
+ assert_equal expected, firm.clients_using_primary_key
821
+ end
822
+ end
823
+
824
+ def test_preload_has_one_using_primary_key
825
+ expected = Firm.find(:first).account_using_primary_key
826
+ firm = Firm.find :first, :include => :account_using_primary_key
827
+ assert_no_queries do
828
+ assert_equal expected, firm.account_using_primary_key
829
+ end
830
+ end
831
+
832
+ unless current_adapter?(:IBM_DBAdapter)
833
+ def test_include_has_one_using_primary_key
834
+ expected = Firm.find(1).account_using_primary_key
835
+ firm = Firm.find(:all, :include => :account_using_primary_key, :order => 'accounts.id').detect {|f| f.id == 1}
836
+ assert_no_queries do
837
+ assert_equal expected, firm.account_using_primary_key
838
+ end
839
+ end
840
+ end
841
+
842
+ end