Fixed a lot of failing tests"

This commit is contained in:
Sam Germain 2021-09-19 20:24:22 -06:00
parent d7c7448632
commit 043bfcd5ad
10 changed files with 155 additions and 116 deletions

View File

@ -871,7 +871,7 @@ def test_hyperopt_list(mocker, capsys, caplog, saved_hyperopt_results, tmpdir):
mocker.patch( mocker.patch(
'freqtrade.optimize.hyperopt_tools.HyperoptTools._test_hyperopt_results_exist', 'freqtrade.optimize.hyperopt_tools.HyperoptTools._test_hyperopt_results_exist',
return_value=True return_value=True
) )
def fake_iterator(*args, **kwargs): def fake_iterator(*args, **kwargs):
yield from [saved_hyperopt_results] yield from [saved_hyperopt_results]
@ -1277,9 +1277,10 @@ def test_start_list_data(testdatadir, capsys):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
# TODO-lev: Short trades?
def test_show_trades(mocker, fee, capsys, caplog): def test_show_trades(mocker, fee, capsys, caplog):
mocker.patch("freqtrade.persistence.init_db") mocker.patch("freqtrade.persistence.init_db")
create_mock_trades(fee) create_mock_trades(fee, False)
args = [ args = [
"show-trades", "show-trades",
"--db-url", "--db-url",

View File

@ -285,22 +285,22 @@ def create_mock_trades_with_leverage(fee, use_db: bool = True):
else: else:
LocalTrade.add_bt_trade(trade) LocalTrade.add_bt_trade(trade)
# Simulate dry_run entries # Simulate dry_run entries
trade = mock_trade_1(fee) trade = mock_trade_1(fee, False)
add_trade(trade) add_trade(trade)
trade = mock_trade_2(fee) trade = mock_trade_2(fee, False)
add_trade(trade) add_trade(trade)
trade = mock_trade_3(fee) trade = mock_trade_3(fee, False)
add_trade(trade) add_trade(trade)
trade = mock_trade_4(fee) trade = mock_trade_4(fee, False)
add_trade(trade) add_trade(trade)
trade = mock_trade_5(fee) trade = mock_trade_5(fee, False)
add_trade(trade) add_trade(trade)
trade = mock_trade_6(fee) trade = mock_trade_6(fee, False)
add_trade(trade) add_trade(trade)
trade = short_trade(fee) trade = short_trade(fee)

View File

@ -111,9 +111,10 @@ def test_load_backtest_data_multi(testdatadir):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
def test_load_trades_from_db(default_conf, fee, mocker): @pytest.mark.parametrize('is_short', [False, True])
def test_load_trades_from_db(default_conf, fee, is_short, mocker):
create_mock_trades(fee) create_mock_trades(fee, is_short)
# remove init so it does not init again # remove init so it does not init again
init_mock = mocker.patch('freqtrade.data.btanalysis.init_db', MagicMock()) init_mock = mocker.patch('freqtrade.data.btanalysis.init_db', MagicMock())

View File

@ -135,7 +135,7 @@ def test_init_ccxt_kwargs(default_conf, mocker, caplog):
assert ex._ccxt_config == {} assert ex._ccxt_config == {}
Exchange._headers = {} Exchange._headers = {}
# TODO-lev: Test with options # TODO-lev: Test with options in ccxt_config
def test_destroy(default_conf, mocker, caplog): def test_destroy(default_conf, mocker, caplog):
@ -420,21 +420,25 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
# With Leverage # With Leverage
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 5.0) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 5.0)
assert isclose(result, expected_result/5) assert isclose(result, expected_result/5)
# min amount and cost are set (cost is minimal)
markets["ETH/BTC"]["limits"] = {
'cost': {'min': 2},
'amount': {'min': 2} 'amount': {'min': 2}
} }
mocker.patch( mocker.patch(
'freqtrade.exchange.Exchange.markets', 'freqtrade.exchange.Exchange.markets',
PropertyMock(return_value=markets) PropertyMock(return_value=markets)
) )
result=exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
expected_result=max(2, 2 * 2) * (1+0.05) / (1-abs(stoploss)) expected_result = max(2, 2 * 2) * (1+0.05) / (1-abs(stoploss))
assert isclose(result, expected_result) assert isclose(result, expected_result)
# With Leverage # With Leverage
result=exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 10) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 10)
assert isclose(result, expected_result/10) assert isclose(result, expected_result/10)
# min amount and cost are set (amount is minial) # min amount and cost are set (amount is minial)
markets["ETH/BTC"]["limits"]={ markets["ETH/BTC"]["limits"] = {
'cost': {'min': 8}, 'cost': {'min': 8},
'amount': {'min': 2} 'amount': {'min': 2}
} }
@ -442,28 +446,36 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
'freqtrade.exchange.Exchange.markets', 'freqtrade.exchange.Exchange.markets',
PropertyMock(return_value=markets) PropertyMock(return_value=markets)
) )
result=exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss)
expected_result=max(8, 2 * 2) * (1+0.05) / (1-abs(stoploss)) expected_result = max(8, 2 * 2) * (1+0.05) / (1-abs(stoploss))
assert isclose(result, expected_result) assert isclose(result, expected_result)
# With Leverage # With Leverage
result=exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 7.0) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, stoploss, 7.0)
assert isclose(result, expected_result/7.0) assert isclose(result, expected_result/7.0)
result=exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4)
expected_result=max(8, 2 * 2) * 1.5 expected_result = max(8, 2 * 2) * 1.5
assert isclose(result, expected_result) assert isclose(result, expected_result)
# With Leverage # With Leverage
result=exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4, 8.0) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -0.4, 8.0)
assert isclose(result, expected_result/8.0) assert isclose(result, expected_result/8.0)
# Really big stoploss
result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1)
expected_result = max(8, 2 * 2) * 1.5
assert isclose(result, expected_result) assert isclose(result, expected_result)
# With Leverage # With Leverage
result=exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1, 12.0) result = exchange.get_min_pair_stake_amount('ETH/BTC', 2, -1, 12.0)
assert isclose(result, expected_result/12) assert isclose(result, expected_result/12)
stoploss=-0.05
markets={'ETH/BTC': {'symbol': 'ETH/BTC'}}
def test_get_min_pair_stake_amount_real_data(mocker, default_conf) -> None:
exchange = get_patched_exchange(mocker, default_conf, id="binance")
stoploss = -0.05
markets = {'ETH/BTC': {'symbol': 'ETH/BTC'}}
# Real Binance data # Real Binance data
markets["ETH/BTC"]["limits"]={ markets["ETH/BTC"]["limits"] = {
'cost': {'min': 0.0001}, 'cost': {'min': 0.0001},
'amount': {'min': 0.001} 'amount': {'min': 0.001}
} }
@ -471,10 +483,10 @@ def test_get_min_pair_stake_amount(mocker, default_conf) -> None:
'freqtrade.exchange.Exchange.markets', 'freqtrade.exchange.Exchange.markets',
PropertyMock(return_value=markets) PropertyMock(return_value=markets)
) )
result=exchange.get_min_pair_stake_amount('ETH/BTC', 0.020405, stoploss) result = exchange.get_min_pair_stake_amount('ETH/BTC', 0.020405, stoploss)
expected_result=max(0.0001, 0.001 * 0.020405) * (1+0.05) / (1-abs(stoploss)) expected_result = max(0.0001, 0.001 * 0.020405) * (1+0.05) / (1-abs(stoploss))
assert round(result, 8) == round(expected_result, 8) assert round(result, 8) == round(expected_result, 8)
result=exchange.get_min_pair_stake_amount('ETH/BTC', 0.020405, stoploss, 3.0) result = exchange.get_min_pair_stake_amount('ETH/BTC', 0.020405, stoploss, 3.0)
assert round(result, 8) == round(expected_result/3, 8) assert round(result, 8) == round(expected_result/3, 8)
@ -482,16 +494,16 @@ def test_set_sandbox(default_conf, mocker):
""" """
Test working scenario Test working scenario
""" """
api_mock=MagicMock() api_mock = MagicMock()
api_mock.load_markets=MagicMock(return_value = { api_mock.load_markets = MagicMock(return_value={
'ETH/BTC': '', 'LTC/BTC': '', 'XRP/BTC': '', 'NEO/BTC': '' 'ETH/BTC': '', 'LTC/BTC': '', 'XRP/BTC': '', 'NEO/BTC': ''
}) })
url_mock=PropertyMock(return_value = {'test': "api-public.sandbox.gdax.com", url_mock = PropertyMock(return_value={'test': "api-public.sandbox.gdax.com",
'api': 'https://api.gdax.com'}) 'api': 'https://api.gdax.com'})
type(api_mock).urls=url_mock type(api_mock).urls = url_mock
exchange=get_patched_exchange(mocker, default_conf, api_mock) exchange = get_patched_exchange(mocker, default_conf, api_mock)
liveurl=exchange._api.urls['api'] liveurl = exchange._api.urls['api']
default_conf['exchange']['sandbox']=True default_conf['exchange']['sandbox'] = True
exchange.set_sandbox(exchange._api, default_conf['exchange'], 'Logname') exchange.set_sandbox(exchange._api, default_conf['exchange'], 'Logname')
assert exchange._api.urls['api'] != liveurl assert exchange._api.urls['api'] != liveurl
@ -500,16 +512,16 @@ def test_set_sandbox_exception(default_conf, mocker):
""" """
Test Fail scenario Test Fail scenario
""" """
api_mock=MagicMock() api_mock = MagicMock()
api_mock.load_markets=MagicMock(return_value = { api_mock.load_markets = MagicMock(return_value={
'ETH/BTC': '', 'LTC/BTC': '', 'XRP/BTC': '', 'NEO/BTC': '' 'ETH/BTC': '', 'LTC/BTC': '', 'XRP/BTC': '', 'NEO/BTC': ''
}) })
url_mock=PropertyMock(return_value = {'api': 'https://api.gdax.com'}) url_mock = PropertyMock(return_value={'api': 'https://api.gdax.com'})
type(api_mock).urls=url_mock type(api_mock).urls = url_mock
with pytest.raises(OperationalException, match = r'does not provide a sandbox api'): with pytest.raises(OperationalException, match=r'does not provide a sandbox api'):
exchange=get_patched_exchange(mocker, default_conf, api_mock) exchange = get_patched_exchange(mocker, default_conf, api_mock)
default_conf['exchange']['sandbox']=True default_conf['exchange']['sandbox'] = True
exchange.set_sandbox(exchange._api, default_conf['exchange'], 'Logname') exchange.set_sandbox(exchange._api, default_conf['exchange'], 'Logname')
@ -519,13 +531,13 @@ def test__load_async_markets(default_conf, mocker, caplog):
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') mocker.patch('freqtrade.exchange.Exchange.validate_timeframes')
mocker.patch('freqtrade.exchange.Exchange._load_markets') mocker.patch('freqtrade.exchange.Exchange._load_markets')
mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency') mocker.patch('freqtrade.exchange.Exchange.validate_stakecurrency')
exchange=Exchange(default_conf) exchange = Exchange(default_conf)
exchange._api_async.load_markets=get_mock_coro(None) exchange._api_async.load_markets = get_mock_coro(None)
exchange._load_async_markets() exchange._load_async_markets()
assert exchange._api_async.load_markets.call_count == 1 assert exchange._api_async.load_markets.call_count == 1
caplog.set_level(logging.DEBUG) caplog.set_level(logging.DEBUG)
exchange._api_async.load_markets=Mock(side_effect = ccxt.BaseError("deadbeef")) exchange._api_async.load_markets = Mock(side_effect=ccxt.BaseError("deadbeef"))
exchange._load_async_markets() exchange._load_async_markets()
assert log_has('Could not load async markets. Reason: deadbeef', caplog) assert log_has('Could not load async markets. Reason: deadbeef', caplog)
@ -533,8 +545,8 @@ def test__load_async_markets(default_conf, mocker, caplog):
def test__load_markets(default_conf, mocker, caplog): def test__load_markets(default_conf, mocker, caplog):
caplog.set_level(logging.INFO) caplog.set_level(logging.INFO)
api_mock=MagicMock() api_mock = MagicMock()
api_mock.load_markets=MagicMock(side_effect = ccxt.BaseError("SomeError")) api_mock.load_markets = MagicMock(side_effect=ccxt.BaseError("SomeError"))
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
mocker.patch('freqtrade.exchange.Exchange.validate_pairs') mocker.patch('freqtrade.exchange.Exchange.validate_pairs')
mocker.patch('freqtrade.exchange.Exchange.validate_timeframes') mocker.patch('freqtrade.exchange.Exchange.validate_timeframes')
@ -543,28 +555,28 @@ def test__load_markets(default_conf, mocker, caplog):
Exchange(default_conf) Exchange(default_conf)
assert log_has('Unable to initialize markets.', caplog) assert log_has('Unable to initialize markets.', caplog)
expected_return={'ETH/BTC': 'available'} expected_return = {'ETH/BTC': 'available'}
api_mock=MagicMock() api_mock = MagicMock()
api_mock.load_markets=MagicMock(return_value = expected_return) api_mock.load_markets = MagicMock(return_value=expected_return)
mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock)) mocker.patch('freqtrade.exchange.Exchange._init_ccxt', MagicMock(return_value=api_mock))
default_conf['exchange']['pair_whitelist']=['ETH/BTC'] default_conf['exchange']['pair_whitelist'] = ['ETH/BTC']
ex=Exchange(default_conf) ex = Exchange(default_conf)
assert ex.markets == expected_return assert ex.markets == expected_return
def test_reload_markets(default_conf, mocker, caplog): def test_reload_markets(default_conf, mocker, caplog):
caplog.set_level(logging.DEBUG) caplog.set_level(logging.DEBUG)
initial_markets={'ETH/BTC': {}} initial_markets = {'ETH/BTC': {}}
updated_markets={'ETH/BTC': {}, "LTC/BTC": {}} updated_markets = {'ETH/BTC': {}, "LTC/BTC": {}}
api_mock=MagicMock() api_mock = MagicMock()
api_mock.load_markets=MagicMock(return_value = initial_markets) api_mock.load_markets = MagicMock(return_value=initial_markets)
default_conf['exchange']['markets_refresh_interval']=10 default_conf['exchange']['markets_refresh_interval'] = 10
exchange=get_patched_exchange(mocker, default_conf, api_mock, id = "binance", exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance",
mock_markets = False) mock_markets=False)
exchange._load_async_markets=MagicMock() exchange._load_async_markets = MagicMock()
exchange._last_markets_refresh=arrow.utcnow().int_timestamp exchange._last_markets_refresh = arrow.utcnow().int_timestamp
assert exchange.markets == initial_markets assert exchange.markets == initial_markets
@ -573,9 +585,9 @@ def test_reload_markets(default_conf, mocker, caplog):
assert exchange.markets == initial_markets assert exchange.markets == initial_markets
assert exchange._load_async_markets.call_count == 0 assert exchange._load_async_markets.call_count == 0
api_mock.load_markets=MagicMock(return_value = updated_markets) api_mock.load_markets = MagicMock(return_value=updated_markets)
# more than 10 minutes have passed, reload is executed # more than 10 minutes have passed, reload is executed
exchange._last_markets_refresh=arrow.utcnow().int_timestamp - 15 * 60 exchange._last_markets_refresh = arrow.utcnow().int_timestamp - 15 * 60
exchange.reload_markets() exchange.reload_markets()
assert exchange.markets == updated_markets assert exchange.markets == updated_markets
assert exchange._load_async_markets.call_count == 1 assert exchange._load_async_markets.call_count == 1
@ -585,10 +597,10 @@ def test_reload_markets(default_conf, mocker, caplog):
def test_reload_markets_exception(default_conf, mocker, caplog): def test_reload_markets_exception(default_conf, mocker, caplog):
caplog.set_level(logging.DEBUG) caplog.set_level(logging.DEBUG)
api_mock=MagicMock() api_mock = MagicMock()
api_mock.load_markets=MagicMock(side_effect = ccxt.NetworkError("LoadError")) api_mock.load_markets = MagicMock(side_effect=ccxt.NetworkError("LoadError"))
default_conf['exchange']['markets_refresh_interval']=10 default_conf['exchange']['markets_refresh_interval'] = 10
exchange=get_patched_exchange(mocker, default_conf, api_mock, id = "binance") exchange = get_patched_exchange(mocker, default_conf, api_mock, id="binance")
# less than 10 minutes have passed, no reload # less than 10 minutes have passed, no reload
exchange.reload_markets() exchange.reload_markets()
@ -596,11 +608,11 @@ def test_reload_markets_exception(default_conf, mocker, caplog):
assert log_has_re(r"Could not reload markets.*", caplog) assert log_has_re(r"Could not reload markets.*", caplog)
@ pytest.mark.parametrize("stake_currency", ['ETH', 'BTC', 'USDT']) @pytest.mark.parametrize("stake_currency", ['ETH', 'BTC', 'USDT'])
def test_validate_stakecurrency(default_conf, stake_currency, mocker, caplog): def test_validate_stakecurrency(default_conf, stake_currency, mocker, caplog):
default_conf['stake_currency']=stake_currency default_conf['stake_currency'] = stake_currency
api_mock=MagicMock() api_mock = MagicMock()
type(api_mock).load_markets=MagicMock(return_value = { type(api_mock).load_markets = MagicMock(return_value={
'ETH/BTC': {'quote': 'BTC'}, 'LTC/BTC': {'quote': 'BTC'}, 'ETH/BTC': {'quote': 'BTC'}, 'LTC/BTC': {'quote': 'BTC'},
'XRP/ETH': {'quote': 'ETH'}, 'NEO/USDT': {'quote': 'USDT'}, 'XRP/ETH': {'quote': 'ETH'}, 'NEO/USDT': {'quote': 'USDT'},
}) })
@ -612,9 +624,9 @@ def test_validate_stakecurrency(default_conf, stake_currency, mocker, caplog):
def test_validate_stakecurrency_error(default_conf, mocker, caplog): def test_validate_stakecurrency_error(default_conf, mocker, caplog):
default_conf['stake_currency']='XRP' default_conf['stake_currency'] = 'XRP'
api_mock=MagicMock() api_mock = MagicMock()
type(api_mock).load_markets=MagicMock(return_value = { type(api_mock).load_markets = MagicMock(return_value={
'ETH/BTC': {'quote': 'BTC'}, 'LTC/BTC': {'quote': 'BTC'}, 'ETH/BTC': {'quote': 'BTC'}, 'LTC/BTC': {'quote': 'BTC'},
'XRP/ETH': {'quote': 'ETH'}, 'NEO/USDT': {'quote': 'USDT'}, 'XRP/ETH': {'quote': 'ETH'}, 'NEO/USDT': {'quote': 'USDT'},
}) })

