From 390e6d246ceb76ead8dbcf7b87591f51fc316082 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 6 Nov 2005 10:18:51 +0000 Subject: r2915@asus: jeremy | 2005-11-06 05:02:53 -0800 Rename Base.constrain to Base.with_scope so it doesn't conflict with existing concept of database constraints. Make scoping more robust: uniform method => parameters, validated method names and supported finder parameters, raise exception on nested scopes. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2888 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/conditions_scoping_test.rb | 51 +++++++++++++++++++++------- 1 file changed, 38 insertions(+), 13 deletions(-) (limited to 'activerecord/test/conditions_scoping_test.rb') diff --git a/activerecord/test/conditions_scoping_test.rb b/activerecord/test/conditions_scoping_test.rb index cf8b670c26..92dfc1c1e6 100644 --- a/activerecord/test/conditions_scoping_test.rb +++ b/activerecord/test/conditions_scoping_test.rb @@ -8,35 +8,35 @@ class ConditionsScopingTest < Test::Unit::TestCase fixtures :developers, :comments, :posts def test_set_conditions - Developer.constrain(:conditions => 'just a test...') do - assert_equal 'just a test...', Thread.current[:constraints][Developer][:conditions] + Developer.with_scope(:find => { :conditions => 'just a test...' }) do + assert_equal 'just a test...', Thread.current[:scoped_methods][Developer][:find][:conditions] end end def test_scoped_find - Developer.constrain(:conditions => "name = 'David'") do + Developer.with_scope(:find => { :conditions => "name = 'David'" }) do assert_nothing_raised { Developer.find(1) } end end def test_scoped_find_first - Developer.constrain(:conditions => "salary = 100000") do + Developer.with_scope(:find => { :conditions => "salary = 100000" }) do assert_equal Developer.find(10), Developer.find(:first, :order => 'name') end end def test_scoped_find_all - Developer.constrain(:conditions => "name = 'David'") do + Developer.with_scope(:find => { :conditions => "name = 'David'" }) do assert_equal [Developer.find(1)], Developer.find(:all) end end def test_scoped_count - Developer.constrain(:conditions => "name = 'David'") do + Developer.with_scope(:find => { :conditions => "name = 'David'" }) do assert_equal 1, Developer.count end - Developer.constrain(:conditions => 'salary = 100000') do + Developer.with_scope(:find => { :conditions => 'salary = 100000' }) do assert_equal 8, Developer.count assert_equal 1, Developer.count("name LIKE 'fixture_1%'") end @@ -45,21 +45,36 @@ class ConditionsScopingTest < Test::Unit::TestCase def test_scoped_create new_comment = nil - VerySpecialComment.constrain(:creation => { :post_id => 1 }) do - assert_equal({ :post_id => 1 }, Thread.current[:constraints][VerySpecialComment][:creation]) + VerySpecialComment.with_scope(:create => { :post_id => 1 }) do + assert_equal({ :post_id => 1 }, Thread.current[:scoped_methods][VerySpecialComment][:create]) new_comment = VerySpecialComment.create :body => "Wonderful world" end - + assert Post.find(1).comments.include?(new_comment) end - def test_immutable_constraint + def test_immutable_scope options = { :conditions => "name = 'David'" } - Developer.constrain(options) do + Developer.with_scope(:find => options) do assert_equal %w(David), Developer.find(:all).map { |d| d.name } options[:conditions] = "name != 'David'" assert_equal %w(David), Developer.find(:all).map { |d| d.name } end + + scope = { :find => { :conditions => "name = 'David'" }} + Developer.with_scope(scope) do + assert_equal %w(David), Developer.find(:all).map { |d| d.name } + scope[:find][:conditions] = "name != 'David'" + assert_equal %w(David), Developer.find(:all).map { |d| d.name } + end + end + + def test_raise_on_nested_scope + Developer.with_scope(:find => { :conditions => '1=1' }) do + assert_raise(ArgumentError) do + Developer.with_scope(:find => { :conditions => '2=2' }) { } + end + end end end @@ -84,7 +99,12 @@ class HasManyScopingTest< Test::Unit::TestCase assert_equal 4, Comment.find_all_by_type('Comment').size assert_equal 2, @welcome.comments.find_all_by_type('Comment').size end - + + def test_raise_on_nested_scope + Comment.with_scope(:find => { :conditions => '1=1' }) do + assert_raise(ArgumentError) { @welcome.comments.what_are_you } + end + end end @@ -106,6 +126,11 @@ class HasAndBelongsToManyScopingTest< Test::Unit::TestCase assert_equal 2, @welcome.categories.find_all_by_type('Category').size end + def test_raise_on_nested_scope + Category.with_scope(:find => { :conditions => '1=1' }) do + assert_raise(ArgumentError) { @welcome.categories.what_are_you } + end + end end -- cgit v1.2.3