|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2025 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Tests for the DeliveryArea class and its protobuf conversion.""" |
| 5 | + |
| 6 | +from dataclasses import dataclass |
| 7 | + |
| 8 | +import pytest |
| 9 | +from frequenz.api.common.v1.grid import delivery_area_pb2 |
| 10 | + |
| 11 | +from frequenz.client.microgrid import DeliveryArea, EnergyMarketCodeType |
| 12 | +from frequenz.client.microgrid._delivery_area_proto import delivery_area_from_proto |
| 13 | + |
| 14 | + |
| 15 | +@dataclass(frozen=True, kw_only=True) |
| 16 | +class _DeliveryAreaTestCase: |
| 17 | + """Test case for DeliveryArea creation.""" |
| 18 | + |
| 19 | + name: str |
| 20 | + """Description of the test case.""" |
| 21 | + |
| 22 | + code: str | None |
| 23 | + """The code to use for the delivery area.""" |
| 24 | + |
| 25 | + code_type: EnergyMarketCodeType | int |
| 26 | + """The type of code being used.""" |
| 27 | + |
| 28 | + expected_str: str |
| 29 | + """Expected string representation.""" |
| 30 | + |
| 31 | + |
| 32 | +@dataclass(frozen=True, kw_only=True) |
| 33 | +class _ProtoConversionTestCase: |
| 34 | + """Test case for protobuf conversion.""" |
| 35 | + |
| 36 | + name: str |
| 37 | + """Description of the test case.""" |
| 38 | + |
| 39 | + code: str | None |
| 40 | + """The code to set in the protobuf message.""" |
| 41 | + |
| 42 | + code_type: int |
| 43 | + """The code type to set in the protobuf message.""" |
| 44 | + |
| 45 | + expected_code: str | None |
| 46 | + """Expected code in the resulting DeliveryArea.""" |
| 47 | + |
| 48 | + expected_code_type: EnergyMarketCodeType | int |
| 49 | + """Expected code type in the resulting DeliveryArea.""" |
| 50 | + |
| 51 | + expect_warning: bool |
| 52 | + """Whether to expect a warning during conversion.""" |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize( |
| 56 | + "case", |
| 57 | + [ |
| 58 | + _DeliveryAreaTestCase( |
| 59 | + name="valid_EIC_code", |
| 60 | + code="10Y1001A1001A450", |
| 61 | + code_type=EnergyMarketCodeType.EUROPE_EIC, |
| 62 | + expected_str="10Y1001A1001A450[EUROPE_EIC]", |
| 63 | + ), |
| 64 | + _DeliveryAreaTestCase( |
| 65 | + name="valid_NERC_code", |
| 66 | + code="PJM", |
| 67 | + code_type=EnergyMarketCodeType.US_NERC, |
| 68 | + expected_str="PJM[US_NERC]", |
| 69 | + ), |
| 70 | + _DeliveryAreaTestCase( |
| 71 | + name="no_code", |
| 72 | + code=None, |
| 73 | + code_type=EnergyMarketCodeType.EUROPE_EIC, |
| 74 | + expected_str="<NO CODE>[EUROPE_EIC]", |
| 75 | + ), |
| 76 | + _DeliveryAreaTestCase( |
| 77 | + name="unspecified_code_type", |
| 78 | + code="TEST", |
| 79 | + code_type=EnergyMarketCodeType.UNSPECIFIED, |
| 80 | + expected_str="TEST[UNSPECIFIED]", |
| 81 | + ), |
| 82 | + _DeliveryAreaTestCase( |
| 83 | + name="unknown_code_type", |
| 84 | + code="TEST", |
| 85 | + code_type=999, |
| 86 | + expected_str="TEST[type=999]", |
| 87 | + ), |
| 88 | + ], |
| 89 | + ids=lambda case: case.name, |
| 90 | +) |
| 91 | +def test_creation(case: _DeliveryAreaTestCase) -> None: |
| 92 | + """Test creating DeliveryArea instances with various parameters.""" |
| 93 | + area = DeliveryArea(code=case.code, code_type=case.code_type) |
| 94 | + assert area.code == case.code |
| 95 | + assert area.code_type == case.code_type |
| 96 | + assert str(area) == case.expected_str |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize( |
| 100 | + "case", |
| 101 | + [ |
| 102 | + _ProtoConversionTestCase( |
| 103 | + name="valid_EIC_code", |
| 104 | + code="10Y1001A1001A450", |
| 105 | + code_type=delivery_area_pb2.EnergyMarketCodeType.ENERGY_MARKET_CODE_TYPE_EUROPE_EIC, |
| 106 | + expected_code="10Y1001A1001A450", |
| 107 | + expected_code_type=EnergyMarketCodeType.EUROPE_EIC, |
| 108 | + expect_warning=False, |
| 109 | + ), |
| 110 | + _ProtoConversionTestCase( |
| 111 | + name="valid_NERC_code", |
| 112 | + code="PJM", |
| 113 | + code_type=delivery_area_pb2.EnergyMarketCodeType.ENERGY_MARKET_CODE_TYPE_US_NERC, |
| 114 | + expected_code="PJM", |
| 115 | + expected_code_type=EnergyMarketCodeType.US_NERC, |
| 116 | + expect_warning=False, |
| 117 | + ), |
| 118 | + _ProtoConversionTestCase( |
| 119 | + name="no_code", |
| 120 | + code=None, |
| 121 | + code_type=delivery_area_pb2.EnergyMarketCodeType.ENERGY_MARKET_CODE_TYPE_EUROPE_EIC, |
| 122 | + expected_code=None, |
| 123 | + expected_code_type=EnergyMarketCodeType.EUROPE_EIC, |
| 124 | + expect_warning=True, |
| 125 | + ), |
| 126 | + _ProtoConversionTestCase( |
| 127 | + name="unspecified_code_type", |
| 128 | + code="TEST", |
| 129 | + code_type=delivery_area_pb2.EnergyMarketCodeType.ENERGY_MARKET_CODE_TYPE_UNSPECIFIED, |
| 130 | + expected_code="TEST", |
| 131 | + expected_code_type=EnergyMarketCodeType.UNSPECIFIED, |
| 132 | + expect_warning=True, |
| 133 | + ), |
| 134 | + _ProtoConversionTestCase( |
| 135 | + name="unknown_code_type", |
| 136 | + code="TEST", |
| 137 | + code_type=999, |
| 138 | + expected_code="TEST", |
| 139 | + expected_code_type=999, |
| 140 | + expect_warning=True, |
| 141 | + ), |
| 142 | + ], |
| 143 | + ids=lambda case: case.name, |
| 144 | +) |
| 145 | +def test_from_proto( |
| 146 | + caplog: pytest.LogCaptureFixture, case: _ProtoConversionTestCase |
| 147 | +) -> None: |
| 148 | + """Test conversion from protobuf message to DeliveryArea.""" |
| 149 | + # We do the type-ignore here because we want to test the case of an |
| 150 | + # arbitrary int too. |
| 151 | + proto = delivery_area_pb2.DeliveryArea( |
| 152 | + code=case.code or "", code_type=case.code_type # type: ignore[arg-type] |
| 153 | + ) |
| 154 | + with caplog.at_level("WARNING"): |
| 155 | + area = delivery_area_from_proto(proto) |
| 156 | + |
| 157 | + assert area.code == case.expected_code |
| 158 | + assert area.code_type == case.expected_code_type |
| 159 | + |
| 160 | + if case.expect_warning: |
| 161 | + assert len(caplog.records) > 0 |
| 162 | + assert "Found issues in delivery area" in caplog.records[0].message |
| 163 | + else: |
| 164 | + assert len(caplog.records) == 0 |
0 commit comments