View File

@ -665,6 +665,7 @@ def test_PerformanceFilter_error(mocker, whitelist_conf, caplog) -> None:
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
# TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
def test_PerformanceFilter_lookback(mocker, whitelist_conf, fee) -> None: def test_PerformanceFilter_lookback(mocker, whitelist_conf, fee) -> None:
whitelist_conf['exchange']['pair_whitelist'].append('XRP/BTC') whitelist_conf['exchange']['pair_whitelist'].append('XRP/BTC')
whitelist_conf['pairlists'] = [ whitelist_conf['pairlists'] = [
@ -679,7 +680,7 @@ def test_PerformanceFilter_lookback(mocker, whitelist_conf, fee) -> None:
assert pm.whitelist == ['ETH/BTC', 'TKN/BTC', 'XRP/BTC'] assert pm.whitelist == ['ETH/BTC', 'TKN/BTC', 'XRP/BTC']
with time_machine.travel("2021-09-01 05:00:00 +00:00") as t: with time_machine.travel("2021-09-01 05:00:00 +00:00") as t:
create_mock_trades(fee) create_mock_trades(fee, False)
pm.refresh_pairlist() pm.refresh_pairlist()
assert pm.whitelist == ['XRP/BTC', 'ETH/BTC', 'TKN/BTC'] assert pm.whitelist == ['XRP/BTC', 'ETH/BTC', 'TKN/BTC']

View File

@ -285,7 +285,8 @@ def test_rpc_daily_profit(default_conf, update, ticker, fee,
rpc._rpc_daily_profit(0, stake_currency, fiat_display_currency) rpc._rpc_daily_profit(0, stake_currency, fiat_display_currency)
def test_rpc_trade_history(mocker, default_conf, markets, fee): @pytest.mark.parametrize('is_short', [True, False])
def test_rpc_trade_history(mocker, default_conf, markets, fee, is_short):
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
@ -293,7 +294,7 @@ def test_rpc_trade_history(mocker, default_conf, markets, fee):
) )
freqtradebot = get_patched_freqtradebot(mocker, default_conf) freqtradebot = get_patched_freqtradebot(mocker, default_conf)
create_mock_trades(fee) create_mock_trades(fee, is_short)
rpc = RPC(freqtradebot) rpc = RPC(freqtradebot)
rpc._fiat_converter = CryptoToFiatConverter() rpc._fiat_converter = CryptoToFiatConverter()
trades = rpc._rpc_trade_history(2) trades = rpc._rpc_trade_history(2)
@ -310,7 +311,8 @@ def test_rpc_trade_history(mocker, default_conf, markets, fee):
assert trades['trades'][0]['pair'] == 'XRP/BTC' assert trades['trades'][0]['pair'] == 'XRP/BTC'
def test_rpc_delete_trade(mocker, default_conf, fee, markets, caplog): @pytest.mark.parametrize('is_short', [True, False])
def test_rpc_delete_trade(mocker, default_conf, fee, markets, caplog, is_short):
mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock()) mocker.patch('freqtrade.rpc.telegram.Telegram', MagicMock())
stoploss_mock = MagicMock() stoploss_mock = MagicMock()
cancel_mock = MagicMock() cancel_mock = MagicMock()
@ -323,7 +325,7 @@ def test_rpc_delete_trade(mocker, default_conf, fee, markets, caplog):
freqtradebot = get_patched_freqtradebot(mocker, default_conf) freqtradebot = get_patched_freqtradebot(mocker, default_conf)
freqtradebot.strategy.order_types['stoploss_on_exchange'] = True freqtradebot.strategy.order_types['stoploss_on_exchange'] = True
create_mock_trades(fee) create_mock_trades(fee, is_short)
rpc = RPC(freqtradebot) rpc = RPC(freqtradebot)
with pytest.raises(RPCException, match='invalid argument'): with pytest.raises(RPCException, match='invalid argument'):
rpc._rpc_delete('200') rpc._rpc_delete('200')

