eager_test.rb 22.2 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
class EagerAssociationTest < ActiveRecord::TestCase
14
  fixtures :posts, :comments, :authors, :categories, :categories_posts,
15 16
            :companies, :accounts, :tags, :taggings, :people, :readers,
            :owners, :pets
17 18 19

  def test_loading_with_one_association
    posts = Post.find(:all, :include => :comments)
20 21 22
    post = posts.find { |p| p.id == 1 }
    assert_equal 2, post.comments.size
    assert post.comments.include?(comments(:greetings))
23 24 25

    post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
    assert_equal 2, post.comments.size
26
    assert post.comments.include?(comments(:greetings))
27 28
  end

29
  def test_loading_conditions_with_or
30
    posts = authors(:david).posts.find(:all, :include => :comments, :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'")
31 32 33 34
    assert_nil posts.detect { |p| p.author_id != authors(:david).id },
      "expected to find only david's posts"
  end

35
  def test_with_ordering
36 37 38 39 40 41
    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
42
  end
43

44 45 46
  def test_with_two_tables_in_from_without_getting_double_quoted
    posts = Post.find(:all,
      :select     => "posts.*",
D
David Heinemeier Hansson 已提交
47
      :from       => "authors, posts",
48
      :include    => :comments,
49 50
      :conditions => "posts.author_id = authors.id",
      :order      => "posts.id"
51 52 53 54 55
    )

    assert_equal 2, posts.first.comments.size
  end

56
  def test_loading_with_multiple_associations
57
    posts = Post.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id")
58
    assert_equal 2, posts.first.comments.size
59
    assert_equal 2, posts.first.categories.size
60
    assert posts.first.comments.include?(comments(:greetings))
61 62
  end

63 64 65 66 67 68 69
  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

70
  def test_loading_from_an_association
71
    posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
72 73 74
    assert_equal 2, posts.first.comments.size
  end

75
  def test_loading_with_no_associations
76
    assert_nil Post.find(posts(:authorless).id, :include => :author).author
77 78
  end

79 80 81 82 83 84
  def test_nested_loading_with_no_associations
    assert_nothing_raised do
      Post.find(posts(:authorless).id, :include => {:author => :author_addresss})
    end
  end

85 86 87 88 89
  def test_eager_association_loading_with_belongs_to_and_foreign_keys
    pets = Pet.find(:all, :include => :owner)
    assert_equal 3, pets.length
  end

90 91
  def test_eager_association_loading_with_belongs_to
    comments = Comment.find(:all, :include => :post)
92
    assert_equal 10, comments.length
93 94
    titles = comments.map { |c| c.post.title }
    assert titles.include?(posts(:welcome).title)
95
    assert titles.include?(posts(:sti_post_and_comments).title)
96
  end
J
Jeremy Kemper 已提交
97

98
  def test_eager_association_loading_with_belongs_to_and_limit
99
    comments = Comment.find(:all, :include => :post, :limit => 5, :order => 'comments.id')
100 101 102 103 104
    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
105
    comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :order => 'comments.id')
106 107 108 109 110
    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
111
    comments = Comment.find(:all, :include => :post, :limit => 3, :offset => 2, :order => 'comments.id')
112 113 114 115 116
    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
117
    comments = Comment.find(:all, :include => :post, :conditions => 'post_id = 4', :limit => 3, :offset => 1, :order => 'comments.id')
118 119 120
    assert_equal 3, comments.length
    assert_equal [6,7,8], comments.collect { |c| c.id }
  end
J
Jeremy Kemper 已提交
121

122 123 124 125 126 127
  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

128 129 130 131 132 133 134
  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
135
    quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
136
    assert_nothing_raised do
137
      Comment.find(:all, :include => :post, :conditions => ["#{quoted_posts_id} = ?",4])
138 139 140 141 142 143 144 145 146 147
    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
