-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from emmo-repo/cwa/fix-306-ontodoc-rdflib-import
Fix ontoconvert rdflib import & add simple run tests for tools.
- Loading branch information
Showing
17 changed files
with
207 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Testing and tooling | ||
|
||
## Unit testing | ||
|
||
The [PyTest](https://pytest.org) framework is used for testing the EMMOntoPy package. | ||
It is a unit testing framework with a plugin system, sporting an extensive plugin library as well as a sound fixture injection system. | ||
|
||
To run the tests locally install the package with the `dev` extra (see the [developer's setup guide](setup.md)) and run: | ||
|
||
```console | ||
$ pytest | ||
=== test session starts === | ||
... | ||
``` | ||
|
||
To understand what options you have, run `pytest --help`. | ||
|
||
## Tools | ||
|
||
Several tools are used to maintain the package, keeping it secure, readable, and easing maintenance. | ||
|
||
### Mypy | ||
|
||
[Mypy](http://mypy-lang.org/) is a static type checker for Python. | ||
|
||
**Documentation**: [mypy.readthedocs.io](https://mypy.readthedocs.io/) | ||
|
||
The signs of this tool will be found in the code especially through the `typing.TYPE_CHECKING` boolean variable, which will be used in the current way: | ||
|
||
```python | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import List | ||
``` | ||
|
||
Since `TYPE_CHECKING` is `False` at runtime, the `if`-block will not be run as part of running the script or module or if importing the module. | ||
However, when Mypy runs to check the static typing, it forcefully runs these blocks, considering `TYPE_CHECKING` to be `True` (see the [`typing.TYPE_CHECKING` section](https://mypy.readthedocs.io/en/stable/runtime_troubles.html#typing-type-checking) in the Mypy documentation). | ||
|
||
This means the imports in the `if`-block are meant to *only* be used for static typing, helping developers to understand the intention of the code as well as to check the invoked methods make sense (through Mypy). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Pytest fixtures for the `tools` dir only.""" | ||
from typing import TYPE_CHECKING | ||
|
||
import pytest | ||
|
||
if TYPE_CHECKING: | ||
from types import ModuleType | ||
from typing import Any, Dict | ||
|
||
|
||
@pytest.fixture | ||
def tool(request: "Dict[str, Any]") -> "ModuleType": | ||
"""Import a tool as a module.""" | ||
from copy import deepcopy | ||
import importlib | ||
from pathlib import Path | ||
import sys | ||
|
||
original_sys_path = deepcopy(sys.path) | ||
original_tool_path: Path = ( | ||
Path(__file__).resolve().parent.parent.parent / "tools" / request.param | ||
) | ||
|
||
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(f"tools.{request.param}") | ||
finally: | ||
if tool_path and tool_path.exists(): | ||
tool_path.rename(tool_path.with_name(request.param)) | ||
sys.path = original_sys_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Test the `emmocheck` tool.""" | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize("tool", ["emmocheck"], indirect=True) | ||
def test_run(tool) -> None: | ||
"""Check that running `emmocheck` works.""" | ||
from pathlib import Path | ||
|
||
test_file = ( | ||
Path(__file__).resolve().parent.parent / "testonto" / "models.ttl" | ||
) | ||
|
||
tool.main([str(test_file)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Test the `ontoconvert` tool.""" | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize("tool", ["ontoconvert"], indirect=True) | ||
def test_run(tool, tmpdir: Path) -> None: | ||
"""Check that running `ontoconvert` works.""" | ||
test_file = ( | ||
Path(__file__).resolve().parent.parent / "testonto" / "models.ttl" | ||
) | ||
|
||
tool.main([str(test_file), str(tmpdir / "test.ttl")]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
"""Test the `ontodoc` tool.""" | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.skip("ontodoc is tested in other ways") | ||
@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" | ||
) | ||
|
||
tool.main([str(test_file), str(tmpdir / "test.md")]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Test the `ontograph` tool.""" | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize("tool", ["ontograph"], indirect=True) | ||
def test_run(tool, tmpdir: Path) -> None: | ||
"""Check that running `ontograph` works.""" | ||
test_file = ( | ||
Path(__file__).resolve().parent.parent / "testonto" / "models.ttl" | ||
) | ||
|
||
tool.main([str(test_file), str(tmpdir / "test.png")]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Test the `ontoversion` tool.""" | ||
import pytest | ||
|
||
|
||
@pytest.mark.parametrize("tool", ["ontoversion"], indirect=True) | ||
def test_run(tool) -> None: | ||
"""Check running `ontoversion` works.""" | ||
from pathlib import Path | ||
|
||
test_file = ( | ||
Path(__file__).resolve().parent.parent / "testonto" / "testonto.ttl" | ||
) | ||
|
||
tool.main([str(test_file), "--format", "ttl"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters