From eb37ed187ee6f2bc5596bc4f073de836f1a6ccdf Mon Sep 17 00:00:00 2001 From: Bernard Siebens Date: Sun, 23 Aug 2026 18:07:47 +0200 Subject: [PATCH] Fix flaky this-week/next-week grouping tests around the evening boundary Both fixtures anchored their "this week" event at a hardcoded 18:00 on the last day of the current week -- fine any other day, but once the suite runs past 18:00 on that day itself, the anchor is already in the past and the view's own start__gte=now filter correctly excludes it, failing the test for a reason that has nothing to do with the grouping logic being tested. Falls back to "a few minutes from now" (still always this week) whenever the fixed anchor would already be behind now. Co-Authored-By: Claude Sonnet 5 --- management/tests.py | 10 +++++++++- mobile/tests.py | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/management/tests.py b/management/tests.py index c156879..c7c1566 100644 --- a/management/tests.py +++ b/management/tests.py @@ -5611,7 +5611,15 @@ class EventManagementTests(ManagementTestBase): # Mirrors mobile.views.CalendarView's own This week/Next week/by-month # agenda grouping -- see EventListView._list_groups. _this_week_start, this_week_end = week_bounds(timezone.localdate()) - this_week_event = Event.objects.create(club=self.club, title="This week event", start=timezone.make_aware(datetime.datetime.combine(this_week_end, datetime.time(18, 0)))) + this_week_event_start = timezone.make_aware(datetime.datetime.combine(this_week_end, datetime.time(18, 0))) + if this_week_event_start <= timezone.now(): + # The fixed 18:00 anchor is only "this week" if it's still ahead of + # now -- when this_week_end is today and it's already past 18:00, + # fall back to a few minutes from now (still always this week) + # instead of a start the view's own start__gte=now filter would + # exclude as already past. + this_week_event_start = timezone.now() + datetime.timedelta(minutes=5) + this_week_event = Event.objects.create(club=self.club, title="This week event", start=this_week_event_start) this_week_event.teams.add(self.own_team) next_week_event = Event.objects.create(club=self.club, title="Next week event", start=timezone.make_aware(datetime.datetime.combine(this_week_end + datetime.timedelta(days=3), datetime.time(18, 0)))) next_week_event.teams.add(self.own_team) diff --git a/mobile/tests.py b/mobile/tests.py index 810378a..9fd2fd5 100644 --- a/mobile/tests.py +++ b/mobile/tests.py @@ -3143,7 +3143,13 @@ class CoachScheduleViewTests(TestCase): def test_events_are_grouped_into_this_week_next_week_and_month_dividers(self): _this_week_start, this_week_end = week_bounds(timezone.localdate()) - this_week_event = Event.objects.create(club=self.club, title="This week practice", kind=Event.EventKind.TRAINING, start=timezone.make_aware(datetime.datetime.combine(this_week_end, datetime.time(18, 0)))) + this_week_event_start = timezone.make_aware(datetime.datetime.combine(this_week_end, datetime.time(18, 0))) + if this_week_event_start <= timezone.now(): + # See management.tests's own identical fallback on the same fixture + # shape -- the fixed 18:00 anchor is only "this week" if it's still + # ahead of now. + this_week_event_start = timezone.now() + datetime.timedelta(minutes=5) + this_week_event = Event.objects.create(club=self.club, title="This week practice", kind=Event.EventKind.TRAINING, start=this_week_event_start) this_week_event.teams.add(self.team) far_future = Event.objects.create(club=self.club, title="Far future practice", kind=Event.EventKind.TRAINING, start=timezone.now() + datetime.timedelta(days=60)) far_future.teams.add(self.team)