Skip to content

Commit

Permalink
Merge pull request #536 from emmo-repo/cwa/test_flb/bypass_punning_in…
Browse files Browse the repository at this point in the history
…_ontodoc

Fix fixtures for Python3.7.
  • Loading branch information
francescalb committed Feb 4, 2023
2 parents 5967df3 + 8e0dc09 commit ae3ec1c
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 69 deletions.
123 changes: 100 additions & 23 deletions tests/tools/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,110 @@

if TYPE_CHECKING:
from types import ModuleType
from typing import Any, Dict
from typing import Callable


@pytest.fixture
def tool(request: "Dict[str, Any]") -> "ModuleType":
"""Import a tool as a module."""
@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 importlib
import os
from pathlib import Path
import shutil
import sys

original_sys_path = deepcopy(sys.path)
original_tool_path: Path = (
Path(__file__).resolve().parent.parent.parent / "tools" / request.param
)
if str(original_tool_path.parent) not in sys.path:
sys.path.append(str(original_tool_path.parent))

assert (
original_tool_path.exists()
), f"The requested tool ({request.param}) was not found in {original_tool_path.parent}"
try:
tool_path = original_tool_path.rename(
original_tool_path.with_name(f"{request.param}.py")
)
yield importlib.import_module(request.param)
finally:
if tool_path and tool_path.exists():
tool_path.rename(tool_path.with_name(request.param))
sys.path = original_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
20 changes: 15 additions & 5 deletions tests/tools/test_emmocheck.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
"""Test the `emmocheck` tool."""
import pytest
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from types import ModuleType
from typing import Callable

@pytest.mark.parametrize("tool", ["emmocheck"], indirect=True)
def test_run(tool) -> None:
"""Check that running `emmocheck` works."""

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.
"""
from pathlib import Path

test_file = (
Path(__file__).resolve().parent.parent / "testonto" / "models.ttl"
)
emmocheck = get_tool("emmocheck")

tool.main([str(test_file)])
emmocheck.main([str(test_file)])
33 changes: 24 additions & 9 deletions tests/tools/test_excel2onto.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
"""Test the `ontograph` tool."""
from pathlib import Path
import os
import pytest
from typing import TYPE_CHECKING

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 `excel2onto` 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

@pytest.mark.parametrize("tool", ["excel2onto"], indirect=True)
def test_run(tool, tmpdir: Path) -> None:
"""Check that running `excel2onto` works."""
test_file = (
Path(__file__).resolve().parent.parent
/ "test_excelparser"
Expand All @@ -17,10 +29,13 @@ def test_run(tool, tmpdir: Path) -> None:
/ "test_excelparser"
/ "onto_update.xlsx"
)
excel2onto = get_tool("excel2onto")

tool.main([f"--output={str(tmpdir)}/onto.ttl", "--force", str(test_file)])
excel2onto.main(
[f"--output={str(tmpdir)}/onto.ttl", "--force", str(test_file)]
)

tool.main(
excel2onto.main(
[
f"--output={str(tmpdir)}/onto.ttl",
"--force",
Expand All @@ -29,7 +44,7 @@ def test_run(tool, tmpdir: Path) -> None:
]
)

tool.main(
excel2onto.main(
[
f"--output={str(tmpdir)}/ontology.ttl",
"--force",
Expand Down
25 changes: 19 additions & 6 deletions tests/tools/test_ontoconvert.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
"""Test the `ontoconvert` tool."""
from pathlib import Path
from typing import TYPE_CHECKING

import pytest
if TYPE_CHECKING:
from pathlib import Path
from types import ModuleType
from typing import Callable


@pytest.mark.parametrize("tool", ["ontoconvert"], indirect=True)
def test_run(tool, tmpdir: Path) -> None:
"""Check that running `ontoconvert` works."""
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.
"""
from pathlib import Path

test_file = (
Path(__file__).resolve().parent.parent / "testonto" / "models.ttl"
)
ontoconvert = get_tool("ontoconvert")

tool.main([str(test_file), str(tmpdir / "test.ttl")])
ontoconvert.main([str(test_file), str(tmpdir / "test.ttl")])
68 changes: 53 additions & 15 deletions tests/tools/test_ontodoc.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,90 @@
"""Test the `ontodoc` tool."""
from pathlib import Path
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.
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

@pytest.mark.parametrize("tool", ["ontodoc"], indirect=True)
def test_run(tool, tmpdir: Path) -> None:
"""Check that running `ontodoc` works."""
test_file = (
Path(__file__).resolve().parent.parent / "testonto" / "models.ttl"
)
ontodoc = get_tool("ontodoc")

tool.main([str(test_file), str(tmpdir / "test.md")])
tool.main(
ontodoc.main([str(test_file), str(tmpdir / "test.md")])
ontodoc.main(
[str(test_file), "--format=simple-html", str(tmpdir / "test.html")]
)


@pytest.mark.parametrize("tool", ["ontodoc"], indirect=True)
def test_run_w_individual(tool, tmpdir: Path) -> None:
"""Check that running `ontodoc` works when there is an individual."""
def test_run_w_individual(
get_tool: "Callable[[str], ModuleType]", tmpdir: "Path"
) -> None:
"""Check that running `ontodoc` works when there is an individual.
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_w_individual.ttl"
)
ontodoc = get_tool("ontodoc")

tool.main([str(test_file), str(tmpdir / "test.md")])
tool.main(
ontodoc.main([str(test_file), str(tmpdir / "test.md")])
ontodoc.main(
[str(test_file), "--format=simple-html", str(tmpdir / "test.html")]
)


@pytest.mark.parametrize("tool", ["ontodoc"], indirect=True)
@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(tool, tmpdir: Path) -> None:
def test_run_w_punning(
get_tool: "Callable[[str], ModuleType]", tmpdir: "Path"
) -> 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

test_file = (
Path(__file__).resolve().parent.parent
/ "testonto"
/ "testonto_w_punning.ttl"
)
ontodoc = get_tool("ontodoc")

tool.main([str(test_file), str(tmpdir / "test.md")])
tool.main(
ontodoc.main([str(test_file), str(tmpdir / "test.md")])
ontodoc.main(
[str(test_file), "--format=simple-html", str(tmpdir / "test.html")]
)
25 changes: 19 additions & 6 deletions tests/tools/test_ontograph.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
"""Test the `ontograph` tool."""
from pathlib import Path
from typing import TYPE_CHECKING

import pytest
if TYPE_CHECKING:
from pathlib import Path
from types import ModuleType
from typing import Callable


@pytest.mark.parametrize("tool", ["ontograph"], indirect=True)
def test_run(tool, tmpdir: Path) -> None:
"""Check that running `ontograph` works."""
def test_run(get_tool: "Callable[[str], ModuleType]", tmpdir: "Path") -> None:
"""Check that running `ontograph` 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" / "models.ttl"
)
ontograph = get_tool("ontograph")

tool.main([str(test_file), str(tmpdir / "test.png")])
ontograph.main([str(test_file), str(tmpdir / "test.png")])
Loading

0 comments on commit ae3ec1c

Please sign in to comment.