From 20a9b1977934af2e8a730ea8fc36314a4471db30 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 31 May 2024 11:45:45 -0400 Subject: [PATCH] test(billing): Calculate time deltas from a fixed time Some time calculations are impacted when using the current day vs. the start of the month. --- internal/billing/repository_test.go | 20 +++++++++++++------ .../handlers/billing/billing_service_test.go | 12 +++++++++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/internal/billing/repository_test.go b/internal/billing/repository_test.go index 3a8b00976e..bf761ed932 100644 --- a/internal/billing/repository_test.go +++ b/internal/billing/repository_test.go @@ -74,8 +74,16 @@ func TestRepository_MonthlyActiveUsers(t *testing.T) { TestGenerateActiveUsers(t, conn) today := time.Now().UTC() - threeMonthsAgo := time.Date(today.AddDate(0, -3, 0).Year(), today.AddDate(0, -3, 0).Month(), 1, 0, 0, 0, 0, time.UTC) - oneMonthAgo := time.Date(today.AddDate(0, -1, 0).Year(), today.AddDate(0, -1, 0).Month(), 1, 0, 0, 0, 0, time.UTC) + // Some time calculations are impacted when using the current day vs. the + // start of the month. For example, if... + // today -> May 30th + // today.AddDate(0, -3, 0).Month() -> March + // February was expcted here, but we get March. This seems to be a + // rounding thing since February 30th is not a valid date. Instead, the + // start of the month is used to ensure the correct months are calculated. + monthStart := time.Date(today.Year(), today.Month(), 1, 0, 0, 0, 0, time.UTC) + threeMonthsAgo := time.Date(monthStart.AddDate(0, -3, 0).Year(), monthStart.AddDate(0, -3, 0).Month(), 1, 0, 0, 0, 0, time.UTC) + oneMonthAgo := time.Date(monthStart.AddDate(0, -1, 0).Year(), monthStart.AddDate(0, -1, 0).Month(), 1, 0, 0, 0, 0, time.UTC) midMonth := time.Date(today.Year(), today.Month(), 15, 0, 0, 0, 0, time.UTC) t.Run("valid-no-options", func(t *testing.T) { @@ -125,14 +133,14 @@ func TestRepository_MonthlyActiveUsers(t *testing.T) { activeUsers, err := repo.MonthlyActiveUsers(ctx, WithStartTime(&threeMonthsAgo), WithEndTime(&oneMonthAgo)) assert.NoError(t, err) require.Len(t, activeUsers, 2) - expectedStartTime := time.Date(today.AddDate(0, -2, 0).Year(), today.AddDate(0, -2, 0).Month(), 1, 0, 0, 0, 0, time.UTC) - expectedEndTime := time.Date(today.AddDate(0, -1, 0).Year(), today.AddDate(0, -1, 0).Month(), 1, 0, 0, 0, 0, time.UTC) + expectedStartTime := time.Date(monthStart.AddDate(0, -2, 0).Year(), monthStart.AddDate(0, -2, 0).Month(), 1, 0, 0, 0, 0, time.UTC) + expectedEndTime := time.Date(monthStart.AddDate(0, -1, 0).Year(), monthStart.AddDate(0, -1, 0).Month(), 1, 0, 0, 0, 0, time.UTC) require.Equal(t, uint32(6), activeUsers[0].ActiveUsersCount) assert.Equal(t, expectedStartTime, activeUsers[0].StartTime) assert.Equal(t, expectedEndTime, activeUsers[0].EndTime) - expectedStartTime = time.Date(today.AddDate(0, -3, 0).Year(), today.AddDate(0, -3, 0).Month(), 1, 0, 0, 0, 0, time.UTC) - expectedEndTime = time.Date(today.AddDate(0, -2, 0).Year(), today.AddDate(0, -2, 0).Month(), 1, 0, 0, 0, 0, time.UTC) + expectedStartTime = time.Date(monthStart.AddDate(0, -3, 0).Year(), monthStart.AddDate(0, -3, 0).Month(), 1, 0, 0, 0, 0, time.UTC) + expectedEndTime = time.Date(monthStart.AddDate(0, -2, 0).Year(), monthStart.AddDate(0, -2, 0).Month(), 1, 0, 0, 0, 0, time.UTC) require.Equal(t, uint32(6), activeUsers[1].ActiveUsersCount) assert.Equal(t, expectedStartTime, activeUsers[1].StartTime) assert.Equal(t, expectedEndTime, activeUsers[1].EndTime) diff --git a/internal/daemon/controller/handlers/billing/billing_service_test.go b/internal/daemon/controller/handlers/billing/billing_service_test.go index 07ec26b743..79aba44fae 100644 --- a/internal/daemon/controller/handlers/billing/billing_service_test.go +++ b/internal/daemon/controller/handlers/billing/billing_service_test.go @@ -41,8 +41,16 @@ func Test_MonthlyActiveUsers(t *testing.T) { } today := time.Now().UTC() - threeMonthsAgo := time.Date(today.AddDate(0, -3, 0).Year(), today.AddDate(0, -3, 0).Month(), 1, 0, 0, 0, 0, time.UTC).Format("2006-01") - oneMonthAgo := time.Date(today.AddDate(0, -1, 0).Year(), today.AddDate(0, -1, 0).Month(), 1, 0, 0, 0, 0, time.UTC).Format("2006-01") + // Some time calculations are impacted when using the current day vs. the + // start of the month. For example, if... + // today -> May 30th + // today.AddDate(0, -3, 0).Month() -> March + // February was expcted here, but we get March. This seems to be a + // rounding thing since February 30th is not a valid date. Instead, the + // start of the month is used to ensure the correct months are calculated. + monthStart := time.Date(today.Year(), today.Month(), 1, 0, 0, 0, 0, time.UTC) + threeMonthsAgo := time.Date(monthStart.AddDate(0, -3, 0).Year(), monthStart.AddDate(0, -3, 0).Month(), 1, 0, 0, 0, 0, time.UTC).Format("2006-01") + oneMonthAgo := time.Date(monthStart.AddDate(0, -1, 0).Year(), monthStart.AddDate(0, -1, 0).Month(), 1, 0, 0, 0, 0, time.UTC).Format("2006-01") badFormat := time.Date(today.Year(), today.Month(), 15, 0, 0, 0, 0, time.UTC).String() cases := []struct {