Skip to content

Commit

Permalink
#486 elapsed_seconds bug when async
Browse files Browse the repository at this point in the history
bflw upgrade
  • Loading branch information
liampauling committed Sep 23, 2021
1 parent 3674986 commit f1666a6
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 11 deletions.
11 changes: 11 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
Release History
---------------

1.20.3 (2021-09-23)
+++++++++++++++++++

**Bug Fixes**

- #486 elapsed_seconds bug when async

**Libraries**

- betfairlightweight upgraded to 2.14.1

1.20.2 (2021-09-20)
+++++++++++++++++++

Expand Down
2 changes: 1 addition & 1 deletion flumine/__version__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__title__ = "flumine"
__description__ = "Betfair trading framework"
__url__ = "https://github.com/liampauling/flumine"
__version__ = "1.20.2"
__version__ = "1.20.3"
__author__ = "Liam Pauling"
__license__ = "MIT"
3 changes: 2 additions & 1 deletion flumine/execution/baseexecution.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def _order_logger(
},
)
if package_type == OrderPackageType.PLACE:
order.responses.placed(instruction_report)
dt = False if order.async_ else True
order.responses.placed(instruction_report, dt=dt)
if instruction_report.bet_id:
order.bet_id = instruction_report.bet_id
self.flumine.log_control(OrderEvent(order))
Expand Down
2 changes: 1 addition & 1 deletion flumine/order/process.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from typing import Optional

from .. import config
from ..markets.markets import Markets
from ..order.order import BaseOrder, OrderStatus
from ..order.trade import Trade
Expand Down Expand Up @@ -75,6 +74,7 @@ def process_current_order(order: BaseOrder, current_order, log_control) -> None:
order.update_current_order(current_order)
# pickup async orders
if order.async_ and order.bet_id is None and current_order.bet_id:
order.responses.placed()
order.bet_id = current_order.bet_id
log_control(OrderEvent(order))
# update status
Expand Down
8 changes: 5 additions & 3 deletions flumine/order/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ def __init__(self):
self.update_responses = []
self._date_time_placed = None

def placed(self, response) -> None:
self.place_response = response
self._date_time_placed = datetime.datetime.utcnow()
def placed(self, response=None, dt: bool = True) -> None:
if response:
self.place_response = response
if dt:
self._date_time_placed = datetime.datetime.utcnow()

def cancelled(self, response) -> None:
self.cancel_responses.append(response)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
betfairlightweight==2.14.0
betfairlightweight==2.14.1
tenacity==8.0.1
python-json-logger==2.0.2
requests
21 changes: 17 additions & 4 deletions tests/test_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,36 @@ def test__return_http_session_err_close(self):

@mock.patch("flumine.execution.baseexecution.OrderEvent")
def test__order_logger_place(self, mock_order_event):
mock_order = mock.Mock()
mock_order = mock.Mock(async_=False)
mock_instruction_report = mock.Mock()
self.execution._order_logger(
mock_order, mock_instruction_report, OrderPackageType.PLACE
)
self.assertEqual(mock_order.bet_id, mock_instruction_report.bet_id)
mock_order.responses.placed.assert_called_with(mock_instruction_report)
mock_order.responses.placed.assert_called_with(mock_instruction_report, dt=True)
self.mock_flumine.log_control.assert_called_with(mock_order_event(mock_order))

@mock.patch("flumine.execution.baseexecution.OrderEvent")
def test__order_logger_place_async(self, mock_order_event):
mock_order = mock.Mock(async_=True)
mock_instruction_report = mock.Mock()
self.execution._order_logger(
mock_order, mock_instruction_report, OrderPackageType.PLACE
)
self.assertEqual(mock_order.bet_id, mock_instruction_report.bet_id)
mock_order.responses.placed.assert_called_with(
mock_instruction_report, dt=False
)
self.mock_flumine.log_control.assert_called_with(mock_order_event(mock_order))

def test__order_logger_place_no_bet_id(self):
mock_order = mock.Mock(bet_id=123)
mock_order = mock.Mock(bet_id=123, async_=False)
mock_instruction_report = mock.Mock(bet_id=None)
self.execution._order_logger(
mock_order, mock_instruction_report, OrderPackageType.PLACE
)
self.assertEqual(mock_order.bet_id, 123)
mock_order.responses.placed.assert_called_with(mock_instruction_report)
mock_order.responses.placed.assert_called_with(mock_instruction_report, dt=True)
self.mock_flumine.log_control.assert_not_called()

def test__order_logger_cancel(self):
Expand Down
14 changes: 14 additions & 0 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ def test_process_current_order(self):
mock_order.update_current_order.assert_called_with(mock_current_order)
mock_order.execution_complete.assert_called()

@mock.patch("flumine.order.process.OrderEvent")
def test_process_current_order_async(self, mock_order_event):
mock_order = mock.Mock(status=OrderStatus.EXECUTABLE, async_=True, bet_id=None)
mock_order.current_order.status = "EXECUTION_COMPLETE"
mock_current_order = mock.Mock(bet_id=1234)
mock_log_control = mock.Mock()
process.process_current_order(mock_order, mock_current_order, mock_log_control)
mock_order.update_current_order.assert_called_with(mock_current_order)
mock_order.execution_complete.assert_called()
self.assertEqual(mock_order.bet_id, 1234)
mock_order.responses.placed.assert_called_with()
mock_order_event.assert_called_with(mock_order)
mock_log_control.assert_called_with(mock_order_event())

def test_create_order_from_current(self):
mock_add_market = mock.Mock()
market_book = mock.Mock()
Expand Down

0 comments on commit f1666a6

Please sign in to comment.