freqtrade_origin/tests/test_directory_operations.py

111 lines
4.2 KiB
Python
Raw Normal View History

# pragma pylint: disable=missing-docstring, protected-access, invalid-name
2021-04-08 18:07:52 +00:00
import os
from pathlib import Path
from unittest.mock import MagicMock
import pytest
2024-05-12 13:08:40 +00:00
from freqtrade.configuration.directory_operations import (
chown_user_directory,
copy_sample_files,
create_datadir,
create_userdata_dir,
)
from freqtrade.exceptions import OperationalException
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)
2024-06-08 07:40:32 +00:00
def test_create_userdata_dir(mocker, tmp_path, caplog) -> None:
mocker.patch.object(Path, "is_dir", MagicMock(return_value=False))
md = mocker.patch.object(Path, "mkdir", MagicMock())
2024-06-08 15:41:05 +00:00
x = create_userdata_dir(tmp_path / "bar", create_dir=True)
assert md.call_count == 10
assert md.call_args[1]["parents"] is False
2024-06-08 15:41:05 +00:00
assert log_has(f'Created user-data directory: {tmp_path / "bar"}', caplog)
assert isinstance(x, Path)
2024-06-08 18:26:50 +00:00
assert str(x) == str(tmp_path / "bar")
2023-11-05 15:25:23 +00:00
def test_create_userdata_dir_and_chown(mocker, tmp_path, caplog) -> None:
sp_mock = mocker.patch("subprocess.check_output")
path = tmp_path / "bar"
2021-04-08 18:07:52 +00:00
assert not path.is_dir()
x = create_userdata_dir(str(path), create_dir=True)
assert sp_mock.call_count == 0
assert log_has(f"Created user-data directory: {path}", caplog)
2021-04-08 18:07:52 +00:00
assert isinstance(x, Path)
assert path.is_dir()
assert (path / "data").is_dir()
2021-04-08 18:07:52 +00:00
os.environ["FT_APP_ENV"] = "docker"
chown_user_directory(path / "data")
2021-04-08 18:07:52 +00:00
assert sp_mock.call_count == 1
del os.environ["FT_APP_ENV"]
2021-04-08 18:07:52 +00:00
2024-06-08 07:40:32 +00:00
def test_create_userdata_dir_exists(mocker, tmp_path) -> None:
mocker.patch.object(Path, "is_dir", MagicMock(return_value=True))
md = mocker.patch.object(Path, "mkdir", MagicMock())
2024-06-08 07:40:32 +00:00
create_userdata_dir(f"{tmp_path}/bar")
assert md.call_count == 0
2024-06-08 07:42:01 +00:00
def test_create_userdata_dir_exists_exception(mocker, tmp_path) -> None:
mocker.patch.object(Path, "is_dir", MagicMock(return_value=False))
md = mocker.patch.object(Path, "mkdir", MagicMock())
2024-06-08 07:42:01 +00:00
with pytest.raises(OperationalException, match=r"Directory `.*.{1,2}bar` does not exist.*"):
create_userdata_dir(f"{tmp_path}/bar", create_dir=False)
assert md.call_count == 0
2024-06-08 07:40:32 +00:00
def test_copy_sample_files(mocker, tmp_path) -> 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())
2024-06-08 07:40:32 +00:00
copy_sample_files(Path(f"{tmp_path}/bar"))
assert copymock.call_count == 3
2024-06-08 15:41:05 +00:00
assert copymock.call_args_list[0][0][1] == str(tmp_path / "bar/strategies/sample_strategy.py")
2019-11-17 09:11:58 +00:00
assert copymock.call_args_list[1][0][1] == str(
2024-06-08 18:26:50 +00:00
tmp_path / "bar/hyperopts/sample_hyperopt_loss.py"
)
assert copymock.call_args_list[2][0][1] == str(
2024-06-08 18:26:50 +00:00
tmp_path / "bar/notebooks/strategy_analysis_example.ipynb"
)
2024-06-08 07:42:01 +00:00
def test_copy_sample_files_errors(mocker, tmp_path, 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())
2024-06-08 07:42:01 +00:00
with pytest.raises(OperationalException, match=r"Directory `.*.{1,2}bar` does not exist\."):
copy_sample_files(Path(f"{tmp_path}/bar"))
mocker.patch.object(Path, "is_dir", MagicMock(side_effect=[True, False]))
with pytest.raises(
OperationalException,
2024-06-08 07:42:01 +00:00
match=r"Directory `.*.{1,2}bar.{1,2}strategies` does not exist\.",
):
2024-06-08 07:42:01 +00:00
copy_sample_files(Path(f"{tmp_path}/bar"))
mocker.patch.object(Path, "is_dir", MagicMock(return_value=True))
mocker.patch.object(Path, "exists", MagicMock(return_value=True))
2024-06-08 07:42:01 +00:00
copy_sample_files(Path(f"{tmp_path}/bar"))
2019-11-01 13:08:55 +00:00
assert log_has_re(r"File `.*` exists already, not deploying sample file\.", caplog)
caplog.clear()
2024-06-08 07:42:01 +00:00
copy_sample_files(Path(f"{tmp_path}/bar"), overwrite=True)
2019-11-01 13:08:55 +00:00
assert log_has_re(r"File `.*` exists already, overwriting\.", caplog)