def test_cancel_request_should_remove_request_even_when_cancel_nothing( self): trader = BithumbTrader() trader._call_callback = MagicMock() dummy_request = { "order_id": "mango_id", "callback": MagicMock(), "result": { "state": "requested", "request": { "id": "mango_request_1234", "type": "buy", "price": "888000", "amount": "0.0001234", }, "type": "buy", "price": 888000.0, "amount": 0.0001234, "msg": "success", }, } trader.order_map["mango_request_1234"] = dummy_request _cancel_order = MagicMock(return_value="done") self.assertTrue("mango_id" not in trader.order_map)
def test__call_callback_NOT_update_when_type_is_not_done(self): trader = BithumbTrader() trader.balance = 550000 trader.asset = (67000000, 1.000001) dummy_result = { "request": { "id": "mango_request_5678", "type": "buy", "price": "888000", "amount": "0.0001234", }, "type": "sell", "price": "62000000", "amount": "0.0012345", "msg": "success", "state": "requested", } dummy_callback = MagicMock() trader._call_callback(dummy_callback, dummy_result) self.assertEqual(trader.balance, 550000) self.assertEqual(trader.asset, (67000000, 1.000001)) dummy_callback.assert_called_once_with(dummy_result)
def test__call_callback_update_balance_correctly_when_sell(self): trader = BithumbTrader() trader.balance = 550000 trader.asset = (67000000, 1.000001) dummy_result = { "request": { "id": "mango_request_5678", "type": "buy", "price": "888000", "amount": "0.0001234", }, "type": "sell", "price": "62000000", "amount": "0.0012345", "msg": "success", "state": "done", } dummy_callback = MagicMock() trader._call_callback(dummy_callback, dummy_result) self.assertEqual(trader.balance, 626501) self.assertEqual(trader.asset, (67000000, 0.998766)) dummy_callback.assert_called_once_with(dummy_result)
def test_cancel_request_should_call__call_callback_when_order_is_traded_already( self): trader = BithumbTrader() trader._call_callback = MagicMock() dummy_request = { "order_id": "mango_id", "callback": MagicMock(), "result": { "state": "requested", "request": { "id": "mango_request_1234", "type": "buy", "price": "888000", "amount": "0.0001234", }, "type": "buy", "price": 888000.0, "amount": 0.0001234, "msg": "success", }, } trader.order_map["mango_request_1234"] = dummy_request _cancel_order = MagicMock(return_value=None) trader._query_order = MagicMock( return_value={ "data": { "order_status": "Completed", "order_id": "mango_id", "state": "done", "transaction_date": "1572497603668315", "order_price": 888000, "order_qty": 0.007, }, }) trader.cancel_request("mango_request_1234") trader._query_order.assert_called_once_with("mango_id") self.assertEqual(trader._call_callback.call_args_list[0][0][0], dummy_request["callback"]) 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"], 888000) self.assertEqual(mango_result["type"], "buy") self.assertEqual(mango_result["state"], "done") self.assertEqual(mango_result["amount"], 0.007)
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()