| 1 | # pragma pylint: disable=missing-docstring, C0103 |
| 2 | import argparse |
| 3 | from pathlib import Path |
| 4 | from unittest.mock import MagicMock |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from freqtrade.commands import Arguments |
| 9 | from freqtrade.commands.cli_options import check_int_nonzero, check_int_positive |
| 10 | |
| 11 | |
| 12 | # Parse common command-line-arguments. Used for all tools |
| 13 | def test_parse_args_none() -> None: |
| 14 | arguments = Arguments(['trade']) |
| 15 | assert isinstance(arguments, Arguments) |
| 16 | x = arguments.get_parsed_arg() |
| 17 | assert isinstance(x, dict) |
| 18 | assert isinstance(arguments.parser, argparse.ArgumentParser) |
| 19 | |
| 20 | |
| 21 | def test_parse_args_defaults(mocker) -> None: |
| 22 | mocker.patch.object(Path, 'is_file', MagicMock(side_effect=[False, True])) |
| 23 | args = Arguments(['trade']).get_parsed_arg() |
| 24 | assert args['config'] == ['config.json'] |
| 25 | assert args['strategy_path'] is None |
| 26 | assert args['datadir'] is None |
| 27 | assert args['verbosity'] == 0 |
| 28 | |
| 29 | |
| 30 | def test_parse_args_default_userdatadir(mocker) -> None: |
| 31 | mocker.patch.object(Path, 'is_file', MagicMock(return_value=True)) |
| 32 | args = Arguments(['trade']).get_parsed_arg() |
| 33 | # configuration defaults to user_data if that is available. |
| 34 | assert args['config'] == [str(Path('user_data/config.json'))] |
| 35 | assert args['strategy_path'] is None |
| 36 | assert args['datadir'] is None |
| 37 | assert args['verbosity'] == 0 |
| 38 | |
| 39 | |
| 40 | def test_parse_args_userdatadir(mocker) -> None: |
| 41 | mocker.patch.object(Path, 'is_file', MagicMock(return_value=True)) |
| 42 | args = Arguments(['trade', '--user-data-dir', 'user_data']).get_parsed_arg() |
| 43 | # configuration defaults to user_data if that is available. |
| 44 | assert args['config'] == [str(Path('user_data/config.json'))] |
| 45 | assert args['strategy_path'] is None |
| 46 | assert args['datadir'] is None |
| 47 | assert args['verbosity'] == 0 |
| 48 | |
| 49 | |
| 50 | def test_parse_args_config() -> None: |
| 51 | args = Arguments(['trade', '-c', '/dev/null']).get_parsed_arg() |
| 52 | assert args['config'] == ['/dev/null'] |
| 53 | |
| 54 | args = Arguments(['trade', '--config', '/dev/null']).get_parsed_arg() |
| 55 | assert args['config'] == ['/dev/null'] |
| 56 | |
| 57 | args = Arguments(['trade', '--config', '/dev/null', |
| 58 | '--config', '/dev/zero'],).get_parsed_arg() |
| 59 | assert args['config'] == ['/dev/null', '/dev/zero'] |
| 60 | |
| 61 | |
| 62 | def test_parse_args_db_url() -> None: |
| 63 | args = Arguments(['trade', '--db-url', 'sqlite:///test.sqlite']).get_parsed_arg() |
| 64 | assert args['db_url'] == 'sqlite:///test.sqlite' |
| 65 | |
| 66 | |
| 67 | def test_parse_args_verbose() -> None: |
| 68 | args = Arguments(['trade', '-v']).get_parsed_arg() |
| 69 | assert args['verbosity'] == 1 |
| 70 | |
| 71 | args = Arguments(['trade', '--verbose']).get_parsed_arg() |
| 72 | assert args['verbosity'] == 1 |
| 73 | |
| 74 | |
| 75 | def test_common_scripts_options() -> None: |
| 76 | args = Arguments(['download-data', '-p', 'ETH/BTC', 'XRP/BTC']).get_parsed_arg() |
| 77 | |
| 78 | assert args['pairs'] == ['ETH/BTC', 'XRP/BTC'] |
| 79 | assert 'func' in args |
| 80 | |
| 81 | |
| 82 | def test_parse_args_version() -> None: |
| 83 | with pytest.raises(SystemExit, match=r'0'): |
| 84 | Arguments(['--version']).get_parsed_arg() |
| 85 | |
| 86 | |
| 87 | def test_parse_args_invalid() -> None: |
| 88 | with pytest.raises(SystemExit, match=r'2'): |
| 89 | Arguments(['-c']).get_parsed_arg() |
| 90 | |
| 91 | |
| 92 | def test_parse_args_strategy() -> None: |
| 93 | args = Arguments(['trade', '--strategy', 'SomeStrategy']).get_parsed_arg() |
| 94 | assert args['strategy'] == 'SomeStrategy' |
| 95 | |
| 96 | |
| 97 | def test_parse_args_strategy_invalid() -> None: |
| 98 | with pytest.raises(SystemExit, match=r'2'): |
| 99 | Arguments(['--strategy']).get_parsed_arg() |
| 100 | |
| 101 | |
| 102 | def test_parse_args_strategy_path() -> None: |
| 103 | args = Arguments(['trade', '--strategy-path', '/some/path']).get_parsed_arg() |
| 104 | assert args['strategy_path'] == '/some/path' |
| 105 | |
| 106 | |
| 107 | def test_parse_args_strategy_path_invalid() -> None: |
| 108 | with pytest.raises(SystemExit, match=r'2'): |
| 109 | Arguments(['--strategy-path']).get_parsed_arg() |
| 110 | |
| 111 | |
| 112 | def test_parse_args_backtesting_invalid() -> None: |
| 113 | with pytest.raises(SystemExit, match=r'2'): |
| 114 | Arguments(['backtesting --ticker-interval']).get_parsed_arg() |
| 115 | |
| 116 | with pytest.raises(SystemExit, match=r'2'): |
| 117 | Arguments(['backtesting --ticker-interval', 'abc']).get_parsed_arg() |
| 118 | |
| 119 | |
| 120 | def test_parse_args_backtesting_custom() -> None: |
| 121 | args = [ |
| 122 | 'backtesting', |
| 123 | '-c', 'test_conf.json', |
| 124 | '--ticker-interval', '1m', |
| 125 | '--strategy-list', |
| 126 | 'DefaultStrategy', |
| 127 | 'SampleStrategy' |
| 128 | ] |
| 129 | call_args = Arguments(args).get_parsed_arg() |
| 130 | assert call_args['config'] == ['test_conf.json'] |
| 131 | assert call_args['verbosity'] == 0 |
| 132 | assert call_args['command'] == 'backtesting' |
| 133 | assert call_args['func'] is not None |
| 134 | assert call_args['timeframe'] == '1m' |
| 135 | assert type(call_args['strategy_list']) is list |
| 136 | assert len(call_args['strategy_list']) == 2 |
| 137 | |
| 138 | |
| 139 | def test_parse_args_hyperopt_custom() -> None: |
| 140 | args = [ |
| 141 | 'hyperopt', |
| 142 | '-c', 'test_conf.json', |
| 143 | '--epochs', '20', |
| 144 | '--spaces', 'buy' |
| 145 | ] |
| 146 | call_args = Arguments(args).get_parsed_arg() |
| 147 | assert call_args['config'] == ['test_conf.json'] |
| 148 | assert call_args['epochs'] == 20 |
| 149 | assert call_args['verbosity'] == 0 |
| 150 | assert call_args['command'] == 'hyperopt' |
| 151 | assert call_args['spaces'] == ['buy'] |
| 152 | assert call_args['func'] is not None |
| 153 | assert callable(call_args['func']) |
| 154 | |
| 155 | |
| 156 | def test_download_data_options() -> None: |
| 157 | args = [ |
| 158 | 'download-data', |
| 159 | '--datadir', 'datadir/directory', |
| 160 | '--pairs-file', 'file_with_pairs', |
| 161 | '--days', '30', |
| 162 | '--exchange', 'binance' |
| 163 | ] |
| 164 | pargs = Arguments(args).get_parsed_arg() |
| 165 | |
| 166 | assert pargs['pairs_file'] == 'file_with_pairs' |
| 167 | assert pargs['datadir'] == 'datadir/directory' |
| 168 | assert pargs['days'] == 30 |
| 169 | assert pargs['exchange'] == 'binance' |
| 170 | |
| 171 | |
| 172 | def test_plot_dataframe_options() -> None: |
| 173 | args = [ |
| 174 | 'plot-dataframe', |
| 175 | '-c', 'config_bittrex.json.example', |
| 176 | '--indicators1', 'sma10', 'sma100', |
| 177 | '--indicators2', 'macd', 'fastd', 'fastk', |
| 178 | '--plot-limit', '30', |
| 179 | '-p', 'UNITTEST/BTC', |
| 180 | ] |
| 181 | pargs = Arguments(args).get_parsed_arg() |
| 182 | |
| 183 | assert pargs['indicators1'] == ['sma10', 'sma100'] |
| 184 | assert pargs['indicators2'] == ['macd', 'fastd', 'fastk'] |
| 185 | assert pargs['plot_limit'] == 30 |
| 186 | assert pargs['pairs'] == ['UNITTEST/BTC'] |
| 187 | |
| 188 | |
| 189 | def test_plot_profit_options() -> None: |
| 190 | args = [ |
| 191 | 'plot-profit', |
| 192 | '-p', 'UNITTEST/BTC', |
| 193 | '--trade-source', 'DB', |
| 194 | '--db-url', 'sqlite:///whatever.sqlite', |
| 195 | ] |
| 196 | pargs = Arguments(args).get_parsed_arg() |
| 197 | |
| 198 | assert pargs['trade_source'] == 'DB' |
| 199 | assert pargs['pairs'] == ['UNITTEST/BTC'] |
| 200 | assert pargs['db_url'] == 'sqlite:///whatever.sqlite' |
| 201 | |
| 202 | |
| 203 | def test_config_notallowed(mocker) -> None: |
| 204 | mocker.patch.object(Path, 'is_file', MagicMock(return_value=False)) |
| 205 | args = [ |
| 206 | 'create-userdir', |
| 207 | ] |
| 208 | pargs = Arguments(args).get_parsed_arg() |
| 209 | |
| 210 | assert 'config' not in pargs |
| 211 | |
| 212 | # When file exists: |
| 213 | mocker.patch.object(Path, 'is_file', MagicMock(return_value=True)) |
| 214 | args = [ |
| 215 | 'create-userdir', |
| 216 | ] |
| 217 | pargs = Arguments(args).get_parsed_arg() |
| 218 | # config is not added even if it exists, since create-userdir is in the notallowed list |
| 219 | assert 'config' not in pargs |
| 220 | |
| 221 | |
| 222 | def test_config_notrequired(mocker) -> None: |
| 223 | mocker.patch.object(Path, 'is_file', MagicMock(return_value=False)) |
| 224 | args = [ |
| 225 | 'download-data', |
| 226 | ] |
| 227 | pargs = Arguments(args).get_parsed_arg() |
| 228 | |
| 229 | assert pargs['config'] is None |
| 230 | |
| 231 | # When file exists: |
| 232 | mocker.patch.object(Path, 'is_file', MagicMock(side_effect=[False, True])) |
| 233 | args = [ |
| 234 | 'download-data', |
| 235 | ] |
| 236 | pargs = Arguments(args).get_parsed_arg() |
| 237 | # config is added if it exists |
| 238 | assert pargs['config'] == ['config.json'] |
| 239 | |
| 240 | |
| 241 | def test_check_int_positive() -> None: |
| 242 | assert check_int_positive('3') == 3 |
| 243 | assert check_int_positive('1') == 1 |
| 244 | assert check_int_positive('100') == 100 |
| 245 | |
| 246 | with pytest.raises(argparse.ArgumentTypeError): |
| 247 | check_int_positive('-2') |
| 248 | |
| 249 | with pytest.raises(argparse.ArgumentTypeError): |
| 250 | check_int_positive('0') |
| 251 | |
| 252 | with pytest.raises(argparse.ArgumentTypeError): |
| 253 | check_int_positive(0) |
| 254 | |
| 255 | with pytest.raises(argparse.ArgumentTypeError): |
| 256 | check_int_positive('3.5') |
| 257 | |
| 258 | with pytest.raises(argparse.ArgumentTypeError): |
| 259 | check_int_positive('DeadBeef') |
| 260 | |
| 261 | |
| 262 | def test_check_int_nonzero() -> None: |
| 263 | assert check_int_nonzero('3') == 3 |
| 264 | assert check_int_nonzero('1') == 1 |
| 265 | assert check_int_nonzero('100') == 100 |
| 266 | |
| 267 | assert check_int_nonzero('-2') == -2 |
| 268 | |
| 269 | with pytest.raises(argparse.ArgumentTypeError): |
| 270 | check_int_nonzero('0') |
| 271 | |
| 272 | with pytest.raises(argparse.ArgumentTypeError): |
| 273 | check_int_nonzero(0) |
| 274 | |
| 275 | with pytest.raises(argparse.ArgumentTypeError): |
| 276 | check_int_nonzero('3.5') |
| 277 | |
| 278 | with pytest.raises(argparse.ArgumentTypeError): |
| 279 | check_int_nonzero('DeadBeef') |