generated from mscheltienne/template-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mscheltienne
committed
Apr 29, 2024
1 parent
90bc4ed
commit 02cc651
Showing
1 changed file
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,66 @@ | ||
from __future__ import annotations | ||
|
||
import random | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
|
||
from fcbg_ruff.check.validator import validate_folder | ||
from fcbg_ruff.utils._path import walk_files | ||
|
||
if TYPE_CHECKING: | ||
from pathlib import Path | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def invalid_folder(folder: Path) -> tuple[Path, list[Path]]: | ||
"""Create a mock documentary structure with invalid file names.""" | ||
files = [elt for elt in walk_files(folder) if elt.parent.name != "__old"] | ||
invalid_files = random.sample(files, 4) | ||
# add a purely invalid fname | ||
invalid_files[0].rename(invalid_files[0].parent / "invalid_file_name") | ||
invalid_files[0] = invalid_files[0].parent / "invalid_file_name" | ||
# add a fname with code not matching parent folder | ||
fname = invalid_files[1].name.split("_") | ||
fname[0] += "a" | ||
fname = "_".join(fname) | ||
invalid_files[1].rename(invalid_files[1].parent / fname) | ||
invalid_files[1] = invalid_files[1].parent / fname | ||
# add a fname with invalid characters in the name | ||
fname = invalid_files[2].name.split("_") | ||
fname[2] += " a" | ||
fname = "_".join(fname) | ||
invalid_files[2].rename(invalid_files[2].parent / fname) | ||
invalid_files[2] = invalid_files[2].parent / fname | ||
# add a fname with a date in the past | ||
fname = invalid_files[3].name.split("_") | ||
fname[1] = f"40{fname[1][2:]}" | ||
fname = "_".join(fname) | ||
invalid_files[3].rename(invalid_files[3].parent / fname) | ||
invalid_files[3] = invalid_files[3].parent / fname | ||
return folder, invalid_files | ||
|
||
def test_validate_folder(folder): | ||
|
||
def test_validate_folder(folder: Path): | ||
"""Test validation of a documentary system tree.""" | ||
for elt in folder.iterdir(): | ||
violations = validate_folder(elt) | ||
assert len(violations["primary"]) == 0 | ||
assert len(violations["secondary"]) == 0 | ||
|
||
|
||
def test_validate_invalid_folder(invalid_folder: Path): | ||
"""Test validation of an invalid documentary system tree.""" | ||
folder = invalid_folder[0] | ||
invalid_files = invalid_folder[1] | ||
violations = {"primary": dict(), "secondary": dict()} | ||
for elt in folder.iterdir(): | ||
violations_ = validate_folder(elt) | ||
for key in ("primary", "secondary"): | ||
violations[key].update(violations_[key]) | ||
assert len(violations["secondary"]) == 0 | ||
assert violations["primary"][invalid_files[0]] == [1] | ||
assert violations["primary"][invalid_files[1]] == [11] | ||
assert violations["primary"][invalid_files[2]] == [3] | ||
assert violations["primary"][invalid_files[3]] == [21] |