diff options
author | Rick Olson <technoweenie@gmail.com> | 2008-03-24 02:50:02 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2008-03-24 02:50:02 +0000 |
commit | 081ddb6f24d70794ee9e071d3ed5302f52b26c4d (patch) | |
tree | a5c8e4a4f02cd38cfb182bdaf0e8491c7a0586d4 /activerecord/test | |
parent | 5bf4cbbdb74dfaaac258dc93e54dcae8295211e9 (diff) | |
download | rails-081ddb6f24d70794ee9e071d3ed5302f52b26c4d.tar.gz rails-081ddb6f24d70794ee9e071d3ed5302f52b26c4d.tar.bz2 rails-081ddb6f24d70794ee9e071d3ed5302f52b26c4d.zip |
Merge the has_finder gem, renamed as 'named_scope'. Closes #11404 [nkallen]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9084 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/cascaded_eager_loading_test.rb | 9 | ||||
-rwxr-xr-x | activerecord/test/cases/base_test.rb | 40 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 8 | ||||
-rwxr-xr-x | activerecord/test/cases/fixtures_test.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/cases/lifecycle_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 116 | ||||
-rwxr-xr-x | activerecord/test/cases/validations_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/fixtures/topics.yml | 24 | ||||
-rw-r--r-- | activerecord/test/models/author.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/comment.rb | 2 | ||||
-rw-r--r-- | activerecord/test/models/post.rb | 2 | ||||
-rwxr-xr-x | activerecord/test/models/topic.rb | 28 |
12 files changed, 208 insertions, 33 deletions
diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb index 6349a3a8b2..3631be76a0 100644 --- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb +++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb @@ -61,16 +61,17 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase def test_eager_association_loading_with_has_many_sti topics = Topic.find(:all, :include => :replies, :order => 'topics.id') - assert_equal topics(:first, :second), topics + first, second, = topics(:first).replies.size, topics(:second).replies.size assert_no_queries do - assert_equal 1, topics[0].replies.size - assert_equal 0, topics[1].replies.size + assert_equal first, topics[0].replies.size + assert_equal second, 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 replies.include?(topics(:second)) + assert !replies.include?(topics(:first)) assert_equal topics(:first), assert_no_queries { replies.first.topic } end diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 91cfdee0cf..d8b107177f 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -477,7 +477,7 @@ class BasicsTest < ActiveRecord::TestCase def test_load topics = Topic.find(:all, :order => 'id') - assert_equal(2, topics.size) + assert_equal(4, topics.size) assert_equal(topics(:first).title, topics.first.title) end @@ -549,10 +549,11 @@ class BasicsTest < ActiveRecord::TestCase end def test_destroy_all - assert_equal 2, Topic.count - - Topic.destroy_all "author_name = 'Mary'" - assert_equal 1, Topic.count + original_count = Topic.count + topics_by_mary = Topic.count(:conditions => mary = "author_name = 'Mary'") + + Topic.destroy_all mary + assert_equal original_count - topics_by_mary, Topic.count end def test_destroy_many @@ -562,8 +563,9 @@ class BasicsTest < ActiveRecord::TestCase end def test_delete_many - Topic.delete([1, 2]) - assert_equal 0, Topic.count + original_count = Topic.count + Topic.delete(deleting = [1, 2]) + assert_equal original_count - deleting.size, Topic.count end def test_boolean_attributes @@ -588,21 +590,21 @@ class BasicsTest < ActiveRecord::TestCase end def test_update_all - assert_equal 2, Topic.update_all("content = 'bulk updated!'") + assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'") assert_equal "bulk updated!", Topic.find(1).content assert_equal "bulk updated!", Topic.find(2).content - assert_equal 2, Topic.update_all(['content = ?', 'bulk updated again!']) + assert_equal Topic.count, Topic.update_all(['content = ?', 'bulk updated again!']) assert_equal "bulk updated again!", Topic.find(1).content assert_equal "bulk updated again!", Topic.find(2).content - assert_equal 2, Topic.update_all(['content = ?', nil]) + assert_equal Topic.count, Topic.update_all(['content = ?', nil]) assert_nil Topic.find(1).content end def test_update_all_with_hash assert_not_nil Topic.find(1).last_read - assert_equal 2, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil) + assert_equal Topic.count, Topic.update_all(:content => 'bulk updated with hash!', :last_read => nil) assert_equal "bulk updated with hash!", Topic.find(1).content assert_equal "bulk updated with hash!", Topic.find(2).content assert_nil Topic.find(1).last_read @@ -637,7 +639,9 @@ class BasicsTest < ActiveRecord::TestCase end def test_delete_all - assert_equal 2, Topic.delete_all + assert Topic.count > 0 + + assert_equal Topic.count, Topic.delete_all end def test_update_by_condition @@ -804,17 +808,17 @@ class BasicsTest < ActiveRecord::TestCase def test_update_attributes! reply = Reply.find(2) - assert_equal "The Second Topic's of the day", reply.title + assert_equal "The Second Topic of the day", reply.title assert_equal "Have a nice day", reply.content - reply.update_attributes!("title" => "The Second Topic's of the day updated", "content" => "Have a nice evening") + reply.update_attributes!("title" => "The Second Topic of the day updated", "content" => "Have a nice evening") reply.reload - assert_equal "The Second Topic's of the day updated", reply.title + assert_equal "The Second Topic of the day updated", reply.title assert_equal "Have a nice evening", reply.content - reply.update_attributes!(:title => "The Second Topic's of the day", :content => "Have a nice day") + reply.update_attributes!(:title => "The Second Topic of the day", :content => "Have a nice day") reply.reload - assert_equal "The Second Topic's of the day", reply.title + assert_equal "The Second Topic of the day", reply.title assert_equal "Have a nice day", reply.content assert_raise(ActiveRecord::RecordInvalid) { reply.update_attributes!(:title => nil, :content => "Have a nice evening") } @@ -1770,7 +1774,7 @@ class BasicsTest < ActiveRecord::TestCase xml = topics(:first).to_xml(:indent => 0, :skip_instruct => true, :include => :replies, :except => :replies_count) assert_equal "<topic>", xml.first(7) assert xml.include?(%(<replies type="array"><reply>)) - assert xml.include?(%(<title>The Second Topic's of the day</title>)) + assert xml.include?(%(<title>The Second Topic of the day</title>)) end def test_array_to_xml_including_has_many_association diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 49015133da..5da8603070 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -542,7 +542,7 @@ class FinderTest < ActiveRecord::TestCase end def test_find_all_by_array_attribute - assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic's of the day"]).size + assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic of the day"]).size end def test_find_all_by_boolean_attribute @@ -551,7 +551,7 @@ class FinderTest < ActiveRecord::TestCase assert topics.include?(topics(:first)) topics = Topic.find_all_by_approved(true) - assert_equal 1, topics.size + assert_equal 3, topics.size assert topics.include?(topics(:second)) end @@ -563,8 +563,8 @@ class FinderTest < ActiveRecord::TestCase def test_find_all_by_nil_attribute topics = Topic.find_all_by_last_read nil - assert_equal 1, topics.size - assert_nil topics[0].last_read + assert_equal 3, topics.size + assert topics.collect(&:last_read).all?(&:nil?) end def test_find_by_nil_and_not_nil_attributes diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index dce04e63be..e30035e79e 100755 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -123,7 +123,7 @@ class FixturesTest < ActiveRecord::TestCase end def test_complete_instantiation - assert_equal 2, @topics.size + assert_equal 4, @topics.size assert_equal "The First Topic", @first.title end diff --git a/activerecord/test/cases/lifecycle_test.rb b/activerecord/test/cases/lifecycle_test.rb index 0df7f4690d..ddcacacc3a 100755 --- a/activerecord/test/cases/lifecycle_test.rb +++ b/activerecord/test/cases/lifecycle_test.rb @@ -68,9 +68,9 @@ class LifecycleTest < ActiveRecord::TestCase fixtures :topics, :developers def test_before_destroy - assert_equal 2, Topic.count - Topic.find(1).destroy - assert_equal 0, Topic.count + original_count = Topic.count + (topic_to_be_destroyed = Topic.find(1)).destroy + assert_equal original_count - (1 + topic_to_be_destroyed.replies.size), Topic.count end def test_after_save diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb new file mode 100644 index 0000000000..3d2cf8ba93 --- /dev/null +++ b/activerecord/test/cases/named_scope_test.rb @@ -0,0 +1,116 @@ +require "cases/helper" +require 'models/post' +require 'models/topic' +require 'models/comment' +require 'models/reply' +require 'models/author' + +class NamedScopeTest < ActiveRecord::TestCase + fixtures :posts, :authors, :topics + + def test_implements_enumerable + assert !Topic.find(:all).empty? + + assert_equal Topic.find(:all), Topic.all + assert_equal Topic.find(:all), Topic.all.collect + assert_equal Topic.find(:first), Topic.all.first + assert_equal Topic.find(:all), Topic.all.each { |i| i } + end + + def test_found_items_are_cached + Topic.columns + all_posts = Topic.all + + assert_queries(1) do + all_posts.collect + all_posts.collect + end + end + + def test_reload_expires_cache_of_found_items + all_posts = Topic.all + all_posts.inspect + + new_post = Topic.create! + assert !all_posts.include?(new_post) + assert all_posts.reload.include?(new_post) + end + + def test_delegates_finds_and_calculations_to_the_base_class + assert !Topic.find(:all).empty? + + assert_equal Topic.find(:all), Topic.all.find(:all) + assert_equal Topic.find(:first), Topic.all.find(:first) + assert_equal Topic.count, Topic.all.count + assert_equal Topic.average(:replies_count), Topic.all.average(:replies_count) + end + + def test_subclasses_inherit_scopes + assert Topic.scopes.include?(:all) + + assert Reply.scopes.include?(:all) + assert_equal Reply.find(:all), Reply.all + end + + def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specified + assert !Topic.find(:all, :conditions => {:approved => true}).empty? + + assert_equal Topic.find(:all, :conditions => {:approved => true}), Topic.approved + assert_equal Topic.count(:conditions => {:approved => true}), Topic.approved.count + end + + def test_scopes_are_composable + assert_equal (approved = Topic.find(:all, :conditions => {:approved => true})), Topic.approved + assert_equal (replied = Topic.find(:all, :conditions => 'replies_count > 0')), Topic.replied + assert !(approved == replied) + assert !(approved & replied).empty? + + assert_equal approved & replied, Topic.approved.replied + end + + def test_procedural_scopes + topics_written_before_the_third = Topic.find(:all, :conditions => ['written_on < ?', topics(:third).written_on]) + topics_written_before_the_second = Topic.find(:all, :conditions => ['written_on < ?', topics(:second).written_on]) + assert_not_equal topics_written_before_the_second, topics_written_before_the_third + + assert_equal topics_written_before_the_third, Topic.written_before(topics(:third).written_on) + assert_equal topics_written_before_the_second, Topic.written_before(topics(:second).written_on) + end + + def test_extensions + assert_equal 1, Topic.anonymous_extension.one + assert_equal 2, Topic.named_extension.two + end + + def test_multiple_extensions + assert_equal 2, Topic.multiple_extensions.extension_two + assert_equal 1, Topic.multiple_extensions.extension_one + end + + def test_has_many_associations_have_access_to_named_scopes + assert_not_equal Post.containing_the_letter_a, authors(:david).posts + assert !Post.containing_the_letter_a.empty? + + assert_equal authors(:david).posts & Post.containing_the_letter_a, authors(:david).posts.containing_the_letter_a + end + + def test_has_many_through_associations_have_access_to_named_scopes + assert_not_equal Comment.containing_the_letter_e, authors(:david).posts + assert !Comment.containing_the_letter_e.empty? + + assert_equal authors(:david).comments & Comment.containing_the_letter_e, authors(:david).comments.containing_the_letter_e + end + + def test_active_records_have_scope_named__all__ + assert !Topic.find(:all).empty? + + assert_equal Topic.find(:all), Topic.all + end + + def test_active_records_have_scope_named__scoped__ + assert !Topic.find(:all, scope = {:conditions => "content LIKE '%Have%'"}).empty? + + assert_equal Topic.find(:all, scope), Topic.scoped(scope) + end + +end diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index f9a1b4620f..34d55dbcf6 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -428,7 +428,7 @@ class ValidationsTest < ActiveRecord::TestCase assert_nil t2.errors.on(:title) assert t2.errors.on(:parent_id) - t2.parent_id = 3 + t2.parent_id = 4 assert t2.save, "Should now save t2 as unique" t2.parent_id = nil diff --git a/activerecord/test/fixtures/topics.yml b/activerecord/test/fixtures/topics.yml index e61d17c00c..1769152445 100644 --- a/activerecord/test/fixtures/topics.yml +++ b/activerecord/test/fixtures/topics.yml @@ -12,11 +12,31 @@ first: second: id: 2 - title: The Second Topic's of the day + title: The Second Topic of the day author_name: Mary - written_on: 2003-07-15t15:28:00.0099+01:00 + written_on: 2004-07-15t15:28:00.0099+01:00 content: Have a nice day approved: true replies_count: 0 parent_id: 1 type: Reply + +third: + id: 3 + title: The Third Topic of the day + author_name: Nick + written_on: 2005-07-15t15:28:00.0099+01:00 + content: I'm a troll + approved: true + replies_count: 1 + +fourth: + id: 4 + title: The Fourth Topic of the day + author_name: Carl + written_on: 2006-07-15t15:28:00.0099+01:00 + content: Why not? + approved: true + type: Reply + parent_id: 3 + diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 523d794fb3..79a121b4a4 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -3,6 +3,7 @@ class Author < ActiveRecord::Base has_many :posts_with_comments, :include => :comments, :class_name => "Post" has_many :posts_with_categories, :include => :categories, :class_name => "Post" has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post" + has_many :posts_containing_the_letter_a, :class_name => "Post" has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension def testing_proxy_owner proxy_owner @@ -15,6 +16,7 @@ class Author < ActiveRecord::Base end end has_many :comments, :through => :posts + has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC' has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1 has_many :funky_comments, :through => :posts, :source => :comments diff --git a/activerecord/test/models/comment.rb b/activerecord/test/models/comment.rb index 73ebdadf73..f7f07c103f 100644 --- a/activerecord/test/models/comment.rb +++ b/activerecord/test/models/comment.rb @@ -1,4 +1,6 @@ class Comment < ActiveRecord::Base + named_scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'" + belongs_to :post, :counter_cache => true def self.what_are_you diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 7f9d53b34b..fcfe260d4a 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -1,4 +1,6 @@ class Post < ActiveRecord::Base + named_scope :containing_the_letter_a, :conditions => "body LIKE '%a%'" + belongs_to :author do def greeting "hello" diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 17e91e4b13..0ef7be97d3 100755 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -1,4 +1,32 @@ class Topic < ActiveRecord::Base + named_scope :written_before, lambda { |time| + { :conditions => ['written_on < ?', time] } + } + named_scope :approved, :conditions => {:approved => true} + named_scope :replied, :conditions => ['replies_count > 0'] + named_scope :anonymous_extension do + def one + 1 + end + end + module NamedExtension + def two + 2 + end + end + module MultipleExtensionOne + def extension_one + 1 + end + end + module MultipleExtensionTwo + def extension_two + 2 + end + end + named_scope :named_extension, :extend => NamedExtension + named_scope :multiple_extensions, :extend => [MultipleExtensionTwo, MultipleExtensionOne] + has_many :replies, :dependent => :destroy, :foreign_key => "parent_id" serialize :content |