2005-12-02 01:03:43 -05:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'fixtures/tag'
|
|
|
|
require 'fixtures/tagging'
|
|
|
|
require 'fixtures/post'
|
|
|
|
require 'fixtures/comment'
|
2005-12-11 13:06:51 -05:00
|
|
|
require 'fixtures/author'
|
|
|
|
require 'fixtures/category'
|
|
|
|
require 'fixtures/categorization'
|
2005-12-02 01:03:43 -05:00
|
|
|
|
2005-12-02 23:29:55 -05:00
|
|
|
class AssociationsJoinModelTest < Test::Unit::TestCase
|
2005-12-04 00:40:50 -05:00
|
|
|
self.use_transactional_fixtures = false
|
2006-03-20 20:07:16 -05:00
|
|
|
fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites
|
2005-12-11 13:06:51 -05:00
|
|
|
|
|
|
|
def test_has_many
|
|
|
|
assert_equal categories(:general), authors(:david).categories.first
|
|
|
|
end
|
2005-12-16 18:24:58 -05:00
|
|
|
|
|
|
|
def test_has_many_inherited
|
|
|
|
assert_equal categories(:sti_test), authors(:mary).categories.first
|
|
|
|
end
|
2005-12-02 01:03:43 -05:00
|
|
|
|
2005-12-16 18:24:58 -05:00
|
|
|
def test_inherited_has_many
|
|
|
|
assert_equal authors(:mary), categories(:sti_test).authors.first
|
|
|
|
end
|
|
|
|
|
2005-12-02 23:29:55 -05:00
|
|
|
def test_polymorphic_has_many
|
2005-12-02 01:03:43 -05:00
|
|
|
assert_equal taggings(:welcome_general), posts(:welcome).taggings.first
|
|
|
|
end
|
2006-02-09 14:37:05 -05:00
|
|
|
|
|
|
|
def test_polymorphic_has_one
|
|
|
|
assert_equal taggings(:welcome_general), posts(:welcome).tagging
|
|
|
|
end
|
2005-12-02 01:03:43 -05:00
|
|
|
|
2005-12-02 23:29:55 -05:00
|
|
|
def test_polymorphic_belongs_to
|
2005-12-02 01:03:43 -05:00
|
|
|
assert_equal posts(:welcome), posts(:welcome).taggings.first.taggable
|
|
|
|
end
|
2005-12-02 23:29:55 -05:00
|
|
|
|
|
|
|
def test_polymorphic_has_many_going_through_join_model
|
2006-03-19 11:13:52 -05:00
|
|
|
assert_equal tags(:general), tag = posts(:welcome).tags.first
|
|
|
|
assert_no_queries do
|
|
|
|
tag.tagging
|
|
|
|
end
|
2005-12-16 18:24:58 -05:00
|
|
|
end
|
2006-03-15 21:46:01 -05:00
|
|
|
|
2006-03-19 11:13:52 -05:00
|
|
|
def test_polymorphic_has_many_going_through_join_model_with_find
|
|
|
|
assert_equal tags(:general), tag = posts(:welcome).tags.find(:first)
|
|
|
|
assert_no_queries do
|
|
|
|
tag.tagging
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_polymorphic_has_many_going_through_join_model_with_include_on_source_reflection
|
|
|
|
assert_equal tags(:general), tag = posts(:welcome).funky_tags.first
|
|
|
|
assert_no_queries do
|
|
|
|
tag.tagging
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_polymorphic_has_many_going_through_join_model_with_include_on_source_reflection_with_find
|
|
|
|
assert_equal tags(:general), tag = posts(:welcome).funky_tags.find(:first)
|
|
|
|
assert_no_queries do
|
|
|
|
tag.tagging
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-21 11:33:22 -05:00
|
|
|
def test_polymorphic_has_many_going_through_join_model_with_disabled_include
|
|
|
|
assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first
|
|
|
|
assert_queries 1 do
|
|
|
|
tag.tagging
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_polymorphic_has_many_going_through_join_model_with_custom_select_and_joins
|
|
|
|
assert_equal tags(:general), tag = posts(:welcome).tags.add_joins_and_select.first
|
|
|
|
tag.author_id
|
|
|
|
end
|
2006-03-19 11:13:52 -05:00
|
|
|
|
2006-03-18 17:38:49 -05:00
|
|
|
def test_polymorphic_has_many_going_through_join_model_with_custom_foreign_key
|
|
|
|
assert_equal tags(:misc), taggings(:welcome_general).super_tag
|
|
|
|
assert_equal tags(:misc), posts(:welcome).super_tags.first
|
|
|
|
end
|
|
|
|
|
2006-03-15 21:46:01 -05:00
|
|
|
def test_polymorphic_has_many_create_model_with_inheritance_and_custom_base_class
|
|
|
|
post = SubStiPost.create :title => 'SubStiPost', :body => 'SubStiPost body'
|
|
|
|
assert_instance_of SubStiPost, post
|
|
|
|
|
|
|
|
tagging = tags(:misc).taggings.create(:taggable => post)
|
|
|
|
assert_equal "SubStiPost", tagging.taggable_type
|
|
|
|
end
|
|
|
|
|
2005-12-16 18:24:58 -05:00
|
|
|
def test_polymorphic_has_many_going_through_join_model_with_inheritance
|
|
|
|
assert_equal tags(:general), posts(:thinking).tags.first
|
|
|
|
end
|
2006-03-19 02:14:48 -05:00
|
|
|
|
|
|
|
def test_polymorphic_has_many_going_through_join_model_with_inheritance_with_custom_class_name
|
|
|
|
assert_equal tags(:general), posts(:thinking).funky_tags.first
|
|
|
|
end
|
|
|
|
|
2006-01-20 16:51:48 -05:00
|
|
|
def test_polymorphic_has_many_create_model_with_inheritance
|
|
|
|
post = posts(:thinking)
|
|
|
|
assert_instance_of SpecialPost, post
|
|
|
|
|
|
|
|
tagging = tags(:misc).taggings.create(:taggable => post)
|
|
|
|
assert_equal "Post", tagging.taggable_type
|
|
|
|
end
|
2006-02-09 14:37:05 -05:00
|
|
|
|
|
|
|
def test_polymorphic_has_one_create_model_with_inheritance
|
|
|
|
tagging = tags(:misc).create_tagging(:taggable => posts(:thinking))
|
|
|
|
assert_equal "Post", tagging.taggable_type
|
|
|
|
end
|
|
|
|
|
2006-02-12 13:03:43 -05:00
|
|
|
def test_set_polymorphic_has_many
|
|
|
|
tagging = tags(:misc).taggings.create
|
|
|
|
posts(:thinking).taggings << tagging
|
|
|
|
assert_equal "Post", tagging.taggable_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_set_polymorphic_has_one
|
|
|
|
tagging = tags(:misc).taggings.create
|
|
|
|
posts(:thinking).tagging = tagging
|
|
|
|
assert_equal "Post", tagging.taggable_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_polymorphic_has_many_with_scope
|
2006-03-15 21:10:11 -05:00
|
|
|
old_count = posts(:welcome).taggings.count
|
|
|
|
tagging = posts(:welcome).taggings.create(:tag => tags(:misc))
|
2006-02-12 13:03:43 -05:00
|
|
|
assert_equal "Post", tagging.taggable_type
|
2006-03-15 21:10:11 -05:00
|
|
|
assert_equal old_count+1, posts(:welcome).taggings.count
|
2006-02-12 13:03:43 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_polymorphic_has_one_with_scope
|
2006-03-15 21:10:11 -05:00
|
|
|
old_count = Tagging.count
|
|
|
|
tagging = posts(:welcome).tagging.create(:tag => tags(:misc))
|
2006-02-12 13:03:43 -05:00
|
|
|
assert_equal "Post", tagging.taggable_type
|
2006-03-15 21:10:11 -05:00
|
|
|
assert_equal old_count+1, Tagging.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_polymorphic_has_many_with_delete_all
|
|
|
|
assert_equal 1, posts(:welcome).taggings.count
|
|
|
|
posts(:welcome).taggings.first.update_attribute :taggable_type, 'PostWithHasManyDeleteAll'
|
|
|
|
post = find_post_with_dependency(1, :has_many, :taggings, :delete_all)
|
|
|
|
|
|
|
|
old_count = Tagging.count
|
|
|
|
post.destroy
|
|
|
|
assert_equal old_count-1, Tagging.count
|
|
|
|
assert_equal 0, posts(:welcome).taggings.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_polymorphic_has_many_with_destroy
|
|
|
|
assert_equal 1, posts(:welcome).taggings.count
|
|
|
|
posts(:welcome).taggings.first.update_attribute :taggable_type, 'PostWithHasManyDestroy'
|
|
|
|
post = find_post_with_dependency(1, :has_many, :taggings, :destroy)
|
|
|
|
|
|
|
|
old_count = Tagging.count
|
|
|
|
post.destroy
|
|
|
|
assert_equal old_count-1, Tagging.count
|
|
|
|
assert_equal 0, posts(:welcome).taggings.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_polymorphic_has_many_with_nullify
|
|
|
|
assert_equal 1, posts(:welcome).taggings.count
|
|
|
|
posts(:welcome).taggings.first.update_attribute :taggable_type, 'PostWithHasManyNullify'
|
|
|
|
post = find_post_with_dependency(1, :has_many, :taggings, :nullify)
|
|
|
|
|
|
|
|
old_count = Tagging.count
|
|
|
|
post.destroy
|
|
|
|
assert_equal old_count, Tagging.count
|
|
|
|
assert_equal 0, posts(:welcome).taggings.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_polymorphic_has_one_with_destroy
|
|
|
|
assert posts(:welcome).tagging
|
|
|
|
posts(:welcome).tagging.update_attribute :taggable_type, 'PostWithHasOneDestroy'
|
|
|
|
post = find_post_with_dependency(1, :has_one, :tagging, :destroy)
|
|
|
|
|
|
|
|
old_count = Tagging.count
|
|
|
|
post.destroy
|
|
|
|
assert_equal old_count-1, Tagging.count
|
|
|
|
assert_nil posts(:welcome).tagging(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete_polymorphic_has_one_with_nullify
|
|
|
|
assert posts(:welcome).tagging
|
|
|
|
posts(:welcome).tagging.update_attribute :taggable_type, 'PostWithHasOneNullify'
|
|
|
|
post = find_post_with_dependency(1, :has_one, :tagging, :nullify)
|
|
|
|
|
|
|
|
old_count = Tagging.count
|
|
|
|
post.destroy
|
|
|
|
assert_equal old_count, Tagging.count
|
|
|
|
assert_nil posts(:welcome).tagging(true)
|
2006-02-12 13:03:43 -05:00
|
|
|
end
|
|
|
|
|
2005-12-20 15:32:47 -05:00
|
|
|
def test_has_many_with_piggyback
|
2006-01-03 23:17:23 -05:00
|
|
|
assert_equal "2", categories(:sti_test).authors.first.post_id.to_s
|
2005-12-20 15:32:47 -05:00
|
|
|
end
|
2006-03-05 13:43:56 -05:00
|
|
|
|
|
|
|
def test_include_has_many_through
|
|
|
|
posts = Post.find(:all, :order => 'posts.id')
|
|
|
|
posts_with_authors = Post.find(:all, :include => :authors, :order => 'posts.id')
|
|
|
|
assert_equal posts.length, posts_with_authors.length
|
|
|
|
posts.length.times do |i|
|
|
|
|
assert_equal posts[i].authors.length, assert_no_queries { posts_with_authors[i].authors.length }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_polymorphic_has_many_through
|
|
|
|
posts = Post.find(:all, :order => 'posts.id')
|
|
|
|
posts_with_tags = Post.find(:all, :include => :tags, :order => 'posts.id')
|
|
|
|
assert_equal posts.length, posts_with_tags.length
|
|
|
|
posts.length.times do |i|
|
|
|
|
assert_equal posts[i].tags.length, assert_no_queries { posts_with_tags[i].tags.length }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_include_polymorphic_has_many
|
|
|
|
posts = Post.find(:all, :order => 'posts.id')
|
|
|
|
posts_with_taggings = Post.find(:all, :include => :taggings, :order => 'posts.id')
|
|
|
|
assert_equal posts.length, posts_with_taggings.length
|
|
|
|
posts.length.times do |i|
|
|
|
|
assert_equal posts[i].taggings.length, assert_no_queries { posts_with_taggings[i].taggings.length }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-12-20 16:20:35 -05:00
|
|
|
def test_has_many_find_all
|
|
|
|
assert_equal [categories(:general)], authors(:david).categories.find(:all)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_find_first
|
|
|
|
assert_equal categories(:general), authors(:david).categories.find(:first)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_find_conditions
|
|
|
|
assert_equal categories(:general), authors(:david).categories.find(:first, :conditions => "categories.name = 'General'")
|
|
|
|
assert_equal nil, authors(:david).categories.find(:first, :conditions => "categories.name = 'Technology'")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_class_methods_called_by_method_missing
|
2006-02-20 19:00:29 -05:00
|
|
|
assert_equal categories(:general), authors(:david).categories.find_all_by_name('General').first
|
2005-12-20 16:20:35 -05:00
|
|
|
# assert_equal nil, authors(:david).categories.find_by_name('Technology')
|
|
|
|
end
|
2006-01-21 18:40:20 -05:00
|
|
|
|
|
|
|
def test_has_many_going_through_join_model_with_custom_foreign_key
|
|
|
|
assert_equal [], posts(:thinking).authors
|
|
|
|
assert_equal [authors(:mary)], posts(:authorless).authors
|
|
|
|
end
|
2006-03-04 10:11:17 -05:00
|
|
|
|
|
|
|
def test_belongs_to_polymorphic_with_counter_cache
|
|
|
|
assert_equal 0, posts(:welcome)[:taggings_count]
|
|
|
|
tagging = posts(:welcome).taggings.create(:tag => tags(:general))
|
|
|
|
assert_equal 1, posts(:welcome, :reload)[:taggings_count]
|
|
|
|
tagging.destroy
|
|
|
|
assert posts(:welcome, :reload)[:taggings_count].zero?
|
|
|
|
end
|
2006-03-15 21:10:11 -05:00
|
|
|
|
2006-03-17 22:39:18 -05:00
|
|
|
def test_unavailable_through_reflection
|
2006-03-18 19:53:24 -05:00
|
|
|
assert_raises (ActiveRecord::HasManyThroughAssociationNotFoundError) { authors(:david).nothings }
|
2006-03-17 22:39:18 -05:00
|
|
|
end
|
|
|
|
|
2006-03-18 18:14:31 -05:00
|
|
|
def test_has_many_through_join_model_with_conditions
|
|
|
|
assert_equal [], posts(:welcome).invalid_taggings
|
|
|
|
assert_equal [], posts(:welcome).invalid_tags
|
|
|
|
end
|
|
|
|
|
2006-03-18 19:53:24 -05:00
|
|
|
def test_has_many_polymorphic
|
|
|
|
assert_raises ActiveRecord::HasManyThroughAssociationPolymorphicError do
|
|
|
|
assert_equal [posts(:welcome), posts(:thinking)], tags(:general).taggables
|
|
|
|
end
|
|
|
|
assert_raises ActiveRecord::EagerLoadPolymorphicError do
|
|
|
|
assert_equal [posts(:welcome), posts(:thinking)], tags(:general).taggings.find(:all, :include => :taggable)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-18 21:01:40 -05:00
|
|
|
def test_has_many_through_has_many_find_all
|
2006-03-18 22:05:21 -05:00
|
|
|
assert_equal comments(:greetings), authors(:david).comments.find(:all, :order => 'comments.id').first
|
2006-03-18 21:01:40 -05:00
|
|
|
end
|
|
|
|
|
2006-03-19 02:14:48 -05:00
|
|
|
def test_has_many_through_has_many_find_all_with_custom_class
|
|
|
|
assert_equal comments(:greetings), authors(:david).funky_comments.find(:all, :order => 'comments.id').first
|
|
|
|
end
|
|
|
|
|
2006-03-18 21:01:40 -05:00
|
|
|
def test_has_many_through_has_many_find_first
|
2006-03-18 22:05:21 -05:00
|
|
|
assert_equal comments(:greetings), authors(:david).comments.find(:first, :order => 'comments.id')
|
2006-03-18 21:01:40 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_through_has_many_find_conditions
|
|
|
|
assert_equal comments(:does_it_hurt), authors(:david).comments.find(:first, :conditions => "comments.type='SpecialComment'", :order => 'comments.id')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_through_has_many_find_by_id
|
|
|
|
assert_equal comments(:more_greetings), authors(:david).comments.find(2)
|
|
|
|
end
|
|
|
|
|
2006-03-18 22:05:21 -05:00
|
|
|
def test_eager_load_has_many_through_has_many
|
|
|
|
author = Author.find :first, :conditions => ['name = ?', 'David'], :include => :comments, :order => 'comments.id'
|
|
|
|
SpecialComment.new; VerySpecialComment.new
|
|
|
|
assert_no_queries do
|
|
|
|
assert_equal [1,2,3,5,6,7,8,9,10], author.comments.collect(&:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-19 14:32:07 -05:00
|
|
|
def test_eager_belongs_to_and_has_one_not_singularized
|
|
|
|
assert_nothing_raised do
|
|
|
|
Author.find(:first, :include => :author_address)
|
|
|
|
AuthorAddress.find(:first, :include => :author)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-20 20:07:16 -05:00
|
|
|
def test_self_referential_has_many_through
|
|
|
|
assert_equal [authors(:mary)], authors(:david).favorite_authors
|
|
|
|
assert_equal [], authors(:mary).favorite_authors
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_to_self_referential_has_many_through
|
|
|
|
new_author = Author.create(:name => "Bob")
|
|
|
|
authors(:david).author_favorites.create :favorite_author => new_author
|
2006-03-21 20:03:25 -05:00
|
|
|
assert_equal new_author, authors(:david).reload.favorite_authors.first
|
2006-03-20 20:07:16 -05:00
|
|
|
end
|
|
|
|
|
2006-03-25 19:36:55 -05:00
|
|
|
def test_has_many_through_uses_correct_attributes
|
|
|
|
assert_nil posts(:thinking).tags.find_by_name("General").attributes["tag_id"]
|
|
|
|
end
|
|
|
|
|
2006-03-15 21:10:11 -05:00
|
|
|
private
|
|
|
|
# create dynamic Post models to allow different dependency options
|
|
|
|
def find_post_with_dependency(post_id, association, association_name, dependency)
|
|
|
|
class_name = "PostWith#{association.to_s.classify}#{dependency.to_s.classify}"
|
|
|
|
Post.find(post_id).update_attribute :type, class_name
|
|
|
|
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
|
|
|
|
klass.set_table_name 'posts'
|
|
|
|
klass.send(association, association_name, :as => :taggable, :dependent => dependency)
|
|
|
|
klass.find(post_id)
|
|
|
|
end
|
2005-12-02 01:03:43 -05:00
|
|
|
end
|