Skip to content

Commit

Permalink
edit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
muddymudskipper committed Jun 27, 2024
1 parent 62d6097 commit 73a7234
Showing 1 changed file with 37 additions and 42 deletions.
79 changes: 37 additions & 42 deletions tests/test_reason.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


@pytest.fixture()
def _setup_reason(request: pytest.FixtureRequest) -> None:
def _setup(request: pytest.FixtureRequest) -> None:
"""Set up"""
res = post(REASON_DATA_GRAPH_IRI, Path(__path__[0]) / "dataset_owl.ttl", replace=True)
if res.status_code != 204: # noqa: PLR2004
Expand All @@ -37,32 +37,26 @@ def _setup_reason(request: pytest.FixtureRequest) -> None:
if res.status_code != 204: # noqa: PLR2004
raise ValueError(f"Response {res.status_code}: {res.url}")

request.addfinalizer(lambda: delete(REASON_DATA_GRAPH_IRI))
request.addfinalizer(lambda: delete(REASON_ONTOLOGY_GRAPH_IRI))
request.addfinalizer(lambda: delete(REASON_RESULT_GRAPH_IRI)) # noqa: PT021


@pytest.fixture()
def _setup_validate(request: pytest.FixtureRequest) -> None:
"""Set up"""
with suppress(Exception):
delete_project(PROJECT_ID)
make_new_project(PROJECT_ID)

res = post(
VALIDATE_ONTOLOGY_GRAPH_IRI, Path(__path__[0]) / "test_validate_ontology.ttl", replace=True
)
if res.status_code != 204: # noqa: PLR2004
raise ValueError(f"Response {res.status_code}: {res.url}")

request.addfinalizer(lambda: delete(REASON_DATA_GRAPH_IRI))
request.addfinalizer(lambda: delete(REASON_ONTOLOGY_GRAPH_IRI))
request.addfinalizer(lambda: delete(REASON_RESULT_GRAPH_IRI))
request.addfinalizer(lambda: delete_project(PROJECT_ID))
request.addfinalizer(lambda: delete(OUTPUT_GRAPH_IRI))
request.addfinalizer(lambda: delete(VALIDATE_ONTOLOGY_GRAPH_IRI))
request.addfinalizer(lambda: Path(MD_FILENAME).unlink()) # noqa: PT021


@needs_cmem
def tests_reason(_setup_reason: None) -> None:
def tests(_setup: None) -> None:
"""Tests for reason plugin"""

def test_reasoner(reasoner: str, err_list: list) -> list:
Expand All @@ -89,42 +83,43 @@ def test_reasoner(reasoner: str, err_list: list) -> list:
err_list.append(reasoner)
return err_list

errors: list[str] = []
def test_validate(errors: str) -> str:
ValidatePlugin(
ontology_graph_iri=VALIDATE_ONTOLOGY_GRAPH_IRI,
produce_graph=True,
output_graph_iri=OUTPUT_GRAPH_IRI,
write_md=True,
md_filename=MD_FILENAME,
).execute((), context=TestExecutionContext(PROJECT_ID))

with Path(MD_FILENAME).open("wb") as md:
md.write(get_resource(PROJECT_ID, MD_FILENAME))
mdfile_test = Path(__path__[0]) / "test_validate.md"
errors = ""
if not cmp(MD_FILENAME, mdfile_test):
errors += "Markdown file error ."

output_graph = Graph().parse(
data=get(OUTPUT_GRAPH_IRI, owl_imports_resolution=False).text,
)
output_graph.remove((URIRef(OUTPUT_GRAPH_IRI), DCTERMS.created, None))
output_graph.remove((URIRef(OUTPUT_GRAPH_IRI), RDFS.label, None))
output_graph.remove((None, RDF.type, OWL.AnnotationProperty))
test = Graph().parse(Path(__path__[0]) / "test_validate_output.ttl", format="turtle")
if to_isomorphic(output_graph) != to_isomorphic(test):
errors += "Output graph error. "
return errors

errors_list: list[str] = []
reasoners = ["elk", "emr", "hermit", "jfact", "structural", "whelk"]
for reasoner in reasoners:
errors = test_reasoner(reasoner, errors)

if errors:
raise AssertionError(f"Test failed for reasoners: {', '.join(errors)}")
errors_list = test_reasoner(reasoner, errors_list)


@needs_cmem
def tests_validate(_setup_validate: None) -> None:
"""Tests for validate plugin"""
ValidatePlugin(
ontology_graph_iri=VALIDATE_ONTOLOGY_GRAPH_IRI,
produce_graph=True,
output_graph_iri=OUTPUT_GRAPH_IRI,
write_md=True,
md_filename=MD_FILENAME,
).execute((), context=TestExecutionContext(PROJECT_ID))

with Path(MD_FILENAME).open("wb") as md:
md.write(get_resource(PROJECT_ID, MD_FILENAME))
mdfile_test = Path(__path__[0]) / "test_validate.md"
errors = ""
if not cmp(MD_FILENAME, mdfile_test):
errors += "Markdown file error ."
if errors_list:
errors += f"Test failed for reasoners: {', '.join(errors_list)}. "

output_graph = Graph().parse(
data=get(OUTPUT_GRAPH_IRI, owl_imports_resolution=False).text,
)
output_graph.remove((URIRef(OUTPUT_GRAPH_IRI), DCTERMS.created, None))
output_graph.remove((URIRef(OUTPUT_GRAPH_IRI), RDFS.label, None))
output_graph.remove((None, RDF.type, OWL.AnnotationProperty))
test = Graph().parse(Path(__path__[0]) / "test_validate_output.ttl", format="turtle")
if to_isomorphic(output_graph) != to_isomorphic(test):
errors += "Output graph error. "
errors = test_validate(errors)

if errors:
raise AssertionError(errors[:-1])

0 comments on commit 73a7234

Please sign in to comment.