提交 949c8c0d 编写于 作者: J Jeremy Kemper

Don't publicize with_scope for tests since it may shadow public misuse

上级 1b91f534
......@@ -462,7 +462,7 @@ def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
posts = nil
Post.with_scope(:find => {
Post.send(:with_scope, :find => {
:include => :comments,
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'"
}) do
......@@ -470,7 +470,7 @@ def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
assert_equal 2, posts.size
end
Post.with_scope(:find => {
Post.send(:with_scope, :find => {
:include => [ :comments, :author ],
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')"
}) do
......@@ -480,7 +480,7 @@ def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
end
def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
Post.with_scope(:find => { :conditions => "1=1" }) do
Post.send(:with_scope, :find => { :conditions => "1=1" }) do
posts = authors(:david).posts.find(:all,
:include => :comments,
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'",
......@@ -499,7 +499,7 @@ def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the
def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
posts_with_explicit_order = Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :order => 'posts.id DESC', :limit => 2)
posts_with_scoped_order = Post.with_scope(:find => {:order => 'posts.id DESC'}) do
posts_with_scoped_order = Post.send(:with_scope, :find => {:order => 'posts.id DESC'}) do
Post.find(:all, :conditions => 'comments.id is not null', :include => :comments, :limit => 2)
end
assert_equal posts_with_explicit_order, posts_with_scoped_order
......
......@@ -1825,7 +1825,7 @@ def test_interpolate_sql
end
def test_scoped_find_conditions
scoped_developers = Developer.with_scope(:find => { :conditions => 'salary > 90000' }) do
scoped_developers = Developer.send(:with_scope, :find => { :conditions => 'salary > 90000' }) do
Developer.find(:all, :conditions => 'id < 5')
end
assert !scoped_developers.include?(developers(:david)) # David's salary is less than 90,000
......@@ -1833,7 +1833,7 @@ def test_scoped_find_conditions
end
def test_scoped_find_limit_offset
scoped_developers = Developer.with_scope(:find => { :limit => 3, :offset => 2 }) do
scoped_developers = Developer.send(:with_scope, :find => { :limit => 3, :offset => 2 }) do
Developer.find(:all, :order => 'id')
end
assert !scoped_developers.include?(developers(:david))
......@@ -1847,17 +1847,17 @@ def test_scoped_find_limit_offset
def test_scoped_find_order
# Test order in scope
scoped_developers = Developer.with_scope(:find => { :limit => 1, :order => 'salary DESC' }) do
scoped_developers = Developer.send(:with_scope, :find => { :limit => 1, :order => 'salary DESC' }) do
Developer.find(:all)
end
assert_equal 'Jamis', scoped_developers.first.name
assert scoped_developers.include?(developers(:jamis))
# Test scope without order and order in find
scoped_developers = Developer.with_scope(:find => { :limit => 1 }) do
scoped_developers = Developer.send(:with_scope, :find => { :limit => 1 }) do
Developer.find(:all, :order => 'salary DESC')
end
# Test scope order + find order, find has priority
scoped_developers = Developer.with_scope(:find => { :limit => 3, :order => 'id DESC' }) do
scoped_developers = Developer.send(:with_scope, :find => { :limit => 3, :order => 'id DESC' }) do
Developer.find(:all, :order => 'salary ASC')
end
assert scoped_developers.include?(developers(:poor_jamis))
......@@ -1869,7 +1869,7 @@ def test_scoped_find_order
end
def test_scoped_find_limit_offset_including_has_many_association
topics = Topic.with_scope(:find => {:limit => 1, :offset => 1, :include => :replies}) do
topics = Topic.send(:with_scope, :find => {:limit => 1, :offset => 1, :include => :replies}) do
Topic.find(:all, :order => "topics.id")
end
assert_equal 1, topics.size
......@@ -1877,7 +1877,7 @@ def test_scoped_find_limit_offset_including_has_many_association
end
def test_scoped_find_order_including_has_many_association
developers = Developer.with_scope(:find => { :order => 'developers.salary DESC', :include => :projects }) do
developers = Developer.send(:with_scope, :find => { :order => 'developers.salary DESC', :include => :projects }) do
Developer.find(:all)
end
assert developers.size >= 2
......@@ -1887,7 +1887,7 @@ def test_scoped_find_order_including_has_many_association
end
def test_scoped_find_with_group_and_having
developers = Developer.with_scope(:find => { :group => 'developers.salary', :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary" }) do
developers = Developer.send(:with_scope, :find => { :group => 'developers.salary', :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary" }) do
Developer.find(:all)
end
assert_equal 3, developers.size
......@@ -1933,7 +1933,7 @@ def test_find_symbol_ordered_last
end
def test_find_scoped_ordered_last
last_developer = Developer.with_scope(:find => { :order => 'developers.salary ASC' }) do
last_developer = Developer.send(:with_scope, :find => { :order => 'developers.salary ASC' }) do
Developer.find(:last)
end
assert_equal last_developer, Developer.find(:all, :order => 'developers.salary ASC').last
......
......@@ -42,7 +42,7 @@ def test_should_get_maximum_of_field_with_include
end
def test_should_get_maximum_of_field_with_scoped_include
Account.with_scope :find => { :include => :firm, :conditions => "companies.name != 'Summit'" } do
Account.send :with_scope, :find => { :include => :firm, :conditions => "companies.name != 'Summit'" } do
assert_equal 50, Account.maximum(:credit_limit)
end
end
......
......@@ -120,7 +120,7 @@ def test_exists_with_aggregate_having_three_mappings_with_one_difference
end
def test_exists_with_scoped_include
Developer.with_scope(:find => { :include => :projects, :order => "projects.name" }) do
Developer.send(:with_scope, :find => { :include => :projects, :order => "projects.name" }) do
assert Developer.exists?
end
end
......@@ -1022,7 +1022,7 @@ def test_with_limiting_with_custom_select
def test_finder_with_scoped_from
all_topics = Topic.find(:all)
Topic.with_scope(:find => { :from => 'fake_topics' }) do
Topic.send(:with_scope, :find => { :from => 'fake_topics' }) do
assert_equal all_topics, Topic.from('topics').to_a
end
end
......
......@@ -47,11 +47,6 @@ def execute_with_query_record(sql, name = nil, &block)
alias_method_chain :execute, :query_record
end
# Make with_scope public for tests
class << ActiveRecord::Base
public :with_scope, :with_exclusive_scope
end
unless ENV['FIXTURE_DEBUG']
module ActiveRecord::TestFixtures::ClassMethods
def try_to_load_dependency_with_silence(*args)
......
......@@ -225,7 +225,7 @@ def test_sane_find_with_lock
def test_sane_find_with_scoped_lock
assert_nothing_raised do
Person.transaction do
Person.with_scope(:find => { :lock => true }) do
Person.send(:with_scope, :find => { :lock => true }) do
Person.find 1
end
end
......
......@@ -72,13 +72,13 @@ def test_has_many_with_through_is_not_implicitly_marked_readonly
end
def test_readonly_scoping
Post.with_scope(:find => { :conditions => '1=1' }) do
Post.send(:with_scope, :find => { :conditions => '1=1' }) do
assert !Post.find(1).readonly?
assert Post.readonly(true).find(1).readonly?
assert !Post.readonly(false).find(1).readonly?
end
Post.with_scope(:find => { :joins => ' ' }) do
Post.send(:with_scope, :find => { :joins => ' ' }) do
assert !Post.find(1).readonly?
assert Post.readonly.find(1).readonly?
assert !Post.readonly(false).find(1).readonly?
......@@ -87,14 +87,14 @@ def test_readonly_scoping
# Oracle barfs on this because the join includes unqualified and
# conflicting column names
unless current_adapter?(:OracleAdapter)
Post.with_scope(:find => { :joins => ', developers' }) do
Post.send(:with_scope, :find => { :joins => ', developers' }) do
assert Post.find(1).readonly?
assert Post.readonly.find(1).readonly?
assert !Post.readonly(false).find(1).readonly?
end
end
Post.with_scope(:find => { :readonly => true }) do
Post.send(:with_scope, :find => { :readonly => true }) do
assert Post.find(1).readonly?
assert Post.readonly.find(1).readonly?
assert !Post.readonly(false).find(1).readonly?
......
......@@ -213,7 +213,7 @@ def test_validate_uniqueness_with_non_standard_table_names
def test_validates_uniqueness_inside_with_scope
Topic.validates_uniqueness_of(:title)
Topic.with_scope(:find => { :conditions => { :author_name => "David" } }) do
Topic.send(:with_scope, :find => { :conditions => { :author_name => "David" } }) do
t1 = Topic.new("title" => "I'm unique!", "author_name" => "Mary")
assert t1.save
t2 = Topic.new("title" => "I'm unique!", "author_name" => "David")
......
......@@ -98,14 +98,14 @@ def test_exception_on_create_bang_many_with_block
end
def test_scoped_create_without_attributes
Reply.with_scope(:create => {}) do
Reply.send(:with_scope, :create => {}) do
assert_raise(ActiveRecord::RecordInvalid) { Reply.create! }
end
end
def test_create_with_exceptions_using_scope_for_protected_attributes
assert_nothing_raised do
ProtectedPerson.with_scope( :create => { :first_name => "Mary" } ) do
ProtectedPerson.send(:with_scope, :create => { :first_name => "Mary" } ) do
person = ProtectedPerson.create! :addon => "Addon"
assert_equal person.first_name, "Mary", "scope should ignore attr_protected"
end
......@@ -114,7 +114,7 @@ def test_create_with_exceptions_using_scope_for_protected_attributes
def test_create_with_exceptions_using_scope_and_empty_attributes
assert_nothing_raised do
ProtectedPerson.with_scope( :create => { :first_name => "Mary" } ) do
ProtectedPerson.send(:with_scope, :create => { :first_name => "Mary" } ) do
person = ProtectedPerson.create!
assert_equal person.first_name, "Mary", "should be ok when no attributes are passed to create!"
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册