eager_test.rb 27.1 KB
Newer Older
1
require "cases/helper"
J
Jeremy Kemper 已提交
2
require 'models/post'
3
require 'models/tagging'
J
Jeremy Kemper 已提交
4 5 6 7 8 9
require 'models/comment'
require 'models/author'
require 'models/category'
require 'models/company'
require 'models/person'
require 'models/reader'
10 11
require 'models/owner'
require 'models/pet'
12 13
require 'models/reference'
require 'models/job'
14 15 16
require 'models/subscriber'
require 'models/subscription'
require 'models/book'
17 18
require 'models/developer'
require 'models/project'
19

20
class EagerAssociationTest < ActiveRecord::TestCase
21
  fixtures :posts, :comments, :authors, :categories, :categories_posts,
22
            :companies, :accounts, :tags, :taggings, :people, :readers,
23
            :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books,
24
            :developers, :projects, :developers_projects
25 26 27

  def test_loading_with_one_association
    posts = Post.find(:all, :include => :comments)
28 29 30
    post = posts.find { |p| p.id == 1 }
    assert_equal 2, post.comments.size
    assert post.comments.include?(comments(:greetings))
31 32 33

    post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
    assert_equal 2, post.comments.size
34
    assert post.comments.include?(comments(:greetings))
35 36 37 38

    posts = Post.find(:all, :include => :last_comment)
    post = posts.find { |p| p.id == 1 }
    assert_equal Post.find(1).last_comment, post.last_comment
39 40
  end

41 42 43 44 45 46
  def test_loading_with_one_association_with_non_preload
    posts = Post.find(:all, :include => :last_comment, :order => 'comments.id DESC')
    post = posts.find { |p| p.id == 1 }
    assert_equal Post.find(1).last_comment, post.last_comment
  end

47
  def test_loading_conditions_with_or
48
    posts = authors(:david).posts.find(:all, :include => :comments, :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'")
49 50 51 52
    assert_nil posts.detect { |p| p.author_id != authors(:david).id },
      "expected to find only david's posts"
  end

53
  def test_with_ordering
54 55 56 57 58 59
    list = Post.find(:all, :include => :comments, :order => "posts.id DESC")
    [:eager_other, :sti_habtm, :sti_post_and_comments, :sti_comments,
     :authorless, :thinking, :welcome
    ].each_with_index do |post, index|
      assert_equal posts(post), list[index]
    end
60
  end
61

62 63 64
  def test_with_two_tables_in_from_without_getting_double_quoted
    posts = Post.find(:all,
      :select     => "posts.*",
D
David Heinemeier Hansson 已提交
65
      :from       => "authors, posts",
66
      :include    => :comments,
67 68
      :conditions => "posts.author_id = authors.id",
      :order      => "posts.id"
69 70 71 72 73
    )

    assert_equal 2, posts.first.comments.size
  end

74
  def test_loading_with_multiple_associations
75
    posts = Post.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id")
76
    assert_equal 2, posts.first.comments.size
77
    assert_equal 2, posts.first.categories.size
78
    assert posts.first.comments.include?(comments(:greetings))
79 80
  end

81 82 83 84 85 86 87
  def test_duplicate_middle_objects
    comments = Comment.find :all, :conditions => 'post_id = 1', :include => [:post => :author]
    assert_no_queries do
      comments.each {|comment| comment.post.author.name}
    end
  end

88
  def test_including_duplicate_objects_from_belongs_to
J
Jeremy Kemper 已提交
89
    popular_post = Post.create!(:title => 'foo', :body => "I like cars!")
90 91 92 93 94 95 96 97 98 99 100 101
    comment = popular_post.comments.create!(:body => "lol")
    popular_post.readers.create!(:person => people(:michael))
    popular_post.readers.create!(:person => people(:david))

    readers = Reader.find(:all, :conditions => ["post_id = ?", popular_post.id],
                                :include => {:post => :comments})
    readers.each do |reader|
      assert_equal [comment], reader.post.comments
    end
  end

  def test_including_duplicate_objects_from_has_many
J
Jeremy Kemper 已提交
102
    car_post = Post.create!(:title => 'foo', :body => "I like cars!")
103 104 105 106 107 108 109 110 111 112 113
    car_post.categories << categories(:general)
    car_post.categories << categories(:technology)

    comment = car_post.comments.create!(:body => "hmm")
    categories = Category.find(:all, :conditions => ["posts.id=?", car_post.id],
                                 :include => {:posts => :comments})
    categories.each do |category|
      assert_equal [comment], category.posts[0].comments
    end
  end

114
  def test_loading_from_an_association
115
    posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
116 117 118
    assert_equal 2, posts.first.comments.size
  end

119
  def test_loading_with_no_associations
120
    assert_nil Post.find(posts(:authorless).id, :include => :author).author
121 122
  end

123 124 125 126 127 128
  def test_nested_loading_with_no_associations
    assert_nothing_raised do
      Post.find(posts(:authorless).id, :include => {:author => :author_addresss})
    end
  end

129 130 131 132 133
  def test_eager_association_loading_with_belongs_to_and_foreign_keys
    pets = Pet.find(:all, :include => :owner)
    assert_equal 3, pets.length
  end

134 135
  def test_eager_association_loading_with_belongs_to
    comments = Comment.find(:all, :include => :post)
136
    assert_equal 10, comments.length
137 138
    titles = comments.map { |c| c.post.title }
    assert titles.include?(posts(:welcome).title)
139
    assert titles.include?(posts(:sti_post_and_comments).title)
140
  end
J
Jeremy Kemper 已提交
141

142
  def test_eager_association_loading_with_belongs_to_and_limit
143
    comments = Comment.find(:all, :include => :post, :limit => 5, :order => 'comments.id')
144 145 146 147 148
    assert_equal 5, comments.length
    assert_equal [1,2,3,5,6], comments.collect { |c| c.id }
  end

  def test_eager_association_loading_with_belongs_to_and_limit_and_conditions
149
    comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :order => 'comments.id')
