Skip to content

Commit

Permalink
cleaner test tree
Browse files Browse the repository at this point in the history
  • Loading branch information
mscheltienne committed Apr 26, 2024
1 parent b770a0d commit b1ebb2e
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions itvalidator/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pytest import Config


_MAX_TREE_DEPTH: int = 4
_MAX_TEST_TREE_DEPTH: int = 4


def pytest_configure(config: Config) -> None:
Expand All @@ -37,24 +37,23 @@ def pytest_configure(config: Config) -> None:
def folder(tmp_path: Path) -> Path:
"""Create a mock documentary structure."""
for code in "F1", "F2", "F3":
segments = random.randint(1, 3)
name = "_".join(str(uuid4()).split("-")[:segments])
curdir = tmp_path / f"_{code}_{name}"
curdir = tmp_path / f"_{code}_{_random_stem()}"
curdir.mkdir()
_create_tree(curdir, code, depth=1)
return tmp_path


def _random_name(code: str) -> str:
"""Create a random file/folder name."""
year = random.randint(19, 25)
month = random.randint(1, 12)
day = random.randint(1, 28)
datecode = f"{year:02d}{month:02d}{day:02d}"
segments = random.randint(1, 3)
name = "_".join(str(uuid4()).split("-")[:segments])
usercode = "".join(random.sample(string.ascii_lowercase, 3)).upper()
return f"{code}_{datecode}_{name}_{usercode}"
def _create_tree(folder: Path, code: str, depth: int) -> None:
"""Create a directory tree."""
_create_files(folder, code)
if depth == _MAX_TEST_TREE_DEPTH:
return
n_subfolders = random.randint(0, 3)
for k in range(n_subfolders + 1):
code_subfolder = f"{code}{string.ascii_lowercase[k]}"
curdir = folder / f"_{code_subfolder}_{_random_stem()}"
curdir.mkdir()
_create_tree(curdir, code_subfolder, depth + 1)


def _create_files(folder: Path, code: str) -> None:
Expand All @@ -71,14 +70,18 @@ def _create_files(folder: Path, code: str) -> None:
(folder / "__old" / fname).with_suffix(".txt").write_text("101")


def _create_tree(folder: Path, code: str, depth: int) -> None:
"""Create a directory tree."""
_create_files(folder, code)
if depth == _MAX_TREE_DEPTH:
return
n_subfolders = random.randint(0, 3)
for k in range(n_subfolders + 1):
code_subfolder = f"{code}{string.ascii_lowercase[k]}"
curdir = folder / f"_{_random_name(code_subfolder)}"
curdir.mkdir()
_create_tree(curdir, code_subfolder, depth + 1)
def _random_stem() -> str:
"""Create a random file/folder stem."""
segments = random.randint(1, 3)
return "_".join(str(uuid4()).split("-")[:segments])


def _random_name(code: str) -> str:
"""Create a random file name."""
year = random.randint(19, 25)
month = random.randint(1, 12)
day = random.randint(1, 28)
datecode = f"{year:02d}{month:02d}{day:02d}"
name = _random_stem()
usercode = "".join(random.sample(string.ascii_lowercase, 3)).upper()
return f"{code}_{datecode}_{name}_{usercode}"

0 comments on commit b1ebb2e

Please sign in to comment.