Skip to content

Commit 061dab0

Browse files
committed
Move creation of the client to the _grpc_server()
This helps reducing the boilerplate even further and also makes sure that the server and client ports are the same. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent fb854a1 commit 061dab0

File tree

1 file changed

+17
-41
lines changed

1 file changed

+17
-41
lines changed

tests/microgrid/test_client.py

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,28 @@
3939
async def _gprc_server(
4040
port: int,
4141
servicer: mock_api.MockMicrogridServicer | None = None,
42-
) -> AsyncIterator[mock_api.MockMicrogridServicer]:
42+
) -> AsyncIterator[tuple[mock_api.MockMicrogridServicer, client.MicrogridApiClient]]:
4343
if servicer is None:
4444
servicer = mock_api.MockMicrogridServicer()
4545
server = mock_api.MockGrpcServer(servicer, port=port)
46+
microgrid = client.MicrogridGrpcClient(
47+
grpc.aio.insecure_channel(f"[::]:{port}"),
48+
f"[::]:{port}",
49+
retry_spec=LinearBackoff(interval=0.0, jitter=0.05),
50+
)
4651
await server.start()
4752
try:
48-
yield servicer
53+
yield servicer, microgrid
4954
finally:
5055
assert await server.graceful_shutdown()
5156

5257

5358
class TestMicrogridGrpcClient:
5459
"""Tests for the microgrid client thin wrapper."""
5560

56-
@staticmethod
57-
def create_client(port: int) -> client.MicrogridApiClient:
58-
"""Create a client for the mock API server."""
59-
return client.MicrogridGrpcClient(
60-
grpc.aio.insecure_channel(f"[::]:{port}"),
61-
f"[::]:{port}",
62-
retry_spec=LinearBackoff(interval=0.0, jitter=0.05),
63-
)
64-
6561
async def test_components(self) -> None:
6662
"""Test the components() method."""
67-
async with _gprc_server(57899) as servicer:
68-
microgrid = self.create_client(57899)
69-
63+
async with _gprc_server(57899) as (servicer, microgrid):
7064
assert set(await microgrid.components()) == set()
7165

7266
servicer.add_component(
@@ -180,9 +174,7 @@ async def test_components(self) -> None:
180174

181175
async def test_connections(self) -> None:
182176
"""Test the connections() method."""
183-
async with _gprc_server(57898) as servicer:
184-
microgrid = self.create_client(57898)
185-
177+
async with _gprc_server(57898) as (servicer, microgrid):
186178
assert set(await microgrid.connections()) == set()
187179

188180
servicer.add_connection(0, 0)
@@ -344,9 +336,7 @@ def ListAllComponents(
344336
) -> microgrid_pb.ComponentList:
345337
return microgrid_pb.ComponentList(components=self._components)
346338

347-
async with _gprc_server(57897, BadServicer()) as servicer:
348-
microgrid = self.create_client(57897)
349-
339+
async with _gprc_server(57897, BadServicer()) as (servicer, microgrid):
350340
assert list(await microgrid.connections()) == []
351341
for component_id in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
352342
servicer.add_component(
@@ -392,9 +382,7 @@ def ListAllComponents(
392382

393383
async def test_meter_data(self) -> None:
394384
"""Test the meter_data() method."""
395-
async with _gprc_server(57899) as servicer:
396-
microgrid = self.create_client(57899)
397-
385+
async with _gprc_server(57899) as (servicer, microgrid):
398386
servicer.add_component(
399387
83, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
400388
)
@@ -418,9 +406,7 @@ async def test_meter_data(self) -> None:
418406

419407
async def test_battery_data(self) -> None:
420408
"""Test the battery_data() method."""
421-
async with _gprc_server(57899) as servicer:
422-
microgrid = self.create_client(57899)
423-
409+
async with _gprc_server(57899) as (servicer, microgrid):
424410
servicer.add_component(
425411
83, components_pb.ComponentCategory.COMPONENT_CATEGORY_BATTERY
426412
)
@@ -444,9 +430,7 @@ async def test_battery_data(self) -> None:
444430

445431
async def test_inverter_data(self) -> None:
446432
"""Test the inverter_data() method."""
447-
async with _gprc_server(57899) as servicer:
448-
microgrid = self.create_client(57899)
449-
433+
async with _gprc_server(57899) as (servicer, microgrid):
450434
servicer.add_component(
451435
83, components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
452436
)
@@ -470,9 +454,7 @@ async def test_inverter_data(self) -> None:
470454

471455
async def test_ev_charger_data(self) -> None:
472456
"""Test the ev_charger_data() method."""
473-
async with _gprc_server(57899) as servicer:
474-
microgrid = self.create_client(57899)
475-
457+
async with _gprc_server(57899) as (servicer, microgrid):
476458
servicer.add_component(
477459
83, components_pb.ComponentCategory.COMPONENT_CATEGORY_EV_CHARGER
478460
)
@@ -496,9 +478,7 @@ async def test_ev_charger_data(self) -> None:
496478

497479
async def test_charge(self) -> None:
498480
"""Check if charge is able to charge component."""
499-
async with _gprc_server(57899) as servicer:
500-
microgrid = self.create_client(57899)
501-
481+
async with _gprc_server(57899) as (servicer, microgrid):
502482
servicer.add_component(
503483
83, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
504484
)
@@ -511,9 +491,7 @@ async def test_charge(self) -> None:
511491

512492
async def test_discharge(self) -> None:
513493
"""Check if discharge is able to discharge component."""
514-
async with _gprc_server(57899) as servicer:
515-
microgrid = self.create_client(57899)
516-
494+
async with _gprc_server(57899) as (servicer, microgrid):
517495
servicer.add_component(
518496
73, components_pb.ComponentCategory.COMPONENT_CATEGORY_METER
519497
)
@@ -526,9 +504,7 @@ async def test_discharge(self) -> None:
526504

527505
async def test_set_bounds(self) -> None:
528506
"""Check if set_bounds is able to set bounds for component."""
529-
async with _gprc_server(57899) as servicer:
530-
microgrid = self.create_client(57899)
531-
507+
async with _gprc_server(57899) as (servicer, microgrid):
532508
servicer.add_component(
533509
38, components_pb.ComponentCategory.COMPONENT_CATEGORY_INVERTER
534510
)

0 commit comments

Comments
 (0)