Skip to content

Commit 4132e6c

Browse files
authored
fix: Raise ValueError for invalid start_time in create() (#264)
2 parents 3aee4e9 + e0ae50d commit 4132e6c

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

RELEASE_NOTES.md

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

33
## Summary
44

5-
<!-- Here goes a general summary of what this release is about -->
5+
Raise `ValueError` when `start_time` passed to `create()` is neither a `datetime` nor `"NOW"`.
66

77
## Upgrading
88

@@ -14,4 +14,4 @@
1414

1515
## Bug Fixes
1616

17-
<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
17+
- `DispatchApiClient.create()`: Passing an invalid `start_time` (not a `datetime` or `"NOW"`) previously silently created a dispatch with an epoch timestamp (1970-01-01). It now raises `ValueError` immediately.

src/frequenz/client/dispatch/_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
33

44
"""Dispatch API client for Python."""
5+
56
from __future__ import annotations
67

78
import warnings
@@ -344,6 +345,8 @@ async def create( # pylint: disable=too-many-positional-arguments
344345
or start_time.tzinfo.utcoffset(start_time) is None
345346
):
346347
raise ValueError("start_time must be timezone aware")
348+
elif start_time != "NOW":
349+
raise ValueError("start_time must be a datetime or 'NOW'")
347350

348351
request = DispatchCreateRequest(
349352
microgrid_id=microgrid_id,

tests/test_client.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import asyncio
77
import random
88
from dataclasses import replace
9-
from datetime import timedelta
9+
from datetime import datetime, timedelta, timezone
1010
from functools import partial
1111

1212
import grpc
@@ -471,3 +471,36 @@ async def expect(dispatch: Dispatch, event: Event) -> None:
471471

472472
# Expect the first dispatch deletion
473473
await expect(dispatches[0], Event.DELETED)
474+
475+
476+
async def test_create_invalid_start_time(client: FakeClient, sample: Dispatch) -> None:
477+
"""Test that create() raises ValueError for invalid start_time types."""
478+
microgrid_id = MicrogridId(random.randint(1, 100))
479+
params = to_create_params(microgrid_id, sample)
480+
481+
for invalid in [0, 1234567890, "2026-01-01T00:00:00", object()]:
482+
params["start_time"] = invalid
483+
with raises(ValueError):
484+
await client.create(**params)
485+
486+
487+
async def test_create_naive_datetime_raises(
488+
client: FakeClient, sample: Dispatch
489+
) -> None:
490+
"""Test that create() raises ValueError for a timezone-naive datetime."""
491+
microgrid_id = MicrogridId(random.randint(1, 100))
492+
params = to_create_params(microgrid_id, sample)
493+
params["start_time"] = datetime(2099, 1, 1, 0, 0, 0) # naive, no tzinfo
494+
with raises(ValueError):
495+
await client.create(**params)
496+
497+
498+
async def test_create_past_datetime_raises(
499+
client: FakeClient, sample: Dispatch
500+
) -> None:
501+
"""Test that create() raises ValueError for a start_time in the past."""
502+
microgrid_id = MicrogridId(random.randint(1, 100))
503+
params = to_create_params(microgrid_id, sample)
504+
params["start_time"] = datetime(2000, 1, 1, tzinfo=timezone.utc)
505+
with raises(ValueError):
506+
await client.create(**params)

0 commit comments

Comments
 (0)