Skip to content

Commit a148578

Browse files
committed
Add missing docstrings
Signed-off-by: Leandro Lucarella <[email protected]>
1 parent ea7dcf5 commit a148578

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

tests/api_client/test_api_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class FakeApiClient(ApiClient):
1414

1515
@classmethod
1616
def api_major_version(cls) -> int:
17+
"""Return the major version of the API supported by the client.
18+
19+
Returns:
20+
The major version of the API supported by the client.
21+
"""
1722
# Specifying the targeted API version here.
1823
return 1
1924

@@ -42,16 +47,20 @@ class FakeGrpcClient(FakeApiClient):
4247

4348
@classmethod
4449
def api_type(cls) -> ApiProtocol:
50+
"""Return the API type."""
4551
# Specifying the API protocol here as gRPC.
4652
return ApiProtocol.GRPC
4753

4854
async def connect(self, connection_params: str) -> None:
55+
"""Connect to the API."""
4956
self.is_connected = True
5057

5158
async def disconnect(self) -> None:
59+
"""Disconnect from the API."""
5260
self.is_connected = False
5361

5462
def get_data(self) -> str:
63+
"""Get data from the API."""
5564
return "grpc data"
5665

5766

@@ -62,16 +71,20 @@ class FakeRestClient(FakeApiClient):
6271

6372
@classmethod
6473
def api_type(cls) -> ApiProtocol:
74+
"""Return the API type."""
6575
# Same as `FakeGrpcClient`, but targeting REST protocol here.
6676
return ApiProtocol.REST
6777

6878
async def connect(self, connection_params: str) -> None:
79+
"""Connect to the API."""
6980
self.is_connected = True
7081

7182
async def disconnect(self) -> None:
83+
"""Disconnect from the API."""
7284
self.is_connected = False
7385

7486
def get_data(self) -> str:
87+
"""Get data from the API."""
7588
return "rest data"
7689

7790

tests/microgrid/test_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,19 @@
3434

3535

3636
class TestMicrogridGrpcClient:
37+
"""Tests for the microgrid client thin wrapper."""
38+
3739
@staticmethod
3840
def create_client(port: int) -> client.MicrogridApiClient:
41+
"""Create a client for the mock API server."""
3942
return client.MicrogridGrpcClient(
4043
grpc.aio.insecure_channel(f"[::]:{port}"),
4144
f"[::]:{port}",
4245
retry_spec=LinearBackoff(interval=0.0, jitter=0.05),
4346
)
4447

4548
async def test_components(self) -> None:
49+
"""Test the components() method."""
4650
servicer = mock_api.MockMicrogridServicer()
4751
server = mock_api.MockGrpcServer(servicer, port=57899)
4852
await server.start()
@@ -165,6 +169,7 @@ async def test_components(self) -> None:
165169
assert await server.graceful_shutdown()
166170

