| 1 | # pragma pylint: disable=missing-docstring, protected-access, invalid-name |
| 2 | import os |
| 3 | from pathlib import Path |
| 4 | from unittest.mock import MagicMock |
| 5 | |
| 6 | import pytest |
| 7 | |
| 8 | from freqtrade.configuration.directory_operations import (chown_user_directory, copy_sample_files, |
| 9 | create_datadir, create_userdata_dir) |
| 10 | from freqtrade.exceptions import OperationalException |
| 11 | from tests.conftest import log_has, log_has_re |
| 12 | |
| 13 | |
| 14 | def test_create_datadir(mocker, default_conf, caplog) -> None: |
| 15 | mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) |
| 16 | md = mocker.patch.object(Path, 'mkdir', MagicMock()) |
| 17 | |
| 18 | create_datadir(default_conf, '/foo/bar') |
| 19 | assert md.call_args[1]['parents'] is True |
| 20 | assert log_has('Created data directory: /foo/bar', caplog) |
| 21 | |
| 22 | |
| 23 | def test_create_userdata_dir(mocker, default_conf, caplog) -> None: |
| 24 | mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) |
| 25 | md = mocker.patch.object(Path, 'mkdir', MagicMock()) |
| 26 | |
| 27 | x = create_userdata_dir('/tmp/bar', create_dir=True) |
| 28 | assert md.call_count == 9 |
| 29 | assert md.call_args[1]['parents'] is False |
| 30 | assert log_has(f'Created user-data directory: {Path("/tmp/bar")}', caplog) |
| 31 | assert isinstance(x, Path) |
| 32 | assert str(x) == str(Path("/tmp/bar")) |
| 33 | |
| 34 | |
| 35 | def test_create_userdata_dir_and_chown(mocker, tmpdir, caplog) -> None: |
| 36 | sp_mock = mocker.patch('subprocess.check_output') |
| 37 | path = Path(tmpdir / 'bar') |
| 38 | assert not path.is_dir() |
| 39 | |
| 40 | x = create_userdata_dir(str(path), create_dir=True) |
| 41 | assert sp_mock.call_count == 0 |
| 42 | assert log_has(f'Created user-data directory: {path}', caplog) |
| 43 | assert isinstance(x, Path) |
| 44 | assert path.is_dir() |
| 45 | assert (path / 'data').is_dir() |
| 46 | |
| 47 | os.environ['FT_APP_ENV'] = 'docker' |
| 48 | chown_user_directory(path / 'data') |
| 49 | assert sp_mock.call_count == 1 |
| 50 | del os.environ['FT_APP_ENV'] |
| 51 | |
| 52 | |
| 53 | def test_create_userdata_dir_exists(mocker, default_conf, caplog) -> None: |
| 54 | mocker.patch.object(Path, "is_dir", MagicMock(return_value=True)) |
| 55 | md = mocker.patch.object(Path, 'mkdir', MagicMock()) |
| 56 | |
| 57 | create_userdata_dir('/tmp/bar') |
| 58 | assert md.call_count == 0 |
| 59 | |
| 60 | |
| 61 | def test_create_userdata_dir_exists_exception(mocker, default_conf, caplog) -> None: |
| 62 | mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) |
| 63 | md = mocker.patch.object(Path, 'mkdir', MagicMock()) |
| 64 | |
| 65 | with pytest.raises(OperationalException, |
| 66 | match=r'Directory `.{1,2}tmp.{1,2}bar` does not exist.*'): |
| 67 | create_userdata_dir('/tmp/bar', create_dir=False) |
| 68 | assert md.call_count == 0 |
| 69 | |
| 70 | |
| 71 | def test_copy_sample_files(mocker, default_conf, caplog) -> None: |
| 72 | mocker.patch.object(Path, "is_dir", MagicMock(return_value=True)) |
| 73 | mocker.patch.object(Path, "exists", MagicMock(return_value=False)) |
| 74 | copymock = mocker.patch('shutil.copy', MagicMock()) |
| 75 | |
| 76 | copy_sample_files(Path('/tmp/bar')) |
| 77 | assert copymock.call_count == 5 |
| 78 | assert copymock.call_args_list[0][0][1] == str( |
| 79 | Path('/tmp/bar') / 'strategies/sample_strategy.py') |
| 80 | assert copymock.call_args_list[1][0][1] == str( |
| 81 | Path('/tmp/bar') / 'hyperopts/sample_hyperopt_advanced.py') |
| 82 | assert copymock.call_args_list[2][0][1] == str( |
| 83 | Path('/tmp/bar') / 'hyperopts/sample_hyperopt_loss.py') |
| 84 | assert copymock.call_args_list[3][0][1] == str( |
| 85 | Path('/tmp/bar') / 'hyperopts/sample_hyperopt.py') |
| 86 | assert copymock.call_args_list[4][0][1] == str( |
| 87 | Path('/tmp/bar') / 'notebooks/strategy_analysis_example.ipynb') |
| 88 | |
| 89 | |
| 90 | def test_copy_sample_files_errors(mocker, default_conf, caplog) -> None: |
| 91 | mocker.patch.object(Path, "is_dir", MagicMock(return_value=False)) |
| 92 | mocker.patch.object(Path, "exists", MagicMock(return_value=False)) |
| 93 | mocker.patch('shutil.copy', MagicMock()) |
| 94 | with pytest.raises(OperationalException, |
| 95 | match=r"Directory `.{1,2}tmp.{1,2}bar` does not exist\."): |
| 96 | copy_sample_files(Path('/tmp/bar')) |
| 97 | |
| 98 | mocker.patch.object(Path, "is_dir", MagicMock(side_effect=[True, False])) |
| 99 | |
| 100 | with pytest.raises(OperationalException, |
| 101 | match=r"Directory `.{1,2}tmp.{1,2}bar.{1,2}strategies` does not exist\."): |
| 102 | copy_sample_files(Path('/tmp/bar')) |
| 103 | mocker.patch.object(Path, "is_dir", MagicMock(return_value=True)) |
| 104 | mocker.patch.object(Path, "exists", MagicMock(return_value=True)) |
| 105 | copy_sample_files(Path('/tmp/bar')) |
| 106 | assert log_has_re(r"File `.*` exists already, not deploying sample file\.", caplog) |
| 107 | caplog.clear() |
| 108 | copy_sample_files(Path('/tmp/bar'), overwrite=True) |
| 109 | assert log_has_re(r"File `.*` exists already, overwriting\.", caplog) |