tests happy

This commit is contained in:
hroff-1902 2019-07-12 00:39:42 +03:00
parent 1bdffcc73b
commit 94e6fb89b3
6 changed files with 76 additions and 140 deletions

View File

@ -44,6 +44,20 @@ def get_args(args):
return Arguments(args, '').get_parsed_arg()
def patched_configuration_open(mocker, config):
file_mock = mocker.patch('freqtrade.configuration.configuration.open', mocker.mock_open(
read_data=json.dumps(config)
))
return file_mock
def patched_configuration_load_config_file(mocker, config) -> None:
mocker.patch(
'freqtrade.configuration.configuration.Configuration._load_config_file',
lambda *args, **kwargs: config
)
def patch_exchange(mocker, api_mock=None, id='bittrex') -> None:
mocker.patch('freqtrade.exchange.Exchange._load_markets', MagicMock(return_value={}))
mocker.patch('freqtrade.exchange.Exchange.validate_pairs', MagicMock())

View File

@ -22,7 +22,8 @@ from freqtrade.optimize.backtesting import Backtesting
from freqtrade.state import RunMode
from freqtrade.strategy.default_strategy import DefaultStrategy
from freqtrade.strategy.interface import SellType
from freqtrade.tests.conftest import get_args, log_has, log_has_re, patch_exchange
from freqtrade.tests.conftest import (get_args, log_has, log_has_re, patch_exchange,
patched_configuration_open)
def trim_dictlist(dict_list, num):
@ -165,9 +166,7 @@ def _trend_alternate(dataframe=None, metadata=None):
# Unit tests
def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = [
'--config', 'config.json',
@ -205,10 +204,8 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) ->
def test_setup_bt_configuration_with_arguments(mocker, default_conf, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
mocker.patch('freqtrade.configuration.Configuration._create_datadir', lambda s, c, x: x)
patched_configuration_open(mocker, default_conf)
mocker.patch('freqtrade.configuration.configuration.Configuration._create_datadir', lambda s, c, x: x)
args = [
'--config', 'config.json',
@ -276,9 +273,7 @@ def test_setup_bt_configuration_with_arguments(mocker, default_conf, caplog) ->
def test_setup_configuration_unlimited_stake_amount(mocker, default_conf, caplog) -> None:
default_conf['stake_amount'] = constants.UNLIMITED_STAKE_AMOUNT
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = [
'--config', 'config.json',
@ -295,9 +290,8 @@ def test_start(mocker, fee, default_conf, caplog) -> None:
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
patch_exchange(mocker)
mocker.patch('freqtrade.optimize.backtesting.Backtesting.start', start_mock)
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = [
'--config', 'config.json',
'--strategy', 'DefaultStrategy',
@ -828,9 +822,7 @@ def test_backtest_start_live(default_conf, mocker, caplog):
patch_exchange(mocker, api_mock)
mocker.patch('freqtrade.optimize.backtesting.Backtesting.backtest', MagicMock())
mocker.patch('freqtrade.optimize.backtesting.Backtesting._generate_text_table', MagicMock())
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = [
'--config', 'config.json',
@ -880,9 +872,7 @@ def test_backtest_start_multi_strat(default_conf, mocker, caplog):
gen_strattable_mock = MagicMock()
mocker.patch('freqtrade.optimize.backtesting.Backtesting._generate_text_table_strategy',
gen_strattable_mock)
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = [
'--config', 'config.json',

View File

@ -8,13 +8,12 @@ from freqtrade.edge import PairInfo
from freqtrade.optimize import setup_configuration, start_edge
from freqtrade.optimize.edge_cli import EdgeCli
from freqtrade.state import RunMode
from freqtrade.tests.conftest import get_args, log_has, log_has_re, patch_exchange
from freqtrade.tests.conftest import (get_args, log_has, log_has_re, patch_exchange,
patched_configuration_open)
def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = [
'--config', 'config.json',
@ -46,10 +45,8 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) ->
def test_setup_edge_configuration_with_arguments(mocker, edge_conf, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(edge_conf)
))
mocker.patch('freqtrade.configuration.Configuration._create_datadir', lambda s, c, x: x)
patched_configuration_open(mocker, edge_conf)
mocker.patch('freqtrade.configuration.configuration.Configuration._create_datadir', lambda s, c, x: x)
args = [
'--config', 'config.json',
@ -92,9 +89,8 @@ def test_start(mocker, fee, edge_conf, caplog) -> None:
mocker.patch('freqtrade.exchange.Exchange.get_fee', fee)
patch_exchange(mocker)
mocker.patch('freqtrade.optimize.edge_cli.EdgeCli.start', start_mock)
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(edge_conf)
))
patched_configuration_open(mocker, edge_conf)
args = [
'--config', 'config.json',
'--strategy', 'DefaultStrategy',

View File

@ -16,7 +16,8 @@ from freqtrade.optimize.hyperopt import Hyperopt, HYPEROPT_LOCKFILE
from freqtrade.optimize import setup_configuration, start_hyperopt
from freqtrade.resolvers.hyperopt_resolver import HyperOptResolver
from freqtrade.state import RunMode
from freqtrade.tests.conftest import get_args, log_has, log_has_re, patch_exchange
from freqtrade.tests.conftest import (get_args, log_has, log_has_re, patch_exchange,
patched_configuration_load_config_file, patched_configuration_open)
@pytest.fixture(scope='function')
@ -44,9 +45,7 @@ def create_trials(mocker, hyperopt) -> None:
def test_setup_hyperopt_configuration_without_arguments(mocker, default_conf, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = [
'--config', 'config.json',
@ -82,10 +81,8 @@ def test_setup_hyperopt_configuration_without_arguments(mocker, default_conf, ca
def test_setup_hyperopt_configuration_with_arguments(mocker, default_conf, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
mocker.patch('freqtrade.configuration.Configuration._create_datadir', lambda s, c, x: x)
patched_configuration_open(mocker, default_conf)
mocker.patch('freqtrade.configuration.configuration.Configuration._create_datadir', lambda s, c, x: x)
args = [
'--config', 'config.json',
@ -148,11 +145,8 @@ def test_setup_hyperopt_configuration_with_arguments(mocker, default_conf, caplo
def test_hyperoptresolver(mocker, default_conf, caplog) -> None:
patched_configuration_load_config_file(mocker, default_conf)
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
hyperopts = DefaultHyperOpts
delattr(hyperopts, 'populate_buy_trend')
delattr(hyperopts, 'populate_sell_trend')
@ -172,10 +166,7 @@ def test_hyperoptresolver(mocker, default_conf, caplog) -> None:
def test_start(mocker, default_conf, caplog) -> None:
start_mock = MagicMock()
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
patched_configuration_load_config_file(mocker, default_conf)
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
patch_exchange(mocker)
@ -198,10 +189,7 @@ def test_start(mocker, default_conf, caplog) -> None:
def test_start_no_data(mocker, default_conf, caplog) -> None:
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
patched_configuration_load_config_file(mocker, default_conf)
mocker.patch('freqtrade.optimize.hyperopt.load_data', MagicMock(return_value={}))
mocker.patch(
'freqtrade.optimize.hyperopt.get_timeframe',
@ -226,10 +214,7 @@ def test_start_no_data(mocker, default_conf, caplog) -> None:
def test_start_failure(mocker, default_conf, caplog) -> None:
start_mock = MagicMock()
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
patched_configuration_load_config_file(mocker, default_conf)
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
patch_exchange(mocker)
@ -250,10 +235,7 @@ def test_start_failure(mocker, default_conf, caplog) -> None:
def test_start_filelock(mocker, default_conf, caplog) -> None:
start_mock = MagicMock(side_effect=Timeout(HYPEROPT_LOCKFILE))
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
patched_configuration_load_config_file(mocker, default_conf)
mocker.patch('freqtrade.optimize.hyperopt.Hyperopt.start', start_mock)
patch_exchange(mocker)

View File

@ -17,7 +17,7 @@ from freqtrade.configuration.json_schema import validate_config_schema
from freqtrade.constants import DEFAULT_DB_DRYRUN_URL, DEFAULT_DB_PROD_URL
from freqtrade.loggers import _set_loggers
from freqtrade.state import RunMode
from freqtrade.tests.conftest import log_has, log_has_re
from freqtrade.tests.conftest import log_has, log_has_re, patched_configuration_open
@pytest.fixture(scope="function")
@ -51,9 +51,7 @@ def test_load_config_incorrect_stake_amount(default_conf) -> None:
def test_load_config_file(default_conf, mocker, caplog) -> None:
file_mock = mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
file_mock = patched_configuration_open(mocker, default_conf)
configuration = Configuration(Namespace())
validated_conf = configuration._load_config_file('somefile')
@ -63,9 +61,7 @@ def test_load_config_file(default_conf, mocker, caplog) -> None:
def test_load_config_max_open_trades_zero(default_conf, mocker, caplog) -> None:
default_conf['max_open_trades'] = 0
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = Arguments([], '').get_parsed_arg()
configuration = Configuration(args)
@ -87,7 +83,7 @@ def test_load_config_combine_dicts(default_conf, mocker, caplog) -> None:
config_files = [conf1, conf2]
configsmock = MagicMock(side_effect=config_files)
mocker.patch('freqtrade.configuration.Configuration._load_config_file', configsmock)
mocker.patch('freqtrade.configuration.configuration.Configuration._load_config_file', configsmock)
arg_list = ['-c', 'test_conf.json', '--config', 'test2_conf.json', ]
args = Arguments(arg_list, '').get_parsed_arg()
@ -107,9 +103,7 @@ def test_load_config_combine_dicts(default_conf, mocker, caplog) -> None:
def test_load_config_max_open_trades_minus_one(default_conf, mocker, caplog) -> None:
default_conf['max_open_trades'] = -1
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = Arguments([], '').get_parsed_arg()
configuration = Configuration(args)
@ -124,7 +118,7 @@ def test_load_config_max_open_trades_minus_one(default_conf, mocker, caplog) ->
def test_load_config_file_exception(mocker) -> None:
mocker.patch(
'freqtrade.configuration.open',
'freqtrade.configuration.configuration.open',
MagicMock(side_effect=FileNotFoundError('File not found'))
)
configuration = Configuration(Namespace())
@ -134,9 +128,7 @@ def test_load_config_file_exception(mocker) -> None:
def test_load_config(default_conf, mocker) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = Arguments([], '').get_parsed_arg()
configuration = Configuration(args)
@ -148,9 +140,8 @@ def test_load_config(default_conf, mocker) -> None:
def test_load_config_with_params(default_conf, mocker) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
arglist = [
'--dynamic-whitelist', '10',
'--strategy', 'TestStrategy',
@ -171,9 +162,7 @@ def test_load_config_with_params(default_conf, mocker) -> None:
conf = default_conf.copy()
conf["dry_run"] = False
conf["db_url"] = "sqlite:///path/to/db.sqlite"
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(conf)
))
patched_configuration_open(mocker, conf)
arglist = [
'--strategy', 'TestStrategy',
@ -189,9 +178,7 @@ def test_load_config_with_params(default_conf, mocker) -> None:
conf = default_conf.copy()
conf["dry_run"] = True
conf["db_url"] = "sqlite:///path/to/db.sqlite"
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(conf)
))
patched_configuration_open(mocker, conf)
arglist = [
'--strategy', 'TestStrategy',
@ -207,9 +194,7 @@ def test_load_config_with_params(default_conf, mocker) -> None:
conf = default_conf.copy()
conf["dry_run"] = False
del conf["db_url"]
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(conf)
))
patched_configuration_open(mocker, conf)
arglist = [
'--strategy', 'TestStrategy',
@ -227,9 +212,7 @@ def test_load_config_with_params(default_conf, mocker) -> None:
conf = default_conf.copy()
conf["dry_run"] = True
conf["db_url"] = DEFAULT_DB_PROD_URL
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(conf)
))
patched_configuration_open(mocker, conf)
arglist = [
'--strategy', 'TestStrategy',
@ -247,9 +230,7 @@ def test_load_custom_strategy(default_conf, mocker) -> None:
'strategy': 'CustomStrategy',
'strategy_path': '/tmp/strategies',
})
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = Arguments([], '').get_parsed_arg()
configuration = Configuration(args)
@ -260,9 +241,8 @@ def test_load_custom_strategy(default_conf, mocker) -> None:
def test_show_info(default_conf, mocker, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
arglist = [
'--dynamic-whitelist', '10',
'--strategy', 'TestStrategy',
@ -285,9 +265,8 @@ def test_show_info(default_conf, mocker, caplog) -> None:
def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
arglist = [
'--config', 'config.json',
'--strategy', 'DefaultStrategy',
@ -325,10 +304,8 @@ def test_setup_configuration_without_arguments(mocker, default_conf, caplog) ->
def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
mocker.patch('freqtrade.configuration.Configuration._create_datadir', lambda s, c, x: x)
patched_configuration_open(mocker, default_conf)
mocker.patch('freqtrade.configuration.configuration.Configuration._create_datadir', lambda s, c, x: x)
arglist = [
'--config', 'config.json',
@ -391,9 +368,7 @@ def test_setup_configuration_with_stratlist(mocker, default_conf, caplog) -> Non
"""
Test setup_configuration() function
"""
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
arglist = [
'--config', 'config.json',
@ -441,9 +416,8 @@ def test_setup_configuration_with_stratlist(mocker, default_conf, caplog) -> Non
def test_hyperopt_with_arguments(mocker, default_conf, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
arglist = [
'hyperopt',
'--epochs', '10',
@ -517,8 +491,8 @@ def test_check_exchange(default_conf, caplog) -> None:
def test_cli_verbose_with_params(default_conf, mocker, caplog) -> None:
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)))
patched_configuration_open(mocker, default_conf)
# Prevent setting loggers
mocker.patch('freqtrade.loggers._set_loggers', MagicMock)
arglist = ['-vvv']
@ -570,8 +544,7 @@ def test_set_loggers() -> None:
def test_set_logfile(default_conf, mocker):
mocker.patch('freqtrade.configuration.open',
mocker.mock_open(read_data=json.dumps(default_conf)))
patched_configuration_open(mocker, default_conf)
arglist = [
'--logfile', 'test_file.log',
@ -588,9 +561,7 @@ def test_set_logfile(default_conf, mocker):
def test_load_config_warn_forcebuy(default_conf, mocker, caplog) -> None:
default_conf['forcebuy_enable'] = True
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
read_data=json.dumps(default_conf)
))
patched_configuration_open(mocker, default_conf)
args = Arguments([], '').get_parsed_arg()
configuration = Configuration(args)

View File

@ -10,7 +10,8 @@ from freqtrade.configuration import Arguments
from freqtrade.freqtradebot import FreqtradeBot
from freqtrade.main import main
from freqtrade.state import State
from freqtrade.tests.conftest import log_has, patch_exchange
from freqtrade.tests.conftest import (log_has, patch_exchange,
patched_configuration_load_config_file)
from freqtrade.worker import Worker
@ -50,10 +51,7 @@ def test_main_fatal_exception(mocker, default_conf, caplog) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=Exception))
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
patched_configuration_load_config_file(mocker, default_conf)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
@ -70,10 +68,7 @@ def test_main_keyboard_interrupt(mocker, default_conf, caplog) -> None:
patch_exchange(mocker)
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=KeyboardInterrupt))
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
patched_configuration_load_config_file(mocker, default_conf)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
@ -93,10 +88,7 @@ def test_main_operational_exception(mocker, default_conf, caplog) -> None:
'freqtrade.worker.Worker._worker',
MagicMock(side_effect=OperationalException('Oh snap!'))
)
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
patched_configuration_load_config_file(mocker, default_conf)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
@ -118,10 +110,7 @@ def test_main_reload_conf(mocker, default_conf, caplog) -> None:
State.RUNNING,
OperationalException("Oh snap!")])
mocker.patch('freqtrade.worker.Worker._worker', worker_mock)
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
patched_configuration_load_config_file(mocker, default_conf)
reconfigure_mock = mocker.patch('freqtrade.main.Worker._reconfigure', MagicMock())
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
@ -145,10 +134,7 @@ def test_reconfigure(mocker, default_conf) -> None:
'freqtrade.worker.Worker._worker',
MagicMock(side_effect=OperationalException('Oh snap!'))
)
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: default_conf
)
patched_configuration_load_config_file(mocker, default_conf)
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
@ -159,10 +145,7 @@ def test_reconfigure(mocker, default_conf) -> None:
# Renew mock to return modified data
conf = deepcopy(default_conf)
conf['stake_amount'] += 1
mocker.patch(
'freqtrade.configuration.Configuration._load_config_file',
lambda *args, **kwargs: conf
)
patched_configuration_load_config_file(mocker, conf)
worker._config = conf
# reconfigure should return a new instance