Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / activerecord / test / cases / associations / eager_test.rb
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_string_with_quoted_table_name
227 quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
228 assert_nothing_raised do
229 Comment.find(:all, :include => :post, :conditions => ["#{quoted_posts_id} = ?",4])
230 end
231 end
232
233 def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
234 assert_nothing_raised do
235 Comment.find(:all, :include => :post, :order => 'posts.id')
236 end
237 end
238
239 def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
240 quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
241 assert_nothing_raised do
242 Comment.find(:all, :include => :post, :order => quoted_posts_id)
243 end
244 end
245
246 def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
247 posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :order => 'posts.id')
248 assert_equal 1, posts.length
249 assert_equal [1], posts.collect { |p| p.id }
250 end
251
252 def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
253 posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id')
254 assert_equal 1, posts.length
255 assert_equal [2], posts.collect { |p| p.id }
256 end
257
258 def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
259 author_favorite = AuthorFavorite.find(:first, :include => :favorite_author)
260 assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author }
261 end
262
263 def test_eager_load_belongs_to_quotes_table_and_column_names
264 job = Job.find jobs(:unicyclist).id, :include => :ideal_reference
265 references(:michael_unicyclist)
266 assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference}
267 end
268
269 def test_eager_load_has_one_quotes_table_and_column_names
270 michael = Person.find(people(:michael), :include => :favourite_reference)
271 references(:michael_unicyclist)
272 assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
273 end
274
275 def test_eager_load_has_many_quotes_table_and_column_names
276 michael = Person.find(people(:michael), :include => :references)
277 references(:michael_magician,:michael_unicyclist)
278 assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&:id) }
279 end
280
281 def test_eager_load_has_many_through_quotes_table_and_column_names
282 michael = Person.find(people(:michael), :include => :jobs)
283 jobs(:magician, :unicyclist)
284 assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&:id) }
285 end
286
287 def test_eager_load_has_many_with_string_keys
288 subscriptions = subscriptions(:webster_awdr, :webster_rfr)
289 subscriber =Subscriber.find(subscribers(:second).id, :include => :subscriptions)
290 assert_equal subscriptions, subscriber.subscriptions.sort_by(&:id)
291 end
292
293 def test_eager_load_has_many_through_with_string_keys
294 books = books(:awdr, :rfr)
295 subscriber = Subscriber.find(subscribers(:second).id, :include => :books)
296 assert_equal books, subscriber.books.sort_by(&:id)
297 end
298
299 def test_eager_load_belongs_to_with_string_keys
300 subscriber = subscribers(:second)
301 subscription = Subscription.find(subscriptions(:webster_awdr).id, :include => :subscriber)
302 assert_equal subscriber, subscription.subscriber
303 end
304
305 def test_eager_association_loading_with_explicit_join
306 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')
307 assert_equal 1, posts.length
308 end
309
310 def test_eager_with_has_many_through
311 posts_with_comments = people(:michael).posts.find(:all, :include => :comments, :order => 'posts.id')
312 posts_with_author = people(:michael).posts.find(:all, :include => :author, :order => 'posts.id')
313 posts_with_comments_and_author = people(:michael).posts.find(:all, :include => [ :comments, :author ], :order => 'posts.id')
314 assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size }
315 assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
316 assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
317 end
318
319 def test_eager_with_has_many_through_a_belongs_to_association
320 author = authors(:mary)
321 post = Post.create!(:author => author, :title => "TITLE", :body => "BODY")
322 author.author_favorites.create(:favorite_author_id => 1)
323 author.author_favorites.create(:favorite_author_id => 2)
324 posts_with_author_favorites = author.posts.find(:all, :include => :author_favorites)
325 assert_no_queries { posts_with_author_favorites.first.author_favorites.first.author_id }
326 end
327
328 def test_eager_with_has_many_through_an_sti_join_model
329 author = Author.find(:first, :include => :special_post_comments, :order => 'authors.id')
330 assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
331 end
332
333 def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
334 author = Author.find(:first, :include => :special_nonexistant_post_comments, :order => 'authors.id')
335 assert_equal [], author.special_nonexistant_post_comments
336 end
337
338 def test_eager_with_has_many_through_join_model_with_conditions
339 assert_equal Author.find(:first, :include => :hello_post_comments,
340 :order => 'authors.id').hello_post_comments.sort_by(&:id),
341 Author.find(:first, :order => 'authors.id').hello_post_comments.sort_by(&:id)
342 end
343
344 def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
345 assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions.first
346 end
347
348 def test_eager_with_has_many_through_join_model_with_include
349 author_comments = Author.find(authors(:david).id, :include => :comments_with_include).comments_with_include.to_a
350 assert_no_queries do
351 author_comments.first.post.title
352 end
353 end
354
355 def test_eager_with_has_many_and_limit
356 posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
357 assert_equal 2, posts.size
358 assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
359 end
360
361 def test_eager_with_has_many_and_limit_and_conditions
362 if current_adapter?(:OpenBaseAdapter)
363 posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "FETCHBLOB(posts.body) = 'hello'", :order => "posts.id")
364 else
365 posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.body = 'hello'", :order => "posts.id")
366 end
367 assert_equal 2, posts.size
368 assert_equal [4,5], posts.collect { |p| p.id }
369 end
370
371 def test_eager_with_has_many_and_limit_and_conditions_array
372 if current_adapter?(:OpenBaseAdapter)
373 posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "FETCHBLOB(posts.body) = ?", 'hello' ], :order => "posts.id")
374 else
375 posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "posts.body = ?", 'hello' ], :order => "posts.id")
376 end
377 assert_equal 2, posts.size
378 assert_equal [4,5], posts.collect { |p| p.id }
379 end
380
381 def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
382 posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
383 assert_equal 2, posts.size
384
385 count = Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
386 assert_equal count, posts.size
387 end
388
389 def test_eager_with_has_many_and_limit_and_high_offset
390 posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
391 assert_equal 0, posts.size
392 end
393
394 def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions
395 assert_queries(1) do
396 posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
397 :conditions => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ])
398 assert_equal 0, posts.size
399 end
400 end
401
402 def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions
403 assert_queries(1) do
404 posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
405 :conditions => { 'authors.name' => 'David', 'comments.body' => 'go crazy' })
406 assert_equal 0, posts.size
407 end
408 end
409
410 def test_count_eager_with_has_many_and_limit_and_high_offset
411 posts = Post.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
412 assert_equal 0, posts
413 end
414
415 def test_eager_with_has_many_and_limit_with_no_results
416 posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'")
417 assert_equal 0, posts.size
418 end
419
420 def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
421 author = authors(:david)
422 author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
423 assert_equal author_posts_without_comments.size, author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null')
424 end
425
426 def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
427 person = people(:michael)
428 person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
429 assert_equal person_posts_without_comments.size, person.posts_with_no_comments.count
430 end
431
432 def test_eager_with_has_and_belongs_to_many_and_limit
433 posts = Post.find(:all, :include => :categories, :order => "posts.id", :limit => 3)
434 assert_equal 3, posts.size
435 assert_equal 2, posts[0].categories.size
436 assert_equal 1, posts[1].categories.size
437 assert_equal 0, posts[2].categories.size
438 assert posts[0].categories.include?(categories(:technology))
439 assert posts[1].categories.include?(categories(:general))
440 end
441
442 def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
443 posts = authors(:david).posts.find(:all,
444 :include => :comments,
445 :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
446 :limit => 2
447 )
448 assert_equal 2, posts.size
449
450 count = Post.count(
451 :include => [ :comments, :author ],
452 :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
453 :limit => 2
454 )
455 assert_equal count, posts.size
456 end
457
458 def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
459 posts = nil
460 Post.with_scope(:find => {
461 :include => :comments,
462 :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
463 }) do
464 posts = authors(:david).posts.find(:all, :limit => 2)
465 assert_equal 2, posts.size
466 end
467
468 Post.with_scope(:find => {
469 :include => [ :comments, :author ],
470 :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
471 }) do
472 count = Post.count(:limit => 2)
473 assert_equal count, posts.size
474 end
475 end
476
477 def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
478 Post.with_scope(:find => { :conditions => "1=1" }) do
479 posts = authors(:david).posts.find(:all,
480 :include => :comments,
481 :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
482 :limit => 2
483 )
484 assert_equal 2, posts.size
485
486 count = Post.count(
487 :include => [ :comments, :author ],
488 :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
489 :limit => 2
490 )
491 assert_equal count, posts.size
492 end
493 end
494
495 def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
496 posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2)
497 posts_with_scoped_order = Post.with_scope(:find => {:order => 'posts.id DESC'}) do
498 Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
499 end
500 assert_equal posts_with_explicit_order, posts_with_scoped_order
501 end
502
503 def test_eager_association_loading_with_habtm
504 posts = Post.find(:all, :include => :categories, :order => "posts.id")
505 assert_equal 2, posts[0].categories.size
506 assert_equal 1, posts[1].categories.size
507 assert_equal 0, posts[2].categories.size
508 assert posts[0].categories.include?(categories(:technology))
509 assert posts[1].categories.include?(categories(:general))
510 end
511
512 def test_eager_with_inheritance
513 posts = SpecialPost.find(:all, :include => [ :comments ])
514 end
515
516 def test_eager_has_one_with_association_inheritance
517 post = Post.find(4, :include => [ :very_special_comment ])
518 assert_equal "VerySpecialComment", post.very_special_comment.class.to_s
519 end
520
521 def test_eager_has_many_with_association_inheritance
522 post = Post.find(4, :include => [ :special_comments ])
523 post.special_comments.each do |special_comment|
524 assert_equal "SpecialComment", special_comment.class.to_s
525 end
526 end
527
528 def test_eager_habtm_with_association_inheritance
529 post = Post.find(6, :include => [ :special_categories ])
530 assert_equal 1, post.special_categories.size
531 post.special_categories.each do |special_category|
532 assert_equal "SpecialCategory", special_category.class.to_s
533 end
534 end
535
536 def test_eager_with_has_one_dependent_does_not_destroy_dependent
537 assert_not_nil companies(:first_firm).account
538 f = Firm.find(:first, :include => :account,
539 :conditions => ["companies.name = ?", "37signals"])
540 assert_not_nil f.account
541 assert_equal companies(:first_firm, :reload).account, f.account
542 end
543
544 def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
545 author = authors(:david)
546 posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
547 assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size
548 assert_equal posts_with_no_comments, author.posts_with_no_comments
549 end
550
551 def test_eager_with_invalid_association_reference
552 assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
553 post = Post.find(6, :include=> :monkeys )
554 }
555 assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
556 post = Post.find(6, :include=>[ :monkeys ])
557 }
558 assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
559 post = Post.find(6, :include=>[ 'monkeys' ])
560 }
561 assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants") {
562 post = Post.find(6, :include=>[ :monkeys, :elephants ])
563 }
564 end
565
566 def find_all_ordered(className, include=nil)
567 className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
568 end
569
570 def test_limited_eager_with_order
571 assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title)', :limit => 2, :offset => 1)
572 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)
573 end
574
575 def test_limited_eager_with_multiple_order_columns
576 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)
577 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)
578 end
579
580 def test_preload_with_interpolation
581 assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions
582 end
583
584 def test_polymorphic_type_condition
585 post = Post.find(posts(:thinking).id, :include => :taggings)
586 assert post.taggings.include?(taggings(:thinking_general))
587 post = SpecialPost.find(posts(:thinking).id, :include => :taggings)
588 assert post.taggings.include?(taggings(:thinking_general))
589 end
590
591 def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
592 # Eager includes of has many and habtm associations aren't necessarily sorted in the same way
593 def assert_equal_after_sort(item1, item2, item3 = nil)
594 assert_equal(item1.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id})
595 assert_equal(item3.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id}) if item3
596 end
597 # Test regular association, association with conditions, association with
598 # STI, and association with conditions assured not to be true
599 post_types = [:posts, :other_posts, :special_posts]
600 # test both has_many and has_and_belongs_to_many
601 [Author, Category].each do |className|
602 d1 = find_all_ordered(className)
603 # test including all post types at once
604 d2 = find_all_ordered(className, post_types)
605 d1.each_index do |i|
606 assert_equal(d1[i], d2[i])
607 assert_equal_after_sort(d1[i].posts, d2[i].posts)
608 post_types[1..-1].each do |post_type|
609 # test including post_types together
610 d3 = find_all_ordered(className, [:posts, post_type])
611 assert_equal(d1[i], d3[i])
612 assert_equal_after_sort(d1[i].posts, d3[i].posts)
613 assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
614 end
615 end
616 end
617 end
618
619 def test_eager_with_multiple_associations_with_same_table_has_one
620 d1 = find_all_ordered(Firm)
621 d2 = find_all_ordered(Firm, :account)
622 d1.each_index do |i|
623 assert_equal(d1[i], d2[i])
624 assert_equal(d1[i].account, d2[i].account)
625 end
626 end
627
628 def test_eager_with_multiple_associations_with_same_table_belongs_to
629 firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
630 d1 = find_all_ordered(Client)
631 d2 = find_all_ordered(Client, firm_types)
632 d1.each_index do |i|
633 assert_equal(d1[i], d2[i])
634 firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
635 end
636 end
637 def test_eager_with_valid_association_as_string_not_symbol
638 assert_nothing_raised { Post.find(:all, :include => 'comments') }
639 end
640
641 def test_eager_with_floating_point_numbers
642 assert_queries(2) do
643 # Before changes, the floating point numbers will be interpreted as table names and will cause this to run in one query
644 Comment.find :all, :conditions => "123.456 = 123.456", :include => :post
645 end
646 end
647
648 def test_preconfigured_includes_with_belongs_to
649 author = posts(:welcome).author_with_posts
650 assert_no_queries {assert_equal 5, author.posts.size}
651 end
652
653 def test_preconfigured_includes_with_has_one
654 comment = posts(:sti_comments).very_special_comment_with_post
655 assert_no_queries {assert_equal posts(:sti_comments), comment.post}
656 end
657
658 def test_preconfigured_includes_with_has_many
659 posts = authors(:david).posts_with_comments
660 one = posts.detect { |p| p.id == 1 }
661 assert_no_queries do
662 assert_equal 5, posts.size
663 assert_equal 2, one.comments.size
664 end
665 end
666
667 def test_preconfigured_includes_with_habtm
668 posts = authors(:david).posts_with_categories
669 one = posts.detect { |p| p.id == 1 }
670 assert_no_queries do
671 assert_equal 5, posts.size
672 assert_equal 2, one.categories.size
673 end
674 end
675
676 def test_preconfigured_includes_with_has_many_and_habtm
677 posts = authors(:david).posts_with_comments_and_categories
678 one = posts.detect { |p| p.id == 1 }
679 assert_no_queries do
680 assert_equal 5, posts.size
681 assert_equal 2, one.comments.size
682 assert_equal 2, one.categories.size
683 end
684 end
685
686 def test_count_with_include
687 if current_adapter?(:SybaseAdapter)
688 assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15")
689 elsif current_adapter?(:OpenBaseAdapter)
690 assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15")
691 else
692 assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15")
693 end
694 end
695
696 def test_load_with_sti_sharing_association
697 assert_queries(2) do #should not do 1 query per subclass
698 Comment.find :all, :include => :post
699 end
700 end
701
702 def test_conditions_on_join_table_with_include_and_limit
703 assert_equal 3, Developer.find(:all, :include => 'projects', :conditions => 'developers_projects.access_level = 1', :limit => 5).size
704 end
705
706 def test_order_on_join_table_with_include_and_limit
707 assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size
708 end
709
710 def test_eager_loading_with_order_on_joined_table_preloads
711 posts = assert_queries(2) do
712 Post.find(:all, :joins => :comments, :include => :author, :order => 'comments.id DESC')
713 end
714 assert_equal posts(:eager_other), posts[0]
715 assert_equal authors(:mary), assert_no_queries { posts[0].author}
716 end
717
718 def test_eager_loading_with_conditions_on_joined_table_preloads
719 posts = assert_queries(2) do
720 Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
721 end
722 assert_equal [posts(:welcome)], posts
723 assert_equal authors(:david), assert_no_queries { posts[0].author}
724
725 posts = assert_queries(2) do
726 Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
727 end
728 assert_equal [posts(:welcome)], posts
729 assert_equal authors(:david), assert_no_queries { posts[0].author}
730
731 posts = assert_queries(2) do
732 Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id')
733 end
734 assert_equal posts(:welcome, :thinking), posts
735
736 posts = assert_queries(2) do
737 Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id')
738 end
739 assert_equal posts(:welcome, :thinking), posts
740
741 end
742
743 def test_eager_loading_with_conditions_on_string_joined_table_preloads
744 posts = assert_queries(2) do
745 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')
746 end
747 assert_equal [posts(:welcome)], posts
748 assert_equal authors(:david), assert_no_queries { posts[0].author}
749
750 posts = assert_queries(2) do
751 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')
752 end
753 assert_equal [posts(:welcome)], posts
754 assert_equal authors(:david), assert_no_queries { posts[0].author}
755
756 end
757
758 def test_eager_loading_with_select_on_joined_table_preloads
759 posts = assert_queries(2) do
760 Post.find(:all, :select => 'posts.*, authors.name as author_name', :include => :comments, :joins => :author, :order => 'posts.id')
761 end
762 assert_equal 'David', posts[0].author_name
763 assert_equal posts(:welcome).comments, assert_no_queries { posts[0].comments}
764 end
765
766 def test_eager_loading_with_conditions_on_join_model_preloads
767 authors = assert_queries(2) do
768 Author.find(:all, :include => :author_address, :joins => :comments, :conditions => "posts.title like 'Welcome%'")
769 end
770 assert_equal authors(:david), authors[0]
771 assert_equal author_addresses(:david_address), authors[0].author_address
772 end
773
774 def test_preload_belongs_to_uses_exclusive_scope
775 people = Person.males.find(:all, :include => :primary_contact)
776 assert_not_equal people.length, 0
777 people.each do |person|
778 assert_no_queries {assert_not_nil person.primary_contact}
779 assert_equal Person.find(person.id).primary_contact, person.primary_contact
780 end
781 end
782
783 def test_preload_has_many_uses_exclusive_scope
784 people = Person.males.find :all, :include => :agents
785 people.each do |person|
786 assert_equal Person.find(person.id).agents, person.agents
787 end
788 end
789
790 def test_preload_has_many_using_primary_key
791 expected = Firm.find(:first).clients_using_primary_key.to_a
792 firm = Firm.find :first, :include => :clients_using_primary_key
793 assert_no_queries do
794 assert_equal expected, firm.clients_using_primary_key
795 end
796 end
797
798 def test_include_has_many_using_primary_key
799 expected = Firm.find(1).clients_using_primary_key.sort_by &:name
800 firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
801 assert_no_queries do
802 assert_equal expected, firm.clients_using_primary_key
803 end
804 end
805
806 def test_preload_has_one_using_primary_key
807 expected = Firm.find(:first).account_using_primary_key
808 firm = Firm.find :first, :include => :account_using_primary_key
809 assert_no_queries do
810 assert_equal expected, firm.account_using_primary_key
811 end
812 end
813
814 def test_include_has_one_using_primary_key
815 expected = Firm.find(1).account_using_primary_key
816 firm = Firm.find(:all, :include => :account_using_primary_key, :order => 'accounts.id').detect {|f| f.id == 1}
817 assert_no_queries do
818 assert_equal expected, firm.account_using_primary_key
819 end
820 end
821
822 end