Repository: liampauling/flumine
Pull request #507 (2021-09-23 08:02:21)

#486 elapsed_seconds bug when async
bflw upgrade


Patch:
diff --git a/HISTORY.rst b/HISTORY.rst
index c4c4018..ca8f635 100644
--- a/HISTORY.rst
+++ b/HISTORY.rst
@@ -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)
 +++++++++++++++++++
 
diff --git a/flumine/__version__.py b/flumine/__version__.py
index c8a8322..d995a2c 100644
--- a/flumine/__version__.py
+++ b/flumine/__version__.py
@@ -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"
diff --git a/flumine/execution/baseexecution.py b/flumine/execution/baseexecution.py
index c1391cb..7b0a5d5 100644
--- a/flumine/execution/baseexecution.py
+++ b/flumine/execution/baseexecution.py
@@ -137,7 +137,8 @@ class BaseExecution:
             },
         )
         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))
diff --git a/flumine/order/process.py b/flumine/order/process.py
index 4fab944..e6a3280 100644
--- a/flumine/order/process.py
+++ b/flumine/order/process.py
@@ -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
@@ -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
diff --git a/flumine/order/responses.py b/flumine/order/responses.py
index e5d87e3..7170636 100644
--- a/flumine/order/responses.py
+++ b/flumine/order/responses.py
@@ -14,9 +14,11 @@ class Responses:
         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)
diff --git a/requirements.txt b/requirements.txt
index 89484a9..1af54e8 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,4 +1,4 @@
-betfairlightweight==2.14.0
+betfairlightweight==2.14.1
 tenacity==8.0.1
 python-json-logger==2.0.2
 requests


Test patch:
diff --git a/tests/test_execution.py b/tests/test_execution.py
index 4c6510d..3075589 100644
--- a/tests/test_execution.py
+++ b/tests/test_execution.py
@@ -156,23 +156,36 @@ class BaseExecutionTest(unittest.TestCase):
 
     @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):
diff --git a/tests/test_process.py b/tests/test_process.py
index 280767c..53e1108 100644
--- a/tests/test_process.py
+++ b/tests/test_process.py
@@ -67,6 +67,20 @@ class BaseOrderTest(unittest.TestCase):
         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()