Skip to content

Commit 226466e

Browse files
committed
Fix that duration=0 was sent & received as None
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 2f62a10 commit 226466e

File tree

5 files changed

+18
-10
lines changed

5 files changed

+18
-10
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22

33
## Bug Fixes
44

5-
* Fix missing dependency in last release.
6-
* The `FakeClient.set_dispatches()` method now correctly updates `FakeService._last_id` which is used to generate unique dispatch IDs.
7-
* Fix that streams were globally shared between all clients.
8-
5+
* Fix that duration=0 was sent & received as None.

src/frequenz/client/dispatch/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def format_line(key: str, value: str, color: str = "cyan") -> str:
128128
lines.append(format_line("ID", str(dispatch.id)))
129129
lines.append(format_line("Type", str(dispatch.type)))
130130
lines.append(format_line("Start Time", format_datetime(dispatch.start_time)))
131-
if dispatch.duration:
131+
if dispatch.duration is not None:
132132
lines.append(format_line("Duration", str(dispatch.duration)))
133133
else:
134134
lines.append(format_line("Duration", "Infinite"))
@@ -407,7 +407,7 @@ async def create(
407407
kwargs = {k: v for k, v in kwargs.items() if v is not None}
408408

409409
# Required for client.create
410-
if not kwargs.get("duration"):
410+
if "duration" not in kwargs:
411411
kwargs["duration"] = None
412412

413413
dispatch = await ctx.obj["client"].create(

src/frequenz/client/dispatch/_internal_types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def to_protobuf(self) -> PBDispatchCreateRequest:
125125
else Timestamp()
126126
),
127127
duration=(
128-
round(self.duration.total_seconds()) if self.duration else None
128+
None
129+
if self.duration is None
130+
else round(self.duration.total_seconds())
129131
),
130132
target=_target_components_to_protobuf(self.target),
131133
is_active=self.active,

src/frequenz/client/dispatch/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ def from_protobuf(cls, pb_object: PBDispatch) -> "Dispatch":
292292
update_time=to_datetime(pb_object.metadata.modification_time),
293293
start_time=to_datetime(pb_object.data.start_time),
294294
duration=(
295-
timedelta(seconds=pb_object.data.duration)
296-
if pb_object.data.duration
297-
else None
295+
None
296+
if pb_object.data.duration is None
297+
else timedelta(seconds=pb_object.data.duration)
298298
),
299299
target=_target_components_from_protobuf(pb_object.data.target),
300300
active=pb_object.data.is_active,

tests/test_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ async def test_create_duration_none(client: FakeClient, sample: Dispatch) -> Non
7474
assert dispatch == sample
7575

7676

77+
async def test_create_duration_0(client: FakeClient, sample: Dispatch) -> None:
78+
"""Test creating a dispatch with a 0 duration."""
79+
microgrid_id = random.randint(1, 100)
80+
sample = replace(sample, duration=timedelta(minutes=0))
81+
dispatch = await client.create(**to_create_params(microgrid_id, sample))
82+
sample = _update_metadata(sample, dispatch)
83+
assert dispatch == sample
84+
85+
7786
async def test_list_dispatches(
7887
client: FakeClient, generator: DispatchGenerator
7988
) -> None:

0 commit comments

Comments
 (0)