aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/finder_test.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-02 13:31:00 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-02 13:31:00 +0000
commitac8fd7dfb91eb0e554537e671eaf1615a1d19757 (patch)
treee4b74bb3d3d7deced9dc04e172786f650581d8c6 /activerecord/test/finder_test.rb
parent93221685f131ea689d458bcd78f638fb3d6dfc90 (diff)
downloadrails-ac8fd7dfb91eb0e554537e671eaf1615a1d19757.tar.gz
rails-ac8fd7dfb91eb0e554537e671eaf1615a1d19757.tar.bz2
rails-ac8fd7dfb91eb0e554537e671eaf1615a1d19757.zip
Added dynamic attribute-based finders as a cleaner way of getting objects by simple queries without turning to SQL.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@307 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/finder_test.rb')
-rwxr-xr-xactiverecord/test/finder_test.rb34
1 files changed, 22 insertions, 12 deletions
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index 74ea11c944..fdbbc68e98 100755
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -4,14 +4,10 @@ require 'fixtures/topic'
require 'fixtures/entrant'
class FinderTest < Test::Unit::TestCase
- def setup
- @company_fixtures = create_fixtures("companies")
- @topic_fixtures = create_fixtures("topics")
- @entrant_fixtures = create_fixtures("entrants")
- end
+ fixtures :companies, :topics, :entrants
def test_find
- assert_equal(@topic_fixtures["first"]["title"], Topic.find(1).title)
+ assert_equal(@topics["first"]["title"], Topic.find(1).title)
end
def test_find_by_array_of_one_id
@@ -21,7 +17,7 @@ class FinderTest < Test::Unit::TestCase
def test_find_by_ids
assert_equal(2, Topic.find(1, 2).length)
- assert_equal(@topic_fixtures["second"]["title"], Topic.find([ 2 ]).first.title)
+ assert_equal(@topics["second"]["title"], Topic.find([ 2 ]).first.title)
end
def test_find_by_ids_missing_one
@@ -34,33 +30,33 @@ class FinderTest < Test::Unit::TestCase
entrants = Entrant.find_all nil, "id ASC", 2
assert_equal(2, entrants.size)
- assert_equal(@entrant_fixtures["first"]["name"], entrants.first.name)
+ assert_equal(@entrants["first"]["name"], entrants.first.name)
end
def test_find_all_with_prepared_limit_and_offset
entrants = Entrant.find_all nil, "id ASC", ["? OFFSET ?", 2, 1]
assert_equal(2, entrants.size)
- assert_equal(@entrant_fixtures["second"]["name"], entrants.first.name)
+ assert_equal(@entrants["second"]["name"], entrants.first.name)
end
def test_find_with_entire_select_statement
topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
assert_equal(1, topics.size)
- assert_equal(@topic_fixtures["second"]["title"], topics.first.title)
+ assert_equal(@topics["second"]["title"], topics.first.title)
end
def test_find_with_prepared_select_statement
topics = Topic.find_by_sql ["SELECT * FROM topics WHERE author_name = ?", "Mary"]
assert_equal(1, topics.size)
- assert_equal(@topic_fixtures["second"]["title"], topics.first.title)
+ assert_equal(@topics["second"]["title"], topics.first.title)
end
def test_find_first
first = Topic.find_first "title = 'The First Topic'"
- assert_equal(@topic_fixtures["first"]["title"], first.title)
+ assert_equal(@topics["first"]["title"], first.title)
end
def test_find_first_failing
@@ -168,6 +164,20 @@ class FinderTest < Test::Unit::TestCase
assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
end
+ def test_find_by_one_attribute
+ assert_equal @topics["first"].find, Topic.find_by_title("The First Topic")
+ assert_nil Topic.find_by_title("The First Topic!")
+ end
+
+ def test_find_by_one_missing_attribute
+ assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
+ end
+
+ def test_find_by_two_attributes
+ assert_equal @topics["first"].find, Topic.find_by_title_and_author_name("The First Topic", "David")
+ assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary")
+ end
+
protected
def bind(statement, *vars)
if vars.first.is_a?(Hash)