aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorPratik Naik <pratiknaik@gmail.com>2008-04-06 22:26:15 +0000
committerPratik Naik <pratiknaik@gmail.com>2008-04-06 22:26:15 +0000
commit4f39382a2fad5e299fce73ae0ba60f28dfddb21a (patch)
tree6b035a7f46206219bb0221917647f6182fe930b3 /activerecord/test
parent917423d664038d6791738a73ad1446437dbb71df (diff)
downloadrails-4f39382a2fad5e299fce73ae0ba60f28dfddb21a.tar.gz
rails-4f39382a2fad5e299fce73ae0ba60f28dfddb21a.tar.bz2
rails-4f39382a2fad5e299fce73ae0ba60f28dfddb21a.zip
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
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/cases/finder_respond_to_test.rb76
-rw-r--r--activerecord/test/cases/finder_test.rb22
2 files changed, 87 insertions, 11 deletions
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