diff --git a/freqtrade/tests/conftest.py b/freqtrade/tests/conftest.py index d101e6e9a..722017a15 100644 --- a/freqtrade/tests/conftest.py +++ b/freqtrade/tests/conftest.py @@ -11,7 +11,7 @@ from telegram import Chat, Message, Update from freqtrade.analyze import Analyze from freqtrade.constants import Constants -from freqtrade.strategy.strategy import Strategy +from freqtrade.freqtradebot import FreqtradeBot logging.getLogger('').setLevel(logging.INFO) @@ -24,6 +24,26 @@ def log_has(line, logs): False) +# Functions for recurrent object patching +def get_patched_freqtradebot(mocker, config) -> FreqtradeBot: + """ + This function patch _init_modules() to not call dependencies + :param mocker: a Mocker object to apply patches + :param config: Config to pass to the bot + :return: None + """ + mocker.patch('freqtrade.fiat_convert.Pymarketcap', {'price_usd': 12345.0}) + mocker.patch('freqtrade.freqtradebot.Analyze', MagicMock()) + mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock()) + mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock()) + mocker.patch('freqtrade.freqtradebot.exchange.init', MagicMock()) + mocker.patch('freqtrade.freqtradebot.RPCManager._init', MagicMock()) + mocker.patch('freqtrade.freqtradebot.RPCManager.send_msg', MagicMock()) + mocker.patch('freqtrade.freqtradebot.Analyze.get_signal', MagicMock()) + + return FreqtradeBot(config) + + @pytest.fixture(scope="module") def default_conf(): """ Returns validated configuration suitable for most tests """ diff --git a/freqtrade/tests/strategy/test_default_strategy.py b/freqtrade/tests/strategy/test_default_strategy.py index f23c1fa48..02b5630fc 100644 --- a/freqtrade/tests/strategy/test_default_strategy.py +++ b/freqtrade/tests/strategy/test_default_strategy.py @@ -2,13 +2,13 @@ import json import pytest from pandas import DataFrame from freqtrade.strategy.default_strategy import DefaultStrategy, class_name -from freqtrade.analyze import parse_ticker_dataframe +from freqtrade.analyze import Analyze @pytest.fixture def result(): with open('freqtrade/tests/testdata/BTC_ETH-1.json') as data_file: - return parse_ticker_dataframe(json.load(data_file)) + return Analyze.parse_ticker_dataframe(json.load(data_file)) def test_default_strategy_class_name(): diff --git a/freqtrade/tests/test_acl_pair.py b/freqtrade/tests/test_acl_pair.py index b70596091..b5f52774d 100644 --- a/freqtrade/tests/test_acl_pair.py +++ b/freqtrade/tests/test_acl_pair.py @@ -1,6 +1,6 @@ -# pragma pylint: disable=missing-docstring,C0103 +# pragma pylint: disable=missing-docstring,C0103,protected-access -from freqtrade.main import refresh_whitelist, gen_pair_whitelist +import freqtrade.tests.conftest as tt # test tools # whitelist, blacklist, filtering, all of that will # eventually become some rules to run on a generic ACL engine @@ -8,21 +8,22 @@ from freqtrade.main import refresh_whitelist, gen_pair_whitelist def whitelist_conf(): - return { - 'stake_currency': 'BTC', - 'exchange': { - 'pair_whitelist': [ - 'BTC_ETH', - 'BTC_TKN', - 'BTC_TRST', - 'BTC_SWT', - 'BTC_BCC' - ], - 'pair_blacklist': [ - 'BTC_BLK' - ], - }, - } + config = tt.default_conf() + + config['stake_currency'] = 'BTC' + config['exchange']['pair_whitelist'] = [ + 'BTC_ETH', + 'BTC_TKN', + 'BTC_TRST', + 'BTC_SWT', + 'BTC_BCC' + ] + + config['exchange']['pair_blacklist'] = [ + 'BTC_BLK' + ] + + return config def get_market_summaries(): @@ -86,11 +87,13 @@ def get_health_empty(): def test_refresh_market_pair_not_in_whitelist(mocker): conf = whitelist_conf() - mocker.patch.dict('freqtrade.main._CONF', conf) - mocker.patch.multiple('freqtrade.main.exchange', - get_wallet_health=get_health) - refreshedwhitelist = refresh_whitelist( - conf['exchange']['pair_whitelist'] + ['BTC_XXX']) + + freqtradebot = tt.get_patched_freqtradebot(mocker, conf) + + mocker.patch('freqtrade.freqtradebot.exchange.get_wallet_health', get_health) + refreshedwhitelist = freqtradebot._refresh_whitelist( + conf['exchange']['pair_whitelist'] + ['BTC_XXX'] + ) # List ordered by BaseVolume whitelist = ['BTC_ETH', 'BTC_TKN'] # Ensure all except those in whitelist are removed @@ -99,10 +102,11 @@ def test_refresh_market_pair_not_in_whitelist(mocker): def test_refresh_whitelist(mocker): conf = whitelist_conf() - mocker.patch.dict('freqtrade.main._CONF', conf) - mocker.patch.multiple('freqtrade.main.exchange', - get_wallet_health=get_health) - refreshedwhitelist = refresh_whitelist(conf['exchange']['pair_whitelist']) + freqtradebot = tt.get_patched_freqtradebot(mocker, conf) + + mocker.patch('freqtrade.freqtradebot.exchange.get_wallet_health', get_health) + refreshedwhitelist = freqtradebot._refresh_whitelist(conf['exchange']['pair_whitelist']) + # List ordered by BaseVolume whitelist = ['BTC_ETH', 'BTC_TKN'] # Ensure all except those in whitelist are removed @@ -111,26 +115,32 @@ def test_refresh_whitelist(mocker): def test_refresh_whitelist_dynamic(mocker): conf = whitelist_conf() - mocker.patch.dict('freqtrade.main._CONF', conf) - mocker.patch.multiple('freqtrade.main.exchange', - get_wallet_health=get_health) - mocker.patch.multiple('freqtrade.main.exchange', - get_market_summaries=get_market_summaries) + freqtradebot = tt.get_patched_freqtradebot(mocker, conf) + mocker.patch.multiple( + 'freqtrade.freqtradebot.exchange', + get_wallet_health=get_health, + get_market_summaries=get_market_summaries + ) + # argument: use the whitelist dynamically by exchange-volume whitelist = ['BTC_TKN', 'BTC_ETH'] - refreshedwhitelist = refresh_whitelist( - gen_pair_whitelist(conf['stake_currency'])) + + refreshedwhitelist = freqtradebot._refresh_whitelist( + freqtradebot._gen_pair_whitelist(conf['stake_currency']) + ) + assert whitelist == refreshedwhitelist def test_refresh_whitelist_dynamic_empty(mocker): conf = whitelist_conf() - mocker.patch.dict('freqtrade.main._CONF', conf) - mocker.patch.multiple('freqtrade.main.exchange', - get_wallet_health=get_health_empty) + freqtradebot = tt.get_patched_freqtradebot(mocker, conf) + mocker.patch('freqtrade.freqtradebot.exchange.get_wallet_health', get_health_empty) + # argument: use the whitelist dynamically by exchange-volume whitelist = [] conf['exchange']['pair_whitelist'] = [] - refresh_whitelist(whitelist) + freqtradebot._refresh_whitelist(whitelist) pairslist = conf['exchange']['pair_whitelist'] + assert set(whitelist) == set(pairslist)