2018-07-29 14:09:44 +00:00
|
|
|
# pragma pylint: disable=missing-docstring
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2018-06-09 02:29:48 +00:00
|
|
|
from copy import deepcopy
|
2017-11-07 21:27:44 +00:00
|
|
|
from unittest.mock import MagicMock
|
2018-03-17 21:44:47 +00:00
|
|
|
|
2017-11-07 21:27:44 +00:00
|
|
|
import pytest
|
2017-09-08 13:51:00 +00:00
|
|
|
|
2018-06-07 20:28:21 +00:00
|
|
|
from freqtrade import OperationalException
|
2018-06-09 02:29:48 +00:00
|
|
|
from freqtrade.arguments import Arguments
|
2019-04-30 17:32:03 +00:00
|
|
|
from freqtrade.freqtradebot import FreqtradeBot
|
2019-03-26 09:42:19 +00:00
|
|
|
from freqtrade.main import main
|
2018-06-09 02:29:48 +00:00
|
|
|
from freqtrade.state import State
|
2018-06-17 20:38:31 +00:00
|
|
|
from freqtrade.tests.conftest import log_has, patch_exchange
|
2019-04-30 17:32:03 +00:00
|
|
|
from freqtrade.worker import Worker
|
2018-01-06 10:21:09 +00:00
|
|
|
|
2018-01-10 09:25:45 +00:00
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
def test_parse_args_backtesting(mocker) -> None:
|
|
|
|
"""
|
|
|
|
Test that main() can start backtesting and also ensure we can pass some specific arguments
|
|
|
|
further argument parsing is done in test_arguments.py
|
|
|
|
"""
|
2019-05-25 18:01:43 +00:00
|
|
|
backtesting_mock = mocker.patch('freqtrade.optimize.start_backtesting', MagicMock())
|
2018-02-04 09:21:16 +00:00
|
|
|
main(['backtesting'])
|
2018-01-06 10:21:09 +00:00
|
|
|
assert backtesting_mock.call_count == 1
|
|
|
|
call_args = backtesting_mock.call_args[0][0]
|
2019-02-20 14:54:20 +00:00
|
|
|
assert call_args.config == ['config.json']
|
2018-01-06 10:21:09 +00:00
|
|
|
assert call_args.live is False
|
2018-07-19 19:12:27 +00:00
|
|
|
assert call_args.loglevel == 0
|
2018-01-06 10:21:09 +00:00
|
|
|
assert call_args.subparser == 'backtesting'
|
|
|
|
assert call_args.func is not None
|
2018-01-30 07:04:28 +00:00
|
|
|
assert call_args.ticker_interval is None
|
2018-01-06 10:21:09 +00:00
|
|
|
|
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
def test_main_start_hyperopt(mocker) -> None:
|
2019-05-25 18:01:43 +00:00
|
|
|
hyperopt_mock = mocker.patch('freqtrade.optimize.start_hyperopt', MagicMock())
|
2018-02-04 09:21:16 +00:00
|
|
|
main(['hyperopt'])
|
2018-01-06 10:21:09 +00:00
|
|
|
assert hyperopt_mock.call_count == 1
|
|
|
|
call_args = hyperopt_mock.call_args[0][0]
|
2019-02-20 14:54:20 +00:00
|
|
|
assert call_args.config == ['config.json']
|
2018-07-19 19:12:27 +00:00
|
|
|
assert call_args.loglevel == 0
|
2018-01-06 10:21:09 +00:00
|
|
|
assert call_args.subparser == 'hyperopt'
|
|
|
|
assert call_args.func is not None
|
2017-09-08 13:51:00 +00:00
|
|
|
|
|
|
|
|
2018-06-07 20:28:21 +00:00
|
|
|
def test_main_fatal_exception(mocker, default_conf, caplog) -> None:
|
2018-06-17 20:38:31 +00:00
|
|
|
patch_exchange(mocker)
|
2019-03-26 08:07:24 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
|
|
|
|
mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=Exception))
|
2018-06-07 20:28:21 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.configuration.Configuration._load_config_file',
|
|
|
|
lambda *args, **kwargs: default_conf
|
|
|
|
)
|
|
|
|
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
|
2019-03-26 08:07:24 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
|
2018-06-07 20:28:21 +00:00
|
|
|
|
2018-03-03 21:39:39 +00:00
|
|
|
args = ['-c', 'config.json.example']
|
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
# Test Main + the KeyboardInterrupt exception
|
2018-06-07 20:28:21 +00:00
|
|
|
with pytest.raises(SystemExit):
|
2018-03-03 21:39:39 +00:00
|
|
|
main(args)
|
2018-06-07 20:28:21 +00:00
|
|
|
assert log_has('Using config: config.json.example ...', caplog.record_tuples)
|
|
|
|
assert log_has('Fatal exception!', caplog.record_tuples)
|
|
|
|
|
2018-02-04 09:21:16 +00:00
|
|
|
|
2018-06-07 20:28:21 +00:00
|
|
|
def test_main_keyboard_interrupt(mocker, default_conf, caplog) -> None:
|
2018-06-17 20:38:31 +00:00
|
|
|
patch_exchange(mocker)
|
2019-03-26 08:07:24 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
|
|
|
|
mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=KeyboardInterrupt))
|
2018-02-04 09:21:16 +00:00
|
|
|
mocker.patch(
|
2018-06-07 20:28:21 +00:00
|
|
|
'freqtrade.configuration.Configuration._load_config_file',
|
|
|
|
lambda *args, **kwargs: default_conf
|
2018-01-04 14:39:01 +00:00
|
|
|
)
|
2018-06-07 20:28:21 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
|
2019-03-26 08:07:24 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
|
2018-06-07 20:28:21 +00:00
|
|
|
|
|
|
|
args = ['-c', 'config.json.example']
|
|
|
|
|
|
|
|
# Test Main + the KeyboardInterrupt exception
|
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
main(args)
|
|
|
|
assert log_has('Using config: config.json.example ...', caplog.record_tuples)
|
|
|
|
assert log_has('SIGINT received, aborting ...', caplog.record_tuples)
|
|
|
|
|
|
|
|
|
|
|
|
def test_main_operational_exception(mocker, default_conf, caplog) -> None:
|
2018-06-17 20:38:31 +00:00
|
|
|
patch_exchange(mocker)
|
2019-03-26 08:07:24 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
|
2019-03-29 23:19:43 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.worker.Worker._worker',
|
|
|
|
MagicMock(side_effect=OperationalException('Oh snap!'))
|
|
|
|
)
|
2018-06-07 20:28:21 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.configuration.Configuration._load_config_file',
|
|
|
|
lambda *args, **kwargs: default_conf
|
|
|
|
)
|
|
|
|
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
|
2019-03-26 08:07:24 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
|
2018-06-07 20:28:21 +00:00
|
|
|
|
|
|
|
args = ['-c', 'config.json.example']
|
|
|
|
|
|
|
|
# Test Main + the KeyboardInterrupt exception
|
2018-02-04 09:21:16 +00:00
|
|
|
with pytest.raises(SystemExit):
|
2018-03-03 21:39:39 +00:00
|
|
|
main(args)
|
2018-06-07 20:28:21 +00:00
|
|
|
assert log_has('Using config: config.json.example ...', caplog.record_tuples)
|
|
|
|
assert log_has('Oh snap!', caplog.record_tuples)
|
2018-06-09 02:29:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_main_reload_conf(mocker, default_conf, caplog) -> None:
|
2018-06-17 20:38:31 +00:00
|
|
|
patch_exchange(mocker)
|
2019-03-26 08:07:24 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
|
2019-04-30 17:32:03 +00:00
|
|
|
# Simulate Running, reload, running workflow
|
|
|
|
worker_mock = MagicMock(side_effect=[State.RUNNING,
|
|
|
|
State.RELOAD_CONF,
|
|
|
|
State.RUNNING,
|
|
|
|
OperationalException("Oh snap!")])
|
|
|
|
mocker.patch('freqtrade.worker.Worker._worker', worker_mock)
|
2018-06-09 02:29:48 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.configuration.Configuration._load_config_file',
|
|
|
|
lambda *args, **kwargs: default_conf
|
|
|
|
)
|
2019-04-30 17:32:03 +00:00
|
|
|
reconfigure_mock = mocker.patch('freqtrade.main.Worker._reconfigure', MagicMock())
|
|
|
|
|
2018-06-09 02:29:48 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
|
2019-03-26 08:07:24 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
|
2018-06-09 02:29:48 +00:00
|
|
|
|
2019-04-30 17:32:03 +00:00
|
|
|
args = Arguments(['-c', 'config.json.example'], '').get_parsed_arg()
|
|
|
|
worker = Worker(args=args, config=default_conf)
|
2018-06-09 02:29:48 +00:00
|
|
|
with pytest.raises(SystemExit):
|
|
|
|
main(['-c', 'config.json.example'])
|
|
|
|
|
|
|
|
assert log_has('Using config: config.json.example ...', caplog.record_tuples)
|
2019-04-30 17:32:03 +00:00
|
|
|
assert worker_mock.call_count == 4
|
|
|
|
assert reconfigure_mock.call_count == 1
|
|
|
|
assert isinstance(worker.freqtrade, FreqtradeBot)
|
2018-06-09 02:29:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_reconfigure(mocker, default_conf) -> None:
|
2018-06-17 20:38:31 +00:00
|
|
|
patch_exchange(mocker)
|
2019-03-26 08:07:24 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock())
|
2019-03-29 23:19:43 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.worker.Worker._worker',
|
|
|
|
MagicMock(side_effect=OperationalException('Oh snap!'))
|
|
|
|
)
|
2018-06-09 02:29:48 +00:00
|
|
|
mocker.patch(
|
|
|
|
'freqtrade.configuration.Configuration._load_config_file',
|
|
|
|
lambda *args, **kwargs: default_conf
|
|
|
|
)
|
|
|
|
mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock())
|
2019-03-26 08:07:24 +00:00
|
|
|
mocker.patch('freqtrade.freqtradebot.persistence.init', MagicMock())
|
2018-06-09 02:29:48 +00:00
|
|
|
|
2019-03-26 09:42:19 +00:00
|
|
|
args = Arguments(['-c', 'config.json.example'], '').get_parsed_arg()
|
|
|
|
worker = Worker(args=args, config=default_conf)
|
|
|
|
freqtrade = worker.freqtrade
|
2018-06-09 02:29:48 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
)
|
|
|
|
|
2019-03-26 09:42:19 +00:00
|
|
|
worker._config = conf
|
2018-06-09 02:29:48 +00:00
|
|
|
# reconfigure should return a new instance
|
2019-03-26 09:42:19 +00:00
|
|
|
worker._reconfigure()
|
|
|
|
freqtrade2 = worker.freqtrade
|
2018-06-09 02:29:48 +00:00
|
|
|
|
|
|
|
# Verify we have a new instance with the new config
|
|
|
|
assert freqtrade is not freqtrade2
|
|
|
|
assert freqtrade.config['stake_amount'] + 1 == freqtrade2.config['stake_amount']
|