Skip to content

Commit

Permalink
Added more associations tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jan 16, 2022
1 parent 6cb9b1a commit 5455c23
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions test/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,37 @@ def test_period
assert_equal expected, user.posts.group_by_period(:day, :created_at).count
end

def test_includes
user = create_user("2014-03-01")
user.posts.create!(created_at: "2014-04-01 00:00:00 UTC")
expected = {
Date.parse("2014-03-01") => 1
}
assert_equal expected, User.includes(:posts).group_by_month(:created_at).count("posts.id")
end

# https://github.com/ankane/groupdate/issues/222#issuecomment-914343044
def test_left_outer_joins
def test_includes_where
date = 11.months.ago.in_time_zone(utc).to_date
create_user(date.to_s)
result = User.left_outer_joins(:posts).where(posts: {id: nil}).group_by_month(:created_at, last: 12, format: "%B").count
result = User.includes(:posts).where(posts: {id: nil}).group_by_month(:created_at, last: 12, format: "%B").count
assert_equal 1, result[date.strftime("%B")]
end

def test_joins
user = create_user("2014-03-01")
user.posts.create!(created_at: "2014-04-01 00:00:00 UTC")
expected = {
Date.parse("2014-03-01") => 1
}
assert_equal expected, User.joins(:posts).group_by_month(:created_at).count("posts.id")
end

# https://github.com/ankane/groupdate/issues/222#issuecomment-914343044
def test_includes
def test_left_outer_joins
date = 11.months.ago.in_time_zone(utc).to_date
create_user(date.to_s)
result = User.includes(:posts).where(posts: {id: nil}).group_by_month(:created_at, last: 12, format: "%B").count
result = User.left_outer_joins(:posts).where(posts: {id: nil}).group_by_month(:created_at, last: 12, format: "%B").count
assert_equal 1, result[date.strftime("%B")]
end
end

0 comments on commit 5455c23

Please sign in to comment.