diff --git a/src/controller.py b/src/controller.py index d31ec9c..7f78e60 100644 --- a/src/controller.py +++ b/src/controller.py @@ -1,3 +1,4 @@ +from src.validation import validate_extracted_data from src.file_manipulator import FileManipulator class Controller: @@ -5,7 +6,12 @@ def __init__(self): self.file_manipulator = FileManipulator() def fill_form(self, user_input: str, fields: list, pdf_form_path: str): - return self.file_manipulator.fill_form(user_input, fields, pdf_form_path) + data = self.file_manipulator.fill_form(user_input, fields, pdf_form_path) + + if not validate_extracted_data(data): + raise ValueError("Invalid extracted data") + + return data def create_template(self, pdf_path: str): - return self.file_manipulator.create_template(pdf_path) \ No newline at end of file + return self.file_manipulator.create_template(pdf_path) diff --git a/src/schema.py b/src/schema.py new file mode 100644 index 0000000..ba1866c --- /dev/null +++ b/src/schema.py @@ -0,0 +1,7 @@ +REQUIRED_FIELDS = { + "incident_type": str, + "location": str, + "incident_time": str, + "units_involved": list, + "summary": str +} diff --git a/src/validation.py b/src/validation.py new file mode 100644 index 0000000..eeaa16c --- /dev/null +++ b/src/validation.py @@ -0,0 +1,11 @@ +from src.schema import REQUIRED_FIELDS + +def validate_extracted_data(data: dict) -> bool: + for field, field_type in REQUIRED_FIELDS.items(): + if field not in data: + return False + if not isinstance(data[field], field_type): + return False + if data[field] in ["", None]: + return False + return True diff --git a/tests/src/tests/test_validation.py b/tests/src/tests/test_validation.py new file mode 100644 index 0000000..3ebb8e9 --- /dev/null +++ b/tests/src/tests/test_validation.py @@ -0,0 +1,19 @@ +def test_valid_data(): + data = { + "incident_type": "Fire", + "location": "Downtown", + "incident_time": "2026-03-20 10:00", + "units_involved": ["Unit1", "Unit2"], + "summary": "Fire contained" + } + assert validate_extracted_data(data) == True + +def test_wrong_type(): + data = { + "incident_type": "Fire", + "location": "Downtown", + "incident_time": "2026-03-20 10:00", + "units_involved": "Unit1", # should be list + "summary": "Fire contained" + } + assert validate_extracted_data(data) == False diff --git a/tests/test_controller.py b/tests/test_controller.py new file mode 100644 index 0000000..541deb6 --- /dev/null +++ b/tests/test_controller.py @@ -0,0 +1,22 @@ +from src.controller import Controller + + +def test_fill_form_validation_fail(monkeypatch): + controller = Controller() + + def mock_fill_form(user_input, fields, pdf_form_path): + return { + "incident_type": "", # invalid (empty) + "location": "Downtown", + "incident_time": "2026-03-20T10:30:00", + "units_involved": ["Unit 42"], + "summary": "Minor incident reported" + } + + monkeypatch.setattr(controller.file_manipulator, "fill_form", mock_fill_form) + + try: + controller.fill_form("input", [], "file.pdf") + assert False # Should not reach here + except ValueError: + assert True diff --git a/tests/test_vallidation.py b/tests/test_vallidation.py new file mode 100644 index 0000000..3ebb8e9 --- /dev/null +++ b/tests/test_vallidation.py @@ -0,0 +1,19 @@ +def test_valid_data(): + data = { + "incident_type": "Fire", + "location": "Downtown", + "incident_time": "2026-03-20 10:00", + "units_involved": ["Unit1", "Unit2"], + "summary": "Fire contained" + } + assert validate_extracted_data(data) == True + +def test_wrong_type(): + data = { + "incident_type": "Fire", + "location": "Downtown", + "incident_time": "2026-03-20 10:00", + "units_involved": "Unit1", # should be list + "summary": "Fire contained" + } + assert validate_extracted_data(data) == False