167171
async def test_connections(self) -> None:
172+
"""Test the connections() method."""
168173
servicer = mock_api.MockMicrogridServicer()
169174
server = mock_api.MockGrpcServer(servicer, port=57898)
170175
await server.start()
@@ -390,6 +395,7 @@ def ListAllComponents(
390395
assert await server.graceful_shutdown()
391396

392397
async def test_meter_data(self) -> None:
398+
"""Test the meter_data() method."""
393399
servicer = mock_api.MockMicrogridServicer()
394400
server = mock_api.MockGrpcServer(servicer, port=57899)
395401
await server.start()
@@ -422,6 +428,7 @@ async def test_meter_data(self) -> None:
422428
assert latest.component_id == 83
423429

424430
async def test_battery_data(self) -> None:
431+
"""Test the battery_data() method."""
425432
servicer = mock_api.MockMicrogridServicer()
426433
server = mock_api.MockGrpcServer(servicer, port=57899)
427434
await server.start()
@@ -454,6 +461,7 @@ async def test_battery_data(self) -> None:
454461
assert latest.component_id == 83
455462

456463
async def test_inverter_data(self) -> None:
464+
"""Test the inverter_data() method."""
457465
servicer = mock_api.MockMicrogridServicer()
458466
server = mock_api.MockGrpcServer(servicer, port=57899)
459467
await server.start()
@@ -486,6 +494,7 @@ async def test_inverter_data(self) -> None:
486494
assert latest.component_id == 83
487495

488496
async def test_ev_charger_data(self) -> None:
497+
"""Test the ev_charger_data() method."""
489498
servicer = mock_api.MockMicrogridServicer()
490499
server = mock_api.MockGrpcServer(servicer, port=57899)
491500
await server.start()
@@ -563,6 +572,7 @@ async def test_discharge(self) -> None:
563572
assert await server.graceful_shutdown()
564573

565574
async def test_set_bounds(self) -> None:
575+
"""Check if set_bounds is able to set bounds for component."""
566576
servicer = mock_api.MockMicrogridServicer()
567577
server = mock_api.MockGrpcServer(servicer, port=57899)
568578
await server.start()

tests/microgrid/test_graph.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class TestComponentGraph:
6464

6565
@pytest.fixture()
6666
def sample_input_components(self) -> Set[Component]:
67+
"""Create a sample set of components for testing purposes."""
6768
return {
6869
Component(11, ComponentCategory.GRID),
6970
Component(21, ComponentCategory.METER),
@@ -74,6 +75,7 @@ def sample_input_components(self) -> Set[Component]:
7475

7576
@pytest.fixture()
7677
def sample_input_connections(self) -> Set[Connection]:
78+
"""Create a sample set of connections for testing purposes."""
7779
return {
7880
Connection(11, 21),
7981
Connection(21, 41),
@@ -95,6 +97,7 @@ def sample_graph(
9597
return _graph_implementation
9698

9799
def test_without_filters(self) -> None:
100+
"""Test the graph component query without filters."""
98101
_graph_implementation = gr._MicrogridComponentGraph()
99102
graph: gr.ComponentGraph = _graph_implementation
100103

@@ -210,6 +213,7 @@ def test_without_filters(self) -> None:
210213
def test_filter_graph_components_by_id(
211214
self, sample_graph: gr.ComponentGraph, ids: Set[int], expected: Set[Component]
212215
) -> None:
216+
"""Test the graph component query with component ID filter."""
213217
# with component_id filter specified, we get back only components whose ID
214218
# matches one of the specified values
215219
assert len(sample_graph.components(component_id=ids)) == len(expected)
@@ -260,6 +264,7 @@ def test_filter_graph_components_by_type(
260264
types: Set[ComponentCategory],
261265
expected: Set[Component],
262266
) -> None:
267+
"""Test the graph component query with component category filter."""
263268
# with component_id filter specified, we get back only components whose ID
264269
# matches one of the specified values
265270
assert len(sample_graph.components(component_category=types)) == len(expected)
@@ -292,6 +297,7 @@ def test_filter_graph_components_with_composite_filter(
292297
types: Set[ComponentCategory],
293298
expected: Set[Component],
294299
) -> None:
300+
"""Test the graph component query with composite filter."""
295301
# when both filters are applied, they are combined via AND logic, i.e.
296302
# the component must have one of the specified IDs and be of one of
297303
# the specified types
@@ -306,11 +312,13 @@ def test_filter_graph_components_with_composite_filter(
306312
def test_components_without_filters(
307313
self, sample_input_components: Set[Component], sample_graph: gr.ComponentGraph
308314
) -> None:
315+
"""Test the graph component query without filters."""
309316
# without any filter applied, we get back all the components in the graph
310317
assert len(sample_graph.components()) == len(sample_input_components)
311318
assert sample_graph.components() == sample_input_components
312319

313320
def test_connection_filters(self) -> None:
321+
"""Test the graph connection query with filters."""
314322
_graph_implementation = gr._MicrogridComponentGraph(
315323
components={
316324
Component(1, ComponentCategory.GRID),
@@ -528,6 +536,7 @@ class Test_MicrogridComponentGraph:
528536
"""
529537

530538
def test___init__(self) -> None:
539+
"""Test the constructor."""
531540
# it is possible to instantiate an empty graph, but
532541
# it will not be considered valid until it has been
533542
# populated with components and connections
@@ -604,6 +613,7 @@ def test___init__(self) -> None:
604613
)
605614

606615
def test_refresh_from(self) -> None:
616+
"""Test the refresh_from method."""
607617
graph = gr._MicrogridComponentGraph()
608618
assert set(graph.components()) == set()
609619
assert list(graph.connections()) == []
@@ -765,6 +775,7 @@ def pretend_to_correct_errors(_g: gr._MicrogridComponentGraph) -> None:
765775
graph.validate()
766776

767777
async def test_refresh_from_api(self) -> None:
778+
"""Test the refresh_from_api method."""
768779
graph = gr._MicrogridComponentGraph()
769780
assert graph.components() == set()
770781
assert graph.connections() == set()
@@ -885,6 +896,7 @@ async def test_refresh_from_api(self) -> None:
885896
assert await server.graceful_shutdown()
886897

887898
def test_validate(self) -> None:
899+
"""Test the validate method."""
888900
# `validate` will fail if any of the following are the case:
889901
#
890902
# * the graph data is not valid
@@ -953,6 +965,7 @@ def test_validate(self) -> None:
953965
graph.validate()
954966

955967
def test__validate_graph(self) -> None:
968+
"""Test the _validate_graph method."""
956969
# to ensure clean testing of the individual method,
957970
# we cheat by setting underlying graph data directly
958971

@@ -1003,6 +1016,7 @@ def test__validate_graph(self) -> None:
10031016
graph._validate_graph()
10041017

10051018
def test__validate_graph_root(self) -> None:
1019+
"""Test the _validate_graph_root method."""
10061020
# to ensure clean testing of the individual method,
10071021
# we cheat by setting underlying graph data directly
10081022

@@ -1125,6 +1139,7 @@ def test__validate_graph_root(self) -> None:
11251139
graph._validate_graph_root()
11261140

11271141
def test__validate_grid_endpoint(self) -> None:
1142+
"""Test the _validate_grid_endpoint method."""
11281143
# to ensure clean testing of the individual method,
11291144
# we cheat by setting underlying graph data directly
11301145

@@ -1194,6 +1209,7 @@ def test__validate_grid_endpoint(self) -> None:
11941209
graph._validate_grid_endpoint()
11951210

11961211
def test__validate_intermediary_components(self) -> None:
1212+
"""Test the _validate_intermediary_components method."""
11971213
# to ensure clean testing of the individual method,
11981214
# we cheat by setting underlying graph data directly
11991215

@@ -1247,6 +1263,7 @@ def test__validate_intermediary_components(self) -> None:
12471263
graph._validate_intermediary_components()
12481264

12491265
def test__validate_leaf_components(self) -> None:
1266+
"""Test the _validate_leaf_components method."""
12501267
# to ensure clean testing of the individual method,
12511268
# we cheat by setting underlying graph data directly
12521269

@@ -1336,6 +1353,7 @@ def test__validate_leaf_components(self) -> None:
13361353
graph._validate_leaf_components()
13371354

13381355
def test_graph_correction(self) -> None:
1356+
"""Test the graph correction functionality."""
13391357
# Simple test cases for our built-in graph correction
13401358
# functionality. We test only with `refresh_from`:
13411359
# for `refresh_from_api` it suffices to test that any

tests/microgrid/test_mock_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424

2525
def test_MockMicrogridServicer() -> None:
26+
"""Test the MockMicrogridServicer."""
2627
api = mock_api.MockMicrogridServicer()
2728
service_context_mock = Mock(spec=grpc.ServicerContext)
2829
assert (
@@ -211,6 +212,7 @@ def test_MockMicrogridServicer() -> None:
211212

212213

213214
async def test_MockGrpcServer() -> None:
215+
"""Test the MockGrpcServer."""
214216
servicer1 = mock_api.MockMicrogridServicer(
215217
components=[
216218
(1, ComponentCategory.COMPONENT_CATEGORY_GRID),

0 commit comments

Comments
 (0)