2005-04-03 17:50:11 +00:00
require 'abstract_unit'
require 'fixtures/post'
require 'fixtures/comment'
require 'fixtures/author'
2005-04-10 17:24:56 +00:00
require 'fixtures/category'
2005-06-10 13:54:58 +00:00
require 'fixtures/company'
2005-04-03 17:50:11 +00:00
class EagerAssociationTest < Test :: Unit :: TestCase
2005-06-10 13:54:58 +00:00
fixtures :posts , :comments , :authors , :categories , :categories_posts ,
:companies , :accounts
2005-04-03 17:50:11 +00:00
def test_loading_with_one_association
posts = Post . find ( :all , :include = > :comments )
2005-07-04 17:16:18 +00:00
post = posts . find { | p | p . id == 1 }
assert_equal 2 , post . comments . size
assert post . comments . include? ( comments ( :greetings ) )
2005-04-03 17:50:11 +00:00
post = Post . find ( :first , :include = > :comments , :conditions = > " posts.title = 'Welcome to the weblog' " )
assert_equal 2 , post . comments . size
2005-06-10 13:54:58 +00:00
assert post . comments . include? ( comments ( :greetings ) )
2005-04-03 17:50:11 +00:00
end
2005-04-19 16:32:57 +00:00
def test_with_ordering
posts = Post . find ( :all , :include = > :comments , :order = > " posts.id DESC " )
2005-07-03 08:21:22 +00:00
assert_equal posts ( :sti_habtm ) , posts [ 0 ]
assert_equal posts ( :sti_post_and_comments ) , posts [ 1 ]
assert_equal posts ( :sti_comments ) , posts [ 2 ]
assert_equal posts ( :authorless ) , posts [ 3 ]
assert_equal posts ( :thinking ) , posts [ 4 ]
assert_equal posts ( :welcome ) , posts [ 5 ]
2005-04-19 16:32:57 +00:00
end
2005-07-10 04:22:08 +00:00
2005-04-03 17:50:11 +00:00
def test_loading_with_multiple_associations
2005-04-18 05:55:20 +00:00
posts = Post . find ( :all , :include = > [ :comments , :author , :categories ] , :order = > " posts.id " )
2005-04-03 17:50:11 +00:00
assert_equal 2 , posts . first . comments . size
2005-04-10 17:24:56 +00:00
assert_equal 2 , posts . first . categories . size
2005-06-10 13:54:58 +00:00
assert posts . first . comments . include? ( comments ( :greetings ) )
2005-04-03 17:50:11 +00:00
end
def test_loading_from_an_association
2005-06-10 13:54:58 +00:00
posts = authors ( :david ) . posts . find ( :all , :include = > :comments , :order = > " posts.id " )
2005-04-03 17:50:11 +00:00
assert_equal 2 , posts . first . comments . size
end
2005-04-14 07:49:13 +00:00
def test_loading_with_no_associations
2005-06-10 13:54:58 +00:00
assert_nil Post . find ( posts ( :authorless ) . id , :include = > :author ) . author
2005-04-14 07:49:13 +00:00
end
2005-04-03 17:50:11 +00:00
def test_eager_association_loading_with_belongs_to
comments = Comment . find ( :all , :include = > :post )
2005-07-11 06:09:08 +00:00
assert_equal 9 , comments . length
2005-06-11 20:56:12 +00:00
titles = comments . map { | c | c . post . title }
assert titles . include? ( posts ( :welcome ) . title )
2005-07-03 08:21:22 +00:00
assert titles . include? ( posts ( :sti_post_and_comments ) . title )
2005-04-03 17:50:11 +00:00
end
2005-07-11 06:09:08 +00:00
def test_eager_association_loading_with_belongs_to_and_limit
comments = Comment . find ( :all , :include = > :post , :limit = > 5 )
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
comments = Comment . find ( :all , :include = > :post , :conditions = > 'post_id = 4' , :limit = > 3 )
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
comments = Comment . find ( :all , :include = > :post , :limit = > 3 , :offset = > 2 )
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
comments = Comment . find ( :all , :include = > :post , :conditions = > 'post_id = 4' , :limit = > 3 , :offset = > 1 )
assert_equal 3 , comments . length
assert_equal [ 6 , 7 , 8 ] , comments . collect { | c | c . id }
end
def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
posts = Post . find ( :all , :include = > [ :author , :very_special_comment ] , :limit = > 1 )
assert_equal 1 , posts . length
assert_equal [ 4 ] , posts . collect { | p | p . id }
end
def test_eager_association_loading_with_belongs_to_and_limit_and_offset_and_multiple_associations
posts = Post . find ( :all , :include = > [ :author , :very_special_comment ] , :limit = > 1 , :offset = > 1 )
assert_equal 0 , posts . length
assert_equal [ ] , posts
end
def test_eager_association_raise_on_limit
assert_raises ( ActiveRecord :: ConfigurationError ) { Post . find ( :all , :include = > [ :author , :comments ] , :limit = > 1 ) }
end
2005-04-10 17:24:56 +00:00
def test_eager_association_loading_with_habtm
2005-04-18 05:55:20 +00:00
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
2005-06-10 13:54:58 +00:00
assert posts [ 0 ] . categories . include? ( categories ( :technology ) )
assert posts [ 1 ] . categories . include? ( categories ( :general ) )
2005-04-10 17:24:56 +00:00
end
2005-07-10 04:22:08 +00:00
2005-04-10 15:49:49 +00:00
def test_eager_with_inheritance
posts = SpecialPost . find ( :all , :include = > [ :comments ] )
2005-07-10 04:22:08 +00:00
end
2005-06-10 13:54:58 +00:00
2005-07-03 08:21:22 +00:00
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
2005-07-10 04:22:08 +00:00
end
2005-07-03 08:21:22 +00:00
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
2005-07-10 04:22:08 +00:00
end
2005-07-03 08:21:22 +00:00
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
2005-06-10 13:54:58 +00:00
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 " ] )
2005-07-10 04:22:08 +00:00
assert_not_nil f . account
assert_equal companies ( :first_firm , :reload ) . account , f . account
2005-06-10 13:54:58 +00:00
end
2005-07-09 17:14:14 +00:00
def test_eager_with_invalid_association_reference
2005-07-11 06:09:08 +00:00
assert_raises ( ActiveRecord :: ConfigurationError , " Association was not found; perhaps you misspelled it? You specified :include => :monkeys " ) {
2005-07-09 17:14:14 +00:00
post = Post . find ( 6 , :include = > [ :monkeys ] )
}
2005-07-11 06:09:08 +00:00
assert_raises ( ActiveRecord :: ConfigurationError , " Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants " ) {
2005-07-09 17:14:14 +00:00
post = Post . find ( 6 , :include = > [ :monkeys , :elephants ] )
}
end
2005-04-03 17:50:11 +00:00
end