View File

@ -451,7 +451,8 @@ def test_api_balance(botclient, mocker, rpc_balance, tickers):
assert 'starting_capital_ratio' in response assert 'starting_capital_ratio' in response
def test_api_count(botclient, mocker, ticker, fee, markets): @pytest.mark.parametrize('is_short', [True, False])
def test_api_count(botclient, mocker, ticker, fee, markets, is_short):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot) patch_get_signal(ftbot)
mocker.patch.multiple( mocker.patch.multiple(
@ -468,7 +469,7 @@ def test_api_count(botclient, mocker, ticker, fee, markets):
assert rc.json()["max"] == 1 assert rc.json()["max"] == 1
# Create some test data # Create some test data
create_mock_trades(fee) create_mock_trades(fee, is_short)
rc = client_get(client, f"{BASE_URI}/count") rc = client_get(client, f"{BASE_URI}/count")
assert_response(rc) assert_response(rc)
assert rc.json()["current"] == 4 assert rc.json()["current"] == 4
@ -549,7 +550,8 @@ def test_api_daily(botclient, mocker, ticker, fee, markets):
assert rc.json()['data'][0]['date'] == str(datetime.utcnow().date()) assert rc.json()['data'][0]['date'] == str(datetime.utcnow().date())
def test_api_trades(botclient, mocker, fee, markets): @pytest.mark.parametrize('is_short', [True, False])
def test_api_trades(botclient, mocker, fee, markets, is_short):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot) patch_get_signal(ftbot)
mocker.patch.multiple( mocker.patch.multiple(
@ -562,7 +564,7 @@ def test_api_trades(botclient, mocker, fee, markets):
assert rc.json()['trades_count'] == 0 assert rc.json()['trades_count'] == 0
assert rc.json()['total_trades'] == 0 assert rc.json()['total_trades'] == 0
create_mock_trades(fee) create_mock_trades(fee, is_short)
Trade.query.session.flush() Trade.query.session.flush()
rc = client_get(client, f"{BASE_URI}/trades") rc = client_get(client, f"{BASE_URI}/trades")
@ -577,6 +579,7 @@ def test_api_trades(botclient, mocker, fee, markets):
assert rc.json()['total_trades'] == 2 assert rc.json()['total_trades'] == 2
# TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
def test_api_trade_single(botclient, mocker, fee, ticker, markets): def test_api_trade_single(botclient, mocker, fee, ticker, markets):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot) patch_get_signal(ftbot)
@ -589,7 +592,7 @@ def test_api_trade_single(botclient, mocker, fee, ticker, markets):
assert_response(rc, 404) assert_response(rc, 404)
assert rc.json()['detail'] == 'Trade not found.' assert rc.json()['detail'] == 'Trade not found.'
create_mock_trades(fee) create_mock_trades(fee, False)
Trade.query.session.flush() Trade.query.session.flush()
rc = client_get(client, f"{BASE_URI}/trade/3") rc = client_get(client, f"{BASE_URI}/trade/3")
@ -597,6 +600,7 @@ def test_api_trade_single(botclient, mocker, fee, ticker, markets):
assert rc.json()['trade_id'] == 3 assert rc.json()['trade_id'] == 3
# TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
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) patch_get_signal(ftbot)
@ -612,7 +616,7 @@ def test_api_delete_trade(botclient, mocker, fee, markets):
# 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, False)
Trade.query.session.flush() Trade.query.session.flush()
ftbot.strategy.order_types['stoploss_on_exchange'] = True ftbot.strategy.order_types['stoploss_on_exchange'] = True
trades = Trade.query.all() trades = Trade.query.all()
@ -687,6 +691,7 @@ def test_api_edge_disabled(botclient, mocker, ticker, fee, markets):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
# TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
def test_api_profit(botclient, mocker, ticker, fee, markets): def test_api_profit(botclient, mocker, ticker, fee, markets):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot) patch_get_signal(ftbot)
@ -702,7 +707,7 @@ def test_api_profit(botclient, mocker, ticker, fee, markets):
assert_response(rc, 200) assert_response(rc, 200)
assert rc.json()['trade_count'] == 0 assert rc.json()['trade_count'] == 0
create_mock_trades(fee) create_mock_trades(fee, False)
# Simulate fulfilled LIMIT_BUY order for trade # Simulate fulfilled LIMIT_BUY order for trade
rc = client_get(client, f"{BASE_URI}/profit") rc = client_get(client, f"{BASE_URI}/profit")
@ -738,7 +743,8 @@ def test_api_profit(botclient, mocker, ticker, fee, markets):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
def test_api_stats(botclient, mocker, ticker, fee, markets,): # TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
def test_api_stats(botclient, mocker, ticker, fee, markets):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot) patch_get_signal(ftbot)
mocker.patch.multiple( mocker.patch.multiple(
@ -754,7 +760,7 @@ def test_api_stats(botclient, mocker, ticker, fee, markets,):
assert 'durations' in rc.json() assert 'durations' in rc.json()
assert 'sell_reasons' in rc.json() assert 'sell_reasons' in rc.json()
create_mock_trades(fee) create_mock_trades(fee, False)
rc = client_get(client, f"{BASE_URI}/stats") rc = client_get(client, f"{BASE_URI}/stats")
assert_response(rc, 200) assert_response(rc, 200)
@ -812,6 +818,10 @@ def test_api_performance(botclient, fee):
{'count': 1, 'pair': 'XRP/ETH', 'profit': -5.57, 'profit_abs': -0.1150375}] {'count': 1, 'pair': 'XRP/ETH', 'profit': -5.57, 'profit_abs': -0.1150375}]
# TODO-lev: @pytest.mark.parametrize('is_short,side', [
# (True, "short"),
# (False, "long")
# ])
def test_api_status(botclient, mocker, ticker, fee, markets): def test_api_status(botclient, mocker, ticker, fee, markets):
ftbot, client = botclient ftbot, client = botclient
patch_get_signal(ftbot) patch_get_signal(ftbot)
@ -827,7 +837,7 @@ def test_api_status(botclient, mocker, ticker, fee, markets):
rc = client_get(client, f"{BASE_URI}/status") rc = client_get(client, f"{BASE_URI}/status")
assert_response(rc, 200) assert_response(rc, 200)
assert rc.json() == [] assert rc.json() == []
create_mock_trades(fee) create_mock_trades(fee, False)
rc = client_get(client, f"{BASE_URI}/status") rc = client_get(client, f"{BASE_URI}/status")
assert_response(rc) assert_response(rc)
@ -880,7 +890,7 @@ def test_api_status(botclient, mocker, ticker, fee, markets):
'is_open': True, 'is_open': True,
'max_rate': ANY, 'max_rate': ANY,
'min_rate': ANY, 'min_rate': ANY,
'open_order_id': 'dry_run_buy_12345', 'open_order_id': 'dry_run_buy_long_12345',
'open_rate_requested': ANY, 'open_rate_requested': ANY,
'open_trade_value': 15.1668225, 'open_trade_value': 15.1668225,
'sell_reason': None, 'sell_reason': None,

