class InnerJoinAssociationTest

Public Instance Methods

test_calculate_honors_implicit_inner_joins() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 85
def test_calculate_honors_implicit_inner_joins
  real_count = Author.all.to_a.sum { |a| a.posts.count }
  assert_equal real_count, Author.joins(:posts).calculate(:count, "authors.id"), "plain inner join count should match the number of referenced posts records"
end
test_calculate_honors_implicit_inner_joins_and_distinct_and_conditions() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 90
def test_calculate_honors_implicit_inner_joins_and_distinct_and_conditions
  real_count = Author.all.to_a.select { |a| a.posts.any? { |p| p.title.start_with?("Welcome") } }.length
  authors_with_welcoming_post_titles = Author.all.merge!(joins: :posts, where: "posts.title like 'Welcome%'").distinct.calculate(:count, "authors.id")
  assert_equal real_count, authors_with_welcoming_post_titles, "inner join and conditions should have only returned authors posting titles starting with 'Welcome'"
end
test_construct_finder_sql_applies_aliases_tables_on_association_conditions() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 18
def test_construct_finder_sql_applies_aliases_tables_on_association_conditions
  result = Author.joins(:thinking_posts, :welcome_posts).to_a
  assert_equal authors(:david), result.first
end
test_construct_finder_sql_does_not_table_name_collide_on_duplicate_associations() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 23
def test_construct_finder_sql_does_not_table_name_collide_on_duplicate_associations
  assert_nothing_raised do
    sql = Person.joins(agents: { agents: :agents }).joins(agents: { agents: { primary_contact: :agents } }).to_sql
    assert_match(/agents_people_4/i, sql)
  end
end
test_construct_finder_sql_ignores_empty_joins_array() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 35
def test_construct_finder_sql_ignores_empty_joins_array
  sql = Author.joins([]).to_sql
  assert_no_match(/JOIN/i, sql)
end
test_construct_finder_sql_ignores_empty_joins_hash() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 30
def test_construct_finder_sql_ignores_empty_joins_hash
  sql = Author.joins({}).to_sql
  assert_no_match(/JOIN/i, sql)
end
test_count_honors_implicit_inner_joins() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 80
def test_count_honors_implicit_inner_joins
  real_count = Author.all.to_a.sum { |a| a.posts.count }
  assert_equal real_count, Author.joins(:posts).count, "plain inner join count should match the number of referenced posts records"
end
test_find_with_conditions_on_reflection() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 105
def test_find_with_conditions_on_reflection
  assert !posts(:welcome).comments.empty?
  assert Post.joins(:nonexistent_comments).where(id: posts(:welcome).id).empty? # [sic!]
end
test_find_with_conditions_on_through_reflection() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 110
def test_find_with_conditions_on_through_reflection
  assert !posts(:welcome).tags.empty?
  assert Post.joins(:misc_tags).where(id: posts(:welcome).id).empty?
end
test_find_with_implicit_inner_joins_does_not_set_associations() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 74
def test_find_with_implicit_inner_joins_does_not_set_associations
  authors = Author.joins(:posts).select("authors.*").to_a
  assert !authors.empty?, "expected authors to be non-empty"
  assert authors.all? { |a| !a.instance_variable_defined?(:@posts) }, "expected no authors to have the @posts association loaded"
end
test_find_with_implicit_inner_joins_honors_readonly_false() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 68
def test_find_with_implicit_inner_joins_honors_readonly_false
  authors = Author.joins(:posts).readonly(false).to_a
  assert !authors.empty?, "expected authors to be non-empty"
  assert authors.all? { |a| !a.readonly? }, "expected no authors to be readonly"
end
test_find_with_implicit_inner_joins_honors_readonly_with_select() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 62
def test_find_with_implicit_inner_joins_honors_readonly_with_select
  authors = Author.joins(:posts).select("authors.*").to_a
  assert !authors.empty?, "expected authors to be non-empty"
  assert authors.all? { |a| !a.readonly? }, "expected no authors to be readonly"
end
test_find_with_implicit_inner_joins_without_select_does_not_imply_readonly() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 56
def test_find_with_implicit_inner_joins_without_select_does_not_imply_readonly
  authors = Author.joins(:posts)
  assert_not authors.empty?, "expected authors to be non-empty"
  assert authors.none?(&:readonly?), "expected no authors to be readonly"
end
test_find_with_sti_join() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 96
def test_find_with_sti_join
  scope = Post.joins(:special_comments).where(id: posts(:sti_comments).id)

  # The join should match SpecialComment and its subclasses only
  assert scope.where("comments.type" => "Comment").empty?
  assert !scope.where("comments.type" => "SpecialComment").empty?
  assert !scope.where("comments.type" => "SubSpecialComment").empty?
end
test_join_association_conditions_support_string_and_arel_expressions() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 46
def test_join_association_conditions_support_string_and_arel_expressions
  assert_equal 0, Author.joins(:welcome_posts_with_one_comment).count
  assert_equal 1, Author.joins(:welcome_posts_with_comments).count
end
test_join_conditions_added_to_join_clause() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 40
def test_join_conditions_added_to_join_clause
  sql = Author.joins(:essays).to_sql
  assert_match(/writer_type.*?=.*?Author/i, sql)
  assert_no_match(/WHERE/i, sql)
end
test_join_conditions_allow_nil_associations() click to toggle source
# File activerecord/test/cases/associations/inner_join_association_test.rb, line 51
def test_join_conditions_allow_nil_associations
  authors = Author.includes(:essays).where(essays: { id: nil })
  assert_equal 2, authors.count
end