From c194474e73d9aea5cde30fe87f613671ea074fe2 Mon Sep 17 00:00:00 2001 From: Pulind Gadhia Date: Tue, 21 Apr 2026 21:04:16 +0530 Subject: [PATCH] Added validation module with pytest tests for input handling --- pytest.ini | 2 ++ tests/test_validation.py | 24 ++++++++++++++++++++++++ utils/__init__.py | 0 utils/validation.py | 13 +++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 pytest.ini create mode 100644 tests/test_validation.py create mode 100644 utils/__init__.py create mode 100644 utils/validation.py 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