150 151 152 153 154
    assert_equal 3, comments.length
    assert_equal [5,6,7], comments.collect { |c| c.id }
  end

  def test_eager_association_loading_with_belongs_to_and_limit_and_offset
155
    comments = Comment.find(:all, :include => :post, :limit => 3, :offset => 2, :order => 'comments.id')
156 157 158 159 160
    assert_equal 3, comments.length
    assert_equal [3,5,6], comments.collect { |c| c.id }
  end

  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions
161
    comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :offset => 1, :order => 'comments.id')
162 163 164
    assert_equal 3, comments.length
    assert_equal [6,7,8], comments.collect { |c| c.id }
  end
J
Jeremy Kemper 已提交
165

166 167 168 169 170 171
  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_conditions_array
    comments = Comment.find(:all, :include => :post, :conditions => ['post_id = ?',4], :limit => 3, :offset => 1, :order => 'comments.id')
    assert_equal 3, comments.length
    assert_equal [6,7,8], comments.collect { |c| c.id }
  end

172 173 174 175 176 177 178
  def test_eager_association_loading_with_belongs_to_and_conditions_string_with_unquoted_table_name
    assert_nothing_raised do
      Comment.find(:all, :include => :post, :conditions => ['posts.id = ?',4])
    end
  end

  def test_eager_association_loading_with_belongs_to_and_conditions_string_with_quoted_table_name
179
    quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
180
    assert_nothing_raised do
181
      Comment.find(:all, :include => :post, :conditions => ["#{quoted_posts_id} = ?",4])
182 183 184 185 186 187 188 189 190 191
    end
  end

  def test_eager_association_loading_with_belongs_to_and_order_string_with_unquoted_table_name
    assert_nothing_raised do
      Comment.find(:all, :include => :post, :order => 'posts.id')
    end
  end

  def test_eager_association_loading_with_belongs_to_and_order_string_with_quoted_table_name
192
    quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
193
    assert_nothing_raised do
194
      Comment.find(:all, :include => :post, :order => quoted_posts_id)
195 196 197
    end
  end

198
  def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
199
    posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :order => 'posts.id')
200
    assert_equal 1, posts.length
201
    assert_equal [1], posts.collect { |p| p.id }
202
  end
J
Jeremy Kemper 已提交
203

204
  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
205 206
    posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id')
    assert_equal 1, posts.length
207
    assert_equal [2], posts.collect { |p| p.id }
208
  end
J
Jeremy Kemper 已提交
209

