272 lines
7.6 KiB
Python
272 lines
7.6 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from dopt_basics import io
|
|
|
|
FILE_SEARCH = "test.txt"
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def base_folder(tmp_path_factory) -> Path:
|
|
folder_structure = "path/to/base/folder/"
|
|
pth = tmp_path_factory.mktemp("search")
|
|
pth = pth / folder_structure
|
|
pth.mkdir(parents=True, exist_ok=True)
|
|
|
|
return pth
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def target_file_pth(base_folder) -> Path:
|
|
# place in folder 'path' of TMP path
|
|
target_folder = base_folder.parents[2]
|
|
target_file = target_folder / FILE_SEARCH
|
|
with open(target_file, "w") as file:
|
|
file.write("TEST")
|
|
|
|
return target_file
|
|
|
|
|
|
def test_save_load_pickle_Success(tmp_path):
|
|
save_obj = (1, 2, 3, 4, 5)
|
|
filename = "TEST"
|
|
io.save_pickle(save_obj, tmp_path, filename)
|
|
loaded = io.load_pickle(tmp_path, filename)
|
|
assert save_obj == loaded
|
|
|
|
|
|
def test_save_load_pickle_SuccessCreateFolders(tmp_path):
|
|
save_obj = (1, 2, 3, 4, 5)
|
|
filename = "TEST"
|
|
tmp_path = tmp_path / "test/dir/"
|
|
io.save_pickle(save_obj, tmp_path, filename, create_folder=True)
|
|
loaded = io.load_pickle(tmp_path, filename)
|
|
assert save_obj == loaded
|
|
|
|
|
|
def test_save_load_pickle_FailNonExistingFolders(tmp_path):
|
|
save_obj = (1, 2, 3, 4, 5)
|
|
filename = "TEST"
|
|
tmp_path = tmp_path / "test/dir/"
|
|
with pytest.raises(FileNotFoundError):
|
|
io.save_pickle(save_obj, tmp_path, filename, create_folder=False)
|
|
with pytest.raises(FileNotFoundError):
|
|
_ = io.load_pickle(tmp_path, filename)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"delete_existing",
|
|
[True, False],
|
|
)
|
|
def test_create_folder(tmp_path, delete_existing):
|
|
target_dir = tmp_path / "test"
|
|
assert not target_dir.exists()
|
|
io.create_folder(target_dir, delete_existing=delete_existing)
|
|
assert target_dir.exists()
|
|
assert target_dir.is_dir()
|
|
io.create_folder(target_dir, delete_existing=delete_existing)
|
|
assert target_dir.exists()
|
|
assert target_dir.is_dir()
|
|
|
|
|
|
def test_prepare_path_SuccessWithCreate(tmp_path):
|
|
base_folder = tmp_path
|
|
dirs = ("target", "dir")
|
|
filename = None
|
|
suffix = None
|
|
target_pth = tmp_path / "/".join(dirs)
|
|
res_pth = io.prepare_path(base_folder, dirs, filename, suffix, create_folder=True)
|
|
assert res_pth.exists()
|
|
assert res_pth == target_pth
|
|
|
|
|
|
def test_prepare_path_SuccessWithCreateTimestamp(tmp_path):
|
|
base_folder = tmp_path
|
|
dirs = ("target", "dir")
|
|
filename = "test"
|
|
suffix = ".pkl"
|
|
res_pth = io.prepare_path(
|
|
base_folder, dirs, filename, suffix, create_folder=True, include_timestamp=True
|
|
)
|
|
assert res_pth.parent.exists()
|
|
|
|
|
|
def test_prepare_path_SuccessWithoutCreate(tmp_path):
|
|
base_folder = tmp_path
|
|
dirs = ("target", "dir")
|
|
filename = None
|
|
suffix = None
|
|
target_pth = tmp_path / "/".join(dirs)
|
|
res_pth = io.prepare_path(base_folder, dirs, filename, suffix, create_folder=False)
|
|
assert not res_pth.exists()
|
|
assert res_pth == target_pth
|
|
|
|
|
|
def test_prepare_path_FailNoTargets(tmp_path):
|
|
base_folder = tmp_path
|
|
dirs = None
|
|
filename = None
|
|
suffix = None
|
|
with pytest.raises(ValueError):
|
|
_ = io.prepare_path(
|
|
base_folder,
|
|
dirs,
|
|
filename,
|
|
suffix,
|
|
create_folder=False,
|
|
)
|
|
|
|
|
|
def test_prepare_path_FailNoFilenameSuffix(tmp_path):
|
|
base_folder = tmp_path
|
|
dirs = None
|
|
filename = None
|
|
suffix = "pkl"
|
|
with pytest.raises(ValueError):
|
|
_ = io.prepare_path(
|
|
base_folder,
|
|
dirs,
|
|
filename,
|
|
suffix,
|
|
create_folder=False,
|
|
)
|
|
|
|
filename = "test"
|
|
suffix = None
|
|
with pytest.raises(ValueError):
|
|
_ = io.prepare_path(
|
|
base_folder,
|
|
dirs,
|
|
filename,
|
|
suffix,
|
|
create_folder=False,
|
|
)
|
|
|
|
|
|
def test_prepare_path_FailTimestampWithoutFilename(tmp_path):
|
|
base_folder = tmp_path
|
|
dirs = ["test"]
|
|
filename = None
|
|
suffix = None
|
|
with pytest.raises(ValueError):
|
|
_ = io.prepare_path(
|
|
base_folder,
|
|
dirs,
|
|
filename,
|
|
suffix,
|
|
create_folder=False,
|
|
include_timestamp=True,
|
|
)
|
|
|
|
|
|
def test_prepare_path_FailBadSuffix(tmp_path):
|
|
base_folder = tmp_path
|
|
dirs = None
|
|
filename = "test"
|
|
suffix = "."
|
|
with pytest.raises(ValueError):
|
|
_ = io.prepare_path(
|
|
base_folder,
|
|
dirs,
|
|
filename,
|
|
suffix,
|
|
create_folder=False,
|
|
include_timestamp=False,
|
|
)
|
|
|
|
|
|
def test_prepare_path_SuccessSuffixAddDot(tmp_path):
|
|
base_folder = tmp_path
|
|
dirs = None
|
|
filename = "test"
|
|
suffix = "pkl"
|
|
target_path = tmp_path / f"{filename}.{suffix}"
|
|
ret_path = io.prepare_path(
|
|
base_folder,
|
|
dirs,
|
|
filename,
|
|
suffix,
|
|
create_folder=False,
|
|
include_timestamp=False,
|
|
)
|
|
assert ret_path == target_path
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
["case", "expect"],
|
|
[
|
|
("http://test.com/ ", "http://test.com"),
|
|
("http://test.com/", "http://test.com"),
|
|
("http://test.com ", "http://test.com"),
|
|
("http://test.com// ", "http://test.com"),
|
|
("http://test.com", "http://test.com"),
|
|
(" /http://test.com", "http://test.com"),
|
|
(" //http://test.com", "http://test.com"),
|
|
("//http://test.com", "http://test.com"),
|
|
],
|
|
)
|
|
def test_strip_url_components(case, expect):
|
|
res = io._strip_url_components(case)
|
|
assert res == expect
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
["base", "route", "expect"],
|
|
[
|
|
("http://test.com/ ", "ping", "http://test.com/ping"),
|
|
("http://test.com/", "ping ", "http://test.com/ping"),
|
|
("http://test.com ", "ping/", "http://test.com/ping"),
|
|
("http://test.com// ", "ping", "http://test.com/ping"),
|
|
("http://test.com", "/ping ", "http://test.com/ping"),
|
|
(" /http://test.com", "/ ping/ ", "http://test.com/ping"),
|
|
(" //http://test.com", "ping", "http://test.com/ping"),
|
|
("//http://test.com", "// ping// ", "http://test.com/ping"),
|
|
],
|
|
)
|
|
def test_combine_route(base, route, expect):
|
|
res = io.combine_route(base, route)
|
|
assert res == expect
|
|
|
|
|
|
def test_search_cwd(monkeypatch, base_folder, target_file_pth):
|
|
monkeypatch.setattr(Path, "cwd", lambda: base_folder)
|
|
assert Path.cwd() == base_folder
|
|
ret = io.search_cwd(FILE_SEARCH)
|
|
assert ret is None
|
|
|
|
target_folder = target_file_pth.parent
|
|
monkeypatch.setattr(Path, "cwd", lambda: target_folder)
|
|
assert Path.cwd() == target_folder
|
|
ret = io.search_cwd(FILE_SEARCH)
|
|
assert ret is not None
|
|
assert ret == target_file_pth
|
|
|
|
|
|
@pytest.mark.parametrize("stop_folder_name", ["to", "base", None])
|
|
def test_search_file_iterative(base_folder, target_file_pth, stop_folder_name):
|
|
# target in parent of 'to': 'path'
|
|
ret = io.search_file_iterative(base_folder, FILE_SEARCH, stop_folder_name)
|
|
if stop_folder_name == "to" or stop_folder_name is None:
|
|
assert ret is not None
|
|
assert ret.name == FILE_SEARCH
|
|
assert ret == target_file_pth
|
|
elif stop_folder_name == "base":
|
|
assert ret is None
|
|
|
|
|
|
def test_search_folder_path(base_folder):
|
|
stop_folder = "123" # should not exist
|
|
found = io.search_folder_path(base_folder, stop_folder_name=stop_folder)
|
|
assert found is None
|
|
stop_folder = "to"
|
|
found = io.search_folder_path(base_folder, stop_folder_name=stop_folder)
|
|
assert found is not None
|
|
assert found.name == "path"
|
|
found = io.search_folder_path(
|
|
base_folder, stop_folder_name=stop_folder, return_inclusive=True
|
|
)
|
|
assert found is not None
|
|
assert found.name == "to"
|
|
assert found.parent.name == "path"
|