aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorTobias Lütke <tobias.luetke@gmail.com>2007-09-18 10:04:11 +0000
committerTobias Lütke <tobias.luetke@gmail.com>2007-09-18 10:04:11 +0000
commit4a1388adeab6aaf0ef28f975e480e22061bb1aad (patch)
treef6ddb310d859796947b9e963ab8902155abf2ccb /activerecord/test
parent7495b5ca753cf7448761fe2c393bdf441a7d5928 (diff)
downloadrails-4a1388adeab6aaf0ef28f975e480e22061bb1aad.tar.gz
rails-4a1388adeab6aaf0ef28f975e480e22061bb1aad.tar.bz2
rails-4a1388adeab6aaf0ef28f975e480e22061bb1aad.zip
Define dynamic finders as real methods after first usage. Close #9317
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7510 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/finder_test.rb41
1 files changed, 41 insertions, 0 deletions
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index b5bc5ae7f3..53fa24194e 100644
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -324,6 +324,21 @@ class FinderTest < Test::Unit::TestCase
assert_equal topics(:first), Topic.find_by_title("The First Topic")
assert_nil Topic.find_by_title("The First Topic!")
end
+
+ 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)
+ t = Topic.find_by_title("The First Topic")
+ assert Topic.respond_to?(: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)
+ 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
def test_find_by_one_attribute_with_order_option
assert_equal accounts(:signals37), Account.find_by_credit_limit(50, :order => 'id')
@@ -334,6 +349,21 @@ class FinderTest < Test::Unit::TestCase
assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
end
+ 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)
+ a = Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
+ assert Account.respond_to?(: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)
+ 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
+
def test_find_by_one_attribute_with_several_options
assert_equal accounts(:unknown), Account.find_by_credit_limit(50, :order => 'id DESC', :conditions => ['id != ?', 3])
end
@@ -440,6 +470,13 @@ class FinderTest < Test::Unit::TestCase
assert sig38.new_record?
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)
+ sig38 = Company.find_or_initialize_by_name("38signals")
+ assert Company.respond_to?(:find_or_initialize_by_name)
+ end
+
def test_find_or_initialize_from_two_attributes
another = Topic.find_or_initialize_by_title_and_author_name("Another topic","John")
assert_equal "Another topic", another.title
@@ -464,6 +501,10 @@ class FinderTest < Test::Unit::TestCase
assert_raises(ArgumentError) { Topic.find :first, :conditions => '1 = 1', :join => "It should be `joins'" }
end
+ def test_dynamic_finder_with_invalid_params
+ assert_raises(ArgumentError) { Topic.find_by_title 'No Title', :join => "It should be `joins'" }
+ end
+
def test_find_all_with_limit
first_five_developers = Developer.find :all, :order => 'id ASC', :limit => 5
assert_equal 5, first_five_developers.length