Skip to content

Commit

Permalink
Fix bug where recurrence is not properly unset when not specified
Browse files Browse the repository at this point in the history
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
  • Loading branch information
Marenz committed Aug 29, 2024
1 parent a221acf commit 5a6abd4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/frequenz/client/dispatch/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions src/frequenz/client/dispatch/_internal_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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

Expand Down
18 changes: 18 additions & 0 deletions tests/test_dispatch_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

0 comments on commit 5a6abd4

Please sign in to comment.