210 211 212 213 214
  def test_eager_association_loading_with_belongs_to_inferred_foreign_key_from_association_name
    author_favorite = AuthorFavorite.find(:first, :include => :favorite_author)
    assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author }
  end

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  def test_eager_load_belongs_to_quotes_table_and_column_names
    job = Job.find jobs(:unicyclist).id, :include => :ideal_reference
    references(:michael_unicyclist)
    assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference}
  end

  def test_eager_load_has_one_quotes_table_and_column_names
    michael = Person.find(people(:michael), :include => :favourite_reference)
    references(:michael_unicyclist)
    assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference}
  end

  def test_eager_load_has_many_quotes_table_and_column_names
    michael = Person.find(people(:michael), :include => :references)
    references(:michael_magician,:michael_unicyclist)
    assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&:id) }
  end

  def test_eager_load_has_many_through_quotes_table_and_column_names
    michael = Person.find(people(:michael), :include => :jobs)
    jobs(:magician, :unicyclist)
    assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&:id) }
  end

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
  def test_eager_load_has_many_with_string_keys
    subscriptions = subscriptions(:webster_awdr, :webster_rfr)
    subscriber =Subscriber.find(subscribers(:second).id, :include => :subscriptions)
    assert_equal subscriptions, subscriber.subscriptions.sort_by(&:id)
  end
  
  def test_eager_load_has_many_through_with_string_keys
    books = books(:awdr, :rfr)
    subscriber = Subscriber.find(subscribers(:second).id, :include => :books)
    assert_equal books, subscriber.books.sort_by(&:id)
  end
  
  def test_eager_load_belongs_to_with_string_keys
    subscriber = subscribers(:second)
    subscription = Subscription.find(subscriptions(:webster_awdr).id, :include => :subscriber)
    assert_equal subscriber, subscription.subscriber
  end

257 258 259 260
  def test_eager_association_loading_with_explicit_join
    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')
    assert_equal 1, posts.length
  end
J
Jeremy Kemper 已提交
261

262
  def test_eager_with_has_many_through
263
    posts_with_comments = people(:michael).posts.find(:all, :include => :comments)
264 265 266
    posts_with_author = people(:michael).posts.find(:all, :include => :author )
    posts_with_comments_and_author = people(:michael).posts.find(:all, :include => [ :comments, :author ])
    assert_equal 2, posts_with_comments.inject(0) { |sum, post| sum += post.comments.size }
267 268
    assert_equal authors(:david), assert_no_queries { posts_with_author.first.author }
    assert_equal authors(:david), assert_no_queries { posts_with_comments_and_author.first.author }
269 270
  end

271 272 273 274
  def test_eager_with_has_many_through_an_sti_join_model
    author = Author.find(:first, :include => :special_post_comments, :order => 'authors.id')
    assert_equal [comments(:does_it_hurt)], assert_no_queries { author.special_post_comments }
  end
J
Jeremy Kemper 已提交
275

276 277 278 279
  def test_eager_with_has_many_through_an_sti_join_model_with_conditions_on_both
    author = Author.find(:first, :include => :special_nonexistant_post_comments, :order => 'authors.id')
    assert_equal [], author.special_nonexistant_post_comments
  end
280

281
  def test_eager_with_has_many_through_join_model_with_conditions
282 283 284
    assert_equal Author.find(:first, :include => :hello_post_comments,
                             :order => 'authors.id').hello_post_comments.sort_by(&:id),
                 Author.find(:first, :order => 'authors.id').hello_post_comments.sort_by(&:id)
285 286
  end

287 288 289 290 291 292 293 294 295 296 297
  def test_eager_with_has_many_through_join_model_with_conditions_on_top_level
    assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions.first
  end

  def test_eager_with_has_many_through_join_model_with_include
    author_comments = Author.find(authors(:david).id, :include => :comments_with_include).comments_with_include.to_a
    assert_no_queries do
      author_comments.first.post.title
    end
  end

298
  def test_eager_with_has_many_and_limit
299
    posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
300 301 302 303
    assert_equal 2, posts.size
    assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
  end

304
  def test_eager_with_has_many_and_limit_and_conditions
305 306 307 308 309
    if current_adapter?(:OpenBaseAdapter)
      posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "FETCHBLOB(posts.body) = 'hello'", :order => "posts.id")
    else
      posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.body = 'hello'", :order => "posts.id")
    end
310 311 312 313 314
    assert_equal 2, posts.size
    assert_equal [4,5], posts.collect { |p| p.id }
  end

  def test_eager_with_has_many_and_limit_and_conditions_array
