Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Apr 13, 2012
1 parent 012bab8 commit 1ad138c
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 81 deletions.
123 changes: 61 additions & 62 deletions activerecord/test/cases/associations/eager_test.rb
Expand Up @@ -61,8 +61,8 @@ def test_loading_with_one_association_with_non_preload
end end


def test_loading_conditions_with_or def test_loading_conditions_with_or
posts = authors(:david).posts.find( posts = authors(:david).posts.references(:comments).find(
:all, :include => :comments, :references => :comments, :all, :include => :comments,
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'" :conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE} = 'SpecialComment'"
) )
assert_nil posts.detect { |p| p.author_id != authors(:david).id }, assert_nil posts.detect { |p| p.author_id != authors(:david).id },
Expand Down Expand Up @@ -276,25 +276,25 @@ def test_nested_loading_through_has_one_association_with_order_on_nested_associa
end end


def test_nested_loading_through_has_one_association_with_conditions def test_nested_loading_through_has_one_association_with_conditions
aa = AuthorAddress.find( aa = AuthorAddress.references(:author_addresses).find(
author_addresses(:david_address).id, :include => {:author => :posts}, author_addresses(:david_address).id, :include => {:author => :posts},
:conditions => "author_addresses.id > 0", :references => :author_addresses :conditions => "author_addresses.id > 0"
) )
assert_equal aa.author.posts.count, aa.author.posts.length assert_equal aa.author.posts.count, aa.author.posts.length
end end


def test_nested_loading_through_has_one_association_with_conditions_on_association def test_nested_loading_through_has_one_association_with_conditions_on_association
aa = AuthorAddress.find( aa = AuthorAddress.references(:authors).find(
author_addresses(:david_address).id, :include => {:author => :posts}, author_addresses(:david_address).id, :include => {:author => :posts},
:conditions => "authors.id > 0", :references => :authors :conditions => "authors.id > 0"
) )
assert_equal aa.author.posts.count, aa.author.posts.length assert_equal aa.author.posts.count, aa.author.posts.length
end end


def test_nested_loading_through_has_one_association_with_conditions_on_nested_association def test_nested_loading_through_has_one_association_with_conditions_on_nested_association
aa = AuthorAddress.find( aa = AuthorAddress.references(:posts).find(
author_addresses(:david_address).id, :include => {:author => :posts}, author_addresses(:david_address).id, :include => {:author => :posts},
:conditions => "posts.id > 0", :references => :posts :conditions => "posts.id > 0"
) )
assert_equal aa.author.posts.count, aa.author.posts.length assert_equal aa.author.posts.count, aa.author.posts.length
end end
Expand Down Expand Up @@ -564,9 +564,9 @@ def test_eager_with_has_many_and_limit_and_high_offset


def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions
assert_queries(1) do assert_queries(1) do
posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, posts = Post.references(:authors, :comments).
:conditions => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ], find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10,
:references => [:authors, :comments]) :conditions => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ])
assert_equal 0, posts.size assert_equal 0, posts.size
end end
end end
Expand All @@ -592,7 +592,7 @@ def test_eager_with_has_many_and_limit_with_no_results
def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional def test_eager_count_performed_on_a_has_many_association_with_multi_table_conditional
author = authors(:david) author = authors(:david)
author_posts_without_comments = author.posts.select { |post| post.comments.blank? } author_posts_without_comments = author.posts.select { |post| post.comments.blank? }
assert_equal author_posts_without_comments.size, author.posts.count(:all, :include => :comments, :conditions => 'comments.id is null', :references => :comments) assert_equal author_posts_without_comments.size, author.posts.includes(:comments).where('comments.id is null').references(:comments).count
end end


def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional def test_eager_count_performed_on_a_has_many_through_association_with_multi_table_conditional
Expand Down Expand Up @@ -628,75 +628,75 @@ def test_has_and_belongs_to_many_should_not_instantiate_same_records_multiple_ti
end end


def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers def test_eager_with_has_many_and_limit_and_conditions_on_the_eagers
posts = authors(:david).posts.find(:all, posts =
:include => :comments, authors(:david).posts
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'", .includes(:comments)
:references => :comments, .where("comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'")
:limit => 2 .references(:comments)
) .limit(2)
.to_a
assert_equal 2, posts.size assert_equal 2, posts.size


count = Post.count( count =
:include => [ :comments, :author ], Post.includes(:comments, :author)
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')", .where("authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')")
:references => [:authors, :comments], .references(:authors, :comments)
:limit => 2 .limit(2)
) .count
assert_equal count, posts.size assert_equal count, posts.size
end end


