diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..de19c9f --- /dev/null +++ b/pytest.ini @@ -0,0 +1,2 @@ +[pytest] +testpaths = tests \ No newline at end of file diff --git a/tests/test_validation.py b/tests/test_validation.py new file mode 100644 index 0000000..836a235 --- /dev/null +++ b/tests/test_validation.py @@ -0,0 +1,24 @@ +import sys +import os +import pytest + +# Add root directory to Python path +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) + +from utils.validation import validate_file + + +def test_valid_file(tmp_path): + file = tmp_path / "test.nii" + file.write_text("dummy") + assert validate_file(str(file)) is True + + +def test_invalid_extension(): + with pytest.raises(ValueError): + validate_file("file.txt") + + +def test_missing_file(): + with pytest.raises(FileNotFoundError): + validate_file("missing.nii") \ No newline at end of file diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/validation.py b/utils/validation.py new file mode 100644 index 0000000..f538fae --- /dev/null +++ b/utils/validation.py @@ -0,0 +1,13 @@ +import os + +def validate_file(path: str) -> bool: + + # FIRST check extension + if not path.endswith(".nii"): + raise ValueError("Only .nii files allowed") + + # THEN check existence + if not os.path.exists(path): + raise FileNotFoundError(f"{path} not found") + + return True \ No newline at end of file