-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement shacl validation exporter + test
- Loading branch information
Showing
4 changed files
with
94 additions
and
7 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
mapping_workbench/backend/shacl_test_suite/adapters/validator_exporter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
mapping_workbench/backend/test_data_suite/adapters/validator_exporter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
24 changes: 24 additions & 0 deletions
24
tests/unit/backend/shacl_test_suite/test_shacl_exporter.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |