def test_start_timer_should_start_Timer(self, mock_timer): trader = BithumbTrader() trader.worker = MagicMock() trader._start_timer() mock_timer.assert_called_once_with(trader.RESULT_CHECKING_INTERVAL, ANY) callback = mock_timer.call_args[0][1] callback() trader.worker.post_task.assert_called_once_with( {"runnable": trader._update_order_result})
def test__execute_order_ignore_when_price_is_zero(self): dummy_task = { "request": { "id": "apple", "price": 0, "amount": 0.0001, "type": "sell" }, "callback": MagicMock(), } trader = BithumbTrader() trader._send_limit_order = MagicMock(return_value={ "status": "0000", "order_id": "apple_order_id" }) trader._create_success_result = MagicMock(return_value="banana") trader._start_timer = MagicMock() trader.cancel_request = MagicMock() trader._execute_order(dummy_task) dummy_task["callback"].assert_not_called() trader._send_limit_order.assert_not_called() trader._create_success_result.assert_not_called() trader._start_timer.assert_not_called()
def test__execute_order_call_callback_with_error_when_try_to_buy_over_balance( self): dummy_task = { "request": { "id": "apple", "price": 50000000, "amount": 0.01, "type": "buy" }, "callback": MagicMock(), } trader = BithumbTrader() trader._send_limit_order = MagicMock(return_value={ "status": "0000", "order_id": "apple_order_id" }) trader._create_success_result = MagicMock(return_value="banana") trader._start_timer = MagicMock() trader.cancel_request = MagicMock() trader._execute_order(dummy_task) dummy_task["callback"].assert_called_once_with("error!") trader._send_limit_order.assert_not_called() trader._create_success_result.assert_not_called() trader._start_timer.assert_not_called()
def test__execute_order_call__send_limit_order_correctly(self): dummy_task = { "request": { "id": "apple", "price": 500, "amount": 0.0001, "type": "buy" }, "callback": MagicMock(), } trader = BithumbTrader() trader._send_limit_order = MagicMock(return_value={ "status": "0000", "order_id": "apple_order_id" }) trader._create_success_result = MagicMock(return_value="banana") trader._start_timer = MagicMock() trader._execute_order(dummy_task) trader._send_limit_order.assert_called_once_with(True, 500, 0.0001) trader._create_success_result.assert_called_once_with( dummy_task["request"]) trader._start_timer.assert_called_once() self.assertEqual(trader.order_map["apple"]["order_id"], "apple_order_id") self.assertEqual(trader.order_map["apple"]["callback"], dummy_task["callback"]) self.assertEqual(trader.order_map["apple"]["result"], "banana") dummy_task["callback"].assert_called_once_with("banana")
def test__update_order_result_should_NOT_start_timer_when_no_request_remains( self): dummy_result = [ { "data": { "order_status": "Completed", "order_id": "mango", "state": "done", "transaction_date": "1572497603668315", "order_price": 500, "order_qty": 0.007, }, }, { "data": { "order_status": "Completed", "order_id": "apple", "state": "cancel", "transaction_date": "1572498603668315", "order_price": 1500, "order_qty": 0.54321, }, }, ] dummy_request_mango = { "order_id": "mango_order", "request": { "id": "mango_id" }, "callback": MagicMock(), "result": { "id": "mango_result", "state": "done", "type": "buy" }, } dummy_request_apple = { "order_id": "apple_order", "request": { "id": "apple_id" }, "callback": MagicMock(), "result": { "id": "apple_result", "state": "done", "type": "buy" }, } trader = BithumbTrader() trader._call_callback = MagicMock() trader._query_order = MagicMock(side_effect=dummy_result) trader._stop_timer = MagicMock() trader._start_timer = MagicMock() trader.order_map["mango"] = dummy_request_mango trader.order_map["apple"] = dummy_request_apple trader._update_order_result(None) self.assertEqual( trader._query_order.call_args_list, [call("mango_order"), call("apple_order")], ) self.assertEqual(len(trader.order_map), 0) trader._stop_timer.assert_called_once() trader._start_timer.assert_not_called()
def test__update_order_result_should_call__call_callback_and_keep_waiting_request( self): dummy_result = [ { "data": { "order_status": "Completed", "order_id": "mango", "state": "done", "transaction_date": "1572497603668315", "order_price": 500, "order_qty": 0.007, "contract": [{ "transaction_date": "1572497603668315", }], }, }, { "data": { "order_status": "Waiting", "order_id": "banana", "state": "cancel", "transaction_date": "1572498603668315", "order_price": 1500, "order_qty": 0.54321, "contract": [{ "transaction_date": "1572498603668315", }], }, }, { "data": { "order_status": "Completed", "order_id": "apple", "state": "cancel", "transaction_date": "1572498603668315", "order_price": 1500, "order_qty": 0.54321, "contract": [{ "transaction_date": "1572498603668315", }], }, }, ] dummy_request_mango = { "order_id": "mango_order", "request": { "id": "mango_id" }, "callback": MagicMock(), "result": { "state": "done", "type": "buy" }, } dummy_request_banana = { "order_id": "banana_order", "request": { "id": "banana_id" }, "callback": MagicMock(), "result": { "state": "done", "type": "sell" }, } dummy_request_apple = { "order_id": "apple_order", "request": { "id": "apple_id" }, "callback": MagicMock(), "result": { "state": "done", "type": "buy" }, } trader = BithumbTrader() trader._call_callback = MagicMock() trader._query_order = MagicMock(side_effect=dummy_result) trader._stop_timer = MagicMock() trader._start_timer = MagicMock() trader.order_map["mango"] = dummy_request_mango trader.order_map["banana"] = dummy_request_banana trader.order_map["apple"] = dummy_request_apple trader._update_order_result(None) self.assertEqual( trader._query_order.call_args_list, [call("mango_order"), call("banana_order"), call("apple_order")], ) trader._call_callback.assert_called() mango_result = trader._call_callback.call_args_list[0][0][1] self.assertEqual(mango_result["date_time"], "2019-10-31T13:53:23") self.assertEqual(mango_result["price"], 500) self.assertEqual(mango_result["type"], "buy") self.assertEqual(mango_result["state"], "done") self.assertEqual(mango_result["amount"], 0.007) self.assertEqual(trader._call_callback.call_args_list[0][0][0], dummy_request_mango["callback"]) apple_result = trader._call_callback.call_args_list[1][0][1] self.assertEqual(apple_result["date_time"], "2019-10-31T14:10:03") self.assertEqual(apple_result["price"], 1500) self.assertEqual(mango_result["type"], "buy") self.assertEqual(mango_result["state"], "done") self.assertEqual(apple_result["amount"], 0.54321) self.assertEqual(trader._call_callback.call_args_list[1][0][0], dummy_request_apple["callback"]) self.assertEqual(len(trader.order_map), 1) self.assertEqual(trader.order_map["banana"]["request"]["id"], "banana_id") trader._stop_timer.assert_called_once() trader._start_timer.assert_called_once()