|
| 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._location._timezone_finder", autospec=True |
| 20 | + ) as mock_timezone_finder: |
| 21 | + yield mock_timezone_finder |
| 22 | + |
| 23 | + |
| 24 | +def test_location_timezone_constructor() -> None: |
| 25 | + """Test the location timezone is not looked up if it is not used.""" |
| 26 | + location = Location(latitude=52.52, longitude=13.405, country_code="DE") |
| 27 | + |
| 28 | + assert location.latitude == 52.52 |
| 29 | + assert location.longitude == 13.405 |
| 30 | + assert location.country_code == "DE" |
| 31 | + |
| 32 | + |
| 33 | +def test_location_timezone_not_looked_up_if_unused(timezone_finder: MagicMock) -> None: |
| 34 | + """Test the location timezone is not looked up if it is not used.""" |
| 35 | + _ = Location(latitude=52.52, longitude=13.405, country_code="DE") |
| 36 | + timezone_finder.timezone_at.assert_not_called() |
| 37 | + |
| 38 | + |
| 39 | +def test_location_timezone_looked_up_but_not_found(timezone_finder: MagicMock) -> None: |
| 40 | + """Test the location timezone is not looked up if it is not used.""" |
| 41 | + timezone_finder.timezone_at.return_value = None |
| 42 | + |
| 43 | + location = Location(latitude=52.52, longitude=13.405, country_code="DE") |
| 44 | + |
| 45 | + assert location.timezone is None |
| 46 | + timezone_finder.timezone_at.assert_called_once_with(lat=52.52, lng=13.405) |
| 47 | + |
| 48 | + |
| 49 | +def test_location_timezone_looked_up_and_found(timezone_finder: MagicMock) -> None: |
| 50 | + """Test the location timezone is not looked up if it is not used.""" |
| 51 | + timezone_finder.timezone_at.return_value = "Europe/Berlin" |
| 52 | + |
| 53 | + location = Location(latitude=52.52, longitude=13.405, country_code="DE") |
| 54 | + |
| 55 | + assert location.timezone == ZoneInfo(key="Europe/Berlin") |
| 56 | + timezone_finder.timezone_at.assert_called_once_with(lat=52.52, lng=13.405) |
0 commit comments