2005-07-22 16:05:42 -04:00
|
|
|
require 'abstract_unit'
|
|
|
|
require 'fixtures/developer'
|
|
|
|
require 'fixtures/comment'
|
|
|
|
require 'fixtures/post'
|
|
|
|
require 'fixtures/category'
|
|
|
|
|
2005-11-06 05:19:28 -05:00
|
|
|
class MethodScopingTest < Test::Unit::TestCase
|
2005-11-04 14:39:50 -05:00
|
|
|
fixtures :developers, :comments, :posts
|
2005-07-22 16:05:42 -04:00
|
|
|
|
|
|
|
def test_set_conditions
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.with_scope(:find => { :conditions => 'just a test...' }) do
|
|
|
|
assert_equal 'just a test...', Thread.current[:scoped_methods][Developer][:find][:conditions]
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_scoped_find
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
|
2005-07-22 16:05:42 -04:00
|
|
|
assert_nothing_raised { Developer.find(1) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_scoped_find_first
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.with_scope(:find => { :conditions => "salary = 100000" }) do
|
2005-07-22 16:05:42 -04:00
|
|
|
assert_equal Developer.find(10), Developer.find(:first, :order => 'name')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_scoped_find_all
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
|
2005-07-22 16:05:42 -04:00
|
|
|
assert_equal [Developer.find(1)], Developer.find(:all)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_scoped_count
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
|
2005-07-22 16:05:42 -04:00
|
|
|
assert_equal 1, Developer.count
|
|
|
|
end
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.with_scope(:find => { :conditions => 'salary = 100000' }) do
|
2005-07-22 16:05:42 -04:00
|
|
|
assert_equal 8, Developer.count
|
|
|
|
assert_equal 1, Developer.count("name LIKE 'fixture_1%'")
|
|
|
|
end
|
|
|
|
end
|
2005-10-28 00:53:22 -04:00
|
|
|
|
2005-11-04 14:39:50 -05:00
|
|
|
def test_scoped_create
|
|
|
|
new_comment = nil
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
VerySpecialComment.with_scope(:create => { :post_id => 1 }) do
|
|
|
|
assert_equal({ :post_id => 1 }, Thread.current[:scoped_methods][VerySpecialComment][:create])
|
2005-11-04 14:39:50 -05:00
|
|
|
new_comment = VerySpecialComment.create :body => "Wonderful world"
|
|
|
|
end
|
2005-11-06 05:18:51 -05:00
|
|
|
|
2005-11-04 14:39:50 -05:00
|
|
|
assert Post.find(1).comments.include?(new_comment)
|
|
|
|
end
|
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_immutable_scope
|
2005-10-28 00:53:22 -04:00
|
|
|
options = { :conditions => "name = 'David'" }
|
2005-11-06 05:18:51 -05:00
|
|
|
Developer.with_scope(:find => options) do
|
2005-10-28 00:53:22 -04:00
|
|
|
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
|
2005-11-06 05:18:51 -05:00
|
|
|
|
|
|
|
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
|
2005-10-28 00:53:22 -04:00
|
|
|
end
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class HasManyScopingTest< Test::Unit::TestCase
|
|
|
|
fixtures :comments, :posts
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@welcome = Post.find(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_of_static_methods
|
|
|
|
assert_equal 'a comment...', Comment.what_are_you
|
|
|
|
assert_equal 'a comment...', @welcome.comments.what_are_you
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_to_scoped
|
|
|
|
assert_equal 4, Comment.search_by_type('Comment').size
|
|
|
|
assert_equal 2, @welcome.comments.search_by_type('Comment').size
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_to_dynamic_finders
|
|
|
|
assert_equal 4, Comment.find_all_by_type('Comment').size
|
|
|
|
assert_equal 2, @welcome.comments.find_all_by_type('Comment').size
|
|
|
|
end
|
2005-11-06 05:18:51 -05:00
|
|
|
|
|
|
|
def test_raise_on_nested_scope
|
|
|
|
Comment.with_scope(:find => { :conditions => '1=1' }) do
|
|
|
|
assert_raise(ArgumentError) { @welcome.comments.what_are_you }
|
|
|
|
end
|
|
|
|
end
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class HasAndBelongsToManyScopingTest< Test::Unit::TestCase
|
2005-10-23 15:02:38 -04:00
|
|
|
fixtures :posts, :categories, :categories_posts
|
2005-07-22 16:05:42 -04:00
|
|
|
|
|
|
|
def setup
|
|
|
|
@welcome = Post.find(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_of_static_methods
|
|
|
|
assert_equal 'a category...', Category.what_are_you
|
|
|
|
assert_equal 'a category...', @welcome.categories.what_are_you
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_to_dynamic_finders
|
|
|
|
assert_equal 1, Category.find_all_by_type('SpecialCategory').size
|
|
|
|
assert_equal 0, @welcome.categories.find_all_by_type('SpecialCategory').size
|
|
|
|
assert_equal 2, @welcome.categories.find_all_by_type('Category').size
|
|
|
|
end
|
2005-10-25 14:14:09 -04:00
|
|
|
|
2005-11-06 05:18:51 -05:00
|
|
|
def test_raise_on_nested_scope
|
|
|
|
Category.with_scope(:find => { :conditions => '1=1' }) do
|
|
|
|
assert_raise(ArgumentError) { @welcome.categories.what_are_you }
|
|
|
|
end
|
|
|
|
end
|
2005-07-22 16:05:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
=begin
|
|
|
|
# We disabled the scoping for has_one and belongs_to as we can't think of a proper use case
|
|
|
|
|
|
|
|
|
|
|
|
class BelongsToScopingTest< Test::Unit::TestCase
|
|
|
|
fixtures :comments, :posts
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@greetings = Comment.find(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_of_static_method
|
|
|
|
assert_equal 'a post...', Post.what_are_you
|
|
|
|
assert_equal 'a post...', @greetings.post.what_are_you
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_to_dynamic_finders
|
|
|
|
assert_equal 4, Post.find_all_by_type('Post').size
|
|
|
|
assert_equal 1, @greetings.post.find_all_by_type('Post').size
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class HasOneScopingTest< Test::Unit::TestCase
|
|
|
|
fixtures :comments, :posts
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@sti_comments = Post.find(4)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_of_static_methods
|
|
|
|
assert_equal 'a comment...', Comment.what_are_you
|
|
|
|
assert_equal 'a very special comment...', @sti_comments.very_special_comment.what_are_you
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_forwarding_to_dynamic_finders
|
|
|
|
assert_equal 1, Comment.find_all_by_type('VerySpecialComment').size
|
|
|
|
assert_equal 1, @sti_comments.very_special_comment.find_all_by_type('VerySpecialComment').size
|
|
|
|
assert_equal 0, @sti_comments.very_special_comment.find_all_by_type('Comment').size
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2005-10-06 18:21:10 -04:00
|
|
|
=end
|