Skip to content

Commit 26ee3bf

Browse files
committed
Add tests for the Location.timezone lookup
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 5872570 commit 26ee3bf

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

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)