315 316 317 318 319
    if current_adapter?(:OpenBaseAdapter)
      posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "FETCHBLOB(posts.body) = ?", 'hello' ], :order => "posts.id")
    else
      posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "posts.body = ?", 'hello' ], :order => "posts.id")
    end
320
    assert_equal 2, posts.size
J
Jeremy Kemper 已提交
321
    assert_equal [4,5], posts.collect { |p| p.id }
322 323 324
  end

  def test_eager_with_has_many_and_limit_and_conditions_array_on_the_eagers
325 326
    posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
    assert_equal 2, posts.size
J
Jeremy Kemper 已提交
327

328 329
    count = Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
    assert_equal count, posts.size
330 331
  end

332 333 334 335 336 337 338 339 340 341
  def test_eager_with_has_many_and_limit_ond_high_offset
    posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
    assert_equal 0, posts.size
  end

  def test_count_eager_with_has_many_and_limit_ond_high_offset
    posts = Post.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ])
    assert_equal 0, posts
  end

342 343 344 345
  def test_eager_with_has_many_and_limit_with_no_results
    posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => "posts.title = 'magic forest'")
    assert_equal 0, posts.size
  end
J
Jeremy Kemper 已提交
346

347 348 349 350 351
  def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
    author = authors(:david)
    author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
    assert_equal author_posts_without_comments.size, author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null')
  end
352 353 354 355 356 357
  
  def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
    person = people(:michael)
    person_posts_without_comments = person.posts.select { |post| post.comments.blank? }
    assert_equal person_posts_without_comments.size, person.posts_with_no_comments.count
  end
358 359 360 361 362 363 364 365 366 367 368 369

  def test_eager_with_has_and_belongs_to_many_and_limit
    posts = Post.find(:all, :include => :categories, :order => "posts.id", :limit => 3)
    assert_equal 3, posts.size
    assert_equal 2, posts[0].categories.size
    assert_equal 1, posts[1].categories.size
    assert_equal 0, posts[2].categories.size
    assert posts[0].categories.include?(categories(:technology))
    assert posts[1].categories.include?(categories(:general))
  end

  def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
J
Jeremy Kemper 已提交
370 371
    posts = authors(:david).posts.find(:all,
      :include    => :comments,
372 373 374 375
      :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
      :limit      => 2
    )
    assert_equal 2, posts.size
J
Jeremy Kemper 已提交
376

377
    count = Post.count(
J
Jeremy Kemper 已提交
378
      :include    => [ :comments, :author ],
379 380 381 382
      :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
      :limit      => 2
    )
    assert_equal count, posts.size
383
  end
384

385 386 387
  def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
    posts = nil
    Post.with_scope(:find => {
J
Jeremy Kemper 已提交
388
      :include    => :comments,
389 390 391 392 393
      :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
    }) do
      posts = authors(:david).posts.find(:all, :limit => 2)
      assert_equal 2, posts.size
    end
J
Jeremy Kemper 已提交
394

395
    Post.with_scope(:find => {
J
Jeremy Kemper 已提交
396
      :include    => [ :comments, :author ],
397 398 399 400 401 402 403 404 405
      :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
    }) do
      count = Post.count(:limit => 2)
      assert_equal count, posts.size
    end
  end

  def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
    Post.with_scope(:find => { :conditions => "1=1" }) do
J
Jeremy Kemper 已提交
406 407
      posts = authors(:david).posts.find(:all,
        :include    => :comments,
408 409 410 411
        :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
        :limit      => 2
      )
      assert_equal 2, posts.size
J
Jeremy Kemper 已提交
412

413
      count = Post.count(
J
Jeremy Kemper 已提交
414
        :include    => [ :comments, :author ],
415 416 417 418 419 420
        :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
        :limit      => 2
      )
      assert_equal count, posts.size
    end
  end
421 422

  def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
423
    posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2)
424
    posts_with_scoped_order = Post.with_scope(:find => {:order => 'posts.id DESC'}) do
425
      Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
426 427 428 429
    end
    assert_equal posts_with_explicit_order, posts_with_scoped_order
  end

430
  def test_eager_association_loading_with_habtm
431 432 433 434
    posts = Post.find(:all, :include => :categories, :order => "posts.id")
    assert_equal 2, posts[0].categories.size
    assert_equal 1, posts[1].categories.size
    assert_equal 0, posts[2].categories.size
