fixed test handle_stoploss_on_exchange

This commit is contained in:
misagh 2018-11-24 19:12:00 +01:00
parent b5192880df
commit c8a0956e1b

View File

@ -908,10 +908,24 @@ def test_add_stoploss_on_exchange(mocker, default_conf, limit_buy_order) -> None
def test_handle_stoploss_on_exchange(mocker, default_conf, fee, def test_handle_stoploss_on_exchange(mocker, default_conf, fee,
markets, limit_buy_order, limit_sell_order) -> None: markets, limit_buy_order, limit_sell_order) -> None:
freqtrade = get_patched_freqtradebot(mocker, default_conf)
stoploss_limit = MagicMock(return_value={'id': 13434334}) stoploss_limit = MagicMock(return_value={'id': 13434334})
mocker.patch('freqtrade.exchange.Exchange.stoploss_limit', stoploss_limit) patch_RPCManager(mocker)
patch_exchange(mocker)
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_ticker=MagicMock(return_value={
'bid': 0.00001172,
'ask': 0.00001173,
'last': 0.00001172
}),
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
sell=MagicMock(return_value={'id': limit_sell_order['id']}),
get_fee=fee,
get_markets=markets,
stoploss_limit=stoploss_limit
)
freqtrade = FreqtradeBot(default_conf)
patch_get_signal(freqtrade)
# 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
@ -925,8 +939,6 @@ def test_handle_stoploss_on_exchange(mocker, default_conf, fee,
assert stoploss_limit.call_count == 1 assert stoploss_limit.call_count == 1
assert trade.stoploss_order_id == "13434334" assert trade.stoploss_order_id == "13434334"
trade.reset_mock()
# 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
trade.is_open = True trade.is_open = True
@ -939,47 +951,25 @@ def test_handle_stoploss_on_exchange(mocker, default_conf, fee,
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 == 100
trade.reset_mock()
# Third case: when stoploss is set and it is hit # Third case: when stoploss is set and it is hit
# should unset stoploss_order_id and return true # should unset stoploss_order_id and return true
# as a trade actually happened # as a trade actually happened
patch_RPCManager(mocker)
patch_exchange(mocker)
mocker.patch.multiple(
'freqtrade.exchange.Exchange',
get_ticker=MagicMock(return_value={
'bid': 0.00001172,
'ask': 0.00001173,
'last': 0.00001172
}),
buy=MagicMock(return_value={'id': limit_buy_order['id']}),
sell=MagicMock(return_value={'id': limit_sell_order['id']}),
get_fee=fee,
get_markets=markets
)
freqtrade = FreqtradeBot(default_conf)
patch_get_signal(freqtrade)
freqtrade.create_trade() freqtrade.create_trade()
trade = Trade.query.first() trade = Trade.query.first()
trade.is_open = True
trade.open_order_id = None
trade.stoploss_order_id = 100
assert trade assert trade
# trade = freqtrade.create stoploss_order_hit = MagicMock(return_value={
# trade.is_open = True 'status': 'closed',
# trade.open_order_id = None 'type': 'stop_loss_limit',
# trade.stoploss_order_id = 100 'price': 2
})
# stoploss_order_hit = MagicMock(return_value={'status': 'closed'}) mocker.patch('freqtrade.exchange.Exchange.get_order', stoploss_order_hit)
# mocker.patch('freqtrade.exchange.Exchange.get_order', stoploss_order_hit) assert freqtrade.handle_stoploss_on_exchange(trade) is True
assert trade.stoploss_order_id is None
# # trade = Trade.query.first() assert trade.is_open is False
# # assert trade
# assert freqtrade.handle_stoploss_on_exchange(trade) is True
# time.sleep(0.01) # Race condition fix
# assert trade.is_open is True
# assert trade.stoploss_order_id is None
def test_process_maybe_execute_buy(mocker, default_conf) -> None: def test_process_maybe_execute_buy(mocker, default_conf) -> None: