From 5a6abd4e11fbd263413d2486ee8de2fec462458a Mon Sep 17 00:00:00 2001 From: "Mathias L. Baumann" Date: Thu, 29 Aug 2024 14:12:27 +0200 Subject: [PATCH] Fix bug where recurrence is not properly unset when not specified Signed-off-by: Mathias L. Baumann --- src/frequenz/client/dispatch/_client.py | 2 +- .../client/dispatch/_internal_types.py | 7 +++++-- tests/test_dispatch_types.py | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/frequenz/client/dispatch/_client.py b/src/frequenz/client/dispatch/_client.py index 9068c76..579d3fc 100644 --- a/src/frequenz/client/dispatch/_client.py +++ b/src/frequenz/client/dispatch/_client.py @@ -221,7 +221,7 @@ async def create( active=active, dry_run=dry_run, payload=payload or {}, - recurrence=recurrence or RecurrenceRule(), + recurrence=recurrence, ) response = await cast( diff --git a/src/frequenz/client/dispatch/_internal_types.py b/src/frequenz/client/dispatch/_internal_types.py index b503fc0..566d2cd 100644 --- a/src/frequenz/client/dispatch/_internal_types.py +++ b/src/frequenz/client/dispatch/_internal_types.py @@ -61,7 +61,7 @@ class DispatchCreateRequest: It is structured as needed for the dispatch operation.""" - recurrence: RecurrenceRule + recurrence: RecurrenceRule | None """The recurrence rule for the dispatch. Defining any repeating patterns or schedules.""" @@ -110,7 +110,10 @@ def to_protobuf(self) -> PBDispatchCreateRequest: pb_request.dispatch_data.is_active = self.active pb_request.dispatch_data.is_dry_run = self.dry_run pb_request.dispatch_data.payload.update(self.payload) - pb_request.dispatch_data.recurrence.CopyFrom(self.recurrence.to_protobuf()) + if self.recurrence: + pb_request.dispatch_data.recurrence.CopyFrom(self.recurrence.to_protobuf()) + else: + pb_request.dispatch_data.ClearField("recurrence") return pb_request diff --git a/tests/test_dispatch_types.py b/tests/test_dispatch_types.py index c284e01..a5d4dcd 100644 --- a/tests/test_dispatch_types.py +++ b/tests/test_dispatch_types.py @@ -6,6 +6,7 @@ from datetime import datetime, timedelta, timezone from frequenz.client.common.microgrid.components import ComponentCategory +from frequenz.client.dispatch._internal_types import DispatchCreateRequest from frequenz.client.dispatch.types import ( Dispatch, EndCriteria, @@ -129,3 +130,20 @@ def test_dispatch() -> None: ), ): assert Dispatch.from_protobuf(dispatch.to_protobuf()) == dispatch + + +def test_dispatch_create_request_with_no_recurrence() -> None: + """Test the dispatch create request with no recurrence.""" + request = DispatchCreateRequest( + microgrid_id=123, + type="test", + start_time=datetime(2024, 10, 10, tzinfo=timezone.utc), + duration=timedelta(days=10), + selector=[1, 2, 3], + active=True, + dry_run=False, + payload={"key": "value"}, + recurrence=None, + ) + + assert request.to_protobuf().dispatch_data.HasField("recurrence") is False