View File

@ -33,6 +33,7 @@ class DummyCls(Telegram):
""" """
Dummy class for testing the Telegram @authorized_only decorator Dummy class for testing the Telegram @authorized_only decorator
""" """
def __init__(self, rpc: RPC, config) -> None: def __init__(self, rpc: RPC, config) -> None:
super().__init__(rpc, config) super().__init__(rpc, config)
self.state = {'called': False} self.state = {'called': False}
@ -479,8 +480,9 @@ def test_profit_handle(default_conf, update, ticker, ticker_sell_up, fee,
assert '*Best Performing:* `ETH/BTC: 6.20%`' in msg_mock.call_args_list[-1][0][0] assert '*Best Performing:* `ETH/BTC: 6.20%`' in msg_mock.call_args_list[-1][0][0]
@pytest.mark.parametrize('is_short', [True, False])
def test_telegram_stats(default_conf, update, ticker, ticker_sell_up, fee, def test_telegram_stats(default_conf, update, ticker, ticker_sell_up, fee,
limit_buy_order, limit_sell_order, mocker) -> None: limit_buy_order, limit_sell_order, mocker, is_short) -> None:
mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0) mocker.patch('freqtrade.rpc.rpc.CryptoToFiatConverter._find_price', return_value=15000.0)
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
@ -496,7 +498,7 @@ def test_telegram_stats(default_conf, update, ticker, ticker_sell_up, fee,
msg_mock.reset_mock() msg_mock.reset_mock()
# Create some test data # Create some test data
create_mock_trades(fee) create_mock_trades(fee, is_short)
telegram._stats(update=update, context=MagicMock()) telegram._stats(update=update, context=MagicMock())
assert msg_mock.call_count == 1 assert msg_mock.call_count == 1
@ -997,9 +999,9 @@ def test_count_handle(default_conf, update, ticker, fee, mocker) -> None:
msg = ('<pre> current max total stake\n--------- ----- -------------\n' msg = ('<pre> current max total stake\n--------- ----- -------------\n'
' 1 {} {}</pre>').format( ' 1 {} {}</pre>').format(
default_conf['max_open_trades'], default_conf['max_open_trades'],
default_conf['stake_amount'] default_conf['stake_amount']
) )
assert msg in msg_mock.call_args_list[0][0][0] assert msg in msg_mock.call_args_list[0][0][0]
@ -1159,6 +1161,7 @@ def test_edge_enabled(edge_conf, update, mocker) -> None:
assert 'Winrate' not in msg_mock.call_args_list[0][0][0] assert 'Winrate' not in msg_mock.call_args_list[0][0][0]
# TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
def test_telegram_trades(mocker, update, default_conf, fee): def test_telegram_trades(mocker, update, default_conf, fee):
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
@ -1177,7 +1180,7 @@ def test_telegram_trades(mocker, update, default_conf, fee):
assert "<pre>" not in msg_mock.call_args_list[0][0][0] assert "<pre>" not in msg_mock.call_args_list[0][0][0]
msg_mock.reset_mock() msg_mock.reset_mock()
create_mock_trades(fee) create_mock_trades(fee, False)
context = MagicMock() context = MagicMock()
context.args = [5] context.args = [5]
@ -1191,6 +1194,7 @@ def test_telegram_trades(mocker, update, default_conf, fee):
msg_mock.call_args_list[0][0][0])) msg_mock.call_args_list[0][0][0]))
# TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
def test_telegram_delete_trade(mocker, update, default_conf, fee): def test_telegram_delete_trade(mocker, update, default_conf, fee):
telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf) telegram, _, msg_mock = get_telegram_testobject(mocker, default_conf)
@ -1201,7 +1205,7 @@ def test_telegram_delete_trade(mocker, update, default_conf, fee):
assert "Trade-id not set." in msg_mock.call_args_list[0][0][0] assert "Trade-id not set." in msg_mock.call_args_list[0][0][0]
msg_mock.reset_mock() msg_mock.reset_mock()
create_mock_trades(fee) create_mock_trades(fee, False)
context = MagicMock() context = MagicMock()
context.args = [1] context.args = [1]

