From 2db0711673f42109150d53edc275820f81c72805 Mon Sep 17 00:00:00 2001 From: "Mathias L. Baumann" Date: Wed, 17 Apr 2024 18:29:55 +0200 Subject: [PATCH] Disallow updating `type` and `dry_run` Signed-off-by: Mathias L. Baumann --- RELEASE_NOTES.md | 1 + src/frequenz/client/dispatch/_client.py | 14 ++++++++------ tests/test_dispatch_client.py | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index bc7c14c6..af911a2e 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -8,6 +8,7 @@ - The `Dispatch` class is now a frozen dataclass, meaning that it is immutable. Modifications can still be done using `replace`: `dispatch = replace(dispatch, start_time=new_start_time)`. - The `Client.update()` method now requires named parameters. +- `Client.update()` no longer accepts changes to the `type` and `dry_run` fields. ## New Features diff --git a/src/frequenz/client/dispatch/_client.py b/src/frequenz/client/dispatch/_client.py index 6758a5a6..bd6a242d 100644 --- a/src/frequenz/client/dispatch/_client.py +++ b/src/frequenz/client/dispatch/_client.py @@ -177,9 +177,14 @@ async def update( For recurrence fields, the keys are preceeded by "recurrence.". + Note that updating `type` and `dry_run` is not supported. + Args: dispatch_id: The dispatch_id to update. new_fields: The fields to update. + + Raises: + ValueError: If updating `type` or `dry_run`. """ msg = DispatchUpdateRequest(id=dispatch_id) @@ -188,7 +193,7 @@ async def update( match path[0]: case "type": - msg.update.type = val + raise ValueError("Updating type is not supported") case "start_time": msg.update.start_time.FromDatetime(val) case "duration": @@ -200,11 +205,8 @@ async def update( case "active": msg.update.is_active = val key = "is_active" - case "is_dry_run": - msg.update.is_dry_run = val - case "dry_run": - msg.update.is_dry_run = val - key = "is_dry_run" + case "is_dry_run" | "dry_run": + raise ValueError("Updating dry_run is not supported") case "recurrence": match path[1]: case "freq": diff --git a/tests/test_dispatch_client.py b/tests/test_dispatch_client.py index 845b0325..b8706493 100644 --- a/tests/test_dispatch_client.py +++ b/tests/test_dispatch_client.py @@ -103,6 +103,29 @@ async def test_update_dispatch() -> None: assert client.dispatches[0].recurrence.interval == 4 +async def test_update_dispatch_fail() -> None: + """Test updating the type and dry_run fields of a dispatch.""" + sampler = DispatchGenerator() + client = FakeClient() + sample = sampler.generate_dispatch() + + await client.create(**to_create_params(sample)) + dispatch = client.dispatches[0] + + assert dispatch is not None + + sample = _update(sample, dispatch) + assert dispatch == sample + + for field, value in [ + ("type", "new_type"), + ("dry_run", True), + ("is_dry_run", True), + ]: + with raises(ValueError): + await client.update(dispatch_id=dispatch.id, new_fields={field: value}) + + async def test_get_dispatch() -> None: """Test getting a dispatch.""" sampler = DispatchGenerator()