2018-02-09 07:35:38 +00:00
|
|
|
# pragma pylint: disable=protected-access, invalid-name
|
2018-03-02 15:22:00 +00:00
|
|
|
|
2018-02-04 06:42:03 +00:00
|
|
|
"""
|
|
|
|
Unit test file for configuration.py
|
|
|
|
"""
|
|
|
|
import json
|
|
|
|
from copy import deepcopy
|
2018-03-06 03:44:27 +00:00
|
|
|
from unittest.mock import MagicMock
|
2018-05-31 19:04:10 +00:00
|
|
|
from argparse import Namespace
|
2018-03-17 21:44:47 +00:00
|
|
|
|
|
|
|
import pytest
|
2018-02-04 06:42:03 +00:00
|
|
|
from jsonschema import ValidationError
|
|
|
|
|
|
|
|
from freqtrade.arguments import Arguments
|
|
|
|
from freqtrade.configuration import Configuration
|
2018-03-04 10:06:40 +00:00
|
|
|
from freqtrade.tests.conftest import log_has
|
2018-03-30 20:14:35 +00:00
|
|
|
from freqtrade import OperationalException
|
2018-02-04 06:42:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_configuration_object() -> None:
|
|
|
|
"""
|
|
|
|
Test the Constants object has the mandatory Constants
|
|
|
|
"""
|
2018-03-03 21:39:39 +00:00
|
|
|
assert hasattr(Configuration, 'load_config')
|
2018-02-04 06:42:03 +00:00
|
|
|
assert hasattr(Configuration, '_load_config_file')
|
|
|
|
assert hasattr(Configuration, '_validate_config')
|
2018-03-03 21:39:39 +00:00
|
|
|
assert hasattr(Configuration, '_load_common_config')
|
|
|
|
assert hasattr(Configuration, '_load_backtesting_config')
|
|
|
|
assert hasattr(Configuration, '_load_hyperopt_config')
|
2018-02-04 06:42:03 +00:00
|
|
|
assert hasattr(Configuration, 'get_config')
|
|
|
|
|
|
|
|
|
2018-03-30 20:14:35 +00:00
|
|
|
def test_load_config_invalid_pair(default_conf) -> None:
|
2018-02-04 06:42:03 +00:00
|
|
|
"""
|
|
|
|
Test the configuration validator with an invalid PAIR format
|
|
|
|
"""
|
|
|
|
conf = deepcopy(default_conf)
|
2018-02-03 16:15:40 +00:00
|
|
|
conf['exchange']['pair_whitelist'].append('ETH-BTC')
|
2018-02-04 06:42:03 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValidationError, match=r'.*does not match.*'):
|
2018-05-31 19:04:10 +00:00
|
|
|
configuration = Configuration(Namespace())
|
2018-02-04 06:42:03 +00:00
|
|
|
configuration._validate_config(conf)
|
|
|
|
|
|
|
|
|
2018-03-30 20:14:35 +00:00
|
|
|
def test_load_config_missing_attributes(default_conf) -> None:
|
2018-02-04 06:42:03 +00:00
|
|
|
"""
|
|
|
|
Test the configuration validator with a missing attribute
|
|
|
|
"""
|
|
|
|
conf = deepcopy(default_conf)
|
|
|
|
conf.pop('exchange')
|
|
|
|
|
|
|
|
with pytest.raises(ValidationError, match=r'.*\'exchange\' is a required property.*'):
|
2018-05-31 19:04:10 +00:00
|
|
|
configuration = Configuration(Namespace())
|
2018-02-04 06:42:03 +00:00
|
|
|
configuration._validate_config(conf)
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_config_file(default_conf, mocker, caplog) -> None:
|
|
|
|
"""
|
2018-02-09 07:35:38 +00:00
|
|
|
Test Configuration._load_config_file() method
|
2018-02-04 06:42:03 +00:00
|
|
|
"""
|
|
|
|
file_mock = mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
|
2018-05-31 19:04:10 +00:00
|
|
|
configuration = Configuration(Namespace())
|
2018-02-04 06:42:03 +00:00
|
|
|
validated_conf = configuration._load_config_file('somefile')
|
|
|
|
assert file_mock.call_count == 1
|
|
|
|
assert validated_conf.items() >= default_conf.items()
|
|
|
|
assert 'internals' in validated_conf
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has('Validating configuration ...', caplog.record_tuples)
|
2018-02-04 06:42:03 +00:00
|
|
|
|
|
|
|
|
2018-05-03 08:48:25 +00:00
|
|
|
def test_load_config_max_open_trades_zero(default_conf, mocker, caplog) -> None:
|
|
|
|
"""
|
|
|
|
Test Configuration._load_config_file() method
|
|
|
|
"""
|
|
|
|
conf = deepcopy(default_conf)
|
|
|
|
conf['max_open_trades'] = 0
|
|
|
|
file_mock = mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(conf)
|
|
|
|
))
|
|
|
|
|
2018-05-31 19:04:10 +00:00
|
|
|
Configuration(Namespace())._load_config_file('somefile')
|
2018-05-03 08:48:25 +00:00
|
|
|
assert file_mock.call_count == 1
|
|
|
|
assert log_has('Validating configuration ...', caplog.record_tuples)
|
|
|
|
|
|
|
|
|
2018-03-06 03:44:27 +00:00
|
|
|
def test_load_config_file_exception(mocker, caplog) -> None:
|
|
|
|
"""
|
|
|
|
Test Configuration._load_config_file() method
|
|
|
|
"""
|
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.configuration.open',
|
|
|
|
MagicMock(side_effect=FileNotFoundError('File not found'))
|
|
|
|
)
|
2018-05-31 19:04:10 +00:00
|
|
|
configuration = Configuration(Namespace())
|
2018-03-06 03:44:27 +00:00
|
|
|
|
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
configuration._load_config_file('somefile')
|
|
|
|
assert log_has(
|
|
|
|
'Config file "somefile" not found. Please create your config file',
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-02-04 06:42:03 +00:00
|
|
|
def test_load_config(default_conf, mocker) -> None:
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
2018-03-03 21:39:39 +00:00
|
|
|
Test Configuration.load_config() without any cli params
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
2018-02-04 06:42:03 +00:00
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
|
|
|
|
args = Arguments([], '').get_parsed_arg()
|
|
|
|
configuration = Configuration(args)
|
2018-03-03 21:39:39 +00:00
|
|
|
validated_conf = configuration.load_config()
|
2018-02-04 06:42:03 +00:00
|
|
|
|
2018-03-27 16:29:51 +00:00
|
|
|
assert validated_conf.get('strategy') == 'DefaultStrategy'
|
|
|
|
assert validated_conf.get('strategy_path') is None
|
2018-02-04 06:42:03 +00:00
|
|
|
assert 'dynamic_whitelist' not in validated_conf
|
|
|
|
assert 'dry_run_db' not in validated_conf
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_config_with_params(default_conf, mocker) -> None:
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
2018-03-03 21:39:39 +00:00
|
|
|
Test Configuration.load_config() with cli params used
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
2018-02-04 06:42:03 +00:00
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
|
2018-05-31 19:04:10 +00:00
|
|
|
arglist = [
|
2018-02-04 06:42:03 +00:00
|
|
|
'--dynamic-whitelist', '10',
|
2018-03-24 19:44:04 +00:00
|
|
|
'--strategy', 'TestStrategy',
|
2018-03-27 16:29:51 +00:00
|
|
|
'--strategy-path', '/some/path',
|
|
|
|
'--dry-run-db',
|
2018-02-04 06:42:03 +00:00
|
|
|
]
|
2018-05-31 19:04:10 +00:00
|
|
|
args = Arguments(arglist, '').get_parsed_arg()
|
2018-02-04 06:42:03 +00:00
|
|
|
|
|
|
|
configuration = Configuration(args)
|
2018-03-03 21:39:39 +00:00
|
|
|
validated_conf = configuration.load_config()
|
2018-02-04 06:42:03 +00:00
|
|
|
|
2018-03-27 16:29:51 +00:00
|
|
|
assert validated_conf.get('dynamic_whitelist') == 10
|
|
|
|
assert validated_conf.get('strategy') == 'TestStrategy'
|
|
|
|
assert validated_conf.get('strategy_path') == '/some/path'
|
|
|
|
assert validated_conf.get('dry_run_db') is True
|
|
|
|
|
|
|
|
|
|
|
|
def test_load_custom_strategy(default_conf, mocker) -> None:
|
|
|
|
"""
|
|
|
|
Test Configuration.load_config() without any cli params
|
|
|
|
"""
|
|
|
|
custom_conf = deepcopy(default_conf)
|
|
|
|
custom_conf.update({
|
|
|
|
'strategy': 'CustomStrategy',
|
|
|
|
'strategy_path': '/tmp/strategies',
|
|
|
|
})
|
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(custom_conf)
|
|
|
|
))
|
|
|
|
|
|
|
|
args = Arguments([], '').get_parsed_arg()
|
|
|
|
configuration = Configuration(args)
|
|
|
|
validated_conf = configuration.load_config()
|
|
|
|
|
|
|
|
assert validated_conf.get('strategy') == 'CustomStrategy'
|
|
|
|
assert validated_conf.get('strategy_path') == '/tmp/strategies'
|
2018-02-04 06:42:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_show_info(default_conf, mocker, caplog) -> None:
|
2018-02-09 07:35:38 +00:00
|
|
|
"""
|
|
|
|
Test Configuration.show_info()
|
|
|
|
"""
|
2018-02-04 06:42:03 +00:00
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
|
2018-05-31 19:04:10 +00:00
|
|
|
arglist = [
|
2018-02-04 06:42:03 +00:00
|
|
|
'--dynamic-whitelist', '10',
|
2018-03-24 19:44:04 +00:00
|
|
|
'--strategy', 'TestStrategy',
|
2018-02-04 06:42:03 +00:00
|
|
|
'--dry-run-db'
|
|
|
|
]
|
2018-05-31 19:04:10 +00:00
|
|
|
args = Arguments(arglist, '').get_parsed_arg()
|
2018-02-04 06:42:03 +00:00
|
|
|
|
|
|
|
configuration = Configuration(args)
|
2018-03-03 21:39:39 +00:00
|
|
|
configuration.get_config()
|
|
|
|
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-03-03 21:39:39 +00:00
|
|
|
'Parameter --dynamic-whitelist detected. '
|
|
|
|
'Using dynamically generated whitelist. '
|
|
|
|
'(not applicable with Backtesting and Hyperopt)',
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
2018-02-04 06:42:03 +00:00
|
|
|
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-03-03 21:39:39 +00:00
|
|
|
'Parameter --dry-run-db detected ...',
|
2018-02-04 06:42:03 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-03-03 21:39:39 +00:00
|
|
|
'Dry_run will use the DB file: "tradesv3.dry_run.sqlite"',
|
2018-02-04 06:42:03 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test the Dry run condition
|
2018-05-31 19:04:10 +00:00
|
|
|
configuration.config.update({'dry_run': False}) # type: ignore
|
|
|
|
configuration._load_common_config(configuration.config) # type: ignore
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-02-04 06:42:03 +00:00
|
|
|
'Dry run is disabled. (--dry_run_db ignored)',
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_setup_configuration_without_arguments(mocker, default_conf, caplog) -> None:
|
|
|
|
"""
|
|
|
|
Test setup_configuration() function
|
|
|
|
"""
|
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
|
2018-05-31 19:04:10 +00:00
|
|
|
arglist = [
|
2018-02-09 07:35:38 +00:00
|
|
|
'--config', 'config.json',
|
2018-03-24 19:44:04 +00:00
|
|
|
'--strategy', 'DefaultStrategy',
|
2018-02-09 07:35:38 +00:00
|
|
|
'backtesting'
|
|
|
|
]
|
|
|
|
|
2018-05-31 19:04:10 +00:00
|
|
|
args = Arguments(arglist, '').get_parsed_arg()
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
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
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-06-02 08:32:05 +00:00
|
|
|
'Using data folder: {} ...'.format(config['datadir']),
|
2018-02-09 07:35:38 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
assert 'ticker_interval' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert not log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
assert 'live' not in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert not log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
assert 'realistic_simulation' not in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert not log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
assert 'refresh_pairs' not in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert not log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
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.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
|
2018-05-31 19:04:10 +00:00
|
|
|
arglist = [
|
2018-02-09 07:35:38 +00:00
|
|
|
'--config', 'config.json',
|
2018-03-24 19:44:04 +00:00
|
|
|
'--strategy', 'DefaultStrategy',
|
2018-06-03 19:30:25 +00:00
|
|
|
'--datadir', 'freqtrade/tests/testdata/',
|
2018-02-09 07:35:38 +00:00
|
|
|
'backtesting',
|
2018-02-03 16:15:40 +00:00
|
|
|
'--ticker-interval', '1m',
|
2018-02-09 07:35:38 +00:00
|
|
|
'--live',
|
|
|
|
'--realistic-simulation',
|
|
|
|
'--refresh-pairs-cached',
|
|
|
|
'--timerange', ':100',
|
2018-06-03 19:30:25 +00:00
|
|
|
'--export', 'user_data/data'
|
2018-02-09 07:35:38 +00:00
|
|
|
]
|
|
|
|
|
2018-05-31 19:04:10 +00:00
|
|
|
args = Arguments(arglist, '').get_parsed_arg()
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
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
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-06-02 08:32:05 +00:00
|
|
|
'Using data folder: {} ...'.format(config['datadir']),
|
2018-02-09 07:35:38 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
assert 'ticker_interval' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has('Parameter -i/--ticker-interval detected ...', caplog.record_tuples)
|
|
|
|
assert log_has(
|
2018-03-24 09:21:59 +00:00
|
|
|
'Using ticker_interval: 1m ...',
|
2018-02-09 07:35:38 +00:00
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
|
|
|
|
assert 'live' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has('Parameter -l/--live detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
assert 'realistic_simulation'in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has('Parameter --realistic-simulation detected ...', caplog.record_tuples)
|
|
|
|
assert log_has('Using max_open_trades: 1 ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
|
|
|
|
assert 'refresh_pairs'in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has('Parameter -r/--refresh-pairs-cached detected ...', caplog.record_tuples)
|
2018-02-09 07:35:38 +00:00
|
|
|
assert 'timerange' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-02-09 07:35:38 +00:00
|
|
|
'Parameter --timerange detected: {} ...'.format(config['timerange']),
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
|
|
|
|
|
|
|
assert 'export' in config
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has(
|
2018-02-09 07:35:38 +00:00
|
|
|
'Parameter --export detected: {} ...'.format(config['export']),
|
|
|
|
caplog.record_tuples
|
|
|
|
)
|
2018-03-04 08:51:22 +00:00
|
|
|
|
|
|
|
|
2018-03-05 07:12:34 +00:00
|
|
|
def test_hyperopt_with_arguments(mocker, default_conf, caplog) -> None:
|
2018-03-04 08:51:22 +00:00
|
|
|
"""
|
|
|
|
Test setup_configuration() function
|
|
|
|
"""
|
|
|
|
mocker.patch('freqtrade.configuration.open', mocker.mock_open(
|
|
|
|
read_data=json.dumps(default_conf)
|
|
|
|
))
|
|
|
|
|
2018-05-31 19:04:10 +00:00
|
|
|
arglist = [
|
2018-03-04 08:51:22 +00:00
|
|
|
'hyperopt',
|
2018-03-05 07:12:34 +00:00
|
|
|
'--epochs', '10',
|
|
|
|
'--use-mongodb',
|
2018-03-04 08:51:22 +00:00
|
|
|
'--spaces', 'all',
|
|
|
|
]
|
|
|
|
|
2018-05-31 19:04:10 +00:00
|
|
|
args = Arguments(arglist, '').get_parsed_arg()
|
2018-03-04 08:51:22 +00:00
|
|
|
|
|
|
|
configuration = Configuration(args)
|
|
|
|
config = configuration.get_config()
|
2018-03-05 07:12:34 +00:00
|
|
|
|
|
|
|
assert 'epochs' in config
|
|
|
|
assert int(config['epochs']) == 10
|
|
|
|
assert log_has('Parameter --epochs detected ...', caplog.record_tuples)
|
|
|
|
assert log_has('Will run Hyperopt with for 10 epochs ...', caplog.record_tuples)
|
|
|
|
|
|
|
|
assert 'mongodb' in config
|
|
|
|
assert config['mongodb'] is True
|
|
|
|
assert log_has('Parameter --use-mongodb detected ...', caplog.record_tuples)
|
|
|
|
|
2018-03-04 08:51:22 +00:00
|
|
|
assert 'spaces' in config
|
2018-03-04 08:58:20 +00:00
|
|
|
assert config['spaces'] == ['all']
|
2018-03-04 10:06:40 +00:00
|
|
|
assert log_has('Parameter -s/--spaces detected: [\'all\']', caplog.record_tuples)
|
2018-03-30 20:14:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_check_exchange(default_conf) -> None:
|
|
|
|
"""
|
|
|
|
Test the configuration validator with a missing attribute
|
|
|
|
"""
|
|
|
|
conf = deepcopy(default_conf)
|
2018-05-31 19:04:10 +00:00
|
|
|
configuration = Configuration(Namespace())
|
2018-03-30 20:14:35 +00:00
|
|
|
|
|
|
|
# Test a valid exchange
|
|
|
|
conf.get('exchange').update({'name': 'BITTREX'})
|
2018-04-04 20:05:17 +00:00
|
|
|
assert configuration.check_exchange(conf)
|
2018-03-30 20:14:35 +00:00
|
|
|
|
|
|
|
# Test a valid exchange
|
|
|
|
conf.get('exchange').update({'name': 'binance'})
|
2018-04-04 20:05:17 +00:00
|
|
|
assert configuration.check_exchange(conf)
|
2018-03-30 20:14:35 +00:00
|
|
|
|
|
|
|
# Test a invalid exchange
|
|
|
|
conf.get('exchange').update({'name': 'unknown_exchange'})
|
|
|
|
configuration.config = conf
|
|
|
|
|
|
|
|
with pytest.raises(
|
|
|
|
OperationalException,
|
|
|
|
match=r'.*Exchange "unknown_exchange" not supported.*'
|
|
|
|
):
|
2018-04-04 20:05:17 +00:00
|
|
|
configuration.check_exchange(conf)
|