Skip to content

Commit f945b1f

Browse files
committed
Add is_valid check to OrderDetail
Can be used by the user to check the validity of an order, which is currently defined as any order with a valid price and quantity, or in cancelled state. Signed-off-by: cwasicki <126617870+cwasicki@users.noreply.github.com>
1 parent 5e7247f commit f945b1f

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
## New Features
1212

13-
<!-- Here goes the main new features and examples or instructions on how to use them -->
13+
* Add check to validate order details.
1414

1515
## Bug Fixes
1616

src/frequenz/client/electricity_trading/_types.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,23 @@ def from_pb(cls, order_detail: electricity_trading_pb2.OrderDetail) -> Self:
13801380

13811381
return od
13821382

1383+
@property
1384+
def is_valid(self) -> bool:
1385+
"""Check if the order detail is valid.
1386+
1387+
Returns:
1388+
True if the order detail is valid, False otherwise.
1389+
"""
1390+
# Only cancelled orders are allowed to have missing price or quantity
1391+
if self.state_detail.state == OrderState.CANCELED:
1392+
return True
1393+
1394+
return not (
1395+
self.order.price.amount.is_nan()
1396+
or self.order.price.currency == Currency.UNSPECIFIED
1397+
or self.order.quantity.mw.is_nan()
1398+
)
1399+
13831400
def to_pb(self) -> electricity_trading_pb2.OrderDetail:
13841401
"""Convert an OrderDetail object to protobuf OrderDetail.
13851402

tests/test_types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,18 +580,26 @@ def test_order_detail_from_pb_missing_fields() -> None:
580580
# Missing price
581581
od_pb1 = electricity_trading_pb2.OrderDetail()
582582
od_pb1.CopyFrom(ORDER_DETAIL_PB)
583+
assert OrderDetail.from_pb(od_pb1).is_valid
583584
od_pb1.order.ClearField("price")
584585
OrderDetail.from_pb(od_pb1)
586+
# Not allowed for active orders
587+
assert not OrderDetail.from_pb(od_pb1).is_valid
585588
od_pb1.state_detail.state = electricity_trading_pb2.OrderState.ORDER_STATE_CANCELED
586589
OrderDetail.from_pb(od_pb1)
590+
# But allowed for canceled orders
591+
assert OrderDetail.from_pb(od_pb1).is_valid
587592

588593
# Missing quantity (same logic as above)
589594
od_pb2 = electricity_trading_pb2.OrderDetail()
590595
od_pb2.CopyFrom(ORDER_DETAIL_PB)
596+
assert OrderDetail.from_pb(od_pb2).is_valid
591597
od_pb2.order.ClearField("quantity")
592598
OrderDetail.from_pb(od_pb2)
599+
assert not OrderDetail.from_pb(od_pb2).is_valid
593600
od_pb2.state_detail.state = electricity_trading_pb2.OrderState.ORDER_STATE_CANCELED
594601
OrderDetail.from_pb(od_pb2)
602+
assert OrderDetail.from_pb(od_pb2).is_valid
595603

596604

597605
def test_order_detail_no_timezone_error() -> None:

0 commit comments

Comments
 (0)