View File

@ -875,10 +875,8 @@ def test_execute_entry(mocker, default_conf, fee, limit_buy_order, limit_sell_or
assert trade.open_rate_requested == 10 assert trade.open_rate_requested == 10
@pytest.mark.parametrize("is_short", [False, True]) # TODO-lev: @pytest.mark.parametrize("is_short", [False, True])
def test_execute_entry_confirm_error(mocker, default_conf, fee, limit_buy_order, def test_execute_entry_confirm_error(mocker, default_conf, fee, limit_buy_order) -> None:
limit_sell_order, is_short) -> None:
order = limit_sell_order if is_short else limit_buy_order
freqtrade = get_patched_freqtradebot(mocker, default_conf) freqtrade = get_patched_freqtradebot(mocker, default_conf)
mocker.patch.multiple( mocker.patch.multiple(
'freqtrade.exchange.Exchange', 'freqtrade.exchange.Exchange',
@ -887,7 +885,7 @@ def test_execute_entry_confirm_error(mocker, default_conf, fee, limit_buy_order,
'ask': 0.00001173, 'ask': 0.00001173,
'last': 0.00001172 'last': 0.00001172
}), }),
create_order=MagicMock(return_value=order), create_order=MagicMock(return_value=limit_buy_order),
get_rate=MagicMock(return_value=0.11), get_rate=MagicMock(return_value=0.11),
get_min_pair_stake_amount=MagicMock(return_value=1), get_min_pair_stake_amount=MagicMock(return_value=1),
get_fee=fee, get_fee=fee,
@ -899,11 +897,11 @@ def test_execute_entry_confirm_error(mocker, default_conf, fee, limit_buy_order,
# TODO-lev: KeyError happens on short, why? # TODO-lev: KeyError happens on short, why?
assert freqtrade.execute_entry(pair, stake_amount) assert freqtrade.execute_entry(pair, stake_amount)
order['id'] = '222' limit_buy_order['id'] = '222'
freqtrade.strategy.confirm_trade_entry = MagicMock(side_effect=Exception) freqtrade.strategy.confirm_trade_entry = MagicMock(side_effect=Exception)
assert freqtrade.execute_entry(pair, stake_amount) assert freqtrade.execute_entry(pair, stake_amount)
order['id'] = '2223' limit_buy_order['id'] = '2223'
freqtrade.strategy.confirm_trade_entry = MagicMock(return_value=True) freqtrade.strategy.confirm_trade_entry = MagicMock(return_value=True)
assert freqtrade.execute_entry(pair, stake_amount) assert freqtrade.execute_entry(pair, stake_amount)
@ -1319,7 +1317,7 @@ def test_handle_stoploss_on_exchange_trailing(mocker, default_conf, fee, is_shor
pair='ETH/BTC', pair='ETH/BTC',
order_types=freqtrade.strategy.order_types, order_types=freqtrade.strategy.order_types,
stop_price=0.00002346 * 0.95, stop_price=0.00002346 * 0.95,
side="sell", side="buy" if is_short else "sell",
leverage=1.0 leverage=1.0
) )
@ -1398,7 +1396,10 @@ def test_handle_stoploss_on_exchange_trailing_error(mocker, default_conf, fee, c
mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order', mocker.patch('freqtrade.exchange.Binance.fetch_stoploss_order',
return_value=stoploss_order_hanging) return_value=stoploss_order_hanging)
freqtrade.handle_trailing_stoploss_on_exchange( freqtrade.handle_trailing_stoploss_on_exchange(
trade, stoploss_order_hanging, side=("buy" if is_short else "sell")) trade,
stoploss_order_hanging,
side=("buy" if is_short else "sell")
)
assert log_has_re(r"Could not cancel stoploss order abcd for pair ETH/BTC.*", caplog) assert log_has_re(r"Could not cancel stoploss order abcd for pair ETH/BTC.*", caplog)
# Still try to create order # Still try to create order
@ -1519,7 +1520,7 @@ def test_handle_stoploss_on_exchange_custom_stop(mocker, default_conf, fee, is_s
pair='ETH/BTC', pair='ETH/BTC',
order_types=freqtrade.strategy.order_types, order_types=freqtrade.strategy.order_types,
stop_price=0.00002346 * 0.96, stop_price=0.00002346 * 0.96,
side="sell", side="buy" if is_short else "sell",
leverage=1.0 leverage=1.0
) )