148
    quoted_posts_id= Comment.connection.quote_table_name('posts') + '.' + Comment.connection.quote_column_name('id')
149
    assert_nothing_raised do
150
      Comment.find(:all, :include => :post, :order => quoted_posts_id)
151 152 153
    end
  end

154
  def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
155
    posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :order => 'posts.id')
156
    assert_equal 1, posts.length
157
    assert_equal [1], posts.collect { |p| p.id }
158
  end
J
Jeremy Kemper 已提交
159

160
  def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
161 162
    posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :offset => 1, :order => 'posts.id')
    assert_equal 1, posts.length
163
    assert_equal [2], posts.collect { |p| p.id }
164
  end
J
Jeremy Kemper 已提交
165

166 167 168 169 170
  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

171 172 173 174
  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 已提交
175

176
  def test_eager_with_has_many_through
177
    posts_with_comments = people(:michael).posts.find(:all, :include => :comments)
178 179 180
    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 }
181 182
    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 }
183 184
  end

185 186 187 188
  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 已提交
189

190 191 192 193
  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
194

195
  def test_eager_with_has_many_through_join_model_with_conditions
196 197 198
    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)
199 200
  end

201
  def test_eager_with_has_many_and_limit
202
    posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
203 204 205 206
    assert_equal 2, posts.size
    assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
  end

207
  def test_eager_with_has_many_and_limit_and_conditions
208 209 210 211 212
    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
213 214 215 216 217
    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
218 219 220 221 222
    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
223
    assert_equal 2, posts.size
J
Jeremy Kemper 已提交
224
    assert_equal [4,5], posts.collect { |p| p.id }
225 226 227
  end

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

231 232
    count = Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
    assert_equal count, posts.size
233 234
  end

235 236 237 238 239 240 241 242 243 244
  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

245 246 247 248
  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 已提交
249

250 251 252 253 254
  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
255 256 257 258 259 260
  
  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
261 262 263 264 265 266 267 268 269 270 271 272

  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 已提交
273 274
    posts = authors(:david).posts.find(:all,
      :include    => :comments,
275 276 277 278
      :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
      :limit      => 2
    )
    assert_equal 2, posts.size
J
Jeremy Kemper 已提交
279

280
    count = Post.count(
J
Jeremy Kemper 已提交
281
      :include    => [ :comments, :author ],
282 283 284 285
      :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
      :limit      => 2
    )
    assert_equal count, posts.size
286
  end
287

288 289 290
  def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
    posts = nil
    Post.with_scope(:find => {
J
Jeremy Kemper 已提交
291
      :include    => :comments,
292 293 294 295 296
      :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 已提交
297

298
    Post.with_scope(:find => {
J
Jeremy Kemper 已提交
299
      :include    => [ :comments, :author ],
300 301 302 303 304 305 306 307 308
      :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 已提交
309 310
      posts = authors(:david).posts.find(:all,
        :include    => :comments,
311 312 313 314
        :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
        :limit      => 2
      )
      assert_equal 2, posts.size
J
Jeremy Kemper 已提交
315

316
      count = Post.count(
J
Jeremy Kemper 已提交
317
        :include    => [ :comments, :author ],
318 319 320 321 322 323
        :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
        :limit      => 2
      )
      assert_equal count, posts.size
    end
  end
324 325

  def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
326
    posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2)
327
    posts_with_scoped_order = Post.with_scope(:find => {:order => 'posts.id DESC'}) do
328
      Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
329 330 331 332
    end
    assert_equal posts_with_explicit_order, posts_with_scoped_order
  end

333
  def test_eager_association_loading_with_habtm
334 335 336 337
    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
338 339
    assert posts[0].categories.include?(categories(:technology))
    assert posts[1].categories.include?(categories(:general))
340
  end
341

342 343
  def test_eager_with_inheritance
    posts = SpecialPost.find(:all, :include => [ :comments ])
344
  end
345

346 347 348
  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
349 350
  end

351 352 353 354 355
  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
356 357
  end

358 359 360 361 362 363 364 365
  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

366 367 368 369
  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"])
370 371
    assert_not_nil f.account
    assert_equal companies(:first_firm, :reload).account, f.account
372
  end
J
Jeremy Kemper 已提交
373

374 375 376 377 378 379
  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
380 381

  def test_eager_with_invalid_association_reference
382 383 384
    assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys") {
      post = Post.find(6, :include=> :monkeys )
    }
385
    assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys") {
386 387
      post = Post.find(6, :include=>[ :monkeys ])
    }
388 389 390
    assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys") {
      post = Post.find(6, :include=>[ 'monkeys' ])
    }
