aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2005-11-06 10:18:51 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2005-11-06 10:18:51 +0000
commit390e6d246ceb76ead8dbcf7b87591f51fc316082 (patch)
treeb8d0e8315472916e90cd74fe26c8e180ba4841cc /activerecord/test
parent6c5572701aa74c90dc5ee3282f450c1a17207c36 (diff)
downloadrails-390e6d246ceb76ead8dbcf7b87591f51fc316082.tar.gz
rails-390e6d246ceb76ead8dbcf7b87591f51fc316082.tar.bz2
rails-390e6d246ceb76ead8dbcf7b87591f51fc316082.zip
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
Diffstat (limited to 'activerecord/test')
-rwxr-xr-xactiverecord/test/base_test.rb10
-rw-r--r--activerecord/test/conditions_scoping_test.rb51
-rwxr-xr-xactiverecord/test/readonly_test.rb14
3 files changed, 50 insertions, 25 deletions
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index 04307267b0..f64821361f 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -1030,8 +1030,8 @@ class BasicsTest < Test::Unit::TestCase
assert_nothing_raised { Category.new.send(:interpolate_sql, 'foo bar} baz') }
end
- def test_constrain_conditions
- developers = Developer.constrain(:conditions => 'salary > 90000') do
+ def test_scoped_find_conditions
+ developers = Developer.with_scope(:find => { :conditions => 'salary > 90000' }) do
Developer.find(:all, :conditions => 'id < 5')
end
david = Developer.find(1)
@@ -1039,8 +1039,8 @@ class BasicsTest < Test::Unit::TestCase
assert_equal 3, developers.size
end
- def test_constrain_limit_offset
- developers = Developer.constrain(:limit => 3, :offset => 2) do
+ def test_scoped_find_limit_offset
+ developers = Developer.with_scope(:find => { :limit => 3, :offset => 2 }) do
Developer.find(:all, :order => 'id')
end
david = Developer.find(1)
@@ -1049,7 +1049,7 @@ class BasicsTest < Test::Unit::TestCase
assert !developers.include?(jamis) # Jamis has id 2
assert_equal 3, developers.size
- # Now test without constraints to make sure we get the whole thing
+ # Test without scoped find conditions to ensure we get the whole thing
developers = Developer.find(:all, :order => 'id')
assert_equal 10, developers.size
end
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
diff --git a/activerecord/test/readonly_test.rb b/activerecord/test/readonly_test.rb
index f8acc16493..bd9b8ef1b7 100755
--- a/activerecord/test/readonly_test.rb
+++ b/activerecord/test/readonly_test.rb
@@ -4,7 +4,7 @@ require 'fixtures/comment'
require 'fixtures/developer'
require 'fixtures/project'
-# Dummy class methods to test implicit association constraints.
+# Dummy class methods to test implicit association scoping.
def Comment.foo() find :first end
def Project.foo() find :first end
@@ -64,14 +64,14 @@ class ReadOnlyTest < Test::Unit::TestCase
end
- def test_readonly_constraint
- Post.constrain(:conditions => '1=1') do
+ def test_readonly_scoping
+ Post.with_scope(:find => { :conditions => '1=1' }) do
assert !Post.find(1).readonly?
assert Post.find(1, :readonly => true).readonly?
assert !Post.find(1, :readonly => false).readonly?
end
- Post.constrain(:joins => ' ') do
+ Post.with_scope(:find => { :joins => ' ' }) do
assert !Post.find(1).readonly?
assert Post.find(1, :readonly => true).readonly?
assert !Post.find(1, :readonly => false).readonly?
@@ -80,21 +80,21 @@ class ReadOnlyTest < Test::Unit::TestCase
# Oracle barfs on this because the join includes unqualified and
# conflicting column names
unless current_adapter?(:OCIAdapter)
- Post.constrain(:joins => ', developers') do
+ Post.with_scope(:find => { :joins => ', developers' }) do
assert Post.find(1).readonly?
assert Post.find(1, :readonly => true).readonly?
assert !Post.find(1, :readonly => false).readonly?
end
end
- Post.constrain(:readonly => true) do
+ Post.with_scope(:find => { :readonly => true }) do
assert Post.find(1).readonly?
assert Post.find(1, :readonly => true).readonly?
assert !Post.find(1, :readonly => false).readonly?
end
end
- def test_association_collection_method_missing_constraint_not_readonly
+ def test_association_collection_method_missing_scoping_not_readonly
assert !Developer.find(1).projects.foo.readonly?
assert !Post.find(1).comments.foo.readonly?
end