Skip to content

Commit c84bbac

Browse files
committed
test: fix order- and timezone-dependent tests
testCalendarCliFormat expected today's UTC offset on a 2007-03-01 timestamp, so it failed in summer time whenever pytest-randomly ran it before any test setting tz.implicit_timezone - a process-wide setting that leaked between tests. Expect the offset in effect at the tested date, and reset tz to its defaults around every test (conftest.py). TestNaturalLanguage compared Oslo-time results to the host's date, so it failed when the two dates differed; compute in Oslo time. Prompt: spawn a subagent to look into the flakey (sic) test. [tests/test_timespec.py::TestParseTimestamp::testCalendarCliFormat, seen failing intermittently] Followup-Prompt: go for it [commit the subagent's proposed test fix on a new branch off master] Assisted-By: Claude Opus 5.5 <noreply@anthropic.com> Reviewed-by: Tobias Brox <tobias@redpill-linpro.com>
1 parent 6fc28d8 commit c84bbac

2 files changed

Lines changed: 35 additions & 8 deletions

File tree

‎tests/conftest.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
3+
from plann.lib import tz
4+
5+
6+
@pytest.fixture(autouse=True)
7+
def _reset_tz():
8+
## tz is a process-wide singleton and its setters ignore None, so a
9+
## test assigning tz.implicit_timezone would otherwise leak into
10+
## whatever pytest-randomly happens to run next. Dropping the
11+
## instance attributes falls back to the class defaults.
12+
tz.__dict__.clear()
13+
yield
14+
tz.__dict__.clear()

‎tests/test_timespec.py‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
from datetime import date, datetime, timedelta, timezone
2+
from zoneinfo import ZoneInfo
23

34
import pytest
45

56
from plann.lib import _ensure_ts, parse_add_dur, parse_dt, parse_timespec, tz
67
from plann.timespec import DURATION_RE, is_duration
78

89
utc = timezone.utc
10+
OSLO = ZoneInfo('Europe/Oslo')
11+
12+
def test_tz_starts_at_defaults():
13+
"""Every test starts with the default tz settings (tests/conftest.py),
14+
whatever an earlier test - in any module - assigned."""
15+
assert tz.implicit_timezone is None
16+
assert tz.store_timezone == ZoneInfo('UTC')
917

1018
class TestParseTimestamp:
1119
@pytest.mark.parametrize("input", ["2012-12-12", "2011-11-11 11:11:11", datetime(2011, 11, 11, 11, 11, 11), date(2012, 12, 12), "+14d"])
@@ -82,7 +90,9 @@ def _expected_tz(self, dt):
8290
## Find the default expected timezone
8391
expected_tz = tz.implicit_timezone
8492
if not expected_tz:
85-
expected_tz = datetime.now().astimezone().tzinfo
93+
## Local UTC offset in effect at dt, not now - they differ
94+
## when the test runs on the other side of a DST change.
95+
expected_tz = dt.astimezone().tzinfo
8696
return expected_tz
8797

8898

@@ -183,23 +193,26 @@ def testCalendarCliFormat(self):
183193
self._testTimeSpec(expected)
184194

185195
class TestNaturalLanguage:
186-
"""Tests for natural language date parsing enabled by switching to dateparser."""
196+
"""Tests for natural language date parsing enabled by switching to dateparser.
197+
198+
The expected values are computed in Europe/Oslo, the implicit timezone
199+
the tests set - not in the host's, whose date may differ."""
187200

188201
def test_yesterday(self):
189202
tz.implicit_timezone = "Europe/Oslo"
190203
result = parse_dt("yesterday")
191-
expected_date = (datetime.now().astimezone() - timedelta(days=1)).date()
204+
expected_date = (datetime.now(OSLO) - timedelta(days=1)).date()
192205
assert result.date() == expected_date
193206

194207
def test_today(self):
195208
tz.implicit_timezone = "Europe/Oslo"
196209
result = parse_dt("today")
197-
assert result.date() == datetime.now().astimezone().date()
210+
assert result.date() == datetime.now(OSLO).date()
198211

199212
def test_relative_hours_ago(self):
200213
tz.implicit_timezone = "Europe/Oslo"
201214
result = parse_dt("3 hours ago")
202-
expected = datetime.now().astimezone() - timedelta(hours=3)
215+
expected = datetime.now(OSLO) - timedelta(hours=3)
203216
assert abs((result - expected).total_seconds()) < 5
204217

205218
def test_day_name(self):
@@ -210,15 +223,15 @@ def test_day_name(self):
210223
def test_relative_future(self):
211224
tz.implicit_timezone = "Europe/Oslo"
212225
result = parse_dt("in 2 days")
213-
expected = (datetime.now().astimezone() + timedelta(days=2)).date()
226+
expected = (datetime.now(OSLO) + timedelta(days=2)).date()
214227
got = result.date() if isinstance(result, datetime) else result
215228
assert got == expected
216229

217230
def test_yesterday_via_timespec(self):
218231
"""Natural-language dates should also flow through parse_timespec()."""
219232
tz.implicit_timezone = "Europe/Oslo"
220233
start, end = parse_timespec("yesterday")
221-
expected = (datetime.now().astimezone() - timedelta(days=1)).date()
234+
expected = (datetime.now(OSLO) - timedelta(days=1)).date()
222235
got = start.date() if isinstance(start, datetime) else start
223236
assert got == expected
224237
assert end is None
@@ -253,7 +266,7 @@ def test_duration_re_is_shared(self):
253266
def test_ensure_ts():
254267
now = datetime.now()
255268
utcnow = now.astimezone(utc)
256-
implicitnow = now.replace(tzinfo=tz.implicit_timezone)
269+
implicitnow = now.replace(tzinfo=tz.implicit_timezone) if tz.implicit_timezone else now.astimezone()
257270

258271
assert(_ensure_ts(now) == implicitnow)
259272
assert(_ensure_ts(utcnow) == utcnow)

0 commit comments

Comments
 (0)