From 73a7234bf5741f79d0228c92262a723d50f9109b Mon Sep 17 00:00:00 2001 From: muddymudskipper Date: Thu, 27 Jun 2024 11:23:11 +0100 Subject: [PATCH] edit tests --- tests/test_reason.py | 79 +++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 42 deletions(-) diff --git a/tests/test_reason.py b/tests/test_reason.py index 95e90ad..79d2cf8 100644 --- a/tests/test_reason.py +++ b/tests/test_reason.py @@ -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 @@ -37,24 +37,18 @@ 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)) @@ -62,7 +56,7 @@ def _setup_validate(request: pytest.FixtureRequest) -> None: @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: @@ -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])