Skip to content

Commit 96484de

Browse files
committed
Refactor: Refresh FakeService.last_id in dedicated function
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 10e3986 commit 96484de

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

src/frequenz/client/dispatch/test/_service.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def __init__(self) -> None:
7272
self._last_id: int = 0
7373
"""Last used dispatch id."""
7474

75+
def refresh_last_id_for(self, microgrid_id: MicrogridId) -> None:
76+
"""Update last id to be the next highest number."""
77+
dispatches = self.dispatches.get(microgrid_id, [])
78+
79+
if len(dispatches) == 0:
80+
return
81+
82+
self._last_id = max(self._last_id, max(dispatch.id for dispatch in dispatches))
83+
7584
def _check_access(self, metadata: grpc.aio.Metadata) -> None:
7685
"""Check if the access key is valid.
7786

src/frequenz/client/dispatch/test/client.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,7 @@ def set_dispatches(self, microgrid_id: int, value: list[Dispatch]) -> None:
5353
value: The list of dispatches to set.
5454
"""
5555
self._service.dispatches[microgrid_id] = value
56-
57-
if len(value) == 0:
58-
return
59-
60-
# Max between last id and the max id in the list
61-
# pylint: disable=protected-access
62-
self._service._last_id = max(
63-
self._service._last_id, max(dispatch.id for dispatch in value)
64-
)
65-
# pylint: enable=protected-access
56+
self._service.refresh_last_id_for(microgrid_id)
6657

6758
@property
6859
def _service(self) -> FakeService:

0 commit comments

Comments
 (0)