Skip to content

Commit 139a7ba

Browse files
committed
Ensure price and quantity exist in non-cancelled orders
Cancelled orders may not have a price or quantity, all other orders must have these set. Signed-off-by: cwasicki <[email protected]>
1 parent 481d99d commit 139a7ba

File tree

1 file changed

+17
-1
lines changed
  • src/frequenz/client/electricity_trading

1 file changed

+17
-1
lines changed

src/frequenz/client/electricity_trading/_types.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1316,8 +1316,11 @@ def from_pb(cls, order_detail: electricity_trading_pb2.OrderDetail) -> Self:
13161316
13171317
Returns:
13181318
OrderDetail object corresponding to the protobuf message.
1319+
1320+
Raises:
1321+
ValueError: If the order price and quantity are not specified for a non-canceled order.
13191322
"""
1320-
return cls(
1323+
od = cls(
13211324
order_id=order_detail.order_id,
13221325
order=Order.from_pb(order_detail.order),
13231326
state_detail=StateDetail.from_pb(order_detail.state_detail),
@@ -1329,6 +1332,19 @@ def from_pb(cls, order_detail: electricity_trading_pb2.OrderDetail) -> Self:
13291332
),
13301333
)
13311334

1335+
# Only cancelled orders are allowed to have missing price or quantity
1336+
missing_price_or_quantity = (
1337+
od.order.price.amount.is_nan()
1338+
or od.order.price.currency == Currency.UNSPECIFIED
1339+
or od.order.quantity.mw.is_nan()
1340+
)
1341+
if missing_price_or_quantity and od.state_detail.state != OrderState.CANCELED:
1342+
raise ValueError(
1343+
f"Price and quantity must be specified for a non-canceled order (`{od}`)."
1344+
)
1345+
1346+
return od
1347+
13321348
def to_pb(self) -> electricity_trading_pb2.OrderDetail:
13331349
"""Convert an OrderDetail object to protobuf OrderDetail.
13341350

0 commit comments

Comments
 (0)