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