From f8783abf0cd409d53e7e104b576d45966252378b Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 3 Apr 2005 17:50:11 +0000 Subject: Made eager loading work even more git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1083 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/associations_go_eager_test.rb | 36 ++++++++++++++++++++++ activerecord/test/associations_test.rb | 27 +--------------- activerecord/test/fixtures/author.rb | 2 +- activerecord/test/fixtures/authors.yml | 2 -- .../test/fixtures/db_definitions/sqlite.sql | 2 +- activerecord/test/fixtures/post.rb | 4 +-- activerecord/test/fixtures/posts.yml | 2 ++ 7 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 activerecord/test/associations_go_eager_test.rb (limited to 'activerecord/test') diff --git a/activerecord/test/associations_go_eager_test.rb b/activerecord/test/associations_go_eager_test.rb new file mode 100644 index 0000000000..59ca4cac85 --- /dev/null +++ b/activerecord/test/associations_go_eager_test.rb @@ -0,0 +1,36 @@ +require 'abstract_unit' +require 'fixtures/post' +require 'fixtures/comment' +require 'fixtures/author' + +class EagerAssociationTest < Test::Unit::TestCase + fixtures :posts, :comments, :authors + + def test_loading_with_one_association + posts = Post.find(:all, :include => :comments) + assert_equal 2, posts.first.comments.size + assert_equal @greetings.body, posts.first.comments.first.body + + post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'") + assert_equal 2, post.comments.size + assert_equal @greetings.body, post.comments.first.body + end + + def test_loading_with_multiple_associations + posts = Post.find(:all, :include => [ :comments, :author ]) + assert_equal 2, posts.first.comments.size + assert_equal @greetings.body, posts.first.comments.first.body + end + + def test_loading_from_an_association + posts = @david.posts.find(:all, :include => :comments) + assert_equal 2, posts.first.comments.size + end + + def test_eager_association_loading_with_belongs_to + comments = Comment.find(:all, :include => :post) + assert_equal @welcome.title, comments.first.post.title + assert_equal @thinking.title, comments.last.post.title + end +end + diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index e1a05e481b..cd9fa7cb18 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -5,9 +5,6 @@ require 'fixtures/company' require 'fixtures/topic' require 'fixtures/reply' require 'fixtures/computer' -require 'fixtures/post' -require 'fixtures/comment' -require 'fixtures/author' # Can't declare new classes in test case methods, so tests before that bad_collection_keys = false @@ -206,7 +203,7 @@ end class HasManyAssociationsTest < Test::Unit::TestCase - fixtures :accounts, :companies, :developers, :projects, :developers_projects, :topics, :posts, :comments + fixtures :accounts, :companies, :developers, :projects, :developers_projects, :topics def setup @signals37 = Firm.find(1) @@ -534,28 +531,6 @@ class HasManyAssociationsTest < Test::Unit::TestCase def test_adding_array_and_collection assert_nothing_raised { Firm.find_first.clients + Firm.find_all.last.clients } end - - def test_eager_association_loading_with_one_association - posts = Post.find(:all, :include => :comments) - assert_equal 2, posts.first.comments.size - assert_equal @greetings.body, posts.first.comments.first.body - - post = Post.find(:first, :include => :comments, :conditions => "posts.title = 'Welcome to the weblog'") - assert_equal 2, post.comments.size - assert_equal @greetings.body, post.comments.first.body - end - - def test_eager_association_loading_with_multiple_associations - posts = Post.find(:all, :include => [ :comments, :author ]) - assert_equal 2, posts.first.comments.size - assert_equal @greetings.body, posts.first.comments.first.body - end - - def xtest_eager_association_loading_with_belongs_to - comments = Comment.find(:all, :include => :post) - assert_equal @welcome.title, comments.first.post.title - assert_equal @thinking.title, comments.last.post.title - end end class BelongsToAssociationsTest < Test::Unit::TestCase diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb index 8d12190086..e159d38597 100644 --- a/activerecord/test/fixtures/author.rb +++ b/activerecord/test/fixtures/author.rb @@ -1,3 +1,3 @@ class Author < ActiveRecord::Base - belongs_to :post + has_many :posts end \ No newline at end of file diff --git a/activerecord/test/fixtures/authors.yml b/activerecord/test/fixtures/authors.yml index 718c317c37..f59b84fa45 100644 --- a/activerecord/test/fixtures/authors.yml +++ b/activerecord/test/fixtures/authors.yml @@ -1,9 +1,7 @@ david: id: 1 - post_id: 1 name: David mary: id: 2 - post_id: 2 name: Mary diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index a6f2efcbfe..db31835807 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -118,6 +118,7 @@ CREATE TABLE 'computers' ( CREATE TABLE 'posts' ( 'id' INTEGER NOT NULL PRIMARY KEY, + 'author_id' INTEGER NOT NULL, 'title' VARCHAR(255) NOT NULL, 'body' TEXT NOT NULL ); @@ -130,7 +131,6 @@ CREATE TABLE 'comments' ( CREATE TABLE 'authors' ( 'id' INTEGER NOT NULL PRIMARY KEY, - 'post_id' INTEGER NOT NULL, 'name' VARCHAR(255) NOT NULL ); diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb index 1fd78b8343..a09e192c04 100644 --- a/activerecord/test/fixtures/post.rb +++ b/activerecord/test/fixtures/post.rb @@ -1,4 +1,4 @@ class Post < ActiveRecord::Base - has_many :comments - has_one :author + belongs_to :author + has_many :comments end \ No newline at end of file diff --git a/activerecord/test/fixtures/posts.yml b/activerecord/test/fixtures/posts.yml index 21a110ef91..7c706b6818 100644 --- a/activerecord/test/fixtures/posts.yml +++ b/activerecord/test/fixtures/posts.yml @@ -1,9 +1,11 @@ welcome: id: 1 + author_id: 1 title: Welcome to the weblog body: Such a lovely day thinking: id: 2 + author_id: 1 title: So I was thinking body: Like I hopefully always am -- cgit v1.2.3