Skip to content

Commit b0929bc

Browse files
committed
Use approx_time to compare timedeltas in tests
pytest 9.1.0 added proper support for `datetime`/`timedelta` in `pytest.approx()`, and as part of it, it now refuses to guess a tolerance for these types, raising instead: TypeError: pytest.approx() requires an explicit tolerance for datetime/timedelta comparisons: e.g. approx(expected, abs=timedelta(seconds=1)) or approx(expected, rel=0.01) This broke 12 tests in `test_clocksinfo.py` and `test_config.py`, which were using the bare `pytest.approx()` on `timedelta`s instead of the `approx_time()` helper used everywhere else in this test package. These assertions never really used a tolerance anyway: all the compared values are exact, so they only ever succeeded through the exact-equality short-circuit in `approx()`, before any tolerance was computed. Switching them to `approx_time()` provides the required tolerance (1ms by default) and makes the whole test package consistent. Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
1 parent c1befb8 commit b0929bc

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

tests/timeseries/_resampling/wall_clock_timer/test_clocksinfo.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from frequenz.sdk.timeseries._resampling._wall_clock_timer import ClocksInfo
1414

15+
from .util import approx_time
16+
1517
_DEFAULT_MONOTONIC_REQUESTED_SLEEP = timedelta(seconds=1.0)
1618
_DEFAULT_MONOTONIC_TIME = 1234.5
1719
_DEFAULT_WALL_CLOCK_TIME = datetime(2023, 1, 1, tzinfo=timezone.utc)
@@ -141,7 +143,7 @@ def test_monotonic_drift(
141143
monotonic_elapsed=monotonic_elapsed,
142144
wall_clock_elapsed=_DEFAULT_WALL_CLOCK_ELAPSED,
143145
)
144-
assert info.monotonic_drift == pytest.approx(expected_drift)
146+
assert info.monotonic_drift == approx_time(expected_drift)
145147

146148

147149
@pytest.mark.parametrize(
@@ -166,7 +168,7 @@ def test_wall_clock_jump(
166168
monotonic_elapsed=monotonic_elapsed,
167169
wall_clock_elapsed=wall_clock_elapsed,
168170
)
169-
assert info.wall_clock_jump == pytest.approx(expected_jump)
171+
assert info.wall_clock_jump == approx_time(expected_jump)
170172

171173

172174
@dataclass(kw_only=True, frozen=True)
@@ -219,7 +221,7 @@ def test_wall_clock_factor(case: _TestCaseWallClockFactor) -> None:
219221
wall_clock_elapsed=case.wall_clock_elapsed,
220222
)
221223
assert info.wall_clock_factor == pytest.approx(case.expected_factor)
222-
assert info.wall_clock_to_monotonic(case.wall_clock_elapsed) == pytest.approx(
224+
assert info.wall_clock_to_monotonic(case.wall_clock_elapsed) == approx_time(
223225
case.monotonic_elapsed
224226
)
225227

tests/timeseries/_resampling/wall_clock_timer/test_config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@
1212

1313
from frequenz.sdk.timeseries._resampling._wall_clock_timer import WallClockTimerConfig
1414

15+
from .util import approx_time
16+
1517

1618
def test_from_interval_defaults() -> None:
1719
"""Test WallClockTimerConfig.from_interval() with only interval (all defaults)."""
1820
interval = timedelta(seconds=10)
1921
config = WallClockTimerConfig.from_interval(interval)
2022
assert config.align_to == UNIX_EPOCH
21-
assert config.async_drift_tolerance == pytest.approx(timedelta(seconds=1.0))
23+
assert config.async_drift_tolerance == approx_time(timedelta(seconds=1.0))
2224
assert config.wall_clock_drift_tolerance_factor == pytest.approx(0.1)
23-
assert config.wall_clock_jump_threshold == pytest.approx(timedelta(seconds=10.0))
25+
assert config.wall_clock_jump_threshold == approx_time(timedelta(seconds=10.0))
2426

2527

2628
def test_from_interval_all_args() -> None:
@@ -38,9 +40,9 @@ def test_from_interval_all_args() -> None:
3840
wall_clock_jump_threshold_factor=jump_factor,
3941
)
4042
assert config.align_to == align_to
41-
assert config.async_drift_tolerance == pytest.approx(timedelta(seconds=1.0))
43+
assert config.async_drift_tolerance == approx_time(timedelta(seconds=1.0))
4244
assert config.wall_clock_drift_tolerance_factor == pytest.approx(0.3)
43-
assert config.wall_clock_jump_threshold == pytest.approx(timedelta(seconds=2.0))
45+
assert config.wall_clock_jump_threshold == approx_time(timedelta(seconds=2.0))
4446

4547

4648
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)