def test__create_jwt_token_should_return_correct_token_without_payload( self, mock_hash, mock_jwt, mock_uuid): trader = UpbitTrader() mock_uuid.return_value = "uuid_mango" trader._create_jwt_token("ak", "sk") mock_jwt.assert_called_once_with( { "access_key": "ak", "nonce": "uuid_mango" }, "sk")
def test__send_order_should_send_correct_limit_order_with_opt_mode_when_query_failed( self): trader = UpbitTrader() class DummyResponse: pass dummy_response = DummyResponse() dummy_response.raise_for_status = MagicMock() dummy_response.json = MagicMock(return_value="mango_response") self.post_mock.return_value = dummy_response self.get_mock.side_effect = requests.exceptions.RequestException() trader.is_opt_mode = True trader._create_jwt_token = MagicMock(return_value="mango_token") trader._create_limit_order_query = MagicMock( return_value="mango_query") response = trader._send_order("mango", True, 500, 0.555) self.assertEqual(response, "mango_response") dummy_response.raise_for_status.assert_called_once() dummy_response.json.assert_called_once() trader._create_limit_order_query.assert_called_once_with( "mango", True, 500, 0.555) trader._create_jwt_token.assert_called_once_with( trader.ACCESS_KEY, trader.SECRET_KEY, "mango_query") self.post_mock.assert_called_once_with( trader.SERVER_URL + "/v1/orders", params="mango_query", headers={"Authorization": "Bearer mango_token"}, )
def test__query_order_list_should_get_correctly_when_is_done_state_False( self, mock_requests): query_states = ["wait", "watch"] uuids = ["banana", "orange"] expected_query_string = ( "states[]=wait&states[]=watch&uuids[]=banana&uuids[]=orange". encode()) class DummyResponse: pass dummy_response = DummyResponse() dummy_response.raise_for_status = MagicMock() dummy_response.json = MagicMock(return_value="mango_response") mock_requests.return_value = dummy_response trader = UpbitTrader() trader._create_jwt_token = MagicMock(return_value="mango_token") response = trader._query_order_list(uuids, False) self.assertEqual(response, "mango_response") 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) mock_requests.assert_called_once_with( trader.SERVER_URL + "/v1/orders", params=expected_query_string, headers={"Authorization": "Bearer mango_token"}, )
def test__send_order_should_return_None_when_receive_invalid_data(self): trader = UpbitTrader() self.post_mock.side_effect = ValueError() trader._create_jwt_token = MagicMock(return_value="mango_token") trader._create_market_price_order_query = MagicMock( return_value="mango_query") response = trader._send_order("mango", False, volume=0.55) self.assertEqual(response, None)
def test__send_order_should_return_None_when_RequestException_occured( self): trader = UpbitTrader() self.post_mock.side_effect = requests.exceptions.RequestException() trader._create_jwt_token = MagicMock(return_value="mango_token") trader._create_market_price_order_query = MagicMock( return_value="mango_query") response = trader._send_order("mango", False, volume=0.55) self.assertEqual(response, None)
def test__create_jwt_token_should_return_correct_token( self, mock_hash, mock_jwt, mock_uuid): trader = UpbitTrader() mock_m = MagicMock() mock_hash.return_value = mock_m mock_uuid.return_value = "uuid_mango" mock_m.hexdigest.return_value = "hash_mango" trader._create_jwt_token("ak", "sk", "mango_query") mock_hash.assert_called_once() mock_m.update.assert_called_once_with("mango_query") mock_m.hexdigest.assert_called_once() mock_jwt.assert_called_once_with( { "access_key": "ak", "nonce": "uuid_mango", "query_hash": "hash_mango", "query_hash_alg": "SHA512", }, "sk", )
def test__send_order_should_send_correct_market_sell_order(self): trader = UpbitTrader() class DummyResponse: pass dummy_response = DummyResponse() dummy_response.raise_for_status = MagicMock() dummy_response.json = MagicMock(return_value="mango_response") self.post_mock.return_value = dummy_response trader._create_jwt_token = MagicMock(return_value="mango_token") trader._create_market_price_order_query = MagicMock( return_value="mango_query") response = trader._send_order("mango", False, volume=0.55) self.assertEqual(response, "mango_response") trader._create_market_price_order_query.assert_called_once_with( "mango", volume=0.55)
def test__query_account_should_send_correct_request(self, mock_requests): class DummyResponse: pass dummy_response = DummyResponse() dummy_response.raise_for_status = MagicMock() dummy_response.json = MagicMock(return_value="mango_response") mock_requests.return_value = dummy_response trader = UpbitTrader() trader._create_jwt_token = MagicMock(return_value="mango_token") response = trader._query_account() self.assertEqual(response, "mango_response") 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) mock_requests.assert_called_once_with( trader.SERVER_URL + "/v1/accounts", headers={"Authorization": "Bearer mango_token"}, )
def test__send_order_should_NOT_send_invaild_order(self): trader = UpbitTrader() class DummyResponse: pass dummy_response = DummyResponse() dummy_response.raise_for_status = MagicMock() dummy_response.json = MagicMock(return_value="mango_response") self.post_mock.return_value = dummy_response trader._create_jwt_token = MagicMock(return_value="mango_token") trader._create_market_price_order_query = MagicMock( return_value="mango_query") response = trader._send_order("mango", True, volume=0.55) self.assertEqual(response, None) response = trader._send_order("mango", False, price=500) self.assertEqual(response, None) response = trader._send_order("mango", True) self.assertEqual(response, None) trader._create_market_price_order_query.assert_not_called()
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)