Skip to content

Commit 03f5e31

Browse files
committed
Add utility functions for testing the wall clock timer
This commit adds a utility module for testing the wall clock timer. The utility functions include a method to convert various time-related types (datetime, timedelta, float) to seconds, and a function to get the current wall clock time from the mocked datetime (when the `datetime_mock` fixture is used). Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 5ba56fe commit 03f5e31

File tree

1 file changed

+36
-0
lines changed
  • tests/timeseries/_resampling/wall_clock_timer

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# License: MIT
2+
# Copyright © 2025 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Utility functions for testing the wall clock timer."""
5+
6+
from datetime import datetime, timedelta, timezone
7+
from typing import assert_never
8+
9+
10+
def to_seconds(value: datetime | timedelta | float) -> float:
11+
"""Convert a datetime, timedelta, or float to seconds."""
12+
match value:
13+
case datetime():
14+
return value.timestamp()
15+
case timedelta():
16+
return value.total_seconds()
17+
case float():
18+
return value
19+
case unexpected:
20+
assert_never(unexpected)
21+
22+
23+
# This is needed to work with the datetime_mock fixture in conftest.py
24+
def wall_now() -> datetime:
25+
"""Get the current wall clock time from the mocked datetime in the target module."""
26+
# Disable isort formatting because it wants to put the ignore in the wrong line
27+
# We now also have to ignore the maximum line length (E501)
28+
# isort: off
29+
# pylint: disable-next=import-outside-toplevel
30+
from frequenz.sdk.timeseries._resampling._wall_clock_timer import ( # type: ignore[attr-defined] # noqa: E501
31+
datetime as mock_datetime,
32+
)
33+
34+
# isort: on
35+
36+
return mock_datetime.now(timezone.utc)

0 commit comments

Comments
 (0)