diff --git a/src/controller.py b/src/controller.py index d31ec9c..7bc315c 100644 --- a/src/controller.py +++ b/src/controller.py @@ -1,11 +1,25 @@ +from src.template_mapper import TemplateMapper +from src.validation import validate_extracted_data from src.file_manipulator import FileManipulator class Controller: def __init__(self): self.file_manipulator = FileManipulator() - + self.mapper = TemplateMapper({ + "patient_name": "NameField", + "age": "AgeField", + "diagnosis": "DiagnosisField" +}) + def map_data(self, validated_data: dict): + return self.mapper.map_to_pdf_fields(validated_data) + 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/template_mapper.py b/src/template_mapper.py new file mode 100644 index 0000000..7e458d8 --- /dev/null +++ b/src/template_mapper.py @@ -0,0 +1,15 @@ +class TemplateMapper: + def __init__(self, mapping_config: dict): + """ + mapping_config: dict mapping JSON fields → PDF field names + """ + self.mapping = mapping_config + + def map_to_pdf_fields(self, structured_data: dict) -> dict: + mapped_data = {} + + for json_field, pdf_field in self.mapping.items(): + if json_field in structured_data: + mapped_data[pdf_field] = structured_data[json_field] + + return mapped_data 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_controller.py b/tests/src/tests/test_controller.py new file mode 100644 index 0000000..a182333 --- /dev/null +++ b/tests/src/tests/test_controller.py @@ -0,0 +1,16 @@ +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 {"patient_name": "", "age": 30, "diagnosis": "Flu"} + + 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/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_template_mapper.py b/tests/test_template_mapper.py new file mode 100644 index 0000000..27ee868 --- /dev/null +++ b/tests/test_template_mapper.py @@ -0,0 +1,43 @@ +from src.template_mapper import TemplateMapper + + +def test_mapping_success(): + mapping = { + "patient_name": "NameField", + "age": "AgeField", + "diagnosis": "DiagnosisField" + } + + data = { + "patient_name": "John Doe", + "age": 45, + "diagnosis": "Burn injury" + } + + mapper = TemplateMapper(mapping) + result = mapper.map_to_pdf_fields(data) + + assert result == { + "NameField": "John Doe", + "AgeField": 45, + "DiagnosisField": "Burn injury" + } + + +def test_missing_fields(): + mapping = { + "patient_name": "NameField", + "age": "AgeField", + "diagnosis": "DiagnosisField" + } + + data = { + "patient_name": "John Doe" + } + + mapper = TemplateMapper(mapping) + result = mapper.map_to_pdf_fields(data) + + assert result == { + "NameField": "John Doe" + }