Improve api response

This commit is contained in:
Matthias 2020-08-04 19:43:05 +02:00
parent b954af33cf
commit 9163c7f3d3
2 changed files with 18 additions and 3 deletions

View File

@ -431,6 +431,10 @@ class ApiServer(RPC):
def _trades_delete(self, tradeid): def _trades_delete(self, tradeid):
""" """
Handler for DELETE /trades/<tradeid> endpoint. Handler for DELETE /trades/<tradeid> endpoint.
Removes the trade from the database (tries to cancel open orders first!)
get:
param:
tradeid: Numeric trade-id assigned to the trade.
""" """
result = self._rpc_delete(tradeid) result = self._rpc_delete(tradeid)
return self.rest_dump(result) return self.rest_dump(result)

View File

@ -385,31 +385,42 @@ def test_api_trades(botclient, mocker, fee, markets):
def test_api_delete_trade(botclient, mocker, fee, markets): def test_api_delete_trade(botclient, mocker, fee, markets):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot, (True, False)) patch_get_signal(ftbot, (True, False))
stoploss_mock = MagicMock()
cancel_mock = MagicMock()
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
markets=PropertyMock(return_value=markets) markets=PropertyMock(return_value=markets),
cancel_order=cancel_mock,
cancel_stoploss_order=stoploss_mock,
) )
rc = client_delete(client, f"{BASE_URI}/trades/1") rc = client_delete(client, f"{BASE_URI}/trades/1")
# Error - trade won't exist yet. # Error - trade won't exist yet.
assert_response(rc, 502) assert_response(rc, 502)
create_mock_trades(fee) create_mock_trades(fee)
ftbot.strategy.order_types['stoploss_on_exchange'] = True
trades = Trade.query.all() trades = Trade.query.all()
trades[1].stoploss_order_id = '1234'
assert len(trades) > 2 assert len(trades) > 2
rc = client_delete(client, f"{BASE_URI}/trades/1") rc = client_delete(client, f"{BASE_URI}/trades/1")
assert_response(rc) assert_response(rc)
assert rc.json['result_msg'] == 'Deleted trade 1.' assert rc.json['result_msg'] == 'Deleted trade 1. Closed 1 open orders.'
assert len(trades) - 1 == len(Trade.query.all()) assert len(trades) - 1 == len(Trade.query.all())
assert cancel_mock.call_count == 1
cancel_mock.reset_mock()
rc = client_delete(client, f"{BASE_URI}/trades/1") rc = client_delete(client, f"{BASE_URI}/trades/1")
# Trade is gone now. # Trade is gone now.
assert_response(rc, 502) assert_response(rc, 502)
assert cancel_mock.call_count == 0
assert len(trades) - 1 == len(Trade.query.all()) assert len(trades) - 1 == len(Trade.query.all())
rc = client_delete(client, f"{BASE_URI}/trades/2") rc = client_delete(client, f"{BASE_URI}/trades/2")
assert_response(rc) assert_response(rc)
assert rc.json['result_msg'] == 'Deleted trade 2.' assert rc.json['result_msg'] == 'Deleted trade 2. Closed 2 open orders.'
assert len(trades) - 2 == len(Trade.query.all()) assert len(trades) - 2 == len(Trade.query.all())
assert stoploss_mock.call_count == 1
def test_api_edge_disabled(botclient, mocker, ticker, fee, markets): def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):