435 436
    assert posts[0].categories.include?(categories(:technology))
    assert posts[1].categories.include?(categories(:general))
437
  end
438

439 440
  def test_eager_with_inheritance
    posts = SpecialPost.find(:all, :include => [ :comments ])
441
  end
442

443 444 445
  def test_eager_has_one_with_association_inheritance
    post = Post.find(4, :include => [ :very_special_comment ])
    assert_equal "VerySpecialComment", post.very_special_comment.class.to_s
446 447
  end

448 449 450 451 452
  def test_eager_has_many_with_association_inheritance
    post = Post.find(4, :include => [ :special_comments ])
    post.special_comments.each do |special_comment|
      assert_equal "SpecialComment", special_comment.class.to_s
    end
453 454
  end

455 456 457 458 459 460 461 462
  def test_eager_habtm_with_association_inheritance
    post = Post.find(6, :include => [ :special_categories ])
    assert_equal 1, post.special_categories.size
    post.special_categories.each do |special_category|
      assert_equal "SpecialCategory", special_category.class.to_s
    end
  end

463 464 465 466
  def test_eager_with_has_one_dependent_does_not_destroy_dependent
    assert_not_nil companies(:first_firm).account
    f = Firm.find(:first, :include => :account,
            :conditions => ["companies.name = ?", "37signals"])
467 468
    assert_not_nil f.account
    assert_equal companies(:first_firm, :reload).account, f.account
469
  end
J
Jeremy Kemper 已提交
470

471 472 473 474 475 476
  def test_eager_with_multi_table_conditional_properly_counts_the_records_when_using_size
    author = authors(:david)
    posts_with_no_comments = author.posts.select { |post| post.comments.blank? }
    assert_equal posts_with_no_comments.size, author.posts_with_no_comments.size
    assert_equal posts_with_no_comments, author.posts_with_no_comments
  end
477 478

  def test_eager_with_invalid_association_reference
479 480 481
    assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys") {
      post = Post.find(6, :include=> :monkeys )
    }
482
    assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys") {
483 484
      post = Post.find(6, :include=>[ :monkeys ])
    }
485 486 487
    assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys") {
      post = Post.find(6, :include=>[ 'monkeys' ])
    }
488
    assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys, :elephants") {
489 490 491
      post = Post.find(6, :include=>[ :monkeys, :elephants ])
    }
  end
J
Jeremy Kemper 已提交
492

493 494 495
  def find_all_ordered(className, include=nil)
    className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
  end
J
Jeremy Kemper 已提交
496

497
  def test_limited_eager_with_order
498 499
    assert_equal posts(:thinking, :sti_comments), Post.find(:all, :include => [:author, :comments], :conditions => "authors.name = 'David'", :order => 'UPPER(posts.title)', :limit => 2, :offset => 1)
    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)
500
  end
J
Jeremy Kemper 已提交
501

502
  def test_limited_eager_with_multiple_order_columns
503 504
    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)
    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)
505
  end
506

507 508 509 510 511 512 513 514 515 516 517
  def test_preload_with_interpolation
    assert_equal [comments(:greetings)], Post.find(posts(:welcome).id, :include => :comments_with_interpolated_conditions).comments_with_interpolated_conditions
  end

  def test_polymorphic_type_condition
    post = Post.find(posts(:thinking).id, :include => :taggings)
    assert post.taggings.include?(taggings(:thinking_general))
    post = SpecialPost.find(posts(:thinking).id, :include => :taggings)
    assert post.taggings.include?(taggings(:thinking_general))
  end

518 519 520 521 522 523 524 525
  def test_eager_with_multiple_associations_with_same_table_has_many_and_habtm
    # Eager includes of has many and habtm associations aren't necessarily sorted in the same way
    def assert_equal_after_sort(item1, item2, item3 = nil)
      assert_equal(item1.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id})
      assert_equal(item3.sort{|a,b| a.id <=> b.id}, item2.sort{|a,b| a.id <=> b.id}) if item3
    end
    # Test regular association, association with conditions, association with
    # STI, and association with conditions assured not to be true
526
    post_types = [:posts, :other_posts, :special_posts]
527 528 529 530
    # test both has_many and has_and_belongs_to_many
    [Author, Category].each do |className|
      d1 = find_all_ordered(className)
      # test including all post types at once
