diff --git a/mapping_workbench/backend/shacl_test_suite/adapters/validator_exporter.py b/mapping_workbench/backend/shacl_test_suite/adapters/validator_exporter.py new file mode 100644 index 000000000..9fd4d4a86 --- /dev/null +++ b/mapping_workbench/backend/shacl_test_suite/adapters/validator_exporter.py @@ -0,0 +1,32 @@ +import abc +from typing import Any, ClassVar + +from jinja2 import Environment, PackageLoader + +from mapping_workbench.backend.shacl_test_suite.models.validator import SHACLFileResourceValidationResult +from mapping_workbench.backend.shacl_test_suite.resources import SHACL_TEST_SUITE_EXECUTION_HTML_REPORT_TEMPLATE +from mapping_workbench.backend.test_data_suite.adapters.validator_exporter import TestDataValidatorExporter + + +class SHACLValidatorExporter(TestDataValidatorExporter): + + @abc.abstractmethod + def export(self, value: Any) -> Any: + """Export the validator. + + :param value: The value to export. + :type value: Any + """ + pass + + +class SHACLValidatorExporterHTML(SHACLValidatorExporter): + """Export SHACL validator result to HTML.""" + + html_template: ClassVar[Any] = Environment( + loader=PackageLoader("mapping_workbench.backend.shacl_test_suite.resources", "templates")) + + def export(self, shacl_result: SHACLFileResourceValidationResult) -> str: + validation_results: dict = shacl_result.model_dump() + html_report = self.html_template.get_template(SHACL_TEST_SUITE_EXECUTION_HTML_REPORT_TEMPLATE).render(validation_results) + return html_report diff --git a/mapping_workbench/backend/shacl_test_suite/resources/templates/shacl_shape_validation_results_report.jinja2 b/mapping_workbench/backend/shacl_test_suite/resources/templates/shacl_shape_validation_results_report.jinja2 index a11cbaccd..795f66352 100644 --- a/mapping_workbench/backend/shacl_test_suite/resources/templates/shacl_shape_validation_results_report.jinja2 +++ b/mapping_workbench/backend/shacl_test_suite/resources/templates/shacl_shape_validation_results_report.jinja2 @@ -101,18 +101,18 @@ - {{ validation_results.identifier }} - {{ validation_results.conforms }} - {{ validation_results.error }} + {{ identifier }} + {{ conforms }} + {{ error }} -{% if validation_results.results_dict %} +{% if results_dict %}

Extended results

-

{{ validation_results.identifier }}

+

{{ identifier }}

@@ -124,7 +124,7 @@ - {% for biding in validation_results.results_dict.results.bindings %} + {% for biding in results_dict.results.bindings %} diff --git a/mapping_workbench/backend/test_data_suite/adapters/validator_exporter.py b/mapping_workbench/backend/test_data_suite/adapters/validator_exporter.py new file mode 100644 index 000000000..07a4d5419 --- /dev/null +++ b/mapping_workbench/backend/test_data_suite/adapters/validator_exporter.py @@ -0,0 +1,31 @@ +import abc +from typing import Any + +from pydantic import BaseModel + +from mapping_workbench.backend import DEFAULT_MODEL_CONFIG + + +class ValidatorExporterABC(abc.ABC): + """Abstract base class for validator exporters.""" + + @abc.abstractmethod + def export(self) -> None: + """Export the validator.""" + pass + + + +class TestDataValidatorExporter(abc.ABC, BaseModel): + """Base class for validator exporters.""" + + model_config = DEFAULT_MODEL_CONFIG + + @abc.abstractmethod + def export(self, value: Any) -> Any: + """Export the validator. + + :param value: The value to export. + :type value: Any + """ + pass \ No newline at end of file diff --git a/tests/unit/backend/shacl_test_suite/test_shacl_exporter.py b/tests/unit/backend/shacl_test_suite/test_shacl_exporter.py new file mode 100644 index 000000000..b4647fc72 --- /dev/null +++ b/tests/unit/backend/shacl_test_suite/test_shacl_exporter.py @@ -0,0 +1,24 @@ +from mapping_workbench.backend.shacl_test_suite.adapters.validator import SHACLValidator +from mapping_workbench.backend.shacl_test_suite.adapters.validator_exporter import SHACLValidatorExporterHTML + +from mapping_workbench.backend.shacl_test_suite.models.entity import SHACLTestSuite +from mapping_workbench.backend.test_data_suite.models.entity import TestDataFileResource + + +def test_shacl_html_exporter(dummy_test_data_file_resource: TestDataFileResource, + dummy_shacl_test_suite: SHACLTestSuite): + shacl_validator = SHACLValidator(test_data=dummy_test_data_file_resource) + shacl_html_exporter = SHACLValidatorExporterHTML() + + validator_result = shacl_validator.validate(shacl_files=dummy_shacl_test_suite.file_resources) + + html_report = shacl_html_exporter.export(validator_result) + + assert html_report is not None + + assert dummy_test_data_file_resource.filename in html_report + assert validator_result.identifier in html_report + assert validator_result.conforms in html_report + # TODO: to continue + # for notice_id in validator_result.notice_ids: + # assert notice_id in html_report
{{ biding.focusNode.value }} {{ biding.resultPath.value }}