View File

@ -1346,11 +1346,12 @@ def test_adjust_min_max_rates(fee):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
@pytest.mark.parametrize('use_db', [True, False]) @pytest.mark.parametrize('use_db', [True, False])
def test_get_open(fee, use_db): @pytest.mark.parametrize('is_short', [True, False])
def test_get_open(fee, is_short, use_db):
Trade.use_db = use_db Trade.use_db = use_db
Trade.reset_trades() Trade.reset_trades()
create_mock_trades(fee, use_db) create_mock_trades(fee, is_short, use_db)
assert len(Trade.get_open_trades()) == 4 assert len(Trade.get_open_trades()) == 4
Trade.use_db = True Trade.use_db = True
@ -1702,14 +1703,15 @@ def test_fee_updated(fee):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
@pytest.mark.parametrize('is_short', [True, False])
@pytest.mark.parametrize('use_db', [True, False]) @pytest.mark.parametrize('use_db', [True, False])
def test_total_open_trades_stakes(fee, use_db): def test_total_open_trades_stakes(fee, is_short, use_db):
Trade.use_db = use_db Trade.use_db = use_db
Trade.reset_trades() Trade.reset_trades()
res = Trade.total_open_trades_stakes() res = Trade.total_open_trades_stakes()
assert res == 0 assert res == 0
create_mock_trades(fee, use_db) create_mock_trades(fee, is_short, use_db)
res = Trade.total_open_trades_stakes() res = Trade.total_open_trades_stakes()
assert res == 0.004 assert res == 0.004
@ -1717,6 +1719,7 @@ def test_total_open_trades_stakes(fee, use_db):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
# TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
@pytest.mark.parametrize('use_db', [True, False]) @pytest.mark.parametrize('use_db', [True, False])
def test_get_total_closed_profit(fee, use_db): def test_get_total_closed_profit(fee, use_db):
@ -1724,7 +1727,7 @@ def test_get_total_closed_profit(fee, use_db):
Trade.reset_trades() Trade.reset_trades()
res = Trade.get_total_closed_profit() res = Trade.get_total_closed_profit()
assert res == 0 assert res == 0
create_mock_trades(fee, use_db) create_mock_trades(fee, False, use_db)
res = Trade.get_total_closed_profit() res = Trade.get_total_closed_profit()
assert res == 0.000739127 assert res == 0.000739127
@ -1732,11 +1735,12 @@ def test_get_total_closed_profit(fee, use_db):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
# TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
@pytest.mark.parametrize('use_db', [True, False]) @pytest.mark.parametrize('use_db', [True, False])
def test_get_trades_proxy(fee, use_db): def test_get_trades_proxy(fee, use_db):
Trade.use_db = use_db Trade.use_db = use_db
Trade.reset_trades() Trade.reset_trades()
create_mock_trades(fee, use_db) create_mock_trades(fee, False, use_db)
trades = Trade.get_trades_proxy() trades = Trade.get_trades_proxy()
assert len(trades) == 6 assert len(trades) == 6
@ -1765,9 +1769,10 @@ def test_get_trades_backtest():
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
# @pytest.mark.parametrize('is_short', [True, False])
def test_get_overall_performance(fee): def test_get_overall_performance(fee):
create_mock_trades(fee) create_mock_trades(fee, False)
res = Trade.get_overall_performance() res = Trade.get_overall_performance()
assert len(res) == 2 assert len(res) == 2
@ -1777,12 +1782,13 @@ def test_get_overall_performance(fee):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
# TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
def test_get_best_pair(fee): def test_get_best_pair(fee):
res = Trade.get_best_pair() res = Trade.get_best_pair()
assert res is None assert res is None
create_mock_trades(fee) create_mock_trades(fee, False)
res = Trade.get_best_pair() res = Trade.get_best_pair()
assert len(res) == 2 assert len(res) == 2
assert res[0] == 'XRP/BTC' assert res[0] == 'XRP/BTC'
@ -1864,8 +1870,9 @@ def test_update_order_from_ccxt(caplog):
@pytest.mark.usefixtures("init_persistence") @pytest.mark.usefixtures("init_persistence")
# TODO-lev: @pytest.mark.parametrize('is_short', [True, False])
def test_select_order(fee): def test_select_order(fee):
create_mock_trades(fee) create_mock_trades(fee, False)
trades = Trade.get_trades().all() trades = Trade.get_trades().all()