Skip to content

Commit b7fff27

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

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ def __init__(self) -> None:
7373
self._last_id: DispatchId = DispatchId(0)
7474
"""Last used dispatch id."""
7575

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

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

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

6960
@property
7061
def _service(self) -> FakeService:

0 commit comments

Comments
 (0)