J
Jeremy Kemper 已提交
531 532
      d2 = find_all_ordered(className, post_types)
      d1.each_index do |i|
533 534 535 536 537 538 539 540 541 542 543 544
        assert_equal(d1[i], d2[i])
        assert_equal_after_sort(d1[i].posts, d2[i].posts)
        post_types[1..-1].each do |post_type|
          # test including post_types together
          d3 = find_all_ordered(className, [:posts, post_type])
          assert_equal(d1[i], d3[i])
          assert_equal_after_sort(d1[i].posts, d3[i].posts)
          assert_equal_after_sort(d1[i].send(post_type), d2[i].send(post_type), d3[i].send(post_type))
        end
      end
    end
  end
J
Jeremy Kemper 已提交
545

546 547 548
  def test_eager_with_multiple_associations_with_same_table_has_one
    d1 = find_all_ordered(Firm)
    d2 = find_all_ordered(Firm, :account)
J
Jeremy Kemper 已提交
549
    d1.each_index do |i|
550 551 552 553
      assert_equal(d1[i], d2[i])
      assert_equal(d1[i].account, d2[i].account)
    end
  end
J
Jeremy Kemper 已提交
554

555 556 557 558
  def test_eager_with_multiple_associations_with_same_table_belongs_to
    firm_types = [:firm, :firm_with_basic_id, :firm_with_other_name, :firm_with_condition]
    d1 = find_all_ordered(Client)
    d2 = find_all_ordered(Client, firm_types)
J
Jeremy Kemper 已提交
559
    d1.each_index do |i|
560 561 562 563
      assert_equal(d1[i], d2[i])
      firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
    end
  end
564 565 566 567
  def test_eager_with_valid_association_as_string_not_symbol
    assert_nothing_raised { Post.find(:all, :include => 'comments') }
  end

568 569 570 571 572 573 574
  def test_eager_with_floating_point_numbers
    assert_queries(2) do
      # Before changes, the floating point numbers will be interpreted as table names and will cause this to run in one query
      Comment.find :all, :conditions => "123.456 = 123.456", :include => :post
    end
  end

575 576
  def test_preconfigured_includes_with_belongs_to
    author = posts(:welcome).author_with_posts
577
    assert_no_queries {assert_equal 5, author.posts.size}
578 579 580 581
  end

  def test_preconfigured_includes_with_has_one
    comment = posts(:sti_comments).very_special_comment_with_post
582
    assert_no_queries {assert_equal posts(:sti_comments), comment.post}
583 584 585 586
  end

  def test_preconfigured_includes_with_has_many
    posts = authors(:david).posts_with_comments
587
    one = posts.detect { |p| p.id == 1 }
588 589 590 591
    assert_no_queries do
      assert_equal 5, posts.size
      assert_equal 2, one.comments.size
    end
592 593 594 595
  end

  def test_preconfigured_includes_with_habtm
    posts = authors(:david).posts_with_categories
596
    one = posts.detect { |p| p.id == 1 }
597 598 599 600
    assert_no_queries do
      assert_equal 5, posts.size
      assert_equal 2, one.categories.size
    end
601 602 603 604
  end

  def test_preconfigured_includes_with_has_many_and_habtm
    posts = authors(:david).posts_with_comments_and_categories
605
    one = posts.detect { |p| p.id == 1 }
606 607 608 609 610
    assert_no_queries do
      assert_equal 5, posts.size
      assert_equal 2, one.comments.size
      assert_equal 2, one.categories.size
    end
611
  end
J
Jeremy Kemper 已提交
612

613
  def test_count_with_include
614
    if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
615
      assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15")
616 617
    elsif current_adapter?(:OpenBaseAdapter)
      assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15")
618 619 620
    else
      assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15")
    end
621
  end
622 623 624 625 626 627

  def test_load_with_sti_sharing_association
    assert_queries(2) do #should not do 1 query per subclass
      Comment.find :all, :include => :post
    end
  end
628 629 630 631 632 633 634 635

  def test_conditions_on_join_table_with_include_and_limit
    assert_equal 3, Developer.find(:all, :include => 'projects', :conditions => 'developers_projects.access_level = 1', :limit => 5).size
  end

  def test_order_on_join_table_with_include_and_limit
    assert_equal 5, Developer.find(:all, :include => 'projects', :order => 'developers_projects.joined_on DESC', :limit => 5).size
  end
636
end