Remove further Magicmock trade

This commit is contained in:
Matthias 2023-03-28 19:38:43 +02:00
parent 513df4515b
commit 6282b42741

View File

@ -1103,7 +1103,8 @@ def test_add_stoploss_on_exchange(mocker, default_conf_usdt, limit_order, is_sho
@pytest.mark.parametrize("is_short", [False, True]) @pytest.mark.parametrize("is_short", [False, True])
def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_short, def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_short,
limit_order) -> None: limit_order) -> None:
stoploss = MagicMock(return_value={'id': 13434334}) stop_order_dict = {'id': "13434334"}
stoploss = MagicMock(return_value=stop_order_dict)
enter_order = limit_order[entry_side(is_short)] enter_order = limit_order[entry_side(is_short)]
exit_order = limit_order[exit_side(is_short)] exit_order = limit_order[exit_side(is_short)]
patch_RPCManager(mocker) patch_RPCManager(mocker)
@ -1128,8 +1129,9 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_
# First case: when stoploss is not yet set but the order is open # First case: when stoploss is not yet set but the order is open
# should get the stoploss order id immediately # should get the stoploss order id immediately
# and should return false as no trade actually happened # and should return false as no trade actually happened
# TODO: should not be magicmock
trade = MagicMock() freqtrade.enter_positions()
trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
trade.is_open = True trade.is_open = True
trade.open_order_id = None trade.open_order_id = None
@ -1141,31 +1143,34 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_
# Second case: when stoploss is set but it is not yet hit # Second case: when stoploss is set but it is not yet hit
# should do nothing and return false # should do nothing and return false
stop_order_dict.update({'id': "102"})
trade.is_open = True trade.is_open = True
trade.open_order_id = None trade.open_order_id = None
trade.stoploss_order_id = "100" trade.stoploss_order_id = "102"
hanging_stoploss_order = MagicMock(return_value={'status': 'open'}) hanging_stoploss_order = MagicMock(return_value={'status': 'open'})
mocker.patch(f'{EXMS}.fetch_stoploss_order', hanging_stoploss_order) mocker.patch(f'{EXMS}.fetch_stoploss_order', hanging_stoploss_order)
assert freqtrade.handle_stoploss_on_exchange(trade) is False assert freqtrade.handle_stoploss_on_exchange(trade) is False
assert trade.stoploss_order_id == "100" assert trade.stoploss_order_id == "102"
# Third case: when stoploss was set but it was canceled for some reason # Third case: when stoploss was set but it was canceled for some reason
# should set a stoploss immediately and return False # should set a stoploss immediately and return False
caplog.clear() caplog.clear()
trade.is_open = True trade.is_open = True
trade.open_order_id = None trade.open_order_id = None
trade.stoploss_order_id = "100" trade.stoploss_order_id = "103"
canceled_stoploss_order = MagicMock(return_value={'status': 'canceled'}) canceled_stoploss_order = MagicMock(return_value={'status': 'canceled'})
mocker.patch(f'{EXMS}.fetch_stoploss_order', canceled_stoploss_order) mocker.patch(f'{EXMS}.fetch_stoploss_order', canceled_stoploss_order)
stoploss.reset_mock() stoploss.reset_mock()
amount_before = trade.amount amount_before = trade.amount
stop_order_dict.update({'id': "103_1"})
assert freqtrade.handle_stoploss_on_exchange(trade) is False assert freqtrade.handle_stoploss_on_exchange(trade) is False
assert stoploss.call_count == 1 assert stoploss.call_count == 1
assert trade.stoploss_order_id == "13434334" assert trade.stoploss_order_id == "103_1"
assert trade.amount == amount_before assert trade.amount == amount_before
# Fourth case: when stoploss is set and it is hit # Fourth case: when stoploss is set and it is hit
@ -1173,14 +1178,16 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_
# as a trade actually happened # as a trade actually happened
caplog.clear() caplog.clear()
freqtrade.enter_positions() freqtrade.enter_positions()
stop_order_dict.update({'id': "104"})
trade = Trade.session.scalars(select(Trade)).first() trade = Trade.session.scalars(select(Trade)).first()
trade.is_short = is_short trade.is_short = is_short
trade.is_open = True trade.is_open = True
trade.open_order_id = None trade.open_order_id = None
trade.stoploss_order_id = "100" trade.stoploss_order_id = "104"
trade.orders.append(Order( trade.orders.append(Order(
ft_order_side='stoploss', ft_order_side='stoploss',
order_id='100', order_id='104',
ft_pair=trade.pair, ft_pair=trade.pair,
ft_is_open=True, ft_is_open=True,
ft_amount=trade.amount, ft_amount=trade.amount,
@ -1189,7 +1196,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_
assert trade assert trade
stoploss_order_hit = MagicMock(return_value={ stoploss_order_hit = MagicMock(return_value={
'id': "100", 'id': "104",
'status': 'closed', 'status': 'closed',
'type': 'stop_loss_limit', 'type': 'stop_loss_limit',
'price': 3, 'price': 3,
@ -1211,7 +1218,8 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_
# Fifth case: fetch_order returns InvalidOrder # Fifth case: fetch_order returns InvalidOrder
# It should try to add stoploss order # It should try to add stoploss order
trade.stoploss_order_id = 100 stop_order_dict.update({'id': "105"})
trade.stoploss_order_id = "105"
stoploss.reset_mock() stoploss.reset_mock()
mocker.patch(f'{EXMS}.fetch_stoploss_order', side_effect=InvalidOrderException()) mocker.patch(f'{EXMS}.fetch_stoploss_order', side_effect=InvalidOrderException())
mocker.patch(f'{EXMS}.create_stoploss', stoploss) mocker.patch(f'{EXMS}.create_stoploss', stoploss)
@ -1231,7 +1239,7 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_
# Seventh case: emergency exit triggered # Seventh case: emergency exit triggered
# Trailing stop should not act anymore # Trailing stop should not act anymore
stoploss_order_cancelled = MagicMock(side_effect=[{ stoploss_order_cancelled = MagicMock(side_effect=[{
'id': "100", 'id': "107",
'status': 'canceled', 'status': 'canceled',
'type': 'stop_loss_limit', 'type': 'stop_loss_limit',
'price': 3, 'price': 3,
@ -1239,13 +1247,14 @@ def test_handle_stoploss_on_exchange(mocker, default_conf_usdt, fee, caplog, is_
'amount': enter_order['amount'], 'amount': enter_order['amount'],
'info': {'stopPrice': 22}, 'info': {'stopPrice': 22},
}]) }])
trade.stoploss_order_id = "100" trade.stoploss_order_id = "107"
trade.is_open = True trade.is_open = True
trade.stoploss_last_update = arrow.utcnow().shift(hours=-1).datetime trade.stoploss_last_update = arrow.utcnow().shift(hours=-1).datetime
trade.stop_loss = 24 trade.stop_loss = 24
freqtrade.config['trailing_stop'] = True freqtrade.config['trailing_stop'] = True
stoploss = MagicMock(side_effect=InvalidOrderException()) stoploss = MagicMock(side_effect=InvalidOrderException())
Trade.commit()
mocker.patch(f'{EXMS}.cancel_stoploss_order_with_result', mocker.patch(f'{EXMS}.cancel_stoploss_order_with_result',
side_effect=InvalidOrderException()) side_effect=InvalidOrderException())
mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_cancelled) mocker.patch(f'{EXMS}.fetch_stoploss_order', stoploss_order_cancelled)