2019-11-01 12:30:26 +00:00
|
|
|
# pragma pylint: disable=missing-docstring, protected-access, invalid-name
|
2021-04-08 18:07:52 +00:00
|
|
|
import os
|
2019-11-01 12:30:26 +00:00
|
|
|
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,
|
|
|
|
)
|
2019-12-30 14:02:17 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2019-11-01 12:30:26 +00:00
|
|
|
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))
|
2024-05-12 13:35:47 +00:00
|
|
|
md = mocker.patch.object(Path, "mkdir", MagicMock())
|
2019-11-01 12:30:26 +00:00
|
|
|
|
2024-05-12 13:35:47 +00:00
|
|
|
create_datadir(default_conf, "/foo/bar")
|
|
|
|
assert md.call_args[1]["parents"] is True
|
|
|
|
assert log_has("Created data directory: /foo/bar", caplog)
|
2019-11-01 12:30:26 +00:00
|
|
|
|
|
|
|
|
2024-06-08 07:40:32 +00:00
|
|
|
def test_create_userdata_dir(mocker, tmp_path, caplog) -> None:
|
2019-11-01 12:30:26 +00:00
|
|
|
mocker.patch.object(Path, "is_dir", MagicMock(return_value=False))
|
2024-05-12 13:35:47 +00:00
|
|
|
md = mocker.patch.object(Path, "mkdir", MagicMock())
|
2019-11-01 12:30:26 +00:00
|
|
|
|
2024-06-08 15:41:05 +00:00
|
|
|
x = create_userdata_dir(tmp_path / "bar", create_dir=True)
|
2022-10-10 16:03:55 +00:00
|
|
|
assert md.call_count == 10
|
2024-05-12 13:35:47 +00:00
|
|
|
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)
|
2019-11-01 12:30:26 +00:00
|
|
|
assert isinstance(x, Path)
|
2024-06-08 18:26:50 +00:00
|
|
|
assert str(x) == str(tmp_path / "bar")
|
2019-11-01 12:30:26 +00:00
|
|
|
|
|
|
|
|
2023-11-05 15:25:23 +00:00
|
|
|
def test_create_userdata_dir_and_chown(mocker, tmp_path, caplog) -> None:
|
2024-05-12 13:35:47 +00:00
|
|
|
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
|
2024-05-12 13:35:47 +00:00
|
|
|
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()
|
2024-05-12 13:35:47 +00:00
|
|
|
assert (path / "data").is_dir()
|
2021-04-08 18:07:52 +00:00
|
|
|
|
2024-05-12 13:35:47 +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
|
2024-05-12 13:35:47 +00:00
|
|
|
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:
|
2019-11-01 12:30:26 +00:00
|
|
|
mocker.patch.object(Path, "is_dir", MagicMock(return_value=True))
|
2024-05-12 13:35:47 +00:00
|
|
|
md = mocker.patch.object(Path, "mkdir", MagicMock())
|
2019-11-01 12:30:26 +00:00
|
|
|
|
2024-06-08 07:40:32 +00:00
|
|
|
create_userdata_dir(f"{tmp_path}/bar")
|
2019-11-01 12:30:26 +00:00
|
|
|
assert md.call_count == 0
|
|
|
|
|
|
|
|
|
2024-06-08 07:42:01 +00:00
|
|
|
def test_create_userdata_dir_exists_exception(mocker, tmp_path) -> None:
|
2019-11-01 12:30:26 +00:00
|
|
|
mocker.patch.object(Path, "is_dir", MagicMock(return_value=False))
|
2024-05-12 13:35:47 +00:00
|
|
|
md = mocker.patch.object(Path, "mkdir", MagicMock())
|
2019-11-01 12:30:26 +00:00
|
|
|
|
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)
|
2019-11-01 12:30:26 +00:00
|
|
|
assert md.call_count == 0
|
|
|
|
|
|
|
|
|
2024-06-08 07:40:32 +00:00
|
|
|
def test_copy_sample_files(mocker, tmp_path) -> None:
|
2019-11-01 12:30:26 +00:00
|
|
|
mocker.patch.object(Path, "is_dir", MagicMock(return_value=True))
|
|
|
|
mocker.patch.object(Path, "exists", MagicMock(return_value=False))
|
2024-05-12 13:35:47 +00:00
|
|
|
copymock = mocker.patch("shutil.copy", MagicMock())
|
2019-11-01 12:30:26 +00:00
|
|
|
|
2024-06-08 07:40:32 +00:00
|
|
|
copy_sample_files(Path(f"{tmp_path}/bar"))
|
2021-09-11 15:52:47 +00:00
|
|
|
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"
|
2024-05-12 13:35:47 +00:00
|
|
|
)
|
2021-09-11 15:52:47 +00:00
|
|
|
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-05-12 13:35:47 +00:00
|
|
|
)
|
2019-11-01 12:30:26 +00:00
|
|
|
|
|
|
|
|
2024-06-08 07:42:01 +00:00
|
|
|
def test_copy_sample_files_errors(mocker, tmp_path, caplog) -> None:
|
2019-11-01 12:30:26 +00:00
|
|
|
mocker.patch.object(Path, "is_dir", MagicMock(return_value=False))
|
|
|
|
mocker.patch.object(Path, "exists", MagicMock(return_value=False))
|
2024-05-12 13:35:47 +00:00
|
|
|
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"))
|
2019-11-01 12:30:26 +00:00
|
|
|
|
|
|
|
mocker.patch.object(Path, "is_dir", MagicMock(side_effect=[True, False]))
|
|
|
|
|
2024-05-12 13:35:47 +00:00
|
|
|
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-05-12 13:35:47 +00:00
|
|
|
):
|
2024-06-08 07:42:01 +00:00
|
|
|
copy_sample_files(Path(f"{tmp_path}/bar"))
|
2019-11-01 12:30:26 +00:00
|
|
|
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)
|