From 6bd672eb0d50fcf3437d7fa244245397747bf7a7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 1 Jan 2005 19:50:23 +0000 Subject: Added that Base#find takes an optional options hash, including :conditions. Base#find_on_conditions deprecated in favor of #find with :conditions #407 [bitsweat] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@305 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/associations_test.rb | 37 +++++++++++++++++++++-- activerecord/test/deprecated_associations_test.rb | 26 ++++++++++------ activerecord/test/finder_test.rb | 34 ++++++++++++++++++++- 3 files changed, 83 insertions(+), 14 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index feccab09ec..f94f04e279 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -183,9 +183,31 @@ class HasManyAssociationsTest < Test::Unit::TestCase assert_equal 0, Firm.find_first.clients_using_zero_counter_sql.size end + def test_find_ids + firm = Firm.find_first + + assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find } + + client = firm.clients.find(2) + assert_kind_of Client, client + + client_ary = firm.clients.find([2]) + assert_kind_of Array, client_ary + assert_equal client, client_ary.first + + client_ary = firm.clients.find(2, 3) + assert_kind_of Array, client_ary + assert_equal 2, client_ary.size + assert_equal client, client_ary.first + + assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) } + end + def test_find_all - assert_equal 2, Firm.find_first.clients.find_all("type = 'Client'").length - assert_equal 1, Firm.find_first.clients.find_all("name = 'Summit'").length + firm = Firm.find_first + assert_equal firm.clients, firm.clients.find_all + assert_equal 2, firm.clients.find_all("type = 'Client'").length + assert_equal 1, firm.clients.find_all("name = 'Summit'").length end def test_find_all_sanitized @@ -193,9 +215,18 @@ class HasManyAssociationsTest < Test::Unit::TestCase assert_equal firm.clients.find_all("name = 'Summit'"), firm.clients.find_all(["name = '%s'", "Summit"]) end + def test_find_first + firm = Firm.find_first + assert_equal firm.clients.first, firm.clients.find_first + assert_equal Client.find(2), firm.clients.find_first("type = 'Client'") + end + + def test_find_first_sanitized + assert_equal Client.find(2), Firm.find_first.clients.find_first(["type = ?", "Client"]) + end + def test_find_in_collection assert_equal Client.find(2).name, @signals37.clients.find(2).name - assert_equal Client.find(2).name, @signals37.clients.find {|c| c.name == @signals37.clients.find(2).name }.name assert_raises(ActiveRecord::RecordNotFound) { @signals37.clients.find(6) } end diff --git a/activerecord/test/deprecated_associations_test.rb b/activerecord/test/deprecated_associations_test.rb index 419e1d6e2f..ed1d3c4055 100755 --- a/activerecord/test/deprecated_associations_test.rb +++ b/activerecord/test/deprecated_associations_test.rb @@ -286,9 +286,11 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase natural = Client.create("name" => "Natural Company") apple.clients << natural assert_equal apple.id, natural.firm_id - assert_equal Client.find(natural.id), Firm.find(apple.id).clients.find { |c| c.id == natural.id } + assert_equal Client.find(natural.id), Firm.find(apple.id).clients.find(natural.id) apple.clients.delete natural - assert_nil Firm.find(apple.id).clients.find { |c| c.id == natural.id } + assert_raises(ActiveRecord::RecordNotFound) { + Firm.find(apple.id).clients.find(natural.id) + } end def test_natural_adding_of_has_and_belongs_to_many @@ -299,17 +301,21 @@ class DeprecatedAssociationsTest < Test::Unit::TestCase rails.developers << john rails.developers << mike - assert_equal Developer.find(john.id), Project.find(rails.id).developers.find { |d| d.id == john.id } - assert_equal Developer.find(mike.id), Project.find(rails.id).developers.find { |d| d.id == mike.id } - assert_equal Project.find(rails.id), Developer.find(mike.id).projects.find { |p| p.id == rails.id } - assert_equal Project.find(rails.id), Developer.find(john.id).projects.find { |p| p.id == rails.id } + assert_equal Developer.find(john.id), Project.find(rails.id).developers.find(john.id) + assert_equal Developer.find(mike.id), Project.find(rails.id).developers.find(mike.id) + assert_equal Project.find(rails.id), Developer.find(mike.id).projects.find(rails.id) + assert_equal Project.find(rails.id), Developer.find(john.id).projects.find(rails.id) ap.developers << john - assert_equal Developer.find(john.id), Project.find(ap.id).developers.find { |d| d.id == john.id } - assert_equal Project.find(ap.id), Developer.find(john.id).projects.find { |p| p.id == ap.id } + assert_equal Developer.find(john.id), Project.find(ap.id).developers.find(john.id) + assert_equal Project.find(ap.id), Developer.find(john.id).projects.find(ap.id) ap.developers.delete john - assert_nil Project.find(ap.id).developers.find { |d| d.id == john.id } - assert_nil Developer.find(john.id).projects.find { |p| p.id == ap.id } + assert_raises(ActiveRecord::RecordNotFound) { + Project.find(ap.id).developers.find(john.id) + } + assert_raises(ActiveRecord::RecordNotFound) { + Developer.find(john.id).projects.find(ap.id) + } end def test_storing_in_pstore diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb index fbe4ef56f3..93b0d06f4e 100755 --- a/activerecord/test/finder_test.rb +++ b/activerecord/test/finder_test.rb @@ -77,6 +77,11 @@ class FinderTest < Test::Unit::TestCase end def test_find_on_conditions + assert Topic.find(1, :conditions => "approved = 0") + assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => "approved = 1") } + end + + def test_deprecated_find_on_conditions assert Topic.find_on_conditions(1, "approved = 0") assert_raises(ActiveRecord::RecordNotFound) { Topic.find_on_conditions(1, "approved = 1") } end @@ -111,7 +116,19 @@ class FinderTest < Test::Unit::TestCase assert Company.find_first(["name = :name", {:name => "37signals' go'es agains"}]) end + def test_bind_arity + assert_nothing_raised { bind '' } + assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '', 1 } + + assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?' } + assert_nothing_raised { bind '?', 1 } + assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 } + end + def test_named_bind_variables + assert_equal '1', bind(':a', :a => 1) # ' ruby-mode + assert_equal '1 1', bind(':a :a', :a => 1) # ' ruby-mode + assert_kind_of Firm, Company.find_first(["name = :name", { :name => "37signals" }]) assert_nil Company.find_first(["name = :name", { :name => "37signals!" }]) assert_nil Company.find_first(["name = :name", { :name => "37signals!' OR 1=1" }]) @@ -124,7 +141,13 @@ class FinderTest < Test::Unit::TestCase } end - + def test_named_bind_arity + assert_nothing_raised { bind '', {} } + assert_nothing_raised { bind '', :a => 1 } + assert_raises(ActiveRecord::PreparedStatementInvalid) { bind ':a', {} } # ' ruby-mode + assert_nothing_raised { bind ':a', :a => 1 } # ' ruby-mode + assert_nothing_raised { bind ':a', :a => 1, :b => 2 } # ' ruby-mode + end def test_string_sanitation assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1") @@ -142,4 +165,13 @@ class FinderTest < Test::Unit::TestCase 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 + + protected + def bind(statement, *vars) + if vars.first.is_a?(Hash) + ActiveRecord::Base.send(:replace_named_bind_variables, statement, vars.first) + else + ActiveRecord::Base.send(:replace_bind_variables, statement, vars) + end + end end -- cgit v1.2.3