391
    assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it?  You specified :include => :monkeys, :elephants") {
392 393 394
      post = Post.find(6, :include=>[ :monkeys, :elephants ])
    }
  end
J
Jeremy Kemper 已提交
395

396 397 398
  def find_all_ordered(className, include=nil)
    className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
  end
J
Jeremy Kemper 已提交
399

400
  def test_limited_eager_with_order
401 402
    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)
403
  end
J
Jeremy Kemper 已提交
404

405
  def test_limited_eager_with_multiple_order_columns
406 407
    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)
408
  end
409

410 411 412 413 414 415 416 417 418 419 420
  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

421 422 423 424 425 426 427 428
  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
429
    post_types = [:posts, :other_posts, :special_posts]
430 431 432 433
    # 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 已提交
434 435
      d2 = find_all_ordered(className, post_types)
      d1.each_index do |i|
436 437 438 439 440 441 442 443 444 445 446 447
        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 已提交
448

449 450 451
  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 已提交
452
    d1.each_index do |i|
453 454 455 456
      assert_equal(d1[i], d2[i])
      assert_equal(d1[i].account, d2[i].account)
    end
  end
J
Jeremy Kemper 已提交
457

458 459 460 461
  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 已提交
462
    d1.each_index do |i|
463 464 465 466
      assert_equal(d1[i], d2[i])
      firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
    end
  end
467 468 469 470
  def test_eager_with_valid_association_as_string_not_symbol
    assert_nothing_raised { Post.find(:all, :include => 'comments') }
  end

471 472
  def test_preconfigured_includes_with_belongs_to
    author = posts(:welcome).author_with_posts
473
    assert_no_queries {assert_equal 5, author.posts.size}
474 475 476 477
  end

  def test_preconfigured_includes_with_has_one
    comment = posts(:sti_comments).very_special_comment_with_post
478
    assert_no_queries {assert_equal posts(:sti_comments), comment.post}
479 480 481 482
  end

  def test_preconfigured_includes_with_has_many
    posts = authors(:david).posts_with_comments
483
    one = posts.detect { |p| p.id == 1 }
484 485 486 487
    assert_no_queries do
      assert_equal 5, posts.size
      assert_equal 2, one.comments.size
    end
488 489 490 491
  end

  def test_preconfigured_includes_with_habtm
    posts = authors(:david).posts_with_categories
492
    one = posts.detect { |p| p.id == 1 }
493 494 495 496
    assert_no_queries do
      assert_equal 5, posts.size
      assert_equal 2, one.categories.size
    end
497 498 499 500
  end

  def test_preconfigured_includes_with_has_many_and_habtm
    posts = authors(:david).posts_with_comments_and_categories
501
    one = posts.detect { |p| p.id == 1 }
502 503 504 505 506
    assert_no_queries do
      assert_equal 5, posts.size
      assert_equal 2, one.comments.size
      assert_equal 2, one.categories.size
    end
507
  end
J
Jeremy Kemper 已提交
508

509
  def test_count_with_include
510
    if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
511
      assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15")
512 513
    elsif current_adapter?(:OpenBaseAdapter)
      assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15")
514 515 516
    else
      assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15")
    end
517
  end
518
end