Skip to content

Commit aaf52ad

Browse files
committed
Add tests for microgrid metadata retrieval
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 258939f commit aaf52ad

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

tests/test_client.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from frequenz.api.common import components_pb2, metrics_pb2
1414
from frequenz.api.microgrid import grid_pb2, inverter_pb2, microgrid_pb2
1515
from frequenz.client.base import retry
16+
from google.protobuf.empty_pb2 import Empty
1617

1718
from frequenz.client.microgrid import (
1819
ApiClientError,
@@ -29,6 +30,7 @@
2930
InverterType,
3031
MeterData,
3132
MicrogridApiClient,
33+
MicrogridId,
3234
)
3335

3436

@@ -45,6 +47,7 @@ def __init__(self, *, retry_strategy: retry.Strategy | None = None) -> None:
4547
mock_stub.SetPowerReactive = mock.AsyncMock("SetPowerReactive")
4648
mock_stub.AddInclusionBounds = mock.AsyncMock("AddInclusionBounds")
4749
mock_stub.StreamComponentData = mock.Mock("StreamComponentData")
50+
mock_stub.GetMicrogridMetadata = mock.AsyncMock("GetMicrogridMetadata")
4851
super().__init__("grpc://mock_host:1234", retry_strategy=retry_strategy)
4952
self.mock_stub = mock_stub
5053
self._stub = mock_stub # pylint: disable=protected-access
@@ -393,6 +396,71 @@ async def test_connections_grpc_error(client: _TestClient) -> None:
393396
await client.connections()
394397

395398

399+
async def test_metadata_success(client: _TestClient) -> None:
400+
"""Test the metadata() method with a successful gRPC call."""
401+
mock_metadata_response = microgrid_pb2.MicrogridMetadata(
402+
microgrid_id=123,
403+
location=microgrid_pb2.Location(latitude=40.7128, longitude=-74.0060),
404+
)
405+
client.mock_stub.GetMicrogridMetadata.return_value = mock_metadata_response
406+
407+
metadata = await client.metadata()
408+
409+
assert metadata.microgrid_id == MicrogridId(123)
410+
assert metadata.location is not None
411+
assert metadata.location.latitude == pytest.approx(40.7128)
412+
assert metadata.location.longitude == pytest.approx(-74.0060)
413+
client.mock_stub.GetMicrogridMetadata.assert_called_once_with(Empty(), timeout=60)
414+
415+
416+
async def test_metadata_no_location(client: _TestClient) -> None:
417+
"""Test the metadata() method when location is not set in the response."""
418+
mock_metadata_response = microgrid_pb2.MicrogridMetadata(microgrid_id=456)
419+
client.mock_stub.GetMicrogridMetadata.return_value = mock_metadata_response
420+
421+
metadata = await client.metadata()
422+
423+
assert metadata.microgrid_id == MicrogridId(456)
424+
assert metadata.location is None
425+
client.mock_stub.GetMicrogridMetadata.assert_called_once_with(Empty(), timeout=60)
426+
427+
428+
async def test_metadata_empty_response(client: _TestClient) -> None:
429+
"""Test the metadata() method when the server returns an empty response."""
430+
client.mock_stub.GetMicrogridMetadata.return_value = None
431+
432+
metadata = await client.metadata()
433+
434+
assert metadata.microgrid_id is None
435+
assert metadata.location is None
436+
client.mock_stub.GetMicrogridMetadata.assert_called_once_with(Empty(), timeout=60)
437+
438+
439+
async def test_metadata_grpc_error(
440+
client: _TestClient, caplog: pytest.LogCaptureFixture
441+
) -> None:
442+
"""Test the metadata() method when the gRPC call fails."""
443+
caplog.set_level(logging.WARNING)
444+
client.mock_stub.GetMicrogridMetadata.side_effect = grpc.aio.AioRpcError(
445+
mock.MagicMock(name="mock_status"),
446+
mock.MagicMock(name="mock_initial_metadata"),
447+
mock.MagicMock(name="mock_trailing_metadata"),
448+
"fake grpc details for metadata",
449+
"fake grpc debug_error_string for metadata",
450+
)
451+
452+
metadata = await client.metadata()
453+
454+
assert metadata.microgrid_id is None
455+
assert metadata.location is None
456+
client.mock_stub.GetMicrogridMetadata.assert_called_once_with(Empty(), timeout=60)
457+
assert len(caplog.records) == 1
458+
assert caplog.records[0].levelname == "ERROR"
459+
assert "The microgrid metadata is not available." in caplog.records[0].message
460+
assert caplog.records[0].exc_text is not None
461+
assert "fake grpc details for metadata" in caplog.records[0].exc_text
462+
463+
396464
@pytest.fixture
397465
def meter83() -> microgrid_pb2.Component:
398466
"""Return a test meter component."""

0 commit comments

Comments
 (0)