|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2025 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Tests for the microgrid metadata types.""" |
| 5 | + |
| 6 | +from collections.abc import Iterator |
| 7 | +from dataclasses import dataclass |
| 8 | +from unittest.mock import MagicMock, patch |
| 9 | +from zoneinfo import ZoneInfo |
| 10 | + |
| 11 | +import pytest |
| 12 | +from frequenz.api.common.v1 import location_pb2 |
| 13 | + |
| 14 | +from frequenz.client.microgrid import Location |
| 15 | +from frequenz.client.microgrid._location_proto import location_from_proto |
| 16 | + |
| 17 | + |
| 18 | +@dataclass(frozen=True, kw_only=True) |
| 19 | +class _ProtoConversionTestCase: # pylint: disable=too-many-instance-attributes |
| 20 | + """Test case for protobuf conversion.""" |
| 21 | + |
| 22 | + name: str |
| 23 | + """The description of the test case.""" |
| 24 | + |
| 25 | + latitude: float |
| 26 | + """The latitude to set in the protobuf message.""" |
| 27 | + |
| 28 | + longitude: float |
| 29 | + """The longitude to set in the protobuf message.""" |
| 30 | + |
| 31 | + country_code: str |
| 32 | + """The country code to set in the protobuf message.""" |
| 33 | + |
| 34 | + expected_none_latitude: bool = False |
| 35 | + """The latitude is expected to be None.""" |
| 36 | + |
| 37 | + expected_none_longitude: bool = False |
| 38 | + """The longitude is expected to be None.""" |
| 39 | + |
| 40 | + expected_none_country_code: bool = False |
| 41 | + """The country code is expected to be None.""" |
| 42 | + |
| 43 | + expect_warning: bool = False |
| 44 | + """Whether to expect a warning during conversion.""" |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture |
| 48 | +def timezone_finder() -> Iterator[MagicMock]: |
| 49 | + """Return a mock timezone finder.""" |
| 50 | + with patch( |
| 51 | + "frequenz.client.microgrid._location._timezone_finder", autospec=True |
| 52 | + ) as mock_timezone_finder: |
| 53 | + yield mock_timezone_finder |
| 54 | + |
| 55 | + |
| 56 | +def test_timezone_not_looked_up_if_unused(timezone_finder: MagicMock) -> None: |
| 57 | + """Test the location timezone is not looked up if it is not used.""" |
| 58 | + location = Location(latitude=52.52, longitude=13.405, country_code="DE") |
| 59 | + |
| 60 | + assert location.latitude == 52.52 |
| 61 | + assert location.longitude == 13.405 |
| 62 | + assert location.country_code == "DE" |
| 63 | + timezone_finder.timezone_at.assert_not_called() |
| 64 | + |
| 65 | + |
| 66 | +def test_timezone_looked_up_but_not_found(timezone_finder: MagicMock) -> None: |
| 67 | + """Test the location timezone is not looked up if it is not used.""" |
| 68 | + timezone_finder.timezone_at.return_value = None |
| 69 | + |
| 70 | + location = Location(latitude=52.52, longitude=13.405, country_code="DE") |
| 71 | + |
| 72 | + assert location.timezone is None |
| 73 | + timezone_finder.timezone_at.assert_called_once_with(lat=52.52, lng=13.405) |
| 74 | + |
| 75 | + |
| 76 | +def test_timezone_looked_up_and_found(timezone_finder: MagicMock) -> None: |
| 77 | + """Test the location timezone is not looked up if it is not used.""" |
| 78 | + timezone_finder.timezone_at.return_value = "Europe/Berlin" |
| 79 | + |
| 80 | + location = Location(latitude=52.52, longitude=13.405, country_code="DE") |
| 81 | + |
| 82 | + assert location.timezone == ZoneInfo(key="Europe/Berlin") |
| 83 | + timezone_finder.timezone_at.assert_called_once_with(lat=52.52, lng=13.405) |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize( |
| 87 | + "case", |
| 88 | + [ |
| 89 | + _ProtoConversionTestCase( |
| 90 | + name="valid", |
| 91 | + latitude=52.52, |
| 92 | + longitude=13.405, |
| 93 | + country_code="DE", |
| 94 | + ), |
| 95 | + _ProtoConversionTestCase( |
| 96 | + name="boundary_latitude", |
| 97 | + latitude=90.0, |
| 98 | + longitude=13.405, |
| 99 | + country_code="DE", |
| 100 | + ), |
| 101 | + _ProtoConversionTestCase( |
| 102 | + name="boundary_longitude", |
| 103 | + latitude=52.52, |
| 104 | + longitude=180.0, |
| 105 | + country_code="DE", |
| 106 | + ), |
| 107 | + _ProtoConversionTestCase( |
| 108 | + name="invalid_latitude", |
| 109 | + latitude=91.0, |
| 110 | + longitude=13.405, |
| 111 | + country_code="DE", |
| 112 | + expected_none_latitude=True, |
| 113 | + expect_warning=True, |
| 114 | + ), |
| 115 | + _ProtoConversionTestCase( |
| 116 | + name="invalid_longitude", |
| 117 | + latitude=52.52, |
| 118 | + longitude=181.0, |
| 119 | + country_code="DE", |
| 120 | + expected_none_longitude=True, |
| 121 | + expect_warning=True, |
| 122 | + ), |
| 123 | + _ProtoConversionTestCase( |
| 124 | + name="empty_country_code", |
| 125 | + latitude=52.52, |
| 126 | + longitude=13.405, |
| 127 | + country_code="", |
| 128 | + expected_none_country_code=True, |
| 129 | + expect_warning=True, |
| 130 | + ), |
| 131 | + _ProtoConversionTestCase( |
| 132 | + name="all_invalid", |
| 133 | + latitude=-91.0, |
| 134 | + longitude=181.0, |
| 135 | + country_code="", |
| 136 | + expected_none_latitude=True, |
| 137 | + expected_none_longitude=True, |
| 138 | + expected_none_country_code=True, |
| 139 | + expect_warning=True, |
| 140 | + ), |
| 141 | + ], |
| 142 | + ids=lambda case: case.name, |
| 143 | +) |
| 144 | +def test_from_proto( |
| 145 | + caplog: pytest.LogCaptureFixture, case: _ProtoConversionTestCase |
| 146 | +) -> None: |
| 147 | + """Test conversion from protobuf message to Location.""" |
| 148 | + proto = location_pb2.Location( |
| 149 | + latitude=case.latitude, |
| 150 | + longitude=case.longitude, |
| 151 | + country_code=case.country_code, |
| 152 | + ) |
| 153 | + with caplog.at_level("WARNING"): |
| 154 | + location = location_from_proto(proto) |
| 155 | + |
| 156 | + if case.expected_none_latitude: |
| 157 | + assert location.latitude is None |
| 158 | + else: |
| 159 | + assert location.latitude == pytest.approx(case.latitude) |
| 160 | + |
| 161 | + if case.expected_none_longitude: |
| 162 | + assert location.longitude is None |
| 163 | + else: |
| 164 | + assert location.longitude == pytest.approx(case.longitude) |
| 165 | + |
| 166 | + if case.expected_none_country_code: |
| 167 | + assert location.country_code is None |
| 168 | + else: |
| 169 | + assert location.country_code == case.country_code |
| 170 | + |
| 171 | + if case.expect_warning: |
| 172 | + assert len(caplog.records) > 0 |
| 173 | + assert "Found issues in location:" in caplog.records[0].message |
| 174 | + else: |
| 175 | + assert len(caplog.records) == 0 |
0 commit comments