From 5455c23b89a8d7e15ae770d48b615858417aa1cd Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sun, 16 Jan 2022 01:44:52 -0500 Subject: [PATCH] Added more associations tests --- test/associations_test.rb | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/test/associations_test.rb b/test/associations_test.rb index ee5f887ef..97cae2f40 100644 --- a/test/associations_test.rb +++ b/test/associations_test.rb @@ -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