Skip to content

Commit ca3bb56

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

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
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: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@
5757
MODIFICATION_TIME_PB = timestamp_pb2.Timestamp(seconds=1672574400)
5858
ORDER = Order(
5959
delivery_area=DeliveryArea(code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC),
60-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
60+
delivery_period=DeliveryPeriod(
61+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
62+
),
6163
type=OrderType.LIMIT,
6264
side=MarketSide.BUY,
6365
price=Price(amount=Decimal("100.00"), currency=Currency.USD),
@@ -86,7 +88,7 @@
8688
side=MarketSide.BUY,
8789
execution_time=EXECUTION_TIME,
8890
delivery_area=DeliveryArea(code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC),
89-
delivery_period=DeliveryPeriod(START_TIME, duration=timedelta(minutes=15)),
91+
delivery_period=DeliveryPeriod(START_TIME, duration=DeliveryDuration.MINUTES_15),
9092
price=Price(amount=Decimal("100.00"), currency=Currency.USD),
9193
quantity=Power(mw=Decimal("5.00")),
9294
state=TradeState.ACTIVE,
@@ -147,7 +149,9 @@
147149
sell_delivery_area=DeliveryArea(
148150
code="ABC", code_type=EnergyMarketCodeType.EUROPE_EIC
149151
),
150-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
152+
delivery_period=DeliveryPeriod(
153+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
154+
),
151155
execution_time=EXECUTION_TIME,
152156
price=Price(amount=Decimal("100.00"), currency=Currency.USD),
153157
quantity=Power(mw=Decimal("5.00")),
@@ -178,7 +182,9 @@
178182
PUBLIC_ORDER = PublicOrder(
179183
public_order_id=42,
180184
delivery_area=DeliveryArea(code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC),
181-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
185+
delivery_period=DeliveryPeriod(
186+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
187+
),
182188
type=OrderType.LIMIT,
183189
side=MarketSide.BUY,
184190
price=Price(amount=Decimal("100.00"), currency=Currency.EUR),
@@ -210,7 +216,9 @@
210216
)
211217
PUBLIC_ORDER_BOOK_FILTER = PublicOrderBookFilter(
212218
delivery_area=DeliveryArea(code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC),
213-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
219+
delivery_period=DeliveryPeriod(
220+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
221+
),
214222
side=MarketSide.SELL,
215223
)
216224
PUBLIC_ORDER_BOOK_FILTER_PB = electricity_trading_pb2.PublicOrderBookFilter(
@@ -227,7 +235,9 @@
227235
GRIDPOOL_ORDER_FILTER = GridpoolOrderFilter(
228236
order_states=[OrderState.ACTIVE, OrderState.CANCELED],
229237
side=MarketSide.BUY,
230-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
238+
delivery_period=DeliveryPeriod(
239+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
240+
),
231241
delivery_area=DeliveryArea(code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC),
232242
tag="test",
233243
)
@@ -257,7 +267,9 @@
257267
code="XYZ", code_type=EnergyMarketCodeType.EUROPE_EIC
258268
),
259269
sell_delivery_area=None,
260-
delivery_period=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
270+
delivery_period=DeliveryPeriod(
271+
start=START_TIME, duration=DeliveryDuration.MINUTES_15
272+
),
261273
)
262274
PUBLIC_TRADE_FILTER_PB = electricity_trading_pb2.PublicTradeFilter(
263275
states=[
@@ -429,7 +441,7 @@ def test_delivery_duration_from_pb() -> None:
429441
def test_delivery_period_to_pb() -> None:
430442
"""Test the client delivery period type conversions to protobuf."""
431443
assert_conversion_to_pb(
432-
original=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
444+
original=DeliveryPeriod(start=START_TIME, duration=DeliveryDuration.MINUTES_15),
433445
expected_pb=delivery_duration_pb2.DeliveryPeriod(
434446
start=START_TIME_PB,
435447
duration=delivery_duration_pb2.DeliveryDuration.DELIVERY_DURATION_15,
@@ -445,30 +457,36 @@ def test_delivery_period_from_pb() -> None:
445457
start=START_TIME_PB,
446458
duration=delivery_duration_pb2.DeliveryDuration.DELIVERY_DURATION_15,
447459
),
448-
expected=DeliveryPeriod(start=START_TIME, duration=timedelta(minutes=15)),
460+
expected=DeliveryPeriod(start=START_TIME, duration=DeliveryDuration.MINUTES_15),
449461
assert_func=assert_equal,
450462
)
451463

452464

453465
def test_invalid_duration_raises_value_error() -> None:
454466
"""Test that an invalid duration raises a ValueError."""
455467
with pytest.raises(ValueError):
456-
DeliveryPeriod(start=datetime.now(timezone.utc), duration=timedelta(minutes=10))
468+
DeliveryPeriod(
469+
start=datetime.now(timezone.utc),
470+
duration=DeliveryDuration.from_timedelta(timedelta(minutes=10)),
471+
)
457472

458473

459474
def test_no_timezone_raises_value_error() -> None:
460475
"""Test that a datetime without timezone raises a ValueError."""
461476
with pytest.raises(ValueError):
462-
DeliveryPeriod(start=datetime.now(), duration=timedelta(minutes=5))
477+
DeliveryPeriod(
478+
start=datetime.now(),
479+
duration=DeliveryDuration.MINUTES_5,
480+
)
463481

464482

465-
def test_invalid_timezone_converted_to_utc() -> None:
483+
def test_invalid_timezone() -> None:
466484
"""Test that non-UTC timezones are converted to UTC."""
467-
start = datetime.now(timezone(timedelta(hours=1)))
468-
period = DeliveryPeriod(start=start, duration=timedelta(minutes=5))
469-
470-
assert period.start.tzinfo == timezone.utc
471-
assert start.hour == period.start.hour + 1
485+
with pytest.raises(ValueError):
486+
DeliveryPeriod(
487+
start=datetime.now(timezone(timedelta(hours=1))),
488+
duration=DeliveryDuration.MINUTES_5,
489+
)
472490

473491

474492
def test_delivery_area_to_pb() -> None:

0 commit comments

Comments
 (0)