aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/finder_test.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-12-07 21:14:20 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-12-07 21:14:20 +0000
commita775cb190312edba8ef3feb6345ac446d7e8f113 (patch)
tree31e730e99c36e0883f348a58df200a8ba8a4d424 /activerecord/test/finder_test.rb
parent7a29764657eb75c516478bc31e88233601aae20e (diff)
downloadrails-a775cb190312edba8ef3feb6345ac446d7e8f113.tar.gz
rails-a775cb190312edba8ef3feb6345ac446d7e8f113.tar.bz2
rails-a775cb190312edba8ef3feb6345ac446d7e8f113.zip
Added the option for sanitizing find_by_sql and the offset parts in regular finds [Sam Stephenson]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@75 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/finder_test.rb')
-rwxr-xr-xactiverecord/test/finder_test.rb37
1 files changed, 36 insertions, 1 deletions
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index cc240c8acc..721ad76d56 100755
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -1,11 +1,13 @@
require 'abstract_unit'
require 'fixtures/company'
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
def test_find
@@ -23,6 +25,20 @@ class FinderTest < Test::Unit::TestCase
}
end
+ def test_find_all_with_limit
+ entrants = Entrant.find_all nil, "id ASC", 2
+
+ assert_equal(2, entrants.size)
+ assert_equal(@entrant_fixtures["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)
+ end
+
def test_find_with_entire_select_statement
topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
@@ -30,6 +46,13 @@ class FinderTest < Test::Unit::TestCase
assert_equal(@topic_fixtures["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)
+ end
+
def test_find_first
first = Topic.find_first "title = 'The First Topic'"
assert_equal(@topic_fixtures["first"]["title"], first.title)
@@ -71,4 +94,16 @@ class FinderTest < Test::Unit::TestCase
assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
assert_equal "'something; select table'", ActiveRecord::Base.sanitize("something; select table")
end
-end \ No newline at end of file
+
+ def test_count
+ assert_equal(0, Entrant.count("id > 3"))
+ assert_equal(1, Entrant.count(["id > ?", 2]))
+ assert_equal(2, Entrant.count(["id > ?", 1]))
+ end
+
+ def test_count_by_sql
+ assert_equal(0, Entrant.count_by_sql("SELECT COUNT(*) FROM entrants WHERE id > 3"))
+ assert_equal(1, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 2]))
+ assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
+ end
+end