From 4f39382a2fad5e299fce73ae0ba60f28dfddb21a Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 6 Apr 2008 22:26:15 +0000 Subject: Ensure that respond_to? considers dynamic finder methods. Closes #11538. [floehopper] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9235 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/cases/finder_respond_to_test.rb | 76 +++++++++++++++++++++++ activerecord/test/cases/finder_test.rb | 22 +++---- 2 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 activerecord/test/cases/finder_respond_to_test.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/finder_respond_to_test.rb b/activerecord/test/cases/finder_respond_to_test.rb new file mode 100644 index 0000000000..4e6fecf11a --- /dev/null +++ b/activerecord/test/cases/finder_respond_to_test.rb @@ -0,0 +1,76 @@ +require "cases/helper" +require 'models/topic' + +class FinderRespondToTest < ActiveRecord::TestCase + + fixtures :topics + + def test_should_preserve_normal_respond_to_behaviour_and_respond_to_newly_added_method + class << Topic; self; end.send(:define_method, :method_added_for_finder_respond_to_test) { } + assert Topic.respond_to?(:method_added_for_finder_respond_to_test) + ensure + class << Topic; self; end.send(:remove_method, :method_added_for_finder_respond_to_test) + end + + def test_should_preserve_normal_respond_to_behaviour_and_respond_to_standard_object_method + assert Topic.respond_to?(:to_s) + end + + def test_should_respond_to_find_by_one_attribute_before_caching + ensure_topic_method_is_not_cached(:find_by_title) + assert Topic.respond_to?(:find_by_title) + end + + def test_should_respond_to_find_all_by_one_attribute + ensure_topic_method_is_not_cached(:find_all_by_title) + assert Topic.respond_to?(:find_all_by_title) + end + + def test_should_respond_to_find_all_by_two_attributes + ensure_topic_method_is_not_cached(:find_all_by_title_and_author_name) + assert Topic.respond_to?(:find_all_by_title_and_author_name) + end + + def test_should_respond_to_find_by_two_attributes + ensure_topic_method_is_not_cached(:find_by_title_and_author_name) + assert Topic.respond_to?(:find_by_title_and_author_name) + end + + def test_should_respond_to_find_or_initialize_from_one_attribute + ensure_topic_method_is_not_cached(:find_or_initialize_by_title) + assert Topic.respond_to?(:find_or_initialize_by_title) + end + + def test_should_respond_to_find_or_initialize_from_two_attributes + ensure_topic_method_is_not_cached(:find_or_initialize_by_title_and_author_name) + assert Topic.respond_to?(:find_or_initialize_by_title_and_author_name) + end + + def test_should_respond_to_find_or_create_from_one_attribute + ensure_topic_method_is_not_cached(:find_or_create_by_title) + assert Topic.respond_to?(:find_or_create_by_title) + end + + def test_should_respond_to_find_or_create_from_two_attributes + ensure_topic_method_is_not_cached(:find_or_create_by_title_and_author_name) + assert Topic.respond_to?(:find_or_create_by_title_and_author_name) + end + + def test_should_not_respond_to_find_by_one_missing_attribute + assert !Topic.respond_to?(:find_by_undertitle) + end + + def test_should_not_respond_to_find_by_invalid_method_syntax + assert !Topic.respond_to?(:fail_to_find_by_title) + assert !Topic.respond_to?(:find_by_title?) + assert !Topic.respond_to?(:fail_to_find_or_create_by_title) + assert !Topic.respond_to?(:find_or_create_by_title?) + end + + private + + def ensure_topic_method_is_not_cached(method_id) + class << Topic; self; end.send(:remove_method, method_id) if Topic.public_methods.any? { |m| m.to_s == method_id.to_s } + end + +end \ No newline at end of file diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 79907c9c64..b7f87fe6e8 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -416,15 +416,15 @@ class FinderTest < ActiveRecord::TestCase def test_find_by_one_attribute_caches_dynamic_finder # ensure this test can run independently of order - class << Topic; self; end.send(:remove_method, :find_by_title) if Topic.respond_to?(:find_by_title) - assert !Topic.respond_to?(:find_by_title) + class << Topic; self; end.send(:remove_method, :find_by_title) if Topic.public_methods.any? { |m| m.to_s == 'find_by_title' } + assert !Topic.public_methods.any? { |m| m.to_s == 'find_by_title' } t = Topic.find_by_title("The First Topic") - assert Topic.respond_to?(:find_by_title) + assert Topic.public_methods.any? { |m| m.to_s == 'find_by_title' } end def test_dynamic_finder_returns_same_results_after_caching # ensure this test can run independently of order - class << Topic; self; end.send(:remove_method, :find_by_title) if Topic.respond_to?(:find_by_title) + class << Topic; self; end.send(:remove_method, :find_by_title) if Topic.public_method_defined?(:find_by_title) t = Topic.find_by_title("The First Topic") assert_equal t, Topic.find_by_title("The First Topic") # find_by_title has been cached end @@ -474,15 +474,15 @@ class FinderTest < ActiveRecord::TestCase def test_dynamic_finder_on_one_attribute_with_conditions_caches_method # ensure this test can run independently of order - class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.respond_to?(:find_by_credit_limit) - assert !Account.respond_to?(:find_by_credit_limit) + class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' } + assert !Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' } a = Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6]) - assert Account.respond_to?(:find_by_credit_limit) + assert Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' } end def test_dynamic_finder_on_one_attribute_with_conditions_returns_same_results_after_caching # ensure this test can run independently of order - class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.respond_to?(:find_by_credit_limit) + class << Account; self; end.send(:remove_method, :find_by_credit_limit) if Account.public_methods.any? { |m| m.to_s == 'find_by_credit_limit' } a = Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6]) assert_equal a, Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6]) # find_by_credit_limit has been cached end @@ -702,10 +702,10 @@ class FinderTest < ActiveRecord::TestCase end def test_dynamic_find_or_initialize_from_one_attribute_caches_method - class << Company; self; end.send(:remove_method, :find_or_initialize_by_name) if Company.respond_to?(:find_or_initialize_by_name) - assert !Company.respond_to?(:find_or_initialize_by_name) + class << Company; self; end.send(:remove_method, :find_or_initialize_by_name) if Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' } + assert !Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' } sig38 = Company.find_or_initialize_by_name("38signals") - assert Company.respond_to?(:find_or_initialize_by_name) + assert Company.public_methods.any? { |m| m.to_s == 'find_or_initialize_by_name' } end def test_find_or_initialize_from_two_attributes -- cgit v1.2.3