Skip to content

Commit

Permalink
Write tests, remove on_cancel callback.
Browse files Browse the repository at this point in the history
  • Loading branch information
forkcs committed May 12, 2020
1 parent 5cc98e0 commit 15ea6b9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 27 deletions.
5 changes: 0 additions & 5 deletions supervisor/core/orders.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(self,
# DO NOT USE Supervisor.stop_cycle() into callbacks!!!
# It causes 100% deadlock
self._on_reject: Callable = None
self._on_cancel: Callable = None
self._on_fill: Callable = None

def __eq__(self, other):
Expand All @@ -47,10 +46,6 @@ def on_reject(self, *args, **kwargs) -> None:
if self._on_reject is not None:
self._on_reject(*args, **kwargs)

def on_cancel(self, *args, **kwargs) -> None:
if self._on_cancel is not None:
self._on_cancel(*args, **kwargs)

def on_fill(self, *args, **kwargs) -> None:
if self._on_fill is not None:
self._on_fill(*args, **kwargs)
Expand Down
83 changes: 81 additions & 2 deletions tests/integration/test_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
class SupervisorEntryTests(unittest.TestCase):

def setUp(self) -> None:
# self.exchange = Exchange(symbol=settings.TEST_SYMBOL, api_key=settings.TEST_API_KEY,
# api_secret=settings.TEST_API_SECRET, test=True)
self.exchange_mock = Mock()
self.exchange_mock.get_open_orders_ws.return_value = []
self.exchange_mock.get_position_size_ws.return_value = 0
Expand Down Expand Up @@ -148,3 +146,84 @@ def test_fb_negative_deviant_position_enter_while_running_cycle(self):
order = Order(order_type='Limit', qty=20, price=850, side='Buy', passive=True)
# assert that Supervisor change position only once with limit order
self.exchange_mock.place_order.assert_called_once_with(order)


class OrdersCallbacksTests(unittest.TestCase):
def setUp(self) -> None:
self.exchange_mock = Mock()
self.exchange_mock.get_open_orders_ws.return_value = []
self.exchange_mock.get_position_size_ws.return_value = 0
self.supervisor = Supervisor(interface=self.exchange_mock)

def tearDown(self) -> None:
self.supervisor.exit_cycle()

def test_filled_callback_will_be_called(self):

order = Order(order_type='Limit', side='Sell', qty=228, price=1000)
order.order_id = 123456

callback = Mock()
order._on_fill = callback
self.supervisor.add_order(order)

self.exchange_mock.get_order_status_ws.return_value = 'Filled'

self.supervisor.run_cycle()

sleep(1)

callback.assert_called_once()
self.assertListEqual([], self.supervisor._orders)

def test_rejected_callback_will_be_called(self):
order = Order(order_type='Limit', side='Sell', qty=228, price=1000)
order.order_id = 123456

callback = Mock()
order._on_reject = callback
self.supervisor.add_order(order)

self.exchange_mock.get_order_status_ws.return_value = 'Rejected'

self.supervisor.run_cycle()

sleep(1)

callback.assert_called_once()
self.assertListEqual([], self.supervisor._orders)

def test_several_filled_orders_at_the_same_time(self):
order_1 = Order(order_type='Limit', side='Sell', qty=228, price=1000)
order_1.order_id = 123456
order_2 = Order(order_type='Limit', side='Sell', qty=229, price=1001)
order_2.order_id = 123457
order_3 = Order(order_type='Limit', side='Sell', qty=2210, price=1002)
order_3.order_id = 123458
order_4 = Order(order_type='Limit', side='Sell', qty=2211, price=1003)
order_4.order_id = 123459

callback_1 = Mock()
callback_2 = Mock()
callback_3 = Mock()
callback_4 = Mock()
order_1._on_fill = callback_1
order_2._on_fill = callback_2
order_3._on_fill = callback_3
order_4._on_fill = callback_4
self.supervisor.add_order(order_1)
self.supervisor.add_order(order_2)
self.supervisor.add_order(order_3)
self.supervisor.add_order(order_4)

self.exchange_mock.get_order_status_ws.return_value = 'Filled'

self.supervisor.run_cycle()

sleep(1)

callback_1.assert_called_once()
callback_2.assert_called_once()
callback_3.assert_called_once()
callback_4.assert_called_once()
self.assertListEqual([], self.supervisor._orders)
20 changes: 0 additions & 20 deletions tests/unit/test_supervisor.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,6 @@ def test_check_several_unplaced_orders(self):

self.exchange_mock.bulk_place_orders.assert_called_once_with([order1, order2, order3])

def test_check_canceled_order(self):
def order_status_mock(_order):
if _order == order:
return 'Canceled'

on_cancel_mock = Mock()

self.exchange_mock.get_order_status_ws.side_effect = order_status_mock

order = Order(order_type='Limit', qty=228, price=1000, side='Buy')
order.order_id = '1234'
order._on_cancel = on_cancel_mock
self.supervisor.add_order(order)

self.supervisor.check_needed_orders()
# assert that Supervisor place order anew
self.exchange_mock.place_order.assert_called_once_with(order)
# assert that Supervisor call matching callback
on_cancel_mock.assert_called_once()

def test_check_rejected_order(self):
def order_status_mock(_order):
if _order == order:
Expand Down

0 comments on commit 15ea6b9

Please sign in to comment.