Skip to content

Commit

Permalink
implement shacl validation exporter + test
Browse files Browse the repository at this point in the history
  • Loading branch information
duprijil committed Nov 17, 2023
1 parent 10513ac commit 55f2294
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 7 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,18 @@
</thead>
<tbody>
<tr>
<td>{{ validation_results.identifier }}</td>
<td{% if validation_results.conforms == "True" %}
class="success"{% endif %}>{{ validation_results.conforms }}</td>
<td>{{ validation_results.error }}</td>
<td>{{ identifier }}</td>
<td{% if conforms == "True" %}
class="success"{% endif %}>{{ conforms }}</td>
<td>{{ error }}</td>
</tr>
</tbody>
</table>

{% if validation_results.results_dict %}
{% if results_dict %}
<hr>
<h2>Extended results</h2>
<h1>{{ validation_results.identifier }}</h1>
<h1>{{ identifier }}</h1>
<table class="display" data-order='[[0, "asc"]]'>
<thead class="center aligned">
<tr>
Expand All @@ -124,7 +124,7 @@
</tr>
</thead>
<tbody>
{% for biding in validation_results.results_dict.results.bindings %}
{% for biding in results_dict.results.bindings %}
<tr>
<td>{{ biding.focusNode.value }}</td>
<td>{{ biding.resultPath.value }}</td>
Expand Down
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 tests/unit/backend/shacl_test_suite/test_shacl_exporter.py
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

0 comments on commit 55f2294

Please sign in to comment.