def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers def test_eager_with_has_many_and_limit_and_scoped_conditions_on_the_eagers
posts = nil posts = nil
Post.send(:with_scope, :find => { Post.includes(:comments)
:include => :comments, .where("comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'")
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'", .references(:comments)
:references => :comments .scoping do
}) do
posts = authors(:david).posts.find(:all, :limit => 2) posts = authors(:david).posts.limit(2).to_a
assert_equal 2, posts.size assert_equal 2, posts.size
end end


Post.send(:with_scope, :find => { Post.includes(:comments, :author)
:include => [ :comments, :author ], .where("authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')")
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')", .references(:authors, :comments)
:references => [:authors, :comments] .scoping do
}) do
count = Post.count(:limit => 2) count = Post.limit(2).count
assert_equal count, posts.size assert_equal count, posts.size
end end
end end


def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers def test_eager_with_has_many_and_limit_and_scoped_and_explicit_conditions_on_the_eagers
Post.send(:with_scope, :find => { :conditions => "1=1" }) do Post.send(:with_scope, :find => { :conditions => "1=1" }) do
posts = authors(:david).posts.find(:all, posts =
:include => :comments, authors(:david).posts
:conditions => "comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'", .includes(:comments)
:references => :comments, .where("comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment'")
:limit => 2 .references(:comments)
) .limit(2)
.to_a
assert_equal 2, posts.size assert_equal 2, posts.size


count = Post.count( count = Post.includes(:comments, :author)
:include => [ :comments, :author ], .where("authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')")
:conditions => "authors.name = 'David' AND (comments.body like 'Normal%' OR comments.#{QUOTED_TYPE}= 'SpecialComment')", .references(:authors, :comments)
:references => [:authors, :comments], .limit(2)
:limit => 2 .count
)
assert_equal count, posts.size assert_equal count, posts.size
end end
end end


def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope def test_eager_with_scoped_order_using_association_limiting_without_explicit_scope
posts_with_explicit_order = Post.find( posts_with_explicit_order =
:all, :conditions => 'comments.id is not null', :references => :comments, Post.where('comments.id is not null').references(:comments)
:include => :comments, :order => 'posts.id DESC', :limit => 2 .includes(:comments).order('posts.id DESC').limit(2)
)
posts_with_scoped_order = Post.send(:with_scope, :find => {:order => 'posts.id DESC'}) do posts_with_scoped_order = Post.order('posts.id DESC').scoping do
Post.find( Post.where('comments.id is not null').references(:comments)
:all, :conditions => 'comments.id is not null', .includes(:comments).limit(2)
:references => :comments, :include => :comments, :limit => 2
)
end end

assert_equal posts_with_explicit_order, posts_with_scoped_order assert_equal posts_with_explicit_order, posts_with_scoped_order
end end


Expand Down Expand Up @@ -844,10 +844,9 @@ def test_limited_eager_with_multiple_order_columns
def test_limited_eager_with_numeric_in_association def test_limited_eager_with_numeric_in_association
assert_equal( assert_equal(
people(:david, :susan), people(:david, :susan),
Person.find( Person.references(:number1_fans_people).find(
:all, :include => [:readers, :primary_contact, :number1_fan], :all, :include => [:readers, :primary_contact, :number1_fan],
:conditions => "number1_fans_people.first_name like 'M%'", :conditions => "number1_fans_people.first_name like 'M%'",
:references => :number1_fans_people,
:order => 'people.id', :limit => 2, :offset => 0 :order => 'people.id', :limit => 2, :offset => 0
) )
) )
Expand Down Expand Up @@ -965,11 +964,11 @@ def test_preconfigured_includes_with_has_many_and_habtm


def test_count_with_include def test_count_with_include
if current_adapter?(:SybaseAdapter) if current_adapter?(:SybaseAdapter)
assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15", :references => :comments) assert_equal 3, authors(:david).posts_with_comments.where("len(comments.body) > 15").references(:comments).count
elsif current_adapter?(:OpenBaseAdapter) elsif current_adapter?(:OpenBaseAdapter)
assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15", :references => :comments) assert_equal 3, authors(:david).posts_with_comments.where("length(FETCHBLOB(comments.body)) > 15").references(:comments).count
else else
assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(comments.body) > 15", :references => :comments) assert_equal 3, authors(:david).posts_with_comments.where("length(comments.body) > 15").references(:comments).count
end end
end end


Expand Down
Expand Up @@ -681,8 +681,8 @@ def test_habtm_respects_select_query_method
def test_join_table_alias def test_join_table_alias
assert_equal( assert_equal(
3, 3,
Developer.find( Developer.references(:developers_projects_join).find(
:all, :include => {:projects => :developers}, :references => :developers_projects_join, :all, :include => {:projects => :developers},
:conditions => 'developers_projects_join.joined_on IS NOT NULL' :conditions => 'developers_projects_join.joined_on IS NOT NULL'
).size ).size
) )
Expand All @@ -697,9 +697,9 @@ def test_join_with_group


assert_equal( assert_equal(
3, 3,
Developer.find( Developer.references(:developers_projects_join).find(
:all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL', :all, :include => {:projects => :developers}, :conditions => 'developers_projects_join.joined_on IS NOT NULL',
:references => :developers_projects_join, :group => group.join(",") :group => group.join(",")
).size ).size
) )
end end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -362,7 +362,7 @@ def test_has_many_polymorphic
end end


assert_raise ActiveRecord::EagerLoadPolymorphicError do assert_raise ActiveRecord::EagerLoadPolymorphicError do
tags(:general).taggings.find(:all, :include => :taggable, :references => :bogus_table, :conditions => 'bogus_table.column = 1') tags(:general).taggings.includes(:taggable).where('bogus_table.column = 1').references(:bogus_table).to_a
end end
end end


Expand Down
10 changes: 5 additions & 5 deletions activerecord/test/cases/calculations_test.rb
Expand Up @@ -49,12 +49,12 @@ def test_should_get_maximum_of_field
end end


def test_should_get_maximum_of_field_with_include def test_should_get_maximum_of_field_with_include
assert_equal 55, Account.maximum(:credit_limit, :include => :firm, :references => :companies, :conditions => "companies.name != 'Summit'") assert_equal 55, Account.where("companies.name != 'Summit'").references(:companies).includes(:firm).maximum(:credit_limit)
end end


def test_should_get_maximum_of_field_with_scoped_include def test_should_get_maximum_of_field_with_scoped_include
Account.send :with_scope, :find => { :include => :firm, :references => :companies, :conditions => "companies.name != 'Summit'" } do Account.send :with_scope, :find => { :include => :firm } do
assert_equal 55, Account.maximum(:credit_limit) assert_equal 55, Account.where("companies.name != 'Summit'").references(:companies).maximum(:credit_limit)
end end
end end


Expand Down Expand Up @@ -270,10 +270,10 @@ def test_should_calculate_grouped_association_with_foreign_key_option
end end


def test_should_not_modify_options_when_using_includes def test_should_not_modify_options_when_using_includes
options = {:conditions => 'companies.id > 1', :include => :firm, :references => :companies} options = {:conditions => 'companies.id > 1', :include => :firm}
options_copy = options.dup options_copy = options.dup


Account.count(:all, options) Account.references(:companies).count(:all, options)
assert_equal options_copy, options assert_equal options_copy, options
end end


Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/finder_test.rb
Expand Up @@ -1182,9 +1182,9 @@ def test_find_with_nil_inside_set_passed_for_attribute
end end


def test_with_limiting_with_custom_select def test_with_limiting_with_custom_select
posts = Post.find( posts = Post.references(:authors).find(
:all, :include => :author, :select => ' posts.*, authors.id as "author_id"', :all, :include => :author, :select => ' posts.*, authors.id as "author_id"',
:references => :authors, :limit => 3, :order => 'posts.id' :limit => 3, :order => 'posts.id'
) )
assert_equal 3, posts.size assert_equal 3, posts.size
assert_equal [0, 1, 1], posts.map(&:author_id).sort assert_equal [0, 1, 1], posts.map(&:author_id).sort
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/modules_test.rb
Expand Up @@ -72,7 +72,7 @@ def test_eager_loading_in_modules
clients = [] clients = []


assert_nothing_raised NameError, "Should be able to resolve all class constants via reflection" do assert_nothing_raised NameError, "Should be able to resolve all class constants via reflection" do
clients << MyApplication::Business::Client.find(3, :include => {:firm => :account}, :conditions => 'accounts.id IS NOT NULL', :references => :accounts) clients << MyApplication::Business::Client.references(:accounts).find(3, :include => {:firm => :account}, :conditions => 'accounts.id IS NOT NULL')
clients << MyApplication::Business::Client.find(3, :include => {:firm => :account}) clients << MyApplication::Business::Client.find(3, :include => {:firm => :account})
end end


Expand Down
6 changes: 0 additions & 6 deletions activerecord/test/cases/relation_test.rb
Expand Up @@ -128,12 +128,6 @@ def test_references_values_dont_duplicate
assert_equal ['foo'], relation.references_values assert_equal ['foo'], relation.references_values
end end


def test_apply_finder_options_takes_references
relation = Relation.new :a, :b
relation = relation.apply_finder_options(:references => :foo)
assert_equal ['foo'], relation.references_values
end

test 'merging a hash into a relation' do test 'merging a hash into a relation' do
relation = Relation.new :a, :b relation = Relation.new :a, :b
relation = relation.merge where: ['lol'], readonly: true relation = relation.merge where: ['lol'], readonly: true
Expand Down

0 comments on commit 1ad138c

Please sign in to comment.