3
require 'models/tagging'
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'
13
require 'models/reference'
15
require 'models/subscriber'
16
require 'models/subscription'
18
require 'models/developer'
19
require 'models/project'
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
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))
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))
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
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
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"
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]
63
def test_with_two_tables_in_from_without_getting_double_quoted
64
posts = Post.find(:all,
66
:from => "authors, posts",
67
:include => :comments,
68
:conditions => "posts.author_id = authors.id",
72
assert_equal 2, posts.first.comments.size
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))
82
def test_duplicate_middle_objects
83
comments = Comment.find :all, :conditions => 'post_id = 1', :include => [:post => :author]
85
comments.each {|comment| comment.post.author.name}
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))
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
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)
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
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!
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
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
135
def test_finding_with_includes_on_belongs_to_association_with_same_include_includes_only_once
136
post = posts(:welcome)
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
141
assert_equal author, post.author_with_address
142
assert_equal author_address, post.author_with_address.author_address
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
151
assert_equal nil, post.author_with_address
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
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)
164
assert !Author.find(authors(:david).id, :include => :hello_posts_with_hash_conditions).hello_posts.empty?
167
def test_loading_with_no_associations
168
assert_nil Post.find(posts(:authorless).id, :include => :author).author
171
def test_nested_loading_with_no_associations
172
assert_nothing_raised do
173
Post.find(posts(:authorless).id, :include => {:author => :author_addresss})
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
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)
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 }
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 }
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 }
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 }
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 }
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])
226
def test_eager_association_loading_with_belongs_to_and_conditions_hash
228
assert_nothing_raised do
229
comments = Comment.find(:all, :include => :post, :conditions => {:posts => {:id => 4}}, :limit => 3, :order => 'comments.id')
231
assert_equal 3, comments.length
232
assert_equal [5,6,7], comments.collect { |c| c.id }
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])
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')
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)
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 }
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 }
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 }
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}
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}
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) }
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) }
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)
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)
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
317
def test_eager_association_loading_with_explicit_join
318
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')
319
assert_equal 1, posts.length
322
def test_eager_with_has_many_through
323
posts_with_comments = people(:michael).posts.find(:all, :include => :comments, :order => 'posts.id')
324
posts_with_author = people(:michael).posts.find(:all, :include => :author, :order => 'posts.id')
325
posts_with_comments_and_author = people(:michael).posts.find(:all, :include => [ :comments, :author ], :order => 'posts.id')
326
assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size }
327
assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
328
assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
331
def test_eager_with_has_many_through_a_belongs_to_association
332
author = authors(:mary)
333
post = Post.create!(:author => author, :title => "TITLE", :body => "BODY")
334
author.author_favorites.create(:favorite_author_id => 1)
335
author.author_favorites.create(:favorite_author_id => 2)
336
posts_with_author_favorites = author.posts.find(:all, :include => :author_favorites)
337
assert_no_queries { posts_with_author_favorites.first.author_favorites.first.author_id }
340
def test_eager_with_has_many_through_an_sti_join_model
341
author = Author.find(:first, :include => :special_post_comments, :order => 'authors.id')
342
assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
345
def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
346
author = Author.find(:first, :include => :special_nonexistant_post_comments, :order => 'authors.id')
347
assert_equal [], author.special_nonexistant_post_comments
350
def test_eager_with_has_many_through_join_model_with_conditions
351
assert_equal Author.find(:first, :include => :hello_post_comments,
352
:order => 'authors.id').hello_post_comments.sort_by(&:id),
353
Author.find(:first, :order => 'authors.id').hello_post_comments.sort_by(&:id)
356
def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
357
assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions.first
360
def test_eager_with_has_many_through_join_model_with_include
361
author_comments = Author.find(authors(:david).id, :include => :comments_with_include).comments_with_include.to_a
363
author_comments.first.post.title
367
def test_eager_with_has_many_and_limit
368
posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
369
assert_equal 2, posts.size
370
assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
373
def test_eager_with_has_many_and_limit_and_conditions
374
if current_adapter?(:OpenBaseAdapter)
375
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "FETCHBLOB(posts.body) = 'hello'", :order => "posts.id")
377
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.body = 'hello'", :order => "posts.id")
379
assert_equal 2, posts.size
380
assert_equal [4,5], posts.collect { |p| p.id }
383
def test_eager_with_has_many_and_limit_and_conditions_array
384
if current_adapter?(:OpenBaseAdapter)
385
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "FETCHBLOB(posts.body) = ?", 'hello' ], :order => "posts.id")
387
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "posts.body = ?", 'hello' ], :order => "posts.id")
389
assert_equal 2, posts.size
390
assert_equal [4,5], posts.collect { |p| p.id }
393
def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
394
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
395
assert_equal 2, posts.size
397
count = Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
398
assert_equal count, posts.size
401
def test_eager_with_has_many_and_limit_and_high_offset
402
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
403
assert_equal 0, posts.size
406
def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions
408
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
409
:conditions => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ])
410
assert_equal 0, posts.size
414
def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions
416
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
417
:conditions => { 'authors.name' => 'David', 'comments.body' => 'go crazy' })
418
assert_equal 0, posts.size
422
def test_count_eager_with_has_many_and_limit_and_high_offset
423
posts = Post.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
424
assert_equal 0, posts
427
def test_eager_with_has_many_and_limit_with_no_results
428
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'")
429
assert_equal 0, posts.size
432
def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
433
author = authors(:david)
434
author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
435
assert_equal author_posts_without_comments.size, author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null')
438
def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
439
person = people(:michael)
440
person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
441
assert_equal person_posts_without_comments.size, person.posts_with_no_comments.count
444
def test_eager_with_has_and_belongs_to_many_and_limit
445
posts = Post.find(:all, :include => :categories, :order => "posts.id", :limit => 3)
446
assert_equal 3, posts.size
447
assert_equal 2, posts[0].categories.size
448
assert_equal 1, posts[1].categories.size
449
assert_equal 0, posts[2].categories.size
450
assert posts[0].categories.include?(categories(:technology))
451
assert posts[1].categories.include?(categories(:general))
454
def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
455
posts = authors(:david).posts.find(:all,
456
:include => :comments,
457
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
460
assert_equal 2, posts.size
463
:include => [ :comments, :author ],
464
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
467
assert_equal count, posts.size
470
def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
472
Post.with_scope(:find => {
473
:include => :comments,
474
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
476
posts = authors(:david).posts.find(:all, :limit => 2)
477
assert_equal 2, posts.size
480
Post.with_scope(:find => {
481
:include => [ :comments, :author ],
482
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
484
count = Post.count(:limit => 2)
485
assert_equal count, posts.size
489
def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
490
Post.with_scope(:find => { :conditions => "1=1" }) do
491
posts = authors(:david).posts.find(:all,
492
:include => :comments,
493
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
496
assert_equal 2, posts.size
499
:include => [ :comments, :author ],
500
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
503
assert_equal count, posts.size
507
def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
508
posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2)
509
posts_with_scoped_order = Post.with_scope(:find => {:order => 'posts.id DESC'}) do
510
Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
512
assert_equal posts_with_explicit_order, posts_with_scoped_order
515
def test_eager_association_loading_with_habtm
516
posts = Post.find(:all, :include => :categories, :order => "posts.id")
517
assert_equal 2, posts[0].categories.size
518
assert_equal 1, posts[1].categories.size
519
assert_equal 0, posts[2].categories.size
520
assert posts[0].categories.include?(categories(:technology))
521
assert posts[1].categories.include?(categories(:general))
524
def test_eager_with_inheritance
525
posts = SpecialPost.find(:all, :include => [ :comments ])
528
def test_eager_has_one_with_association_inheritance
529
post = Post.find(4, :include => [ :very_special_comment ])
530
assert_equal "VerySpecialComment", post.very_special_comment.class.to_s
533
def test_eager_has_many_with_association_inheritance
534
post = Post.find(4, :include => [ :special_comments ])
535
post.special_comments.each do |special_comment|
536
assert_equal "SpecialComment", special_comment.class.to_s
540
def test_eager_habtm_with_association_inheritance
541
post = Post.find(6, :include => [ :special_categories ])
542
assert_equal 1, post.special_categories.size
543
post.special_categories.each do |special_category|
544
assert_equal "SpecialCategory", special_category.class.to_s
548
def test_eager_with_has_one_dependent_does_not_destroy_dependent
549
assert_not_nil companies(:first_firm).account
550
f = Firm.find(:first, :include => :account,
551
:conditions => ["companies.name = ?", "37signals"])
552
assert_not_nil f.account
553
assert_equal companies(:first_firm, :reload).account, f.account
556
def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
557
author = authors(:david)
558
posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
559
assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size
560
assert_equal posts_with_no_comments, author.posts_with_no_comments
563
def test_eager_with_invalid_association_reference
564
assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
565
post = Post.find(6, :include=> :monkeys )
567
assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
568
post = Post.find(6, :include=>[ :monkeys ])
570
assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
571
post = Post.find(6, :include=>[ 'monkeys' ])
573
assert_raise(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants") {
574
post = Post.find(6, :include=>[ :monkeys, :elephants ])
578
def find_all_ordered(className, include=nil)
579
className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
582
def test_limited_eager_with_order
583
assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title)', :limit => 2, :offset => 1)
584
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)
587
def test_limited_eager_with_multiple_order_columns
588
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)
589
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)
592
def test_preload_with_interpolation
593
assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions
596
def test_polymorphic_type_condition
597
post = Post.find(posts(:thinking).id, :include => :taggings)
598
assert post.taggings.include?(taggings(:thinking_general))
599
post = SpecialPost.find(posts(:thinking).id, :include => :taggings)
600
assert post.taggings.include?(taggings(:thinking_general))
603
def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
604
# Eager includes of has many and habtm associations aren't necessarily sorted in the same way
605
def assert_equal_after_sort(item1, item2, item3 = nil)
606
assert_equal(item1.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id})
607
assert_equal(item3.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id}) if item3
609
# Test regular association, association with conditions, association with
610
# STI, and association with conditions assured not to be true
611
post_types = [:posts, :other_posts, :special_posts]
612
# test both has_many and has_and_belongs_to_many
613
[Author, Category].each do |className|
614
d1 = find_all_ordered(className)
615
# test including all post types at once
616
d2 = find_all_ordered(className, post_types)
618
assert_equal(d1[i], d2[i])
619
assert_equal_after_sort(d1[i].posts, d2[i].posts)
620
post_types[1..-1].each do |post_type|
621
# test including post_types together
622
d3 = find_all_ordered(className, [:posts, post_type])
623
assert_equal(d1[i], d3[i])
624
assert_equal_after_sort(d1[i].posts, d3[i].posts)
625
assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
631
def test_eager_with_multiple_associations_with_same_table_has_one
632
d1 = find_all_ordered(Firm)
633
d2 = find_all_ordered(Firm, :account)
635
assert_equal(d1[i], d2[i])
636
assert_equal(d1[i].account, d2[i].account)
640
def test_eager_with_multiple_associations_with_same_table_belongs_to
641
firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
642
d1 = find_all_ordered(Client)
643
d2 = find_all_ordered(Client, firm_types)
645
assert_equal(d1[i], d2[i])
646
firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
649
def test_eager_with_valid_association_as_string_not_symbol
650
assert_nothing_raised { Post.find(:all, :include => 'comments') }
653
def test_eager_with_floating_point_numbers
655
# Before changes, the floating point numbers will be interpreted as table names and will cause this to run in one query
656
Comment.find :all, :conditions => "123.456 = 123.456", :include => :post
660
def test_preconfigured_includes_with_belongs_to
661
author = posts(:welcome).author_with_posts
662
assert_no_queries {assert_equal 5, author.posts.size}
665
def test_preconfigured_includes_with_has_one
666
comment = posts(:sti_comments).very_special_comment_with_post
667
assert_no_queries {assert_equal posts(:sti_comments), comment.post}
670
def test_preconfigured_includes_with_has_many
671
posts = authors(:david).posts_with_comments
672
one = posts.detect { |p| p.id == 1 }
674
assert_equal 5, posts.size
675
assert_equal 2, one.comments.size
679
def test_preconfigured_includes_with_habtm
680
posts = authors(:david).posts_with_categories
681
one = posts.detect { |p| p.id == 1 }
683
assert_equal 5, posts.size
684
assert_equal 2, one.categories.size
688
def test_preconfigured_includes_with_has_many_and_habtm
689
posts = authors(:david).posts_with_comments_and_categories
690
one = posts.detect { |p| p.id == 1 }
692
assert_equal 5, posts.size
693
assert_equal 2, one.comments.size
694
assert_equal 2, one.categories.size
698
def test_count_with_include
699
if current_adapter?(:SybaseAdapter)
700
assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15")
701
elsif current_adapter?(:OpenBaseAdapter)
702
assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15")
704
assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15")
708
def test_load_with_sti_sharing_association
709
assert_queries(2) do #should not do 1 query per subclass
710
Comment.find :all, :include => :post
714
def test_conditions_on_join_table_with_include_and_limit
715
assert_equal 3, Developer.find(:all, :include => 'projects', :conditions => 'developers_projects.access_level = 1', :limit => 5).size
718
def test_order_on_join_table_with_include_and_limit
719
assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size
722
def test_eager_loading_with_order_on_joined_table_preloads
723
posts = assert_queries(2) do
724
Post.find(:all, :joins => :comments, :include => :author, :order => 'comments.id DESC')
726
assert_equal posts(:eager_other), posts[0]
727
assert_equal authors(:mary), assert_no_queries { posts[0].author}
730
def test_eager_loading_with_conditions_on_joined_table_preloads
731
posts = assert_queries(2) do
732
Post.find(:all, :select => 'distinct posts.*', :include => :author, :joins => [:comments], :conditions => "comments.body like 'Thank you%'", :order => 'posts.id')
734
assert_equal [posts(:welcome)], posts
735
assert_equal authors(:david), assert_no_queries { posts[0].author}
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')
740
assert_equal [posts(:welcome)], posts
741
assert_equal authors(:david), assert_no_queries { posts[0].author}
743
posts = assert_queries(2) do
744
Post.find(:all, :include => :author, :joins => {:taggings => :tag}, :conditions => "tags.name = 'General'", :order => 'posts.id')
746
assert_equal posts(:welcome, :thinking), posts
748
posts = assert_queries(2) do
749
Post.find(:all, :include => :author, :joins => {:taggings => {:tag => :taggings}}, :conditions => "taggings_tags.super_tag_id=2", :order => 'posts.id')
751
assert_equal posts(:welcome, :thinking), posts
755
def test_eager_loading_with_conditions_on_string_joined_table_preloads
756
posts = assert_queries(2) do
757
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')
759
assert_equal [posts(:welcome)], posts
760
assert_equal authors(:david), assert_no_queries { posts[0].author}
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')
765
assert_equal [posts(:welcome)], posts
766
assert_equal authors(:david), assert_no_queries { posts[0].author}
770
def test_eager_loading_with_select_on_joined_table_preloads
771
posts = assert_queries(2) do
772
Post.find(:all, :select => 'posts.*, authors.name as author_name', :include => :comments, :joins => :author, :order => 'posts.id')
774
assert_equal 'David', posts[0].author_name
775
assert_equal posts(:welcome).comments, assert_no_queries { posts[0].comments}
778
def test_eager_loading_with_conditions_on_join_model_preloads
779
authors = assert_queries(2) do
780
Author.find(:all, :include => :author_address, :joins => :comments, :conditions => "posts.title like 'Welcome%'")
782
assert_equal authors(:david), authors[0]
783
assert_equal author_addresses(:david_address), authors[0].author_address
786
def test_preload_belongs_to_uses_exclusive_scope
787
people = Person.males.find(:all, :include => :primary_contact)
788
assert_not_equal people.length, 0
789
people.each do |person|
790
assert_no_queries {assert_not_nil person.primary_contact}
791
assert_equal Person.find(person.id).primary_contact, person.primary_contact
795
def test_preload_has_many_uses_exclusive_scope
796
people = Person.males.find :all, :include => :agents
797
people.each do |person|
798
assert_equal Person.find(person.id).agents, person.agents
802
def test_preload_has_many_using_primary_key
803
expected = Firm.find(:first).clients_using_primary_key.to_a
804
firm = Firm.find :first, :include => :clients_using_primary_key
806
assert_equal expected, firm.clients_using_primary_key
810
def test_include_has_many_using_primary_key
811
expected = Firm.find(1).clients_using_primary_key.sort_by &:name
812
firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
814
assert_equal expected, firm.clients_using_primary_key
818
def test_preload_has_one_using_primary_key
819
expected = Firm.find(:first).account_using_primary_key
820
firm = Firm.find :first, :include => :account_using_primary_key
822
assert_equal expected, firm.account_using_primary_key
826
def test_include_has_one_using_primary_key
827
expected = Firm.find(1).account_using_primary_key
828
firm = Firm.find(:all, :include => :account_using_primary_key, :order => 'accounts.id').detect {|f| f.id == 1}
830
assert_equal expected, firm.account_using_primary_key