Skip to content

Commit 4a947ed

Browse files
committed
Address Copilot PR reviews: timezone-aware tests, robust assertions, Ruff compliance (Issue #2626)
1 parent 8309653 commit 4a947ed

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
11
import datetime
2+
import re
23

34
from django.template.loader import render_to_string
45
from django.test import TestCase
6+
from django.utils import timezone
57

6-
from apps.events.models import Event, Calendar
8+
from apps.events.models import Calendar, Event
79

810

911
class TimeTagTemplateTests(TestCase):
1012
def test_single_day_event_year_rendering(self):
1113
"""
1214
Verify that a single-day event does not render the year twice (Issue #2626).
1315
"""
14-
# Create a single day event in the future to trigger the year rendering condition
15-
future_year = datetime.date.today().year + 1
16-
16+
future_year = timezone.now().date().year + 1
17+
1718
calendar = Calendar.objects.create(
1819
name="Test Calendar",
1920
slug="test-calendar-time-tag-single-day-event",
2021
)
21-
2222
event = Event.objects.create(
2323
title="Single Day Future Event",
2424
description="Test event",
2525
calendar=calendar,
2626
)
27-
28-
# Manually create the next_time context object
27+
28+
# Use timezone-aware datetimes to match the project's USE_TZ = True setting.
2929
class MockTime:
3030
def __init__(self):
31-
self.dt_start = datetime.datetime(future_year, 5, 25, 12, 0)
32-
self.dt_end = datetime.datetime(future_year, 5, 25, 14, 0)
31+
self.dt_start = datetime.datetime(
32+
future_year, 5, 25, 12, 0, tzinfo=datetime.UTC
33+
)
34+
self.dt_end = datetime.datetime(
35+
future_year, 5, 25, 14, 0, tzinfo=datetime.UTC
36+
)
3337
self.single_day = True
3438
self.all_day = False
3539
self.valid_dt_end = True
3640

3741
context = {
3842
"next_time": MockTime(),
39-
"scheduled_start_this_year": False, # Event is in the future year
43+
"scheduled_start_this_year": False,
4044
"scheduled_end_this_year": False,
4145
"object": event,
4246
}
4347

4448
rendered = render_to_string("events/includes/time_tag.html", context)
45-
46-
# The year should only appear visibly once in the output (not counting the datetime ISO tag).
49+
50+
# Count visible year occurrences inside <span> tags using a regex.
51+
# This avoids brittle exact-whitespace matching and ignores the
52+
# year inside the <time datetime="..."> ISO attribute.
4753
year_str = str(future_year)
48-
# Using string splitting to exclude the `datetime="2027...` occurrence by checking how many times
49-
# it appears wrapped with whitespace or inside a span.
50-
visible_occurrences = rendered.split(">\n " + year_str + "\n </span>")
54+
year_in_span = re.findall(
55+
r"<span[^>]*>\s*" + re.escape(year_str) + r"\s*</span>", rendered
56+
)
5157
self.assertEqual(
52-
len(visible_occurrences) - 1,
58+
len(year_in_span),
5359
1,
54-
f"Expected the visible span containing {year_str} to appear exactly once, but it was duplicated: {rendered}"
60+
f"Expected the year {year_str} to appear in exactly one <span>, "
61+
f"but found {len(year_in_span)}: {rendered}",
5562
)

0 commit comments

Comments
 (0)