Skip to content

Commit c8f931e

Browse files
committed
Use DeliveryDuration when constructing DeliveryPeriod in tests
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent bc1f343 commit c8f931e

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

tests/test_client.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
Client,
2020
Currency,
2121
DeliveryArea,
22+
DeliveryDuration,
2223
DeliveryPeriod,
2324
EnergyMarketCodeType,
2425
MarketActor,
@@ -80,7 +81,7 @@ def set_up() -> Generator[Any, Any, Any]:
8081
delivery_area = DeliveryArea(code="DE", code_type=EnergyMarketCodeType.EUROPE_EIC)
8182
delivery_period = DeliveryPeriod(
8283
start=delivery_start,
83-
duration=timedelta(minutes=15),
84+
duration=DeliveryDuration.MINUTES_15,
8485
)
8586
order_type = OrderType.LIMIT
8687
side = MarketSide.BUY
@@ -342,7 +343,7 @@ async def test_list_gridpool_orders(
342343
Power(mw=Decimal("0.1")),
343344
DeliveryPeriod(
344345
start=(datetime.now(timezone.utc) + timedelta(days=1)),
345-
duration=timedelta(hours=1),
346+
duration=DeliveryDuration.MINUTES_60,
346347
),
347348
None,
348349
OrderExecutionOption.AON, # Using AON here but valid_until is None
@@ -354,7 +355,7 @@ async def test_list_gridpool_orders(
354355
Power(mw=Decimal("0.1234")),
355356
DeliveryPeriod(
356357
start=(datetime.now(timezone.utc) + timedelta(days=1)),
357-
duration=timedelta(hours=1),
358+
duration=DeliveryDuration.MINUTES_60,
358359
),
359360
None,
360361
OrderExecutionOption.AON,
@@ -366,7 +367,7 @@ async def test_list_gridpool_orders(
366367
Power(mw=Decimal("0.1")),
367368
DeliveryPeriod(
368369
start=(datetime.now(timezone.utc) - timedelta(days=1)),
369-
duration=timedelta(hours=1),
370+
duration=DeliveryDuration.MINUTES_60,
370371
),
371372
None,
372373
OrderExecutionOption.AON,
@@ -378,7 +379,7 @@ async def test_list_gridpool_orders(
378379
Power(mw=Decimal("0.1")),
379380
DeliveryPeriod(
380381
start=(datetime.now(timezone.utc) + timedelta(days=1)),
381-
duration=timedelta(hours=1),
382+
duration=DeliveryDuration.MINUTES_60,
382383
),
383384
datetime.now(timezone.utc) - timedelta(hours=1),
384385
OrderExecutionOption.UNSPECIFIED, # Using an option that allows valid_until
@@ -390,7 +391,7 @@ async def test_list_gridpool_orders(
390391
Power(mw=Decimal("0.1")),
391392
DeliveryPeriod(
392393
start=(datetime.now(timezone.utc) + timedelta(days=1)),
393-
duration=timedelta(hours=1),
394+
duration=DeliveryDuration.MINUTES_60,
394395
),
395396
datetime.now(timezone.utc) + timedelta(days=1),
396397
OrderExecutionOption.AON, # Invalid case with AON and valid_until set

tests/test_types.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@
5858
MODIFICATION_TIME_PB = timestamp_pb2.Timestamp(seconds=1672574400)
5959
ORDER = Order(
6060
delivery_area=DeliveryArea(code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC),
61-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
61+
delivery_period=DeliveryPeriod(
62+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
63+
),
6264
type=OrderType.LIMIT,
6365
side=MarketSide.BUY,
6466
price=Price(amount=Decimal("100.00"), currency=Currency.USD),
@@ -87,7 +89,7 @@
8789
side=MarketSide.BUY,
8890
execution_time=EXECUTION_TIME,
8991
delivery_area=DeliveryArea(code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC),
90-
delivery_period=DeliveryPeriod(START_TIME, duration=timedelta(minutes=15)),
92+
delivery_period=DeliveryPeriod(START_TIME, duration=DeliveryDuration.MINUTES_15),
9193
price=Price(amount=Decimal("100.00"), currency=Currency.USD),
9294
quantity=Power(mw=Decimal("5.00")),
9395
state=TradeState.ACTIVE,
@@ -148,7 +150,9 @@
148150
sell_delivery_area=DeliveryArea(
149151
code="ABC", code_type=EnergyMarketCodeType.EUROPE_EIC
150152
),
151-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
153+
delivery_period=DeliveryPeriod(
154+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
155+
),
152156
execution_time=EXECUTION_TIME,
153157
price=Price(amount=Decimal("100.00"), currency=Currency.USD),
154158
quantity=Power(mw=Decimal("5.00")),
@@ -179,7 +183,9 @@
179183
PUBLIC_ORDER = PublicOrder(
180184
public_order_id=42,
181185
delivery_area=DeliveryArea(code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC),
182-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
186+
delivery_period=DeliveryPeriod(
187+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
188+
),
183189
type=OrderType.LIMIT,
184190
side=MarketSide.BUY,
185191
price=Price(amount=Decimal("100.00"), currency=Currency.EUR),
@@ -211,7 +217,9 @@
211217
)
212218
PUBLIC_ORDER_BOOK_FILTER = PublicOrderBookFilter(
213219
delivery_area=DeliveryArea(code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC),
214-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
220+
delivery_period=DeliveryPeriod(
221+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
222+
),
215223
side=MarketSide.SELL,
216224
)
217225
PUBLIC_ORDER_BOOK_FILTER_PB = electricity_trading_pb2.PublicOrderBookFilter(
@@ -266,7 +274,9 @@
266274
code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC
267275
),
268276
sell_delivery_area=None,
269-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
277+
delivery_period=DeliveryPeriod(
278+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
279+
),
270280
)
271281
PUBLIC_TRADE_FILTER_PB = electricity_trading_pb2.PublicTradeFilter(
272282
states=[
@@ -438,7 +448,7 @@ def test_delivery_duration_from_pb() -> None:
438448
def test_delivery_period_to_pb() -> None:
439449
"""Test the client delivery period type conversions to protobuf."""
440450
assert_conversion_to_pb(
441-
original=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
451+
original=DeliveryPeriod(start=START_TIME, duration=DeliveryDuration.MINUTES_15),
442452
expected_pb=delivery_duration_pb2.DeliveryPeriod(
443453
start=START_TIME_PB,
444454
duration=delivery_duration_pb2.DeliveryDuration.DELIVERY_DURATION_15,
@@ -454,30 +464,36 @@ def test_delivery_period_from_pb() -> None:
454464
start=START_TIME_PB,
455465
duration=delivery_duration_pb2.DeliveryDuration.DELIVERY_DURATION_15,
456466
),
457-
expected=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
467+
expected=DeliveryPeriod(start=START_TIME, duration=DeliveryDuration.MINUTES_15),
458468
assert_func=assert_equal,
459469
)
460470

461471

462472
def test_invalid_duration_raises_value_error() -> None:
463473
"""Test that an invalid duration raises a ValueError."""
464474
with pytest.raises(ValueError):
465-
DeliveryPeriod(start=datetime.now(timezone.utc), duration=timedelta(minutes=10))
475+
DeliveryPeriod(
476+
start=datetime.now(timezone.utc),
477+
duration=DeliveryDuration.from_timedelta(timedelta(minutes=10)),
478+
)
466479

467480

468481
def test_no_timezone_raises_value_error() -> None:
469482
"""Test that a datetime without timezone raises a ValueError."""
470483
with pytest.raises(ValueError):
471-
DeliveryPeriod(start=datetime.now(), duration=timedelta(minutes=5))
484+
DeliveryPeriod(
485+
start=datetime.now(),
486+
duration=DeliveryDuration.MINUTES_5,
487+
)
472488

473489

474-
def test_invalid_timezone_converted_to_utc() -> None:
490+
def test_invalid_timezone() -> None:
475491
"""Test that non-UTC timezones are converted to UTC."""
476-
start = datetime.now(timezone(timedelta(hours=1)))
477-
period = DeliveryPeriod(start=start, duration=timedelta(minutes=5))
478-
479-
assert period.start.tzinfo == timezone.utc
480-
assert start.hour == period.start.hour + 1
492+
with pytest.raises(ValueError):
493+
DeliveryPeriod(
494+
start=datetime.now(timezone(timedelta(hours=1))),
495+
duration=DeliveryDuration.MINUTES_5,
496+
)
481497

482498

483499
def test_delivery_area_to_pb() -> None:

0 commit comments

Comments
 (0)