Skip to content

Commit 2fd3f6b

Browse files
authored
Improve Location docs and tests (#29)
- **Improve `Location.timezone` documentation** - **Add tests for the `Location.timezone` lookup**
2 parents d8d50ff + 26ee3bf commit 2fd3f6b

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

src/frequenz/client/microgrid/_metadata.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ class Location:
2424
timezone: ZoneInfo | None = None
2525
"""The timezone of the microgrid.
2626
27-
The timezone will be set to None if the latitude or longitude points
28-
are not set or the timezone cannot be found given the location points.
27+
If not passed during construction (or `None` is passed), and there is a `longitude`
28+
and `latitude`, then the timezone wil be looked up in a database based on the
29+
coordinates. This lookup could fail, in which case the timezone will still be
30+
`None`.
2931
"""
3032

3133
def __post_init__(self) -> None:

tests/test_metadata.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# License: MIT
2+
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
3+
4+
"""Tests for the microgrid metadata types."""
5+
6+
from typing import Iterator
7+
from unittest.mock import MagicMock, patch
8+
from zoneinfo import ZoneInfo
9+
10+
import pytest
11+
12+
from frequenz.client.microgrid import Location
13+
14+
15+
@pytest.fixture
16+
def timezone_finder() -> Iterator[MagicMock]:
17+
"""Return a mock timezone finder."""
18+
with patch(
19+
"frequenz.client.microgrid._metadata._timezone_finder", autospec=True
20+
) as mock_timezone_finder:
21+
yield mock_timezone_finder
22+
23+
24+
@pytest.mark.parametrize(
25+
"latitude, longitude, timezone",
26+
[
27+
(None, None, None),
28+
(52.52, None, None),
29+
(None, 13.405, None),
30+
(None, None, ZoneInfo(key="UTC")),
31+
(52.52, None, ZoneInfo(key="UTC")),
32+
(None, 13.405, ZoneInfo(key="UTC")),
33+
(52.52, 13.405, ZoneInfo(key="UTC")),
34+
],
35+
ids=str,
36+
)
37+
def test_location_timezone_not_looked_up_if_not_possible_or_necessary(
38+
timezone_finder: MagicMock,
39+
latitude: float | None,
40+
longitude: float | None,
41+
timezone: ZoneInfo | None,
42+
) -> None:
43+
"""Test the location timezone is not looked up if is not necessary or possible."""
44+
timezone_finder.timezone_at.return_value = "Europe/Berlin"
45+
46+
location = Location(latitude=latitude, longitude=longitude, timezone=timezone)
47+
48+
assert location.latitude == latitude
49+
assert location.longitude == longitude
50+
assert location.timezone == timezone
51+
timezone_finder.timezone_at.assert_not_called()
52+
53+
54+
@pytest.mark.parametrize("timezone", [None, "Europe/Berlin"], ids=str)
55+
def test_location_timezone_lookup(
56+
timezone_finder: MagicMock, timezone: str | None
57+
) -> None:
58+
"""Test the location timezone is looked up if not provided and there is enough info."""
59+
timezone_finder.timezone_at.return_value = timezone
60+
61+
location = Location(latitude=52.52, longitude=13.405)
62+
63+
if timezone is None:
64+
assert location.timezone is None
65+
else:
66+
assert location.timezone == ZoneInfo(key=timezone)
67+
timezone_finder.timezone_at.assert_called_once_with(lat=52.52, lng=13.405)

0 commit comments

Comments
 (0)