| 1 | # pragma pylint: disable=missing-docstring |
| 2 | |
| 3 | from copy import deepcopy |
| 4 | from pathlib import Path |
| 5 | from unittest.mock import MagicMock, PropertyMock |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from freqtrade.commands import Arguments |
| 10 | from freqtrade.exceptions import FreqtradeException, OperationalException |
| 11 | from freqtrade.freqtradebot import FreqtradeBot |
| 12 | from freqtrade.main import main |
| 13 | from freqtrade.state import State |
| 14 | from freqtrade.worker import Worker |
| 15 | from tests.conftest import (log_has, log_has_re, patch_exchange, |
| 16 | patched_configuration_load_config_file) |
| 17 | |
| 18 | |
| 19 | def test_parse_args_None(caplog) -> None: |
| 20 | with pytest.raises(SystemExit): |
| 21 | main([]) |
| 22 | assert log_has_re(r"Usage of Freqtrade requires a subcommand.*", caplog) |
| 23 | |
| 24 | |
| 25 | def test_parse_args_backtesting(mocker) -> None: |
| 26 | """ |
| 27 | Test that main() can start backtesting and also ensure we can pass some specific arguments |
| 28 | further argument parsing is done in test_arguments.py |
| 29 | """ |
| 30 | mocker.patch.object(Path, "is_file", MagicMock(side_effect=[False, True])) |
| 31 | backtesting_mock = mocker.patch('freqtrade.commands.start_backtesting') |
| 32 | backtesting_mock.__name__ = PropertyMock("start_backtesting") |
| 33 | # it's sys.exit(0) at the end of backtesting |
| 34 | with pytest.raises(SystemExit): |
| 35 | main(['backtesting']) |
| 36 | assert backtesting_mock.call_count == 1 |
| 37 | call_args = backtesting_mock.call_args[0][0] |
| 38 | assert call_args['config'] == ['config.json'] |
| 39 | assert call_args['verbosity'] == 0 |
| 40 | assert call_args['command'] == 'backtesting' |
| 41 | assert call_args['func'] is not None |
| 42 | assert callable(call_args['func']) |
| 43 | assert call_args['timeframe'] is None |
| 44 | |
| 45 | |
| 46 | def test_main_start_hyperopt(mocker) -> None: |
| 47 | mocker.patch.object(Path, 'is_file', MagicMock(side_effect=[False, True])) |
| 48 | hyperopt_mock = mocker.patch('freqtrade.commands.start_hyperopt', MagicMock()) |
| 49 | hyperopt_mock.__name__ = PropertyMock('start_hyperopt') |
| 50 | # it's sys.exit(0) at the end of hyperopt |
| 51 | with pytest.raises(SystemExit): |
| 52 | main(['hyperopt']) |
| 53 | assert hyperopt_mock.call_count == 1 |
| 54 | call_args = hyperopt_mock.call_args[0][0] |
| 55 | assert call_args['config'] == ['config.json'] |
| 56 | assert call_args['verbosity'] == 0 |
| 57 | assert call_args['command'] == 'hyperopt' |
| 58 | assert call_args['func'] is not None |
| 59 | assert callable(call_args['func']) |
| 60 | |
| 61 | |
| 62 | def test_main_fatal_exception(mocker, default_conf, caplog) -> None: |
| 63 | patch_exchange(mocker) |
| 64 | mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock()) |
| 65 | mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=Exception)) |
| 66 | patched_configuration_load_config_file(mocker, default_conf) |
| 67 | mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock()) |
| 68 | mocker.patch('freqtrade.freqtradebot.init_db', MagicMock()) |
| 69 | |
| 70 | args = ['trade', '-c', 'config_bittrex.json.example'] |
| 71 | |
| 72 | # Test Main + the KeyboardInterrupt exception |
| 73 | with pytest.raises(SystemExit): |
| 74 | main(args) |
| 75 | assert log_has('Using config: config_bittrex.json.example ...', caplog) |
| 76 | assert log_has('Fatal exception!', caplog) |
| 77 | |
| 78 | |
| 79 | def test_main_keyboard_interrupt(mocker, default_conf, caplog) -> None: |
| 80 | patch_exchange(mocker) |
| 81 | mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock()) |
| 82 | mocker.patch('freqtrade.worker.Worker._worker', MagicMock(side_effect=KeyboardInterrupt)) |
| 83 | patched_configuration_load_config_file(mocker, default_conf) |
| 84 | mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock()) |
| 85 | mocker.patch('freqtrade.wallets.Wallets.update', MagicMock()) |
| 86 | mocker.patch('freqtrade.freqtradebot.init_db', MagicMock()) |
| 87 | |
| 88 | args = ['trade', '-c', 'config_bittrex.json.example'] |
| 89 | |
| 90 | # Test Main + the KeyboardInterrupt exception |
| 91 | with pytest.raises(SystemExit): |
| 92 | main(args) |
| 93 | assert log_has('Using config: config_bittrex.json.example ...', caplog) |
| 94 | assert log_has('SIGINT received, aborting ...', caplog) |
| 95 | |
| 96 | |
| 97 | def test_main_operational_exception(mocker, default_conf, caplog) -> None: |
| 98 | patch_exchange(mocker) |
| 99 | mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock()) |
| 100 | mocker.patch( |
| 101 | 'freqtrade.worker.Worker._worker', |
| 102 | MagicMock(side_effect=FreqtradeException('Oh snap!')) |
| 103 | ) |
| 104 | patched_configuration_load_config_file(mocker, default_conf) |
| 105 | mocker.patch('freqtrade.wallets.Wallets.update', MagicMock()) |
| 106 | mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock()) |
| 107 | mocker.patch('freqtrade.freqtradebot.init_db', MagicMock()) |
| 108 | |
| 109 | args = ['trade', '-c', 'config_bittrex.json.example'] |
| 110 | |
| 111 | # Test Main + the KeyboardInterrupt exception |
| 112 | with pytest.raises(SystemExit): |
| 113 | main(args) |
| 114 | assert log_has('Using config: config_bittrex.json.example ...', caplog) |
| 115 | assert log_has('Oh snap!', caplog) |
| 116 | |
| 117 | |
| 118 | def test_main_operational_exception1(mocker, default_conf, caplog) -> None: |
| 119 | patch_exchange(mocker) |
| 120 | mocker.patch( |
| 121 | 'freqtrade.commands.list_commands.validate_exchanges', |
| 122 | MagicMock(side_effect=ValueError('Oh snap!')) |
| 123 | ) |
| 124 | patched_configuration_load_config_file(mocker, default_conf) |
| 125 | |
| 126 | args = ['list-exchanges'] |
| 127 | |
| 128 | # Test Main + the KeyboardInterrupt exception |
| 129 | with pytest.raises(SystemExit): |
| 130 | main(args) |
| 131 | |
| 132 | assert log_has('Fatal exception!', caplog) |
| 133 | assert not log_has_re(r'SIGINT.*', caplog) |
| 134 | mocker.patch( |
| 135 | 'freqtrade.commands.list_commands.validate_exchanges', |
| 136 | MagicMock(side_effect=KeyboardInterrupt) |
| 137 | ) |
| 138 | with pytest.raises(SystemExit): |
| 139 | main(args) |
| 140 | |
| 141 | assert log_has_re(r'SIGINT.*', caplog) |
| 142 | |
| 143 | |
| 144 | def test_main_reload_config(mocker, default_conf, caplog) -> None: |
| 145 | patch_exchange(mocker) |
| 146 | mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock()) |
| 147 | # Simulate Running, reload, running workflow |
| 148 | worker_mock = MagicMock(side_effect=[State.RUNNING, |
| 149 | State.RELOAD_CONFIG, |
| 150 | State.RUNNING, |
| 151 | OperationalException("Oh snap!")]) |
| 152 | mocker.patch('freqtrade.worker.Worker._worker', worker_mock) |
| 153 | patched_configuration_load_config_file(mocker, default_conf) |
| 154 | mocker.patch('freqtrade.wallets.Wallets.update', MagicMock()) |
| 155 | reconfigure_mock = mocker.patch('freqtrade.worker.Worker._reconfigure', MagicMock()) |
| 156 | |
| 157 | mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock()) |
| 158 | mocker.patch('freqtrade.freqtradebot.init_db', MagicMock()) |
| 159 | |
| 160 | args = Arguments(['trade', '-c', 'config_bittrex.json.example']).get_parsed_arg() |
| 161 | worker = Worker(args=args, config=default_conf) |
| 162 | with pytest.raises(SystemExit): |
| 163 | main(['trade', '-c', 'config_bittrex.json.example']) |
| 164 | |
| 165 | assert log_has('Using config: config_bittrex.json.example ...', caplog) |
| 166 | assert worker_mock.call_count == 4 |
| 167 | assert reconfigure_mock.call_count == 1 |
| 168 | assert isinstance(worker.freqtrade, FreqtradeBot) |
| 169 | |
| 170 | |
| 171 | def test_reconfigure(mocker, default_conf) -> None: |
| 172 | patch_exchange(mocker) |
| 173 | mocker.patch('freqtrade.freqtradebot.FreqtradeBot.cleanup', MagicMock()) |
| 174 | mocker.patch( |
| 175 | 'freqtrade.worker.Worker._worker', |
| 176 | MagicMock(side_effect=OperationalException('Oh snap!')) |
| 177 | ) |
| 178 | mocker.patch('freqtrade.wallets.Wallets.update', MagicMock()) |
| 179 | patched_configuration_load_config_file(mocker, default_conf) |
| 180 | mocker.patch('freqtrade.freqtradebot.RPCManager', MagicMock()) |
| 181 | mocker.patch('freqtrade.freqtradebot.init_db', MagicMock()) |
| 182 | |
| 183 | args = Arguments(['trade', '-c', 'config_bittrex.json.example']).get_parsed_arg() |
| 184 | worker = Worker(args=args, config=default_conf) |
| 185 | freqtrade = worker.freqtrade |
| 186 | |
| 187 | # Renew mock to return modified data |
| 188 | conf = deepcopy(default_conf) |
| 189 | conf['stake_amount'] += 1 |
| 190 | patched_configuration_load_config_file(mocker, conf) |
| 191 | |
| 192 | worker._config = conf |
| 193 | # reconfigure should return a new instance |
| 194 | worker._reconfigure() |
| 195 | freqtrade2 = worker.freqtrade |
| 196 | |
| 197 | # Verify we have a new instance with the new config |
| 198 | assert freqtrade is not freqtrade2 |
| 199 | assert freqtrade.config['stake_amount'] + 1 == freqtrade2.config['stake_amount'] |