Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for dic2owl CLI and setup testing framework #62

Merged
merged 8 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .github/workflows/dic2owl_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ on:
push:
paths:
- 'dic2owl/**'
- 'tests/**'
- '.github/**'

jobs:

Expand Down Expand Up @@ -41,5 +43,5 @@ jobs:
- name: Lint with MyPy
run: mypy --ignore-missing-imports --scripts-are-modules dic2owl/dic2owl

# - name: Run unittests
# run: pytest --cov dic2owl/dic2owl tests/
- name: Run unit tests with PyTest
run: pytest --cov dic2owl tests/
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ __pycache__
*.pyc

.mypy*
.coverage

*.dic
*.cif

!test-cif/**/*.cif
!tests/**/*.dic
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ repos:
exclude: .*(\.md|\.ttl|\.cif)$

- repo: https://github.com/ambv/black
rev: 21.7b0
rev: 21.11b1
hooks:
- id: black
args:
- --config=dic2owl/pyproject.toml

- repo: https://github.com/pycqa/pylint
rev: 'v2.10.1'
rev: 'v2.12.1'
hooks:
- id: pylint
args:
Expand Down
3 changes: 3 additions & 0 deletions dic2owl/dic2owl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
terminal.
"""
# pylint: disable=line-too-long
from .dic2owl import Generator

__version__ = "0.2.0"
__author__ = "Jesper Friis <jesper.friis@sintef.no>, Casper Welzel Andersen <casper.w.andersen@sintef.no>, Francesca Lønstad Bleken <francesca.l.bleken@sintef.no>"
__author_email__ = "cif@emmo-repo.eu"

__all__ = ("Generator",)
33 changes: 17 additions & 16 deletions dic2owl/dic2owl/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
ontology-generation tool for CIF `.dic`-files.
"""
import argparse
import logging

# import logging
from pathlib import Path


LOGGING_LEVELS = [
logging.getLevelName(level).lower() for level in range(0, 51, 10)
]
# LOGGING_LEVELS = [
# logging.getLevelName(level).lower() for level in range(0, 51, 10)
# ]


def main(argv: list = None) -> None:
Expand All @@ -33,18 +34,18 @@ def main(argv: list = None) -> None:
help="Show the version and exit.",
version=f"dic2owl version {__version__}",
)
parser.add_argument(
"--log-level",
type=str,
help="Set the stdout log-level (verbosity).",
choices=LOGGING_LEVELS,
default="info",
)
parser.add_argument(
"--debug",
action="store_true",
help="Overrule log-level option, setting it to 'debug'.",
)
# parser.add_argument(
# "--log-level",
# type=str,
# help="Set the stdout log-level (verbosity).",
# choices=LOGGING_LEVELS,
# default="info",
# )
# parser.add_argument(
# "--debug",
# action="store_true",
# help="Overrule log-level option, setting it to 'debug'.",
# )
parser.add_argument(
"-o",
"--output",
Expand Down
42 changes: 25 additions & 17 deletions dic2owl/dic2owl/dic2owl.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# import textwrap
import types
from typing import Any, Set, Union, Sequence
from typing import TYPE_CHECKING
import urllib.request

from CifFile import CifDic
Expand All @@ -19,24 +19,29 @@
with open(DEVNULL, "w") as handle: # pylint: disable=unspecified-encoding
with redirect_stderr(handle):
from emmo import World
from emmo.ontology import Ontology

from owlready2 import locstr


if TYPE_CHECKING:
from _typeshed import StrPath
from typing import Any, Sequence, Set, Union

from emmo.ontology import Ontology

# Workaround for flaw in EMMO-Python
# To be removed when EMMO-Python doesn't requires ontologies to import SKOS
import emmo.ontology # noqa: E402
import emmo.ontology # pylint: disable=wrong-import-position

emmo.ontology.DEFAULT_LABEL_ANNOTATIONS = [
"http://www.w3.org/2000/01/rdf-schema#label",
]

"""The absolute, normalized path to the `ontology` directory in this
repository"""
ONTOLOGY_DIR = (
Path(__file__).resolve().parent.parent.parent.joinpath("ontology")
)
"""The absolute, normalized path to the `ontology` directory in this
repository"""


def lang_en(string: str) -> locstr:
Expand Down Expand Up @@ -67,40 +72,41 @@ class Generator:

"""

CIF_DDL = (
"https://raw.githubusercontent.com/emmo-repo/CIF-ontology/main/"
"ontology/cif-ddl.ttl"
)

# TODO:
# Should `comments` be replaced with a dict `annotations` for annotating
# the ontology itself? If so, we should import Dublin Core.

def __init__(
self,
dicfile: str,
dicfile: "StrPath",
base_iri: str,
comments: Sequence[str] = (),
comments: "Sequence[str]" = (),
) -> None:
self.dicfile = dicfile
self.dic = CifDic(dicfile, do_dREL=False)
self.dic = CifDic(str(self.dicfile), do_dREL=False)
self.comments = comments

# Create new ontology
self.world = World()
self.onto = self.world.get_ontology(base_iri)

# Load cif-ddl ontology and append it to imported ontologies
cif_ddl = (
"https://raw.githubusercontent.com/emmo-repo/CIF-ontology/main/"
"ontology/cif-ddl.ttl"
)
self.ddl = self.world.get_ontology(str(cif_ddl)).load()
self.ddl = self.world.get_ontology(self.CIF_DDL).load()
self.ddl.sync_python_names()
self.onto.imported_ontologies.append(self.ddl)

# Load Dublin core for metadata and append it to imported ontologies
# dcterms = self.world.get_ontology('http://purl.org/dc/terms/').load()
# self.onto.imported_ontologies.append(dcterms)

self.items: Set[dict] = set()
self.items: "Set[dict]" = set()

def generate(self) -> Ontology:
def generate(self) -> "Ontology":
"""Generate ontology for the CIF dictionary.

Returns:
Expand Down Expand Up @@ -194,7 +200,7 @@ def _add_data_value(self, item: dict) -> None:

self._add_annotations(cls, item)

def _add_annotations(self, cls: Any, item: dict) -> None:
def _add_annotations(self, cls: "Any", item: dict) -> None:
"""Add annotations for dic item `item` to generated ontology
class `cls`.

Expand Down Expand Up @@ -228,7 +234,9 @@ def _add_metadata(self) -> None:
)


def main(dicfile: Union[str, Path], ttlfile: Union[str, Path]) -> Generator:
def main(
dicfile: "Union[str, Path]", ttlfile: "Union[str, Path]"
) -> Generator:
"""Main function for ontology generation.

Parameters:
Expand Down
2 changes: 2 additions & 0 deletions dic2owl/requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ bandit~=1.7.0
mypy==0.910
pre-commit~=2.15
pylint~=2.11
pytest~=6.2
pytest-cov~=2.12
Loading