Skip to content

Commit bcea558

Browse files
committed
fixup! Implement GetMicrogridMetadata
Add `DeliveryArea` tests
1 parent e583477 commit bcea558

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed

tests/test_delivery_area.py

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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="unspecified code type",
72+
code="TEST",
73+
code_type=EnergyMarketCodeType.UNSPECIFIED,
74+
expected_str="TEST[UNSPECIFIED]",
75+
),
76+
DeliveryAreaTestCase(
77+
name="no code",
78+
code=None,
79+
code_type=EnergyMarketCodeType.EUROPE_EIC,
80+
expected_str="<NO CODE>[EUROPE_EIC]",
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_delivery_area_creation(case: DeliveryAreaTestCase) -> None:
92+
"""Test creating DeliveryArea instances with various parameters.
93+
94+
Args:
95+
case: Test case parameters
96+
97+
Raises:
98+
AssertionError: If any of the test assertions fail
99+
"""
100+
area = DeliveryArea(code=case.code, code_type=case.code_type)
101+
assert area.code == case.code
102+
assert area.code_type == case.code_type
103+
assert str(area) == case.expected_str
104+
105+
106+
@pytest.mark.parametrize(
107+
"case",
108+
[
109+
ProtoConversionTestCase(
110+
name="valid EIC code",
111+
code="10Y1001A1001A450",
112+
code_type=delivery_area_pb2.EnergyMarketCodeType.ENERGY_MARKET_CODE_TYPE_EUROPE_EIC,
113+
expected_code="10Y1001A1001A450",
114+
expected_code_type=EnergyMarketCodeType.EUROPE_EIC,
115+
expect_warning=False,
116+
),
117+
ProtoConversionTestCase(
118+
name="valid NERC code",
119+
code="PJM",
120+
code_type=delivery_area_pb2.EnergyMarketCodeType.ENERGY_MARKET_CODE_TYPE_US_NERC,
121+
expected_code="PJM",
122+
expected_code_type=EnergyMarketCodeType.US_NERC,
123+
expect_warning=False,
124+
),
125+
ProtoConversionTestCase(
126+
name="no code",
127+
code=None,
128+
code_type=delivery_area_pb2.EnergyMarketCodeType.ENERGY_MARKET_CODE_TYPE_EUROPE_EIC,
129+
expected_code=None,
130+
expected_code_type=EnergyMarketCodeType.EUROPE_EIC,
131+
expect_warning=True,
132+
),
133+
ProtoConversionTestCase(
134+
name="unspecified type",
135+
code="TEST",
136+
code_type=delivery_area_pb2.EnergyMarketCodeType.ENERGY_MARKET_CODE_TYPE_UNSPECIFIED,
137+
expected_code="TEST",
138+
expected_code_type=EnergyMarketCodeType.UNSPECIFIED,
139+
expect_warning=True,
140+
),
141+
ProtoConversionTestCase(
142+
name="unknown code type",
143+
code="TEST",
144+
code_type=999,
145+
expected_code="TEST",
146+
expected_code_type=999,
147+
expect_warning=True,
148+
),
149+
],
150+
ids=lambda case: case.name,
151+
)
152+
def test_delivery_area_from_proto(
153+
caplog: pytest.LogCaptureFixture, case: ProtoConversionTestCase
154+
) -> None:
155+
"""Test conversion from protobuf message to DeliveryArea.
156+
157+
Args:
158+
caplog: Fixture to capture log messages
159+
case: Test case parameters
160+
161+
Raises:
162+
AssertionError: If any of the test assertions fail
163+
"""
164+
# We do the type-ignore here because we want to test the case of an
165+
# arbitrary int too.
166+
proto = delivery_area_pb2.DeliveryArea(
167+
code=case.code or "", code_type=case.code_type # type: ignore[arg-type]
168+
)
169+
with caplog.at_level("WARNING"):
170+
area = delivery_area_from_proto(proto)
171+
172+
assert area.code == case.expected_code
173+
assert area.code_type == case.expected_code_type
174+
175+
if case.expect_warning:
176+
assert len(caplog.records) > 0
177+
assert "Found issues in delivery area" in caplog.records[0].message
178+
else:
179+
assert len(caplog.records) == 0

0 commit comments

Comments
 (0)