Move Backtesting to a class and add unit tests
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
# pragma pylint: disable=protected-access, invalid-name, missing-docstring
|
||||
|
||||
# pragma pylint: disable=protected-access, invalid-name
|
||||
"""
|
||||
Unit test file for configuration.py
|
||||
"""
|
||||
@@ -18,7 +17,6 @@ import freqtrade.tests.conftest as tt # test tools
|
||||
def test_configuration_object() -> None:
|
||||
"""
|
||||
Test the Constants object has the mandatory Constants
|
||||
:return: None
|
||||
"""
|
||||
assert hasattr(Configuration, '_load_config')
|
||||
assert hasattr(Configuration, '_load_config_file')
|
||||
@@ -30,12 +28,11 @@ def test_configuration_object() -> None:
|
||||
def test_load_config_invalid_pair(default_conf, mocker) -> None:
|
||||
"""
|
||||
Test the configuration validator with an invalid PAIR format
|
||||
:param default_conf: Configuration already read from a file (JSON format)
|
||||
:return: None
|
||||
"""
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.configuration.Configuration',
|
||||
_load_config=MagicMock(return_value=[])
|
||||
_load_config=MagicMock(return_value=[]),
|
||||
show_info=MagicMock
|
||||
)
|
||||
conf = deepcopy(default_conf)
|
||||
conf['exchange']['pair_whitelist'].append('BTC-ETH')
|
||||
@@ -48,12 +45,11 @@ def test_load_config_invalid_pair(default_conf, mocker) -> None:
|
||||
def test_load_config_missing_attributes(default_conf, mocker) -> None:
|
||||
"""
|
||||
Test the configuration validator with a missing attribute
|
||||
:param default_conf: Configuration already read from a file (JSON format)
|
||||
:return: None
|
||||
"""
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.configuration.Configuration',
|
||||
_load_config=MagicMock(return_value=[])
|
||||
_load_config=MagicMock(return_value=[]),
|
||||
show_info=MagicMock
|
||||
)
|
||||
conf = deepcopy(default_conf)
|
||||
conf.pop('exchange')
|
||||
@@ -65,12 +61,12 @@ def test_load_config_missing_attributes(default_conf, mocker) -> None:
|
||||
|
||||
def test_load_config_file(default_conf, mocker, caplog) -> None:
|
||||
"""
|
||||
Test _load_config_file() method
|
||||
:return:
|
||||
Test Configuration._load_config_file() method
|
||||
"""
|
||||
mocker.patch.multiple(
|
||||
'freqtrade.configuration.Configuration',
|
||||
_load_config=MagicMock(return_value=[])
|
||||
_load_config=MagicMock(return_value=[]),
|
||||
show_info=MagicMock
|
||||
)
|
||||
file_mock = mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
@@ -85,6 +81,9 @@ def test_load_config_file(default_conf, mocker, caplog) -> None:
|
||||
|
||||
|
||||
def test_load_config(default_conf, mocker) -> None:
|
||||
"""
|
||||
Test Configuration._load_config() without any cli params
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
@@ -100,6 +99,9 @@ def test_load_config(default_conf, mocker) -> None:
|
||||
|
||||
|
||||
def test_load_config_with_params(default_conf, mocker) -> None:
|
||||
"""
|
||||
Test Configuration._load_config() with cli params used
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
@@ -123,6 +125,9 @@ def test_load_config_with_params(default_conf, mocker) -> None:
|
||||
|
||||
|
||||
def test_show_info(default_conf, mocker, caplog) -> None:
|
||||
"""
|
||||
Test Configuration.show_info()
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
@@ -155,3 +160,118 @@ def test_show_info(default_conf, mocker, caplog) -> None:
|
||||
'Dry run is disabled. (--dry_run_db ignored)',
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
|
||||
def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test setup_configuration() function
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.Configuration.show_info', MagicMock)
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = [
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'default_strategy',
|
||||
'backtesting'
|
||||
]
|
||||
|
||||
args = Arguments(args, '').get_parsed_arg()
|
||||
|
||||
configuration = Configuration(args)
|
||||
config = configuration.get_config()
|
||||
assert 'max_open_trades' in config
|
||||
assert 'stake_currency' in config
|
||||
assert 'stake_amount' in config
|
||||
assert 'exchange' in config
|
||||
assert 'pair_whitelist' in config['exchange']
|
||||
assert 'datadir' in config
|
||||
assert tt.log_has(
|
||||
'Parameter --datadir detected: {} ...'.format(config['datadir']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
assert 'ticker_interval' in config
|
||||
assert not tt.log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'live' not in config
|
||||
assert not tt.log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'realistic_simulation' not in config
|
||||
assert not tt.log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'refresh_pairs' not in config
|
||||
assert not tt.log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'timerange' not in config
|
||||
assert 'export' not in config
|
||||
|
||||
|
||||
def test_setup_configuration_with_arguments(mocker, default_conf, caplog) -> None:
|
||||
"""
|
||||
Test setup_configuration() function
|
||||
"""
|
||||
mocker.patch('freqtrade.configuration.Configuration.show_info', MagicMock)
|
||||
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
||||
read_data=json.dumps(default_conf)
|
||||
))
|
||||
|
||||
args = [
|
||||
'--config', 'config.json',
|
||||
'--strategy', 'default_strategy',
|
||||
'--datadir', '/foo/bar',
|
||||
'backtesting',
|
||||
'--ticker-interval', '1',
|
||||
'--live',
|
||||
'--realistic-simulation',
|
||||
'--refresh-pairs-cached',
|
||||
'--timerange', ':100',
|
||||
'--export', '/bar/foo'
|
||||
]
|
||||
|
||||
args = Arguments(args, '').get_parsed_arg()
|
||||
|
||||
configuration = Configuration(args)
|
||||
config = configuration.get_config()
|
||||
assert 'max_open_trades' in config
|
||||
assert 'stake_currency' in config
|
||||
assert 'stake_amount' in config
|
||||
assert 'exchange' in config
|
||||
assert 'pair_whitelist' in config['exchange']
|
||||
assert 'datadir' in config
|
||||
assert tt.log_has(
|
||||
'Parameter --datadir detected: {} ...'.format(config['datadir']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
assert 'ticker_interval' in config
|
||||
assert tt.log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples)
|
||||
assert tt.log_has(
|
||||
'Using ticker_interval: 1 ...',
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
assert 'live' in config
|
||||
assert tt.log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
||||
|
||||
assert 'realistic_simulation'in config
|
||||
assert tt.log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
|
||||
assert tt.log_has('Using max_open_trades: 1 ...', caplog.record_tuples)
|
||||
|
||||
assert 'refresh_pairs'in config
|
||||
assert tt.log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
||||
|
||||
import pprint
|
||||
pprint.pprint(caplog.record_tuples)
|
||||
pprint.pprint(config['timerange'])
|
||||
assert 'timerange' in config
|
||||
assert tt.log_has(
|
||||
'Parameter --timerange detected: {} ...'.format(config['timerange']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
assert 'export' in config
|
||||
assert tt.log_has(
|
||||
'Parameter --export detected: {} ...'.format(config['export']),
|
||||
caplog.record_tuples
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user