aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-03-05 18:43:56 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-03-05 18:43:56 +0000
commit4f00c70580e376691bd1d6c1f9d09efbaa7bf9c9 (patch)
tree23865027d66ca498ad6c63c5cbb997ee85dc41d0 /activerecord/test
parent84b8920a11985a771e6dbddbf5ed4e3852a2e790 (diff)
downloadrails-4f00c70580e376691bd1d6c1f9d09efbaa7bf9c9.tar.gz
rails-4f00c70580e376691bd1d6c1f9d09efbaa7bf9c9.tar.bz2
rails-4f00c70580e376691bd1d6c1f9d09efbaa7bf9c9.zip
Fixed eager loading problems with single-table inheritance [Rick Olson] Added smarter table aliasing for eager associations for multiple self joins [Rick Olson] (closes #3580)
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3776 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-xactiverecord/test/abstract_unit.rb22
-rw-r--r--activerecord/test/associations_cascaded_eager_loading_test.rb26
-rw-r--r--activerecord/test/associations_go_eager_test.rb8
-rw-r--r--activerecord/test/associations_join_model_test.rb29
-rwxr-xr-xactiverecord/test/base_test.rb4
-rw-r--r--activerecord/test/finder_test.rb3
-rwxr-xr-xactiverecord/test/fixtures/topic.rb2
-rw-r--r--activerecord/test/fixtures/topics.yml1
8 files changed, 78 insertions, 17 deletions
diff --git a/activerecord/test/abstract_unit.rb b/activerecord/test/abstract_unit.rb
index 1bd2f6baaf..70487980c5 100755
--- a/activerecord/test/abstract_unit.rb
+++ b/activerecord/test/abstract_unit.rb
@@ -29,6 +29,19 @@ class Test::Unit::TestCase #:nodoc:
assert_equal expected.to_s, actual.to_s, message
end
end
+
+ def assert_no_queries
+ ActiveRecord::Base.connection.class.class_eval do
+ self.query_count = 0
+ alias_method :execute, :execute_with_query_counting
+ end
+ yield
+ ensure
+ ActiveRecord::Base.connection.class.class_eval do
+ alias_method :execute, :execute_without_query_counting
+ end
+ assert_equal 0, ActiveRecord::Base.connection.query_count, "1 or more queries were executed"
+ end
end
def current_adapter?(type)
@@ -36,5 +49,14 @@ def current_adapter?(type)
ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
end
+ActiveRecord::Base.connection.class.class_eval do
+ cattr_accessor :query_count
+ alias_method :execute_without_query_counting, :execute
+ def execute_with_query_counting(sql, name = nil)
+ self.query_count += 1
+ execute_without_query_counting(sql, name)
+ end
+end
+
#ActiveRecord::Base.logger = Logger.new(STDOUT)
#ActiveRecord::Base.colorize_logging = false
diff --git a/activerecord/test/associations_cascaded_eager_loading_test.rb b/activerecord/test/associations_cascaded_eager_loading_test.rb
index 797bc85bc3..8f0a41f593 100644
--- a/activerecord/test/associations_cascaded_eager_loading_test.rb
+++ b/activerecord/test/associations_cascaded_eager_loading_test.rb
@@ -56,23 +56,33 @@ class CascadedEagerLoadingTest < Test::Unit::TestCase
def test_eager_association_loading_with_acts_as_tree
roots = TreeMixin.find(:all, :include=>"children", :conditions=>"mixins.parent_id IS NULL", :order=>"mixins.id")
assert_equal [mixins(:tree_1), mixins(:tree2_1), mixins(:tree3_1)], roots
- assert_equal 2, roots[0].children.size
- assert_equal 0, roots[1].children.size
- assert_equal 0, roots[2].children.size
+ assert_no_queries do
+ assert_equal 2, roots[0].children.size
+ assert_equal 0, roots[1].children.size
+ assert_equal 0, roots[2].children.size
+ end
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, firms.first.account.firm.account
- assert_equal companies(:first_firm).account.firm.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_sti
+ def test_eager_association_loading_with_has_many_sti
topics = Topic.find(:all, :include => :replies, :order => 'topics.id')
assert_equal [topics(:first), topics(:second)], topics
- assert_equal 1, topics[0].replies.size
- assert_equal 0, topics[1].replies.size
+ 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
end
diff --git a/activerecord/test/associations_go_eager_test.rb b/activerecord/test/associations_go_eager_test.rb
index 2212596099..50cb9bec59 100644
--- a/activerecord/test/associations_go_eager_test.rb
+++ b/activerecord/test/associations_go_eager_test.rb
@@ -94,13 +94,13 @@ class EagerAssociationTest < Test::Unit::TestCase
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 [3], posts.collect { |p| p.id }
+ 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 [4], posts.collect { |p| p.id }
+ assert_equal [2], posts.collect { |p| p.id }
end
def test_eager_with_has_many_through
@@ -108,8 +108,8 @@ class EagerAssociationTest < Test::Unit::TestCase
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), posts_with_author.first.author
- assert_equal authors(:david), posts_with_comments_and_author.first.author
+ 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_and_limit
diff --git a/activerecord/test/associations_join_model_test.rb b/activerecord/test/associations_join_model_test.rb
index a2f42f060e..b14693d7a6 100644
--- a/activerecord/test/associations_join_model_test.rb
+++ b/activerecord/test/associations_join_model_test.rb
@@ -81,7 +81,34 @@ class AssociationsJoinModelTest < Test::Unit::TestCase
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_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
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index a84e0ed5e8..c5834f2e5b 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -567,7 +567,7 @@ class BasicsTest < Test::Unit::TestCase
end
def test_equality
- assert_equal Topic.find(1), Topic.find(2).parent
+ assert_equal Topic.find(1), Topic.find(2).topic
end
def test_equality_of_new_records
@@ -575,7 +575,7 @@ class BasicsTest < Test::Unit::TestCase
end
def test_hashing
- assert_equal [ Topic.find(1) ], [ Topic.find(2).parent ] & [ Topic.find(1) ]
+ assert_equal [ Topic.find(1) ], [ Topic.find(2).topic ] & [ Topic.find(1) ]
end
def test_destroy_new_record
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index 26a4688108..bf7237b684 100644
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -1,6 +1,7 @@
require 'abstract_unit'
require 'fixtures/company'
require 'fixtures/topic'
+require 'fixtures/reply'
require 'fixtures/entrant'
require 'fixtures/developer'
require 'fixtures/post'
@@ -92,7 +93,7 @@ class FinderTest < Test::Unit::TestCase
Topic.find(1).parent
}
- Topic.find(2).parent
+ Topic.find(2).topic
end
def test_find_only_some_columns
diff --git a/activerecord/test/fixtures/topic.rb b/activerecord/test/fixtures/topic.rb
index 4c46122450..1941080ec7 100755
--- a/activerecord/test/fixtures/topic.rb
+++ b/activerecord/test/fixtures/topic.rb
@@ -6,7 +6,7 @@ class Topic < ActiveRecord::Base
before_destroy :destroy_children
def parent
- self.class.find(parent_id)
+ Topic.find(parent_id)
end
protected
diff --git a/activerecord/test/fixtures/topics.yml b/activerecord/test/fixtures/topics.yml
index 6d4f5d800b..810bbcf4e8 100644
--- a/activerecord/test/fixtures/topics.yml
+++ b/activerecord/test/fixtures/topics.yml
@@ -19,3 +19,4 @@ second:
approved: true
replies_count: 2
parent_id: 1
+ type: Reply