(t *testing.T)
| 5408 | } |
| 5409 | |
| 5410 | func TestGetUserStatusCounts(t *testing.T) { |
| 5411 | t.Parallel() |
| 5412 | |
| 5413 | type testCase struct { |
| 5414 | timezone string |
| 5415 | location *time.Location |
| 5416 | reportFrom time.Time |
| 5417 | reportUntil time.Time |
| 5418 | } |
| 5419 | testCases := []testCase{} |
| 5420 | |
| 5421 | // GetUserStatusCounts is sensitive to DST transitions, because it generates timestamps exactly |
| 5422 | // one day apart from one another, and specific days can have varying lengths depending on the timezone. |
| 5423 | // Therefore, we test with a variety of timezones. |
| 5424 | timezones := []string{ |
| 5425 | "America/St_Johns", |
| 5426 | "Africa/Johannesburg", |
| 5427 | "America/New_York", |
| 5428 | "Europe/London", |
| 5429 | "Asia/Tokyo", |
| 5430 | "Australia/Sydney", |
| 5431 | } |
| 5432 | |
| 5433 | // assemble test cases |
| 5434 | for _, tz := range timezones { |
| 5435 | location, err := time.LoadLocation(tz) |
| 5436 | if err != nil { |
| 5437 | t.Fatalf("failed to load location: %v", err) |
| 5438 | } |
| 5439 | |
| 5440 | // Testing based on the current system date will flake due to DST transitions. |
| 5441 | // Instead, we test with a fixed range of dates that is large enough to span multiple DST transitions. |
| 5442 | startOfTestDateRange := time.Date(2025, 1, 1, 0, 0, 0, 0, location) |
| 5443 | endOfTestDateRange := time.Date(2026, 1, 1, 0, 0, 0, 0, location) |
| 5444 | // To keep the number of test cases manageable given the large date range, |
| 5445 | // we test with a suitable large interval. This interval is also the length of each report. |
| 5446 | // this ensures we have full coverage of the date range. |
| 5447 | testDateRangeInterval := 60 |
| 5448 | |
| 5449 | for reportFrom := startOfTestDateRange; !reportFrom.After(endOfTestDateRange); reportFrom = reportFrom.AddDate(0, 0, testDateRangeInterval) { |
| 5450 | testCases = append(testCases, testCase{ |
| 5451 | timezone: tz, |
| 5452 | location: location, |
| 5453 | reportFrom: dbtime.Time(reportFrom), |
| 5454 | reportUntil: dbtime.Time(reportFrom.AddDate(0, 0, testDateRangeInterval)), |
| 5455 | }) |
| 5456 | } |
| 5457 | } |
| 5458 | |
| 5459 | for _, tc := range testCases { |
| 5460 | t.Run(fmt.Sprintf("%s/%s", tc.timezone, tc.reportUntil.Format("2006-01-02T15:04:05Z")), func(t *testing.T) { |
| 5461 | t.Parallel() |
| 5462 | |
| 5463 | userCreatedAt := tc.reportUntil.AddDate(0, 0, -60) |
| 5464 | firstStatusChange := userCreatedAt.AddDate(0, 0, 29) |
| 5465 | secondStatusChange := firstStatusChange.AddDate(0, 0, 29) |
| 5466 | |
| 5467 | t.Run("No Users", func(t *testing.T) { |
nothing calls this directly
no test coverage detected