13
13
from frequenz .api .common import components_pb2 , metrics_pb2
14
14
from frequenz .api .microgrid import grid_pb2 , inverter_pb2 , microgrid_pb2
15
15
from frequenz .client .base import retry
16
+ from google .protobuf .empty_pb2 import Empty
16
17
17
18
from frequenz .client .microgrid import (
18
19
ApiClientError ,
29
30
InverterType ,
30
31
MeterData ,
31
32
MicrogridApiClient ,
33
+ MicrogridId ,
32
34
)
33
35
34
36
@@ -45,6 +47,7 @@ def __init__(self, *, retry_strategy: retry.Strategy | None = None) -> None:
45
47
mock_stub .SetPowerReactive = mock .AsyncMock ("SetPowerReactive" )
46
48
mock_stub .AddInclusionBounds = mock .AsyncMock ("AddInclusionBounds" )
47
49
mock_stub .StreamComponentData = mock .Mock ("StreamComponentData" )
50
+ mock_stub .GetMicrogridMetadata = mock .AsyncMock ("GetMicrogridMetadata" )
48
51
super ().__init__ ("grpc://mock_host:1234" , retry_strategy = retry_strategy )
49
52
self .mock_stub = mock_stub
50
53
self ._stub = mock_stub # pylint: disable=protected-access
@@ -393,6 +396,71 @@ async def test_connections_grpc_error(client: _TestClient) -> None:
393
396
await client .connections ()
394
397
395
398
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
+
396
464
@pytest .fixture
397
465
def meter83 () -> microgrid_pb2 .Component :
398
466
"""Return a test meter component."""
0 commit comments