From dbd1ca86fd78425ea73021b8b42a0ad8b47f8698 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Tue, 16 Jan 2024 19:16:05 +0100 Subject: [PATCH 1/7] Updated the getattr patch Ensure that we can access annotations added by imported ontologies. --- ontopy/patch.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ontopy/patch.py b/ontopy/patch.py index 61c68e50c..2b744b5aa 100644 --- a/ontopy/patch.py +++ b/ontopy/patch.py @@ -29,6 +29,7 @@ def render_func(entity): # Extending ThingClass (classes) # ============================== +# Save a copy of the unpatched ThingClass.__getattr__() method. save_getattr = ThingClass.__getattr__ @@ -133,7 +134,13 @@ def _getattr(self, name): entity = self.namespace.ontology.get_by_label(name) # add annotation property to world._props for faster access later self.namespace.world._props[name] = entity - return save_getattr(self, entity.name) + + # Try first unpatched getattr method to avoid risking + # infinite recursion. + try: + return save_getattr(self, entity.name) + except AttributeError: + return getattr(self, entity.name) raise err From 8a0d55627817bd3e99d02abc8ac6098075f7a594 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sun, 25 Feb 2024 17:47:59 +0100 Subject: [PATCH 2/7] Added --iri and --base-iri options to ontoconvert for working around a bug in Owlready2 --- ontopy/ontology.py | 35 ++++++++++++++++++++++++++++------- tools/ontoconvert | 18 +++++++++++++++--- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/ontopy/ontology.py b/ontopy/ontology.py index 7820ebee3..955f01d09 100644 --- a/ontopy/ontology.py +++ b/ontopy/ontology.py @@ -905,7 +905,7 @@ def save( """ # pylint: disable=redefined-builtin,too-many-arguments # pylint: disable=too-many-statements,too-many-branches - # pylint: disable=too-many-locals,arguments-renamed + # pylint: disable=too-many-locals,arguments-renamed,invalid-name if not _validate_installed_version( package="rdflib", min_version="6.0.0" ) and format == FMAP.get("ttl", ""): @@ -992,21 +992,42 @@ def save( if squash: URIRef, RDF, OWL = rdflib.URIRef, rdflib.RDF, rdflib.OWL - iri = self.iri if self.iri else self.base_iri - graph = self.world.as_rdflib_graph() - graph.namespace_manager.bind("", rdflib.Namespace(iri)) + + # Make a copy of the owlready2 graph object to not mess with + # owlready2 internals + graph = rdflib.Graph() + graph_owlready2 = self.world.as_rdflib_graph() + for triple in graph_owlready2.triples((None, None, None)): + graph.add(triple) + + # Add namespaces + graph.namespace_manager.bind("", rdflib.Namespace(self.base_iri)) + graph.namespace_manager.bind( + "swrl", rdflib.Namespace("http://www.w3.org/2003/11/swrl#") + ) # Remove all ontology-declarations in the graph that are # not the current ontology. - for s, _, _ in graph.triples((None, RDF.type, OWL.Ontology)): + for s, _, _ in graph.triples( # pylint: disable=not-an-iterable + (None, RDF.type, OWL.Ontology) + ): if str(s).rstrip("/#") != self.base_iri.rstrip("/#"): - for _, p, o in graph.triples((s, None, None)): + for ( + _, + p, + o, + ) in graph.triples( # pylint: disable=not-an-iterable + (s, None, None) + ): graph.remove((s, p, o)) graph.remove((s, OWL.imports, None)) + # Insert correct IRI of the ontology if self.iri: base_iri = URIRef(self.base_iri) - for s, p, o in graph.triples((base_iri, None, None)): + for s, p, o in graph.triples( # pylint: disable=not-an-iterable + (base_iri, None, None) + ): graph.remove((s, p, o)) graph.add((URIRef(self.iri), p, o)) diff --git a/tools/ontoconvert b/tools/ontoconvert index 8c23570b4..e10870adc 100755 --- a/tools/ontoconvert +++ b/tools/ontoconvert @@ -86,13 +86,19 @@ def main(argv: list = None): action="store_true", help="Do not infer imported ontologies.", ) + parser.add_argument( + "--iri", + "-I", + help="IRI of converted ontology.", + ) parser.add_argument( "--base-iri", "-b", help=( - "Base iri of inferred ontology. The default is the base iri of " - 'the input ontology with "-inferred" appended to it. Used ' - "together with --reasoner." + "Base IRI of converted ontology. " + "This argument can be used to workaround the bug in Owlready2 " + "that changes the base IRI of the ontology to always end with a " + "slash." ), ) parser.add_argument( @@ -189,6 +195,12 @@ def main(argv: list = None): url_from_catalog=args.url_from_catalog, ) + if args.iri: + onto.iri = args.iri + + if args.base_iri: + onto.base_iri = args.base_iri + if args.annotate_source: annotate_source(onto) From 3fb3fbaa54ffb854c504c71e5fa3f0919747aad0 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Mon, 26 Feb 2024 11:40:40 +0100 Subject: [PATCH 3/7] Added a second ontoconvert test --- tests/tools/test_ontoconvert.py | 48 +++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/tests/tools/test_ontoconvert.py b/tests/tools/test_ontoconvert.py index fda0af10f..f0c330869 100644 --- a/tests/tools/test_ontoconvert.py +++ b/tests/tools/test_ontoconvert.py @@ -7,21 +7,53 @@ from typing import Callable -def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None: +if True: + # def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None: """Check that running `ontoconvert` works. Parameters: get_tool: Local module fixture to load a named tool as a module. See the current folder's `conftest.py` file. - tmpdir: A generic pytest fixture to generate a temporary directory, which will - exist only for the lifetime of this test function. - + tmpdir: A generic pytest fixture to generate a temporary directory, + which will exist only for the lifetime of this test function. """ + import re from pathlib import Path + import sys + import importlib + + testdir = Path(__file__).resolve().parent.parent + ontodir = testdir / "testonto" + outdir = testdir / "output" + toolsdir = testdir.parent / "tools" + + # ontoconvert = get_tool("ontoconvert") - test_file = ( - Path(__file__).resolve().parent.parent / "testonto" / "models.ttl" + sys.path.append(str(toolsdir)) + ontoconvert = importlib.machinery.SourceFileLoader( + "ontoconvert", str(toolsdir / "ontoconvert") + ).load_module() + + # Test 1 + ontoconvert.main( + [str(ontodir / "models.ttl"), str(outdir / "test_ontoconvert1.ttl")] ) - ontoconvert = get_tool("ontoconvert") + output1 = (outdir / "test_ontoconvert1.ttl").read_text() + assert re.search("@prefix : ", output1) + assert re.search(" .* owl:Ontology", output1) + assert re.search("testclass .* owl:Class", output1) - ontoconvert.main([str(test_file), str(tmpdir / "test.ttl")]) + # Test 2 - squash + ontoconvert.main( + [ + "-asw", + "--iri=https://w3id.org/ex/testonto", + "--base-iri=https://w3id.org/ex/testonto#", + str(ontodir / "testonto.ttl"), + str(outdir / "test_ontoconvert2.ttl"), + ] + ) + output2 = (outdir / "test_ontoconvert2.ttl").read_text() + assert re.search("@prefix : ", output2) + assert re.search(" .* owl:Ontology", output2) + assert re.search("testclass .* owl:Class", output2) From 6f49d4ddebf1bf5b42b9c100725a97624eb0d256 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Wed, 28 Feb 2024 12:48:56 +0100 Subject: [PATCH 4/7] Updated test to pytest --- tests/tools/test_ontoconvert.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/tools/test_ontoconvert.py b/tests/tools/test_ontoconvert.py index f0c330869..244b61529 100644 --- a/tests/tools/test_ontoconvert.py +++ b/tests/tools/test_ontoconvert.py @@ -7,8 +7,8 @@ from typing import Callable -if True: - # def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None: +# if True: +def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None: """Check that running `ontoconvert` works. Parameters: From 6e71479326d368b16d0e701b5aa714616e8b6422 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Wed, 28 Feb 2024 17:48:25 +0100 Subject: [PATCH 5/7] Replaced badly designed tests/tools/conftest with a general usable testutils module --- docs/api_reference/ontopy/testutils.md | 3 + ontopy/testutils.py | 29 +++++++ tests/tools/conftest.py | 114 ------------------------- tests/tools/test_emmocheck.py | 24 ++---- tests/tools/test_excel2onto.py | 40 ++++----- tests/tools/test_ontoconvert.py | 27 +----- tests/tools/test_ontodoc.py | 83 +++++------------- tests/tools/test_ontograph.py | 28 ++---- tests/tools/test_ontoversion.py | 25 ++---- tests/tools/test_redirectioncheck.py | 24 ++---- 10 files changed, 96 insertions(+), 301 deletions(-) create mode 100644 docs/api_reference/ontopy/testutils.md create mode 100644 ontopy/testutils.py delete mode 100644 tests/tools/conftest.py diff --git a/docs/api_reference/ontopy/testutils.md b/docs/api_reference/ontopy/testutils.md new file mode 100644 index 000000000..ea138234f --- /dev/null +++ b/docs/api_reference/ontopy/testutils.md @@ -0,0 +1,3 @@ +# testutils + +::: ontopy.testutils diff --git a/ontopy/testutils.py b/ontopy/testutils.py new file mode 100644 index 000000000..927c7f50f --- /dev/null +++ b/ontopy/testutils.py @@ -0,0 +1,29 @@ +"""Motule primarly intended to be imported by tests. + +It defines some directories and some utility functions that can be used +with and without conftest. +""" +import sys +from pathlib import Path +from importlib.util import spec_from_loader, module_from_spec +from importlib.machinery import SourceFileLoader + + +rootdir = Path(__file__).resolve().parent.parent +testdir = rootdir / "tests" +ontodir = testdir / "testonto" +outdir = testdir / "output" +toolsdir = rootdir / "tools" + + +def get_tool_module(name): + """Imports and returns the module for the EMMOntoPy tool + corresponding to `name`.""" + if str(toolsdir) not in sys.path: + sys.path.append(str(toolsdir)) + + # For Python 3.4+ + spec = spec_from_loader(name, SourceFileLoader(name, str(toolsdir / name))) + module = module_from_spec(spec) + spec.loader.exec_module(module) + return module diff --git a/tests/tools/conftest.py b/tests/tools/conftest.py deleted file mode 100644 index 61910dd74..000000000 --- a/tests/tools/conftest.py +++ /dev/null @@ -1,114 +0,0 @@ -"""Pytest fixtures for the `tools` dir only.""" -from typing import TYPE_CHECKING - -import pytest - -if TYPE_CHECKING: - from types import ModuleType - from typing import Callable - - -@pytest.fixture(scope="module", autouse=True) -def rename_tools() -> None: - """Add a `.py` extension to all tools. - - Run prior to all tests in this module. - First, rename all tools (adding a `.py` suffix) to make them importable as a - module. Then stop executing this fixture for a while (yield) and run all the tests - in the module. Then after they're done, rename the tools back (remove the `.py` - suffix) and also return `sys.path` to its original state prior to running the - tests. - To make the importability work, the `tools` folder had to be added to the - `sys.path`. - """ - from copy import deepcopy - import os - from pathlib import Path - import shutil - import sys - - original_sys_path = deepcopy(sys.path) - tools_path: Path = Path(__file__).resolve().parent.parent.parent / "tools" - - if str(tools_path) not in sys.path: - sys.path.append(str(tools_path)) - - # Add ".py" suffix to all tools - for ( - dirpath, - dirnames, - filenames, - ) in os.walk(tools_path): - if dirpath != str(tools_path): - continue - - if dirnames: - for dirname in dirnames: - if dirname == "__pycache__": - shutil.rmtree( - Path(dirpath) / "__pycache__", ignore_errors=True - ) - - for filename in filenames: - filepath = Path(dirpath) / filename - assert ( - filepath.suffix == "" - ), f"A suffix was found (not expected) for file: {filepath}" - - filepath.rename(filepath.with_suffix(".py")) - - yield - - # Remove ".py" suffix from all tools - for ( - dirpath, - dirnames, - filenames, - ) in os.walk(tools_path): - if dirpath != str(tools_path): - continue - - if dirnames: - for dirname in dirnames: - if dirname == "__pycache__": - shutil.rmtree( - Path(dirpath) / "__pycache__", ignore_errors=True - ) - - for filename in filenames: - filepath = Path(dirpath) / filename - assert ( - filepath.suffix == ".py" - ), f"A suffix was NOT found (not expected) for file: {filepath}" - - filepath.rename(filepath.with_suffix("")) - - sys.path = original_sys_path - - -@pytest.fixture -def get_tool() -> "Callable[[str], ModuleType]": - """Import a tool as a module. - - Requires the fixture `rename_tools` to have been run already. - """ - import importlib - from pathlib import Path - import sys - - def _get_tool(name: str) -> "ModuleType": - """Import and return named tool.""" - tool_path: Path = ( - Path(__file__).resolve().parent.parent.parent / "tools" / name - ).with_suffix(".py") - assert ( - str(tool_path.parent) in sys.path - ), f"'tools' dir not found in sys.path. Did `rename_tools` fixture run?\nsys.path: {sys.path}" - - assert ( - tool_path.exists() - ), f"The requested tool ({name}) was not found in {tool_path.parent}." - - return importlib.import_module(name) - - return _get_tool diff --git a/tests/tools/test_emmocheck.py b/tests/tools/test_emmocheck.py index bc3316610..705c205ec 100644 --- a/tests/tools/test_emmocheck.py +++ b/tests/tools/test_emmocheck.py @@ -1,24 +1,14 @@ """Test the `emmocheck` tool.""" -from typing import TYPE_CHECKING -if TYPE_CHECKING: - from types import ModuleType - from typing import Callable - -def test_run(get_tool: "Callable[[str], ModuleType]") -> None: - """Check that running `emmocheck` works. - - Parameters: - get_tool: Local module fixture to load a named tool as a module. - See the current folder's `conftest.py` file. - - """ +# if True: +def test_run() -> None: + """Check that running `emmocheck` works.""" from pathlib import Path - test_file = ( - Path(__file__).resolve().parent.parent / "testonto" / "models.ttl" - ) - emmocheck = get_tool("emmocheck") + from ontopy.testutils import ontodir, get_tool_module + + test_file = ontodir / "models.ttl" + emmocheck = get_tool_module("emmocheck") emmocheck.main([str(test_file)]) diff --git a/tests/tools/test_excel2onto.py b/tests/tools/test_excel2onto.py index 3a047a75a..124ccd048 100644 --- a/tests/tools/test_excel2onto.py +++ b/tests/tools/test_excel2onto.py @@ -1,16 +1,9 @@ """Test the `ontograph` tool.""" -from typing import TYPE_CHECKING - import pytest -if TYPE_CHECKING: - from pathlib import Path - from types import ModuleType - from typing import Callable - @pytest.mark.filterwarnings("ignore::UserWarning") -def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None: +def test_run() -> None: """Check that running `excel2onto` works. Parameters: @@ -20,36 +13,33 @@ def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None: exist only for the lifetime of this test function. """ - from pathlib import Path + from ontopy.testutils import ontodir, outdir, testdir, get_tool_module - test_file = ( - Path(__file__).resolve().parent.parent - / "test_excelparser" - / "onto.xlsx" - ) - test_file2 = ( - Path(__file__).resolve().parent.parent - / "test_excelparser" - / "onto_update.xlsx" - ) - excel2onto = get_tool("excel2onto") + test_file = testdir / "test_excelparser" / "onto.xlsx" + test_file2 = testdir / "test_excelparser" / "onto_update.xlsx" + excel2onto = get_tool_module("excel2onto") - excel2onto.main( - [f"--output={str(tmpdir)}/onto.ttl", "--force", str(test_file)] - ) + outfile = outdir / "onto.ttl" + if outfile.exists(): # consider to add an --overwrite option to excel2onto + outfile.unlink() + excel2onto.main([f"--output={outfile}", "--force", str(test_file)]) + # Append to outfile excel2onto.main( [ - f"--output={str(tmpdir)}/onto.ttl", + f"--output={outdir}/onto.ttl", "--force", "--input_ontology=newonto.ttl", str(test_file2), ] ) + outfile = outdir / "ontology.ttl" + if outfile.exists(): + outfile.unlink() excel2onto.main( [ - f"--output={str(tmpdir)}/ontology.ttl", + f"--output={outfile}", "--force", "--update=False", str(test_file), diff --git a/tests/tools/test_ontoconvert.py b/tests/tools/test_ontoconvert.py index 244b61529..e42ee0a87 100644 --- a/tests/tools/test_ontoconvert.py +++ b/tests/tools/test_ontoconvert.py @@ -8,31 +8,12 @@ # if True: -def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None: - """Check that running `ontoconvert` works. - - Parameters: - get_tool: Local module fixture to load a named tool as a module. - See the current folder's `conftest.py` file. - tmpdir: A generic pytest fixture to generate a temporary directory, - which will exist only for the lifetime of this test function. - """ +def test_run() -> None: + """Check that running `ontoconvert` works.""" import re - from pathlib import Path - import sys - import importlib - - testdir = Path(__file__).resolve().parent.parent - ontodir = testdir / "testonto" - outdir = testdir / "output" - toolsdir = testdir.parent / "tools" - - # ontoconvert = get_tool("ontoconvert") + from ontopy.testutils import ontodir, outdir, get_tool_module - sys.path.append(str(toolsdir)) - ontoconvert = importlib.machinery.SourceFileLoader( - "ontoconvert", str(toolsdir / "ontoconvert") - ).load_module() + ontoconvert = get_tool_module("ontoconvert") # Test 1 ontoconvert.main( diff --git a/tests/tools/test_ontodoc.py b/tests/tools/test_ontodoc.py index 5c52a7415..ecbf7bd33 100644 --- a/tests/tools/test_ontodoc.py +++ b/tests/tools/test_ontodoc.py @@ -1,90 +1,51 @@ """Test the `ontodoc` tool.""" -from typing import TYPE_CHECKING - import pytest -if TYPE_CHECKING: - from pathlib import Path - from types import ModuleType - from typing import Callable - -def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None: - """Check that running `ontodoc` works. +def test_run() -> None: + """Check that running `ontodoc` works.""" + from ontopy.testutils import ontodir, outdir, get_tool_module - Parameters: - get_tool: Local module fixture to load a named tool as a module. - See the current folder's `conftest.py` file. - tmpdir: A generic pytest fixture to generate a temporary directory, which will - exist only for the lifetime of this test function. + ontodoc = get_tool_module("ontodoc") - """ from pathlib import Path - test_file = ( - Path(__file__).resolve().parent.parent / "testonto" / "models.ttl" - ) - ontodoc = get_tool("ontodoc") + test_file = ontodir / "models.ttl" + + ontodoc = get_tool_module("ontodoc") - ontodoc.main([str(test_file), str(tmpdir / "test.md")]) + ontodoc.main([str(test_file), str(outdir / "test.md")]) ontodoc.main( - [str(test_file), "--format=simple-html", str(tmpdir / "test.html")] + [str(test_file), "--format=simple-html", str(outdir / "test.html")] ) -def test_run_w_individual( - get_tool: "Callable[[str], ModuleType]", tmpdir: "Path" -) -> None: - """Check that running `ontodoc` works when there is an individual. +def test_run_w_individual() -> None: + """Check that running `ontodoc` works when there is an individual.""" + from ontopy.testutils import ontodir, outdir, get_tool_module - Parameters: - get_tool: Local module fixture to load a named tool as a module. - See the current folder's `conftest.py` file. - tmpdir: A generic pytest fixture to generate a temporary directory, which will - exist only for the lifetime of this test function. + test_file = ontodir / "testonto_w_individual.ttl" + ontodoc = get_tool_module("ontodoc") - """ - from pathlib import Path - - test_file = ( - Path(__file__).resolve().parent.parent - / "testonto" - / "testonto_w_individual.ttl" - ) - ontodoc = get_tool("ontodoc") - - ontodoc.main([str(test_file), str(tmpdir / "test.md")]) + ontodoc.main([str(test_file), str(outdir / "test.md")]) ontodoc.main( - [str(test_file), "--format=simple-html", str(tmpdir / "test.html")] + [str(test_file), "--format=simple-html", str(outdir / "test.html")] ) @pytest.mark.filterwarnings( "ignore:Ignoring instance" ) # currently pytest is set to accept warnings, but this might change in the future -def test_run_w_punning( - get_tool: "Callable[[str], ModuleType]", tmpdir: "Path" -) -> None: +def test_run_w_punning() -> None: """Check that running `ontodoc` works even if there is a punned individual. This will throw and extra warning as the punned individual will be ignored. - - Parameters: - get_tool: Local module fixture to load a named tool as a module. - See the current folder's `conftest.py` file. - tmpdir: A generic pytest fixture to generate a temporary directory, which will - exist only for the lifetime of this test function. - """ - from pathlib import Path + from ontopy.testutils import ontodir, outdir, get_tool_module - test_file = ( - Path(__file__).resolve().parent.parent - / "testonto" - / "testonto_w_punning.ttl" - ) - ontodoc = get_tool("ontodoc") + test_file = ontodir / "testonto_w_punning.ttl" + ontodoc = get_tool_module("ontodoc") - ontodoc.main([str(test_file), str(tmpdir / "test.md")]) + ontodoc.main([str(test_file), str(outdir / "test.md")]) ontodoc.main( - [str(test_file), "--format=simple-html", str(tmpdir / "test.html")] + [str(test_file), "--format=simple-html", str(outdir / "test.html")] ) diff --git a/tests/tools/test_ontograph.py b/tests/tools/test_ontograph.py index 552ff1a88..f972bb261 100644 --- a/tests/tools/test_ontograph.py +++ b/tests/tools/test_ontograph.py @@ -1,27 +1,11 @@ """Test the `ontograph` tool.""" -from typing import TYPE_CHECKING -if TYPE_CHECKING: - from pathlib import Path - from types import ModuleType - from typing import Callable +def test_run() -> None: + """Check that running `ontograph` works.""" + from ontopy.testutils import ontodir, outdir, get_tool_module -def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None: - """Check that running `ontograph` works. + test_file = ontodir / "models.ttl" + ontograph = get_tool_module("ontograph") - Parameters: - get_tool: Local module fixture to load a named tool as a module. - See the current folder's `conftest.py` file. - tmpdir: A generic pytest fixture to generate a temporary directory, which will - exist only for the lifetime of this test function. - - """ - from pathlib import Path - - test_file = ( - Path(__file__).resolve().parent.parent / "testonto" / "models.ttl" - ) - ontograph = get_tool("ontograph") - - ontograph.main([str(test_file), str(tmpdir / "test.png")]) + ontograph.main([str(test_file), str(outdir / "test.png")]) diff --git a/tests/tools/test_ontoversion.py b/tests/tools/test_ontoversion.py index c0b1a8658..810be8535 100644 --- a/tests/tools/test_ontoversion.py +++ b/tests/tools/test_ontoversion.py @@ -1,26 +1,11 @@ """Test the `ontoversion` tool.""" -from typing import TYPE_CHECKING -if TYPE_CHECKING: - from types import ModuleType - from typing import Callable +def test_run() -> None: + """Check running `ontoversion` works.""" + from ontopy.testutils import ontodir, outdir, get_tool_module -def test_run(get_tool: "Callable[[str], ModuleType]") -> None: - """Check running `ontoversion` works. - - Parameters: - get_tool: Local module fixture to load a named tool as a module. - See the current folder's `conftest.py` file. - tmpdir: A generic pytest fixture to generate a temporary directory, which will - exist only for the lifetime of this test function. - - """ - from pathlib import Path - - test_file = ( - Path(__file__).resolve().parent.parent / "testonto" / "testonto.ttl" - ) - ontoversion = get_tool("ontoversion") + test_file = ontodir / "testonto.ttl" + ontoversion = get_tool_module("ontoversion") ontoversion.main([str(test_file), "--format", "ttl"]) diff --git a/tests/tools/test_redirectioncheck.py b/tests/tools/test_redirectioncheck.py index 139c5ef1f..7f6409850 100644 --- a/tests/tools/test_redirectioncheck.py +++ b/tests/tools/test_redirectioncheck.py @@ -1,26 +1,12 @@ """Test the `redirectioncheck` tool.""" -from typing import TYPE_CHECKING -if TYPE_CHECKING: - from pathlib import Path - from types import ModuleType - from typing import Callable +def test_run() -> None: + """Check that running `redirectioncheck` works.""" + from ontopy.testutils import testdir, get_tool_module -def test_run(get_tool: "Callable[[str], ModuleType]") -> None: - """Check that running `redirectioncheck` works. - - Parameters: - get_tool: Local module fixture to load a named tool as a module. - See the current folder's `conftest.py` file. - - """ - from pathlib import Path - - yamlfile = ( - Path(__file__).resolve().parent / "input" / "expected_redirections.yaml" - ) - redirectioncheck = get_tool("redirectioncheck") + yamlfile = testdir / "tools" / "input" / "expected_redirections.yaml" + redirectioncheck = get_tool_module("redirectioncheck") # Make output more readable when running pytest with -s option print("\n\n") From 3c528631f405bb31f6a552a3d57b35153bfde36a Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Wed, 28 Feb 2024 22:28:49 +0100 Subject: [PATCH 6/7] Removed unused imports --- tests/tools/test_emmocheck.py | 2 -- tests/tools/test_ontoconvert.py | 6 ------ tests/tools/test_ontodoc.py | 3 --- 3 files changed, 11 deletions(-) diff --git a/tests/tools/test_emmocheck.py b/tests/tools/test_emmocheck.py index 705c205ec..c403b307c 100644 --- a/tests/tools/test_emmocheck.py +++ b/tests/tools/test_emmocheck.py @@ -4,8 +4,6 @@ # if True: def test_run() -> None: """Check that running `emmocheck` works.""" - from pathlib import Path - from ontopy.testutils import ontodir, get_tool_module test_file = ontodir / "models.ttl" diff --git a/tests/tools/test_ontoconvert.py b/tests/tools/test_ontoconvert.py index e42ee0a87..c8989ede7 100644 --- a/tests/tools/test_ontoconvert.py +++ b/tests/tools/test_ontoconvert.py @@ -1,10 +1,4 @@ """Test the `ontoconvert` tool.""" -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from pathlib import Path - from types import ModuleType - from typing import Callable # if True: diff --git a/tests/tools/test_ontodoc.py b/tests/tools/test_ontodoc.py index ecbf7bd33..067c31568 100644 --- a/tests/tools/test_ontodoc.py +++ b/tests/tools/test_ontodoc.py @@ -8,10 +8,7 @@ def test_run() -> None: ontodoc = get_tool_module("ontodoc") - from pathlib import Path - test_file = ontodir / "models.ttl" - ontodoc = get_tool_module("ontodoc") ontodoc.main([str(test_file), str(outdir / "test.md")]) From a493e97b7d1756f9d6e4eb1c47a6534ebda8db5e Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Thu, 29 Feb 2024 08:34:41 +0100 Subject: [PATCH 7/7] Update ontopy/testutils.py Co-authored-by: Francesca L. Bleken <48128015+francescalb@users.noreply.github.com> --- ontopy/testutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ontopy/testutils.py b/ontopy/testutils.py index 927c7f50f..33492a37e 100644 --- a/ontopy/testutils.py +++ b/ontopy/testutils.py @@ -1,4 +1,4 @@ -"""Motule primarly intended to be imported by tests. +"""Module primarly intended to be imported by tests. It defines some directories and some utility functions that can be used with and without conftest.