aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/associations
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/test/associations')
-rw-r--r--activerecord/test/associations/callbacks_test.rb147
-rw-r--r--activerecord/test/associations/cascaded_eager_loading_test.rb110
-rw-r--r--activerecord/test/associations/eager_singularization_test.rb145
-rw-r--r--activerecord/test/associations/eager_test.rb447
-rw-r--r--activerecord/test/associations/extension_test.rb47
-rw-r--r--activerecord/test/associations/inner_join_association_test.rb88
-rw-r--r--activerecord/test/associations/join_model_test.rb559
7 files changed, 0 insertions, 1543 deletions
diff --git a/activerecord/test/associations/callbacks_test.rb b/activerecord/test/associations/callbacks_test.rb
deleted file mode 100644
index 29f93788e5..0000000000
--- a/activerecord/test/associations/callbacks_test.rb
+++ /dev/null
@@ -1,147 +0,0 @@
-require 'abstract_unit'
-require 'fixtures/post'
-require 'fixtures/comment'
-require 'fixtures/author'
-require 'fixtures/category'
-require 'fixtures/project'
-require 'fixtures/developer'
-
-class AssociationCallbacksTest < ActiveSupport::TestCase
- fixtures :posts, :authors, :projects, :developers
-
- def setup
- @david = authors(:david)
- @thinking = posts(:thinking)
- @authorless = posts(:authorless)
- assert @david.post_log.empty?
- end
-
- def test_adding_macro_callbacks
- @david.posts_with_callbacks << @thinking
- assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log
- @david.posts_with_callbacks << @thinking
- assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}",
- "after_adding#{@thinking.id}"], @david.post_log
- end
-
- def test_adding_with_proc_callbacks
- @david.posts_with_proc_callbacks << @thinking
- assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log
- @david.posts_with_proc_callbacks << @thinking
- assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}",
- "after_adding#{@thinking.id}"], @david.post_log
- end
-
- def test_removing_with_macro_callbacks
- first_post, second_post = @david.posts_with_callbacks[0, 2]
- @david.posts_with_callbacks.delete(first_post)
- assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log
- @david.posts_with_callbacks.delete(second_post)
- assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}",
- "after_removing#{second_post.id}"], @david.post_log
- end
-
- def test_removing_with_proc_callbacks
- first_post, second_post = @david.posts_with_callbacks[0, 2]
- @david.posts_with_proc_callbacks.delete(first_post)
- assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log
- @david.posts_with_proc_callbacks.delete(second_post)
- assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}",
- "after_removing#{second_post.id}"], @david.post_log
- end
-
- def test_multiple_callbacks
- @david.posts_with_multiple_callbacks << @thinking
- assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}",
- "after_adding_proc#{@thinking.id}"], @david.post_log
- @david.posts_with_multiple_callbacks << @thinking
- assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}",
- "after_adding_proc#{@thinking.id}", "before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}",
- "after_adding#{@thinking.id}", "after_adding_proc#{@thinking.id}"], @david.post_log
- end
-
- def test_has_many_callbacks_with_create
- morten = Author.create :name => "Morten"
- post = morten.posts_with_proc_callbacks.create! :title => "Hello", :body => "How are you doing?"
- assert_equal ["before_adding<new>", "after_adding#{post.id}"], morten.post_log
- end
-
- def test_has_many_callbacks_with_create!
- morten = Author.create! :name => "Morten"
- post = morten.posts_with_proc_callbacks.create :title => "Hello", :body => "How are you doing?"
- assert_equal ["before_adding<new>", "after_adding#{post.id}"], morten.post_log
- end
-
- def test_has_many_callbacks_for_save_on_parent
- jack = Author.new :name => "Jack"
- post = jack.posts_with_callbacks.build :title => "Call me back!", :body => "Before you wake up and after you sleep"
-
- callback_log = ["before_adding<new>", "after_adding#{jack.posts_with_callbacks.first.id}"]
- assert_equal callback_log, jack.post_log
- assert jack.save
- assert_equal 1, jack.posts_with_callbacks.count
- assert_equal callback_log, jack.post_log
- end
-
- def test_has_and_belongs_to_many_add_callback
- david = developers(:david)
- ar = projects(:active_record)
- assert ar.developers_log.empty?
- ar.developers_with_callbacks << david
- assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], ar.developers_log
- ar.developers_with_callbacks << david
- assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",
- "after_adding#{david.id}"], ar.developers_log
- end
-
- def test_has_and_belongs_to_many_remove_callback
- david = developers(:david)
- jamis = developers(:jamis)
- activerecord = projects(:active_record)
- assert activerecord.developers_log.empty?
- activerecord.developers_with_callbacks.delete(david)
- assert_equal ["before_removing#{david.id}", "after_removing#{david.id}"], activerecord.developers_log
-
- activerecord.developers_with_callbacks.delete(jamis)
- assert_equal ["before_removing#{david.id}", "after_removing#{david.id}", "before_removing#{jamis.id}",
- "after_removing#{jamis.id}"], activerecord.developers_log
- end
-
- def test_has_and_belongs_to_many_remove_callback_on_clear
- activerecord = projects(:active_record)
- assert activerecord.developers_log.empty?
- if activerecord.developers_with_callbacks.size == 0
- activerecord.developers << developers(:david)
- activerecord.developers << developers(:jamis)
- activerecord.reload
- assert activerecord.developers_with_callbacks.size == 2
- end
- log_array = activerecord.developers_with_callbacks.collect {|d| ["before_removing#{d.id}","after_removing#{d.id}"]}.flatten.sort
- assert activerecord.developers_with_callbacks.clear
- assert_equal log_array, activerecord.developers_log.sort
- end
-
- def test_has_many_and_belongs_to_many_callbacks_for_save_on_parent
- project = Project.new :name => "Callbacks"
- project.developers_with_callbacks.build :name => "Jack", :salary => 95000
-
- callback_log = ["before_adding<new>", "after_adding<new>"]
- assert_equal callback_log, project.developers_log
- assert project.save
- assert_equal 1, project.developers_with_callbacks.size
- assert_equal callback_log, project.developers_log
- end
-
- def test_dont_add_if_before_callback_raises_exception
- assert !@david.unchangable_posts.include?(@authorless)
- begin
- @david.unchangable_posts << @authorless
- rescue Exception => e
- end
- assert @david.post_log.empty?
- assert !@david.unchangable_posts.include?(@authorless)
- @david.reload
- assert !@david.unchangable_posts.include?(@authorless)
- end
-end
-
diff --git a/activerecord/test/associations/cascaded_eager_loading_test.rb b/activerecord/test/associations/cascaded_eager_loading_test.rb
deleted file mode 100644
index 9d6dffbd48..0000000000
--- a/activerecord/test/associations/cascaded_eager_loading_test.rb
+++ /dev/null
@@ -1,110 +0,0 @@
-require 'abstract_unit'
-require 'fixtures/post'
-require 'fixtures/comment'
-require 'fixtures/author'
-require 'fixtures/category'
-require 'fixtures/categorization'
-require 'fixtures/company'
-require 'fixtures/topic'
-require 'fixtures/reply'
-
-class CascadedEagerLoadingTest < ActiveSupport::TestCase
- fixtures :authors, :mixins, :companies, :posts, :topics
-
- def test_eager_association_loading_with_cascaded_two_levels
- authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id")
- assert_equal 2, authors.size
- assert_equal 5, authors[0].posts.size
- assert_equal 1, authors[1].posts.size
- assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
- end
-
- def test_eager_association_loading_with_cascaded_two_levels_and_one_level
- authors = Author.find(:all, :include=>[{:posts=>:comments}, :categorizations], :order=>"authors.id")
- assert_equal 2, authors.size
- assert_equal 5, authors[0].posts.size
- assert_equal 1, authors[1].posts.size
- assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
- assert_equal 1, authors[0].categorizations.size
- assert_equal 2, authors[1].categorizations.size
- end
-
- def test_eager_association_loading_with_cascaded_two_levels_with_two_has_many_associations
- authors = Author.find(:all, :include=>{:posts=>[:comments, :categorizations]}, :order=>"authors.id")
- assert_equal 2, authors.size
- assert_equal 5, authors[0].posts.size
- assert_equal 1, authors[1].posts.size
- assert_equal 9, authors[0].posts.collect{|post| post.comments.size }.inject(0){|sum,i| sum+i}
- end
-
- def test_eager_association_loading_with_cascaded_two_levels_and_self_table_reference
- authors = Author.find(:all, :include=>{:posts=>[:comments, :author]}, :order=>"authors.id")
- assert_equal 2, authors.size
- assert_equal 5, authors[0].posts.size
- assert_equal authors(:david).name, authors[0].name
- assert_equal [authors(:david).name], authors[0].posts.collect{|post| post.author.name}.uniq
- end
-
- def test_eager_association_loading_with_cascaded_two_levels_with_condition
- authors = Author.find(:all, :include=>{:posts=>:comments}, :conditions=>"authors.id=1", :order=>"authors.id")
- assert_equal 1, authors.size
- assert_equal 5, authors[0].posts.size
- end
-
- def test_eager_association_loading_with_cascaded_three_levels_by_ping_pong
- firms = Firm.find(:all, :include=>{:account=>{:firm=>:account}}, :order=>"companies.id")
- assert_equal 2, firms.size
- assert_equal firms.first.account, firms.first.account.firm.account
- assert_equal companies(:first_firm).account, assert_no_queries { firms.first.account.firm.account }
- assert_equal companies(:first_firm).account.firm.account, assert_no_queries { firms.first.account.firm.account }
- end
-
- def test_eager_association_loading_with_has_many_sti
- topics = Topic.find(:all, :include => :replies, :order => 'topics.id')
- assert_equal topics(:first, :second), topics
- assert_no_queries do
- assert_equal 1, topics[0].replies.size
- assert_equal 0, topics[1].replies.size
- end
- end
-
- def test_eager_association_loading_with_belongs_to_sti
- replies = Reply.find(:all, :include => :topic, :order => 'topics.id')
- assert_equal [topics(:second)], replies
- assert_equal topics(:first), assert_no_queries { replies.first.topic }
- end
-
- def test_eager_association_loading_with_multiple_stis_and_order
- author = Author.find(:first, :include => { :posts => [ :special_comments , :very_special_comment ] }, :order => 'authors.name, comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4')
- assert_equal authors(:david), author
- assert_no_queries do
- author.posts.first.special_comments
- author.posts.first.very_special_comment
- end
- end
-
- def test_eager_association_loading_of_stis_with_multiple_references
- authors = Author.find(:all, :include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4')
- assert_equal [authors(:david)], authors
- assert_no_queries do
- authors.first.posts.first.special_comments.first.post.special_comments
- authors.first.posts.first.special_comments.first.post.very_special_comment
- end
- end
-end
-
-require 'fixtures/vertex'
-require 'fixtures/edge'
-class CascadedEagerLoadingTest < ActiveSupport::TestCase
- fixtures :edges, :vertices
-
- def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through
- source = Vertex.find(:first, :include=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id')
- assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first }
- end
-
- def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many
- sink = Vertex.find(:first, :include=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC')
- assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first }
- end
-end
diff --git a/activerecord/test/associations/eager_singularization_test.rb b/activerecord/test/associations/eager_singularization_test.rb
deleted file mode 100644
index 2990684afc..0000000000
--- a/activerecord/test/associations/eager_singularization_test.rb
+++ /dev/null
@@ -1,145 +0,0 @@
-require 'abstract_unit'
-
-class Virus < ActiveRecord::Base
- belongs_to :octopus
-end
-class Octopus < ActiveRecord::Base
- has_one :virus
-end
-class Pass < ActiveRecord::Base
- belongs_to :bus
-end
-class Bus < ActiveRecord::Base
- has_many :passes
-end
-class Mess < ActiveRecord::Base
- has_and_belongs_to_many :crises
-end
-class Crisis < ActiveRecord::Base
- has_and_belongs_to_many :messes
- has_many :analyses, :dependent => :destroy
- has_many :successes, :through => :analyses
- has_many :dresses, :dependent => :destroy
- has_many :compresses, :through => :dresses
-end
-class Analysis < ActiveRecord::Base
- belongs_to :crisis
- belongs_to :success
-end
-class Success < ActiveRecord::Base
- has_many :analyses, :dependent => :destroy
- has_many :crises, :through => :analyses
-end
-class Dress < ActiveRecord::Base
- belongs_to :crisis
- has_many :compresses
-end
-class Compress < ActiveRecord::Base
- belongs_to :dress
-end
-
-
-class EagerSingularizationTest < ActiveSupport::TestCase
-
- def setup
- if ActiveRecord::Base.connection.supports_migrations?
- ActiveRecord::Base.connection.create_table :viri do |t|
- t.column :octopus_id, :integer
- t.column :species, :string
- end
- ActiveRecord::Base.connection.create_table :octopi do |t|
- t.column :species, :string
- end
- ActiveRecord::Base.connection.create_table :passes do |t|
- t.column :bus_id, :integer
- t.column :rides, :integer
- end
- ActiveRecord::Base.connection.create_table :buses do |t|
- t.column :name, :string
- end
- ActiveRecord::Base.connection.create_table :crises_messes, :id => false do |t|
- t.column :crisis_id, :integer
- t.column :mess_id, :integer
- end
- ActiveRecord::Base.connection.create_table :messes do |t|
- t.column :name, :string
- end
- ActiveRecord::Base.connection.create_table :crises do |t|
- t.column :name, :string
- end
- ActiveRecord::Base.connection.create_table :successes do |t|
- t.column :name, :string
- end
- ActiveRecord::Base.connection.create_table :analyses do |t|
- t.column :crisis_id, :integer
- t.column :success_id, :integer
- end
- ActiveRecord::Base.connection.create_table :dresses do |t|
- t.column :crisis_id, :integer
- end
- ActiveRecord::Base.connection.create_table :compresses do |t|
- t.column :dress_id, :integer
- end
- @have_tables = true
- else
- @have_tables = false
- end
- end
-
- def teardown
- ActiveRecord::Base.connection.drop_table :viri
- ActiveRecord::Base.connection.drop_table :octopi
- ActiveRecord::Base.connection.drop_table :passes
- ActiveRecord::Base.connection.drop_table :buses
- ActiveRecord::Base.connection.drop_table :crises_messes
- ActiveRecord::Base.connection.drop_table :messes
- ActiveRecord::Base.connection.drop_table :crises
- ActiveRecord::Base.connection.drop_table :successes
- ActiveRecord::Base.connection.drop_table :analyses
- ActiveRecord::Base.connection.drop_table :dresses
- ActiveRecord::Base.connection.drop_table :compresses
- end
-
- def test_eager_no_extra_singularization_belongs_to
- return unless @have_tables
- assert_nothing_raised do
- Virus.find(:all, :include => :octopus)
- end
- end
-
- def test_eager_no_extra_singularization_has_one
- return unless @have_tables
- assert_nothing_raised do
- Octopus.find(:all, :include => :virus)
- end
- end
-
- def test_eager_no_extra_singularization_has_many
- return unless @have_tables
- assert_nothing_raised do
- Bus.find(:all, :include => :passes)
- end
- end
-
- def test_eager_no_extra_singularization_has_and_belongs_to_many
- return unless @have_tables
- assert_nothing_raised do
- Crisis.find(:all, :include => :messes)
- Mess.find(:all, :include => :crises)
- end
- end
-
- def test_eager_no_extra_singularization_has_many_through_belongs_to
- return unless @have_tables
- assert_nothing_raised do
- Crisis.find(:all, :include => :successes)
- end
- end
-
- def test_eager_no_extra_singularization_has_many_through_has_many
- return unless @have_tables
- assert_nothing_raised do
- Crisis.find(:all, :include => :compresses)
- end
- end
-end
diff --git a/activerecord/test/associations/eager_test.rb b/activerecord/test/associations/eager_test.rb
deleted file mode 100644
index bdadada3f4..0000000000
--- a/activerecord/test/associations/eager_test.rb
+++ /dev/null
@@ -1,447 +0,0 @@
-require 'abstract_unit'
-require 'fixtures/post'
-require 'fixtures/comment'
-require 'fixtures/author'
-require 'fixtures/category'
-require 'fixtures/company'
-require 'fixtures/person'
-require 'fixtures/reader'
-
-class EagerAssociationTest < ActiveSupport::TestCase
- fixtures :posts, :comments, :authors, :categories, :categories_posts,
- :companies, :accounts, :tags, :people, :readers
-
- def test_loading_with_one_association
- posts = Post.find(:all, :include => :comments)
- post = posts.find { |p| p.id == 1 }
- assert_equal 2, post.comments.size
- assert post.comments.include?(comments(:greetings))
-
- post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'")
- assert_equal 2, post.comments.size
- assert post.comments.include?(comments(:greetings))
- end
-
- def test_loading_conditions_with_or
- posts = authors(:david).posts.find(:all, :include => :comments, :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'")
- assert_nil posts.detect { |p| p.author_id != authors(:david).id },
- "expected to find only david's posts"
- end
-
- def test_with_ordering
- 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
- end
-
- def test_with_two_tables_in_from_without_getting_double_quoted
- posts = Post.find(:all,
- :select => "posts.*",
- :from => "authors, posts",
- :include => :comments,
- :conditions => "posts.author_id = authors.id",
- :order => "posts.id"
- )
-
- assert_equal 2, posts.first.comments.size
- end
-
- def test_loading_with_multiple_associations
- posts = Post.find(:all, :include => [ :comments, :author, :categories ], :order => "posts.id")
- assert_equal 2, posts.first.comments.size
- assert_equal 2, posts.first.categories.size
- assert posts.first.comments.include?(comments(:greetings))
- end
-
- def test_loading_from_an_association
- posts = authors(:david).posts.find(:all, :include => :comments, :order => "posts.id")
- assert_equal 2, posts.first.comments.size
- end
-
- def test_loading_with_no_associations
- assert_nil Post.find(posts(:authorless).id, :include => :author).author
- end
-
- def test_eager_association_loading_with_belongs_to
- comments = Comment.find(:all, :include => :post)
- assert_equal 10, comments.length
- titles = comments.map { |c| c.post.title }
- assert titles.include?(posts(:welcome).title)
- assert titles.include?(posts(:sti_post_and_comments).title)
- end
-
- def test_eager_association_loading_with_belongs_to_and_limit
- comments = Comment.find(:all, :include => :post, :limit => 5, :order => 'comments.id')
- 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, :order => 'comments.id')
- 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, :order => 'comments.id')
- 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, :order => 'comments.id')
- 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_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
-
- def test_eager_association_loading_with_belongs_to_and_limit_and_multiple_associations
- posts = Post.find(:all, :include => [:author, :very_special_comment], :limit => 1, :order => 'posts.id')
- assert_equal 1, posts.length
- assert_equal [1], 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, :order => 'posts.id')
- assert_equal 1, posts.length
- assert_equal [2], posts.collect { |p| p.id }
- end
-
- 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
-
- 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
-
- def test_eager_with_has_many_through
- posts_with_comments = people(:michael).posts.find(:all, :include => :comments)
- 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 }
- 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 }
- end
-
- 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
-
- 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
-
- def test_eager_with_has_many_through_join_model_with_conditions
- 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)
- end
-
- def test_eager_with_has_many_and_limit
- posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2)
- assert_equal 2, posts.size
- assert_equal 3, posts.inject(0) { |sum, post| sum += post.comments.size }
- end
-
- def test_eager_with_has_many_and_limit_and_conditions
- 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
- 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
- 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
- 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_on_the_eagers
- posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
- assert_equal 2, posts.size
-
- count = Post.count(:include => [ :author, :comments ], :limit => 2, :conditions => [ "authors.name = ?", 'David' ])
- assert_equal count, posts.size
- end
-
- 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
-
- 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
-
- 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
-
- 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
- posts = authors(:david).posts.find(:all,
- :include => :comments,
- :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
- :limit => 2
- )
- assert_equal 2, posts.size
-
- count = Post.count(
- :include => [ :comments, :author ],
- :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
- :limit => 2
- )
- assert_equal count, posts.size
- end
-
- def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
- posts = nil
- Post.with_scope(:find => {
- :include => :comments,
- :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
-
- Post.with_scope(:find => {
- :include => [ :comments, :author ],
- :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
- posts = authors(:david).posts.find(:all,
- :include => :comments,
- :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
- :limit => 2
- )
- assert_equal 2, posts.size
-
- count = Post.count(
- :include => [ :comments, :author ],
- :conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')",
- :limit => 2
- )
- assert_equal count, posts.size
- end
- end
-
- def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
- posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2)
- posts_with_scoped_order = Post.with_scope(:find => {:order => 'posts.id DESC'}) do
- Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
- end
- assert_equal posts_with_explicit_order, posts_with_scoped_order
- end
-
- def test_eager_association_loading_with_habtm
- 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
- assert posts[0].categories.include?(categories(:technology))
- assert posts[1].categories.include?(categories(:general))
- end
-
- def test_eager_with_inheritance
- posts = SpecialPost.find(:all, :include => [ :comments ])
- end
-
- 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
- end
-
- 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
- end
-
- 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
-
- 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"])
- assert_not_nil f.account
- assert_equal companies(:first_firm, :reload).account, f.account
- end
-
- 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
-
- def test_eager_with_invalid_association_reference
- assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
- post = Post.find(6, :include=> :monkeys )
- }
- assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
- post = Post.find(6, :include=>[ :monkeys ])
- }
- assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys") {
- post = Post.find(6, :include=>[ 'monkeys' ])
- }
- assert_raises(ActiveRecord::ConfigurationError, "Association was not found; perhaps you misspelled it? You specified :include => :monkeys, :elephants") {
- post = Post.find(6, :include=>[ :monkeys, :elephants ])
- }
- end
-
- def find_all_ordered(className, include=nil)
- className.find(:all, :order=>"#{className.table_name}.#{className.primary_key}", :include=>include)
- end
-
- def test_limited_eager_with_order
- 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)
- end
-
- def test_limited_eager_with_multiple_order_columns
- 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)
- end
-
- 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
- post_types = [:posts, :other_posts, :special_posts]
- # 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
- d2 = find_all_ordered(className, post_types)
- d1.each_index do |i|
- 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
-
- def test_eager_with_multiple_associations_with_same_table_has_one
- d1 = find_all_ordered(Firm)
- d2 = find_all_ordered(Firm, :account)
- d1.each_index do |i|
- assert_equal(d1[i], d2[i])
- assert_equal(d1[i].account, d2[i].account)
- end
- end
-
- 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)
- d1.each_index do |i|
- assert_equal(d1[i], d2[i])
- firm_types.each { |type| assert_equal(d1[i].send(type), d2[i].send(type)) }
- end
- end
- def test_eager_with_valid_association_as_string_not_symbol
- assert_nothing_raised { Post.find(:all, :include => 'comments') }
- end
-
- def test_preconfigured_includes_with_belongs_to
- author = posts(:welcome).author_with_posts
- assert_equal 5, author.posts.size
- end
-
- def test_preconfigured_includes_with_has_one
- comment = posts(:sti_comments).very_special_comment_with_post
- assert_equal posts(:sti_comments), comment.post
- end
-
- def test_preconfigured_includes_with_has_many
- posts = authors(:david).posts_with_comments
- one = posts.detect { |p| p.id == 1 }
- assert_equal 5, posts.size
- assert_equal 2, one.comments.size
- end
-
- def test_preconfigured_includes_with_habtm
- posts = authors(:david).posts_with_categories
- one = posts.detect { |p| p.id == 1 }
- assert_equal 5, posts.size
- assert_equal 2, one.categories.size
- end
-
- def test_preconfigured_includes_with_has_many_and_habtm
- posts = authors(:david).posts_with_comments_and_categories
- one = posts.detect { |p| p.id == 1 }
- assert_equal 5, posts.size
- assert_equal 2, one.comments.size
- assert_equal 2, one.categories.size
- end
-
- def test_count_with_include
- if current_adapter?(:SQLServerAdapter, :SybaseAdapter)
- assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15")
- elsif current_adapter?(:OpenBaseAdapter)
- assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15")
- else
- assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15")
- end
- end
-end
diff --git a/activerecord/test/associations/extension_test.rb b/activerecord/test/associations/extension_test.rb
deleted file mode 100644
index 3749f7ac41..0000000000
--- a/activerecord/test/associations/extension_test.rb
+++ /dev/null
@@ -1,47 +0,0 @@
-require 'abstract_unit'
-require 'fixtures/post'
-require 'fixtures/comment'
-require 'fixtures/project'
-require 'fixtures/developer'
-
-class AssociationsExtensionsTest < ActiveSupport::TestCase
- fixtures :projects, :developers, :developers_projects, :comments, :posts
-
- def test_extension_on_has_many
- assert_equal comments(:more_greetings), posts(:welcome).comments.find_most_recent
- end
-
- def test_extension_on_habtm
- assert_equal projects(:action_controller), developers(:david).projects.find_most_recent
- end
-
- def test_named_extension_on_habtm
- assert_equal projects(:action_controller), developers(:david).projects_extended_by_name.find_most_recent
- end
-
- def test_named_two_extensions_on_habtm
- assert_equal projects(:action_controller), developers(:david).projects_extended_by_name_twice.find_most_recent
- assert_equal projects(:active_record), developers(:david).projects_extended_by_name_twice.find_least_recent
- end
-
- def test_named_extension_and_block_on_habtm
- assert_equal projects(:action_controller), developers(:david).projects_extended_by_name_and_block.find_most_recent
- assert_equal projects(:active_record), developers(:david).projects_extended_by_name_and_block.find_least_recent
- end
-
- def test_marshalling_extensions
- david = developers(:david)
- assert_equal projects(:action_controller), david.projects.find_most_recent
-
- david = Marshal.load(Marshal.dump(david))
- assert_equal projects(:action_controller), david.projects.find_most_recent
- end
-
- def test_marshalling_named_extensions
- david = developers(:david)
- assert_equal projects(:action_controller), david.projects_extended_by_name.find_most_recent
-
- david = Marshal.load(Marshal.dump(david))
- assert_equal projects(:action_controller), david.projects_extended_by_name.find_most_recent
- end
-end \ No newline at end of file
diff --git a/activerecord/test/associations/inner_join_association_test.rb b/activerecord/test/associations/inner_join_association_test.rb
deleted file mode 100644
index 56735afae5..0000000000
--- a/activerecord/test/associations/inner_join_association_test.rb
+++ /dev/null
@@ -1,88 +0,0 @@
-require 'abstract_unit'
-require 'fixtures/post'
-require 'fixtures/comment'
-require 'fixtures/author'
-require 'fixtures/category'
-require 'fixtures/categorization'
-
-class InnerJoinAssociationTest < ActiveSupport::TestCase
- fixtures :authors, :posts, :comments, :categories, :categories_posts, :categorizations
-
- def test_construct_finder_sql_creates_inner_joins
- sql = Author.send(:construct_finder_sql, :joins => :posts)
- assert_match /INNER JOIN .?posts.? ON .?posts.?.author_id = authors.id/, sql
- end
-
- def test_construct_finder_sql_cascades_inner_joins
- sql = Author.send(:construct_finder_sql, :joins => {:posts => :comments})
- assert_match /INNER JOIN .?posts.? ON .?posts.?.author_id = authors.id/, sql
- assert_match /INNER JOIN .?comments.? ON .?comments.?.post_id = posts.id/, sql
- end
-
- def test_construct_finder_sql_inner_joins_through_associations
- sql = Author.send(:construct_finder_sql, :joins => :categorized_posts)
- assert_match /INNER JOIN .?categorizations.?.*INNER JOIN .?posts.?/, sql
- end
-
- def test_construct_finder_sql_applies_association_conditions
- sql = Author.send(:construct_finder_sql, :joins => :categories_like_general, :conditions => "TERMINATING_MARKER")
- assert_match /INNER JOIN .?categories.? ON.*AND.*.?General.?.*TERMINATING_MARKER/, sql
- end
-
- def test_construct_finder_sql_unpacks_nested_joins
- sql = Author.send(:construct_finder_sql, :joins => {:posts => [[:comments]]})
- assert_no_match /inner join.*inner join.*inner join/i, sql, "only two join clauses should be present"
- assert_match /INNER JOIN .?posts.? ON .?posts.?.author_id = authors.id/, sql
- assert_match /INNER JOIN .?comments.? ON .?comments.?.post_id = .?posts.?.id/, sql
- end
-
- def test_construct_finder_sql_ignores_empty_joins_hash
- sql = Author.send(:construct_finder_sql, :joins => {})
- assert_no_match /JOIN/i, sql
- end
-
- def test_construct_finder_sql_ignores_empty_joins_array
- sql = Author.send(:construct_finder_sql, :joins => [])
- assert_no_match /JOIN/i, sql
- end
-
- def test_find_with_implicit_inner_joins_honors_readonly_without_select
- authors = Author.find(:all, :joins => :posts)
- assert !authors.empty?, "expected authors to be non-empty"
- assert authors.all? {|a| a.readonly? }, "expected all authors to be readonly"
- end
-
- def test_find_with_implicit_inner_joins_honors_readonly_with_select
- authors = Author.find(:all, :select => 'authors.*', :joins => :posts)
- assert !authors.empty?, "expected authors to be non-empty"
- assert authors.all? {|a| !a.readonly? }, "expected no authors to be readonly"
- end
-
- def test_find_with_implicit_inner_joins_honors_readonly_false
- authors = Author.find(:all, :joins => :posts, :readonly => false)
- assert !authors.empty?, "expected authors to be non-empty"
- assert authors.all? {|a| !a.readonly? }, "expected no authors to be readonly"
- end
-
- def test_find_with_implicit_inner_joins_does_not_set_associations
- authors = Author.find(:all, :select => 'authors.*', :joins => :posts)
- assert !authors.empty?, "expected authors to be non-empty"
- assert authors.all? {|a| !a.send(:instance_variable_names).include?("@posts")}, "expected no authors to have the @posts association loaded"
- end
-
- def test_count_honors_implicit_inner_joins
- real_count = Author.find(:all).sum{|a| a.posts.count }
- assert_equal real_count, Author.count(:joins => :posts), "plain inner join count should match the number of referenced posts records"
- end
-
- def test_calculate_honors_implicit_inner_joins
- real_count = Author.find(:all).sum{|a| a.posts.count }
- assert_equal real_count, Author.calculate(:count, 'authors.id', :joins => :posts), "plain inner join count should match the number of referenced posts records"
- end
-
- def test_calculate_honors_implicit_inner_joins_and_distinct_and_conditions
- real_count = Author.find(:all).select {|a| a.posts.any? {|p| p.title =~ /^Welcome/} }.length
- authors_with_welcoming_post_titles = Author.calculate(:count, 'authors.id', :joins => :posts, :distinct => true, :conditions => "posts.title like 'Welcome%'")
- assert_equal real_count, authors_with_welcoming_post_titles, "inner join and conditions should have only returned authors posting titles starting with 'Welcome'"
- end
-end
diff --git a/activerecord/test/associations/join_model_test.rb b/activerecord/test/associations/join_model_test.rb
deleted file mode 100644
index d55faf70c6..0000000000
--- a/activerecord/test/associations/join_model_test.rb
+++ /dev/null
@@ -1,559 +0,0 @@
-require 'abstract_unit'
-require 'fixtures/tag'
-require 'fixtures/tagging'
-require 'fixtures/post'
-require 'fixtures/item'
-require 'fixtures/comment'
-require 'fixtures/author'
-require 'fixtures/category'
-require 'fixtures/categorization'
-require 'fixtures/vertex'
-require 'fixtures/edge'
-require 'fixtures/book'
-require 'fixtures/citation'
-
-class AssociationsJoinModelTest < ActiveSupport::TestCase
- self.use_transactional_fixtures = false
- fixtures :posts, :authors, :categories, :categorizations, :comments, :tags, :taggings, :author_favorites, :vertices, :items, :books
-
- def test_has_many
- assert authors(:david).categories.include?(categories(:general))
- end
-
- def test_has_many_inherited
- assert authors(:mary).categories.include?(categories(:sti_test))
- end
-
- def test_inherited_has_many
- assert categories(:sti_test).authors.include?(authors(:mary))
- end
-
- def test_has_many_uniq_through_join_model
- assert_equal 2, authors(:mary).categorized_posts.size
- assert_equal 1, authors(:mary).unique_categorized_posts.size
- end
-
- def test_has_many_uniq_through_count
- author = authors(:mary)
- assert !authors(:mary).unique_categorized_posts.loaded?
- assert_queries(1) { assert_equal 1, author.unique_categorized_posts.count }
- assert_queries(1) { assert_equal 1, author.unique_categorized_posts.count(:title) }
- assert_queries(1) { assert_equal 0, author.unique_categorized_posts.count(:title, :conditions => "title is NULL") }
- assert !authors(:mary).unique_categorized_posts.loaded?
- end
-
- def test_polymorphic_has_many
- assert posts(:welcome).taggings.include?(taggings(:welcome_general))
- end
-
- def test_polymorphic_has_one
- assert_equal taggings(:welcome_general), posts(:welcome).tagging
- end
-
- def test_polymorphic_belongs_to
- assert_equal posts(:welcome), posts(:welcome).taggings.first.taggable
- end
-
- def test_polymorphic_has_many_going_through_join_model
- assert_equal tags(:general), tag = posts(:welcome).tags.first
- assert_no_queries do
- tag.tagging
- end
- end
-
- def test_count_polymorphic_has_many
- assert_equal 1, posts(:welcome).taggings.count
- assert_equal 1, posts(:welcome).tags.count
- end
-
- 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
-
- 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
-
- 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
-
- 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
-
- def test_polymorphic_has_many_going_through_join_model_with_inheritance
- assert_equal tags(:general), posts(:thinking).tags.first
- end
-
- 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
-
- 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
-
- def test_polymorphic_has_one_create_model_with_inheritance
- tagging = tags(:misc).create_tagging(:taggable => posts(:thinking))
- assert_equal "Post", tagging.taggable_type
- end
-
- 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
- old_count = posts(:welcome).taggings.count
- tagging = posts(:welcome).taggings.create(:tag => tags(:misc))
- assert_equal "Post", tagging.taggable_type
- assert_equal old_count+1, posts(:welcome).taggings.count
- end
-
- def test_create_bang_polymorphic_with_has_many_scope
- old_count = posts(:welcome).taggings.count
- tagging = posts(:welcome).taggings.create!(:tag => tags(:misc))
- assert_equal "Post", tagging.taggable_type
- assert_equal old_count+1, posts(:welcome).taggings.count
- end
-
- def test_create_polymorphic_has_one_with_scope
- old_count = Tagging.count
- tagging = posts(:welcome).tagging.create(:tag => tags(:misc))
- assert_equal "Post", tagging.taggable_type
- 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)
- end
-
- def test_has_many_with_piggyback
- assert_equal "2", categories(:sti_test).authors.first.post_id.to_s
- end
-
- 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_one
- post = Post.find_by_id(posts(:welcome).id, :include => :tagging)
- tagging = taggings(:welcome_general)
- assert_no_queries do
- assert_equal tagging, post.tagging
- end
- end
-
- def test_include_polymorphic_has_one_defined_in_abstract_parent
- item = Item.find_by_id(items(:dvd).id, :include => :tagging)
- tagging = taggings(:godfather)
- assert_no_queries do
- assert_equal tagging, item.tagging
- 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
-
- 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_with_hash_conditions
- assert_equal categories(:general), authors(:david).categories_like_general.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
- assert_equal categories(:general), authors(:david).categories.find_all_by_name('General').first
- assert_equal nil, authors(:david).categories.find_by_name('Technology')
- end
-
- 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
-
- 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
-
- def test_unavailable_through_reflection
- assert_raise(ActiveRecord::HasManyThroughAssociationNotFoundError) { authors(:david).nothings }
- end
-
- def test_has_many_through_join_model_with_conditions
- assert_equal [], posts(:welcome).invalid_taggings
- assert_equal [], posts(:welcome).invalid_tags
- end
-
- def test_has_many_polymorphic
- assert_raise ActiveRecord::HasManyThroughAssociationPolymorphicError do
- assert_equal posts(:welcome, :thinking), tags(:general).taggables
- end
- assert_raise ActiveRecord::EagerLoadPolymorphicError do
- assert_equal posts(:welcome, :thinking), tags(:general).taggings.find(:all, :include => :taggable)
- end
- end
-
- def test_has_many_polymorphic_with_source_type
- assert_equal posts(:welcome, :thinking), tags(:general).tagged_posts
- end
-
- def test_eager_has_many_polymorphic_with_source_type
- tag_with_include = Tag.find(tags(:general).id, :include => :tagged_posts)
- desired = posts(:welcome, :thinking)
- assert_no_queries do
- assert_equal desired, tag_with_include.tagged_posts
- end
- end
-
- def test_has_many_through_has_many_find_all
- assert_equal comments(:greetings), authors(:david).comments.find(:all, :order => 'comments.id').first
- end
-
- 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
-
- def test_has_many_through_has_many_find_first
- assert_equal comments(:greetings), authors(:david).comments.find(:first, :order => 'comments.id')
- end
-
- def test_has_many_through_has_many_find_conditions
- options = { :conditions => "comments.#{QUOTED_TYPE}='SpecialComment'", :order => 'comments.id' }
- assert_equal comments(:does_it_hurt), authors(:david).comments.find(:first, options)
- end
-
- def test_has_many_through_has_many_find_by_id
- assert_equal comments(:more_greetings), authors(:david).comments.find(2)
- end
-
- def test_has_many_through_polymorphic_has_one
- assert_raise(ActiveRecord::HasManyThroughSourceAssociationMacroError) { authors(:david).tagging }
- end
-
- def test_has_many_through_polymorphic_has_many
- assert_equal taggings(:welcome_general, :thinking_general), authors(:david).taggings.uniq.sort_by { |t| t.id }
- end
-
- def test_include_has_many_through_polymorphic_has_many
- author = Author.find_by_id(authors(:david).id, :include => :taggings)
- expected_taggings = taggings(:welcome_general, :thinking_general)
- assert_no_queries do
- assert_equal expected_taggings, author.taggings.uniq.sort_by { |t| t.id }
- end
- end
-
- def test_has_many_through_has_many_through
- assert_raise(ActiveRecord::HasManyThroughSourceAssociationMacroError) { authors(:david).tags }
- end
-
- def test_has_many_through_habtm
- assert_raise(ActiveRecord::HasManyThroughSourceAssociationMacroError) { authors(:david).post_categories }
- end
-
- 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
-
- def test_eager_load_has_many_through_has_many_with_conditions
- post = Post.find(:first, :include => :invalid_tags)
- assert_no_queries do
- post.invalid_tags
- end
- end
-
- 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
-
- 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
- assert_equal new_author, authors(:david).reload.favorite_authors.first
- end
-
- def test_has_many_through_uses_conditions_specified_on_the_has_many_association
- author = Author.find(:first)
- assert !author.comments.blank?
- assert author.nonexistant_comments.blank?
- end
-
- def test_has_many_through_uses_correct_attributes
- assert_nil posts(:thinking).tags.find_by_name("General").attributes["tag_id"]
- end
-
- def test_raise_error_when_adding_new_record_to_has_many_through
- assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags << tags(:general).clone }
- assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).clone.tags << tags(:general) }
- assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags.build }
- assert_raise(ActiveRecord::HasManyThroughCantAssociateNewRecords) { posts(:thinking).tags.new }
- end
-
- def test_create_associate_when_adding_to_has_many_through
- count = posts(:thinking).tags.count
- push = Tag.create!(:name => 'pushme')
- post_thinking = posts(:thinking)
- assert_nothing_raised { post_thinking.tags << push }
- assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag },
- message = "Expected a Tag in tags collection, got #{wrong.class}.")
- assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging },
- message = "Expected a Tagging in taggings collection, got #{wrong.class}.")
- assert_equal(count + 1, post_thinking.tags.size)
- assert_equal(count + 1, post_thinking.tags(true).size)
-
- assert_kind_of Tag, post_thinking.tags.create!(:name => 'foo')
- assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag },
- message = "Expected a Tag in tags collection, got #{wrong.class}.")
- assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging },
- message = "Expected a Tagging in taggings collection, got #{wrong.class}.")
- assert_equal(count + 2, post_thinking.tags.size)
- assert_equal(count + 2, post_thinking.tags(true).size)
-
- assert_nothing_raised { post_thinking.tags.concat(Tag.create!(:name => 'abc'), Tag.create!(:name => 'def')) }
- assert_nil( wrong = post_thinking.tags.detect { |t| t.class != Tag },
- message = "Expected a Tag in tags collection, got #{wrong.class}.")
- assert_nil( wrong = post_thinking.taggings.detect { |t| t.class != Tagging },
- message = "Expected a Tagging in taggings collection, got #{wrong.class}.")
- assert_equal(count + 4, post_thinking.tags.size)
- assert_equal(count + 4, post_thinking.tags(true).size)
-
- # Raises if the wrong reflection name is used to set the Edge belongs_to
- assert_nothing_raised { vertices(:vertex_1).sinks << vertices(:vertex_5) }
- end
-
- def test_has_many_through_collection_size_doesnt_load_target_if_not_loaded
- author = authors(:david)
- assert_equal 9, author.comments.size
- assert !author.comments.loaded?
- end
-
- uses_mocha('has_many_through_collection_size_uses_counter_cache_if_it_exists') do
- def test_has_many_through_collection_size_uses_counter_cache_if_it_exists
- author = authors(:david)
- author.stubs(:read_attribute).with('comments_count').returns(100)
- assert_equal 100, author.comments.size
- assert !author.comments.loaded?
- end
- end
-
- def test_adding_junk_to_has_many_through_should_raise_type_mismatch
- assert_raise(ActiveRecord::AssociationTypeMismatch) { posts(:thinking).tags << "Uhh what now?" }
- end
-
- def test_adding_to_has_many_through_should_return_self
- tags = posts(:thinking).tags
- assert_equal tags, posts(:thinking).tags.push(tags(:general))
- end
-
- def test_delete_associate_when_deleting_from_has_many_through_with_nonstandard_id
- count = books(:awdr).references.count
- references_before = books(:awdr).references
- book = Book.create!(:name => 'Getting Real')
- book_awdr = books(:awdr)
- book_awdr.references << book
- assert_equal(count + 1, book_awdr.references(true).size)
-
- assert_nothing_raised { book_awdr.references.delete(book) }
- assert_equal(count, book_awdr.references.size)
- assert_equal(count, book_awdr.references(true).size)
- assert_equal(references_before.sort, book_awdr.references.sort)
- end
-
- def test_delete_associate_when_deleting_from_has_many_through
- count = posts(:thinking).tags.count
- tags_before = posts(:thinking).tags
- tag = Tag.create!(:name => 'doomed')
- post_thinking = posts(:thinking)
- post_thinking.tags << tag
- assert_equal(count + 1, post_thinking.tags(true).size)
-
- assert_nothing_raised { post_thinking.tags.delete(tag) }
- assert_equal(count, post_thinking.tags.size)
- assert_equal(count, post_thinking.tags(true).size)
- assert_equal(tags_before.sort, post_thinking.tags.sort)
- end
-
- def test_delete_associate_when_deleting_from_has_many_through_with_multiple_tags
- count = posts(:thinking).tags.count
- tags_before = posts(:thinking).tags
- doomed = Tag.create!(:name => 'doomed')
- doomed2 = Tag.create!(:name => 'doomed2')
- quaked = Tag.create!(:name => 'quaked')
- post_thinking = posts(:thinking)
- post_thinking.tags << doomed << doomed2
- assert_equal(count + 2, post_thinking.tags(true).size)
-
- assert_nothing_raised { post_thinking.tags.delete(doomed, doomed2, quaked) }
- assert_equal(count, post_thinking.tags.size)
- assert_equal(count, post_thinking.tags(true).size)
- assert_equal(tags_before.sort, post_thinking.tags.sort)
- end
-
- def test_deleting_junk_from_has_many_through_should_raise_type_mismatch
- assert_raise(ActiveRecord::AssociationTypeMismatch) { posts(:thinking).tags.delete("Uhh what now?") }
- end
-
- def test_has_many_through_sum_uses_calculations
- assert_nothing_raised { authors(:david).comments.sum(:post_id) }
- end
-
- def test_has_many_through_has_many_with_sti
- assert_equal [comments(:does_it_hurt)], authors(:david).special_post_comments
- end
-
- def test_uniq_has_many_through_should_retain_order
- comment_ids = authors(:david).comments.map(&:id)
- assert_equal comment_ids.sort, authors(:david).ordered_uniq_comments.map(&:id)
- assert_equal comment_ids.sort.reverse, authors(:david).ordered_uniq_comments_desc.map(&:id)
- end
-
- 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
-end