From 1d2ef5c2ce8d1fd0ce9310aaa38dbaeba0ca6258 Mon Sep 17 00:00:00 2001 From: Matthias Date: Fri, 1 Nov 2019 13:30:26 +0100 Subject: [PATCH] Extract directory_operation tests to it's own test file --- tests/test_configuration.py | 73 -------------------------- tests/test_directory_operations.py | 82 ++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 73 deletions(-) create mode 100644 tests/test_directory_operations.py diff --git a/tests/test_configuration.py b/tests/test_configuration.py index d74d64c95..e971d15ab 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -17,8 +17,6 @@ from freqtrade.configuration.config_validation import validate_config_schema from freqtrade.configuration.deprecated_settings import ( check_conflicting_settings, process_deprecated_setting, process_temporary_deprecated_settings) -from freqtrade.configuration.directory_operations import (create_datadir, - create_userdata_dir, copy_sample_files) from freqtrade.configuration.load_config import load_config_file from freqtrade.constants import DEFAULT_DB_DRYRUN_URL, DEFAULT_DB_PROD_URL from freqtrade.loggers import _set_loggers @@ -670,77 +668,6 @@ def test_validate_default_conf(default_conf) -> None: validate(default_conf, constants.CONF_SCHEMA, Draft4Validator) -def test_create_datadir(mocker, default_conf, caplog) -> None: - mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) - md = mocker.patch.object(Path, 'mkdir', MagicMock()) - - create_datadir(default_conf, '/foo/bar') - assert md.call_args[1]['parents'] is True - assert log_has('Created data directory: /foo/bar', caplog) - - -def test_create_userdata_dir(mocker, default_conf, caplog) -> None: - mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) - md = mocker.patch.object(Path, 'mkdir', MagicMock()) - - x = create_userdata_dir('/tmp/bar', create_dir=True) - assert md.call_count == 7 - assert md.call_args[1]['parents'] is False - assert log_has(f'Created user-data directory: {Path("/tmp/bar")}', caplog) - assert isinstance(x, Path) - assert str(x) == str(Path("/tmp/bar")) - - -def test_create_userdata_dir_exists(mocker, default_conf, caplog) -> None: - mocker.patch.object(Path, "is_dir", MagicMock(return_value=True)) - md = mocker.patch.object(Path, 'mkdir', MagicMock()) - - create_userdata_dir('/tmp/bar') - assert md.call_count == 0 - - -def test_create_userdata_dir_exists_exception(mocker, default_conf, caplog) -> None: - mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) - md = mocker.patch.object(Path, 'mkdir', MagicMock()) - - with pytest.raises(OperationalException, - match=r'Directory `.{1,2}tmp.{1,2}bar` does not exist.*'): - create_userdata_dir('/tmp/bar', create_dir=False) - assert md.call_count == 0 - - -def test_copy_sample_files(mocker, default_conf, caplog) -> None: - mocker.patch.object(Path, "is_dir", MagicMock(return_value=True)) - mocker.patch.object(Path, "exists", MagicMock(return_value=False)) - copymock = mocker.patch('shutil.copy', MagicMock()) - - copy_sample_files(Path('/tmp/bar')) - assert copymock.call_count == 4 - assert copymock.call_args_list[0][0][1] == '/tmp/bar/strategies/sample_strategy.py' - assert copymock.call_args_list[1][0][1] == '/tmp/bar/hyperopts/sample_hyperopt_advanced.py' - assert copymock.call_args_list[2][0][1] == '/tmp/bar/hyperopts/sample_hyperopt_loss.py' - assert copymock.call_args_list[3][0][1] == '/tmp/bar/hyperopts/sample_hyperopt.py' - - -def test_copy_sample_files_errors(mocker, default_conf, caplog) -> None: - mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) - mocker.patch.object(Path, "exists", MagicMock(return_value=False)) - mocker.patch('shutil.copy', MagicMock()) - with pytest.raises(OperationalException, - match=r"Directory `.{1,2}tmp.{1,2}bar` does not exist\."): - copy_sample_files(Path('/tmp/bar')) - - mocker.patch.object(Path, "is_dir", MagicMock(side_effect=[True, False])) - - with pytest.raises(OperationalException, - match=r"Directory `.{1,2}tmp.{1,2}bar.{1,2}strategies` does not exist\."): - copy_sample_files(Path('/tmp/bar')) - mocker.patch.object(Path, "is_dir", MagicMock(return_value=True)) - mocker.patch.object(Path, "exists", MagicMock(return_value=True)) - copy_sample_files(Path('/tmp/bar')) - assert log_has_re(r"File `.*` exists already, not deploying sample.*", caplog) - - def test_validate_tsl(default_conf): default_conf['stoploss'] = 0.0 with pytest.raises(OperationalException, match='The config stoploss needs to be different ' diff --git a/tests/test_directory_operations.py b/tests/test_directory_operations.py new file mode 100644 index 000000000..a7d98795b --- /dev/null +++ b/tests/test_directory_operations.py @@ -0,0 +1,82 @@ +# pragma pylint: disable=missing-docstring, protected-access, invalid-name +from pathlib import Path +from unittest.mock import MagicMock + +import pytest + +from freqtrade import OperationalException +from freqtrade.configuration.directory_operations import (copy_sample_files, + create_datadir, + create_userdata_dir) +from tests.conftest import log_has, log_has_re + + +def test_create_datadir(mocker, default_conf, caplog) -> None: + mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) + md = mocker.patch.object(Path, 'mkdir', MagicMock()) + + create_datadir(default_conf, '/foo/bar') + assert md.call_args[1]['parents'] is True + assert log_has('Created data directory: /foo/bar', caplog) + + +def test_create_userdata_dir(mocker, default_conf, caplog) -> None: + mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) + md = mocker.patch.object(Path, 'mkdir', MagicMock()) + + x = create_userdata_dir('/tmp/bar', create_dir=True) + assert md.call_count == 7 + assert md.call_args[1]['parents'] is False + assert log_has(f'Created user-data directory: {Path("/tmp/bar")}', caplog) + assert isinstance(x, Path) + assert str(x) == str(Path("/tmp/bar")) + + +def test_create_userdata_dir_exists(mocker, default_conf, caplog) -> None: + mocker.patch.object(Path, "is_dir", MagicMock(return_value=True)) + md = mocker.patch.object(Path, 'mkdir', MagicMock()) + + create_userdata_dir('/tmp/bar') + assert md.call_count == 0 + + +def test_create_userdata_dir_exists_exception(mocker, default_conf, caplog) -> None: + mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) + md = mocker.patch.object(Path, 'mkdir', MagicMock()) + + with pytest.raises(OperationalException, + match=r'Directory `.{1,2}tmp.{1,2}bar` does not exist.*'): + create_userdata_dir('/tmp/bar', create_dir=False) + assert md.call_count == 0 + + +def test_copy_sample_files(mocker, default_conf, caplog) -> None: + mocker.patch.object(Path, "is_dir", MagicMock(return_value=True)) + mocker.patch.object(Path, "exists", MagicMock(return_value=False)) + copymock = mocker.patch('shutil.copy', MagicMock()) + + copy_sample_files(Path('/tmp/bar')) + assert copymock.call_count == 4 + assert copymock.call_args_list[0][0][1] == '/tmp/bar/strategies/sample_strategy.py' + assert copymock.call_args_list[1][0][1] == '/tmp/bar/hyperopts/sample_hyperopt_advanced.py' + assert copymock.call_args_list[2][0][1] == '/tmp/bar/hyperopts/sample_hyperopt_loss.py' + assert copymock.call_args_list[3][0][1] == '/tmp/bar/hyperopts/sample_hyperopt.py' + + +def test_copy_sample_files_errors(mocker, default_conf, caplog) -> None: + mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) + mocker.patch.object(Path, "exists", MagicMock(return_value=False)) + mocker.patch('shutil.copy', MagicMock()) + with pytest.raises(OperationalException, + match=r"Directory `.{1,2}tmp.{1,2}bar` does not exist\."): + copy_sample_files(Path('/tmp/bar')) + + mocker.patch.object(Path, "is_dir", MagicMock(side_effect=[True, False])) + + with pytest.raises(OperationalException, + match=r"Directory `.{1,2}tmp.{1,2}bar.{1,2}strategies` does not exist\."): + copy_sample_files(Path('/tmp/bar')) + mocker.patch.object(Path, "is_dir", MagicMock(return_value=True)) + mocker.patch.object(Path, "exists", MagicMock(return_value=True)) + copy_sample_files(Path('/tmp/bar')) + assert log_has_re(r"File `.*` exists already, not deploying sample.*", caplog)