def test_cancel_all_requests_should_call_cancel_request_correctly(self): trader = UpbitTrader() dummy_request = { "uuid": "mango_uuid", "callback": MagicMock(), "result": { "request": { "id": "mango_request_1234", "type": "buy", "price": "888000", "amount": "0.0001234", }, "type": "buy", "price": "888000", "amount": "0.0001234", "msg": "success", }, } trader.order_map["mango_request_1234"] = dummy_request dummy_request2 = { "uuid": "mango_uuid2", "callback": MagicMock(), "result": { "request": { "id": "mango_request_5678", "type": "buy", "price": "888000", "amount": "0.0001234", }, "type": "buy", "price": "888000", "amount": "0.0001234", "msg": "success", }, } trader.order_map["mango_request_5678"] = dummy_request2 trader.cancel_request = MagicMock() trader.cancel_all_requests() trader.cancel_request.assert_called() self.assertEqual(trader.cancel_request.call_args_list[0][0][0], "mango_request_1234") self.assertEqual(trader.cancel_request.call_args_list[1][0][0], "mango_request_5678")
def test__execute_order_call_cancel_request_when_request_type_is_cancel( self): dummy_task = { "request": { "id": "apple", "price": 500, "amount": 0.0001, "type": "cancel" }, "callback": "kiwi", } trader = UpbitTrader() trader.cancel_request = MagicMock() trader._send_order = MagicMock() trader._create_success_result = MagicMock() trader._start_timer = MagicMock() trader._execute_order(dummy_task) trader._send_order.assert_not_called() trader._create_success_result.assert_not_called() trader._start_timer.assert_not_called() trader.cancel_request.assert_called_once_with("apple")
def test_cancel_request_should_remove_request_even_when_cancel_nothing( self): trader = UpbitTrader() dummy_request = { "uuid": "mango_uuid", "callback": MagicMock(), "result": { "request": { "id": "mango_request_1234", "type": "buy", "price": "888000", "amount": "0.0001234", }, "type": "buy", "price": "888000", "amount": "0.0001234", "msg": "success", }, } trader.order_map["mango_request_1234"] = dummy_request dummy_response = MagicMock() dummy_response.json.side_effect = ValueError() expected_query_string_delete = "uuid=mango_uuid".encode() expected_result = { "request": { "id": "mango_request_1234", "type": "buy", "price": "888000", "amount": "0.0001234", }, "type": "buy", "price": "887000", "amount": "0.0000034", "msg": "success", } self.delete_mock.return_value = dummy_response expected_query_string_get = "states[]=done&states[]=cancel&uuids[]=mango_uuid".encode( ) dummy_response_get = MagicMock() dummy_response_get.json.return_value = [] self.get_mock.return_value = dummy_response_get trader._create_jwt_token = MagicMock(return_value="mango_token") trader.cancel_request("mango_request_1234") dummy_response.raise_for_status.assert_called_once() dummy_response.json.assert_called_once() self.assertEqual(trader._create_jwt_token.call_args_list[0][0][0], trader.ACCESS_KEY) self.assertEqual(trader._create_jwt_token.call_args_list[0][0][1], trader.SECRET_KEY) self.assertEqual(trader._create_jwt_token.call_args_list[0][0][2], expected_query_string_delete) self.delete_mock.assert_called_once_with( trader.SERVER_URL + "/v1/order", params=expected_query_string_delete, headers={"Authorization": "Bearer mango_token"}, ) dummy_response_get.raise_for_status.assert_called_once() dummy_response_get.json.assert_called_once() self.assertEqual(trader._create_jwt_token.call_args_list[1][0][0], trader.ACCESS_KEY) self.assertEqual(trader._create_jwt_token.call_args_list[1][0][1], trader.SECRET_KEY) self.assertEqual(trader._create_jwt_token.call_args_list[1][0][2], expected_query_string_get) self.get_mock.assert_called_once_with( trader.SERVER_URL + "/v1/orders", params=expected_query_string_get, headers={"Authorization": "Bearer mango_token"}, ) dummy_request["callback"].assert_not_called() self.assertFalse("mango_request_1234" in trader.order_map)
def test_cancel_request_should_call__call_callback_when_cancel_order_return_info( self): trader = UpbitTrader() trader._call_callback = MagicMock() dummy_request = { "uuid": "mango_uuid", "callback": MagicMock(), "result": { "request": { "id": "mango_request_1234", "type": "buy", "price": "888000", "amount": "0.0001234", }, "type": "buy", "price": "888000", "amount": "0.0001234", "msg": "success", }, } trader.order_map["mango_request_1234"] = dummy_request dummy_response = MagicMock() dummy_response.json.return_value = { "created_at": "2018-04-10T15:42:23+09:00", "type": "buy", "price": "887000", "executed_volume": "0.0000034", "msg": "success", } expected_result = { "request": { "id": "mango_request_1234", "type": "buy", "price": "888000", "amount": "0.0001234", }, "type": "buy", "price": 887000.0, "amount": 0.0000034, "msg": "success", "date_time": "2018-04-10T15:42:23", "state": "done", } self.delete_mock.return_value = dummy_response trader._create_jwt_token = MagicMock(return_value="mango_token") expected_query_string = "uuid=mango_uuid".encode() trader.cancel_request("mango_request_1234") dummy_response.raise_for_status.assert_called_once() dummy_response.json.assert_called_once() trader._create_jwt_token.assert_called_once_with( trader.ACCESS_KEY, trader.SECRET_KEY, expected_query_string) self.delete_mock.assert_called_once_with( trader.SERVER_URL + "/v1/order", params=expected_query_string, headers={"Authorization": "Bearer mango_token"}, ) trader._call_callback.assert_called_once_with( dummy_request["callback"], expected_result) self.assertFalse("mango_request_1234" in trader.order_map)