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

Added an additional test for convert #223

Merged
merged 13 commits into from
Jun 20, 2024
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
3 changes: 3 additions & 0 deletions .github/workflows/ci_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@ jobs:
# https://pyup.io/vulnerabilities/CVE-2021-41495/44715/
# https://pyup.io/vulnerabilities/CVE-2021-41496/44716/
# https://pyup.io/vulnerabilities/CVE-2021-34141/44717/
# 70612: Jinja2 vulnerability. Only used as subdependency for mkdocs++ in tripper.
# https://data.safetycli.com/v/70612/97c/
safety_options: |
--ignore=48547
--ignore=44715
--ignore=44716
--ignore=44717
--ignore=70612

## Build package
run_build_package: true
Expand Down
48 changes: 48 additions & 0 deletions tests/convert/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest


# if True:
def test_convertions():
"""Test convertions. Uses rdflib as triplestore backend"""

Expand Down Expand Up @@ -33,30 +34,77 @@ def test_convertions():
"key5": True,
"key6": False,
"key7": ["a", 1, 2.2, True, None],
"key8": [{"a": 1, "b": 2}, {"a": 2.2, "b": 3.3}],
"key9": [["a11", "a12", "a13"], ["a21", "a22", "a23"]],
},
}

config3 = {
"Simulation": {
"command": "run_sim",
"files": [
{"target_file": "run_sim.sh", "source_iri": "http://..."},
{"target_file": "other.txt", "source_iri": "http://..."},
],
"input": {
"onto:DataSet": [
{
"function": {
"functionType": "application/vnd.dlite-generate",
"configuration": {
"driver": "myplugin",
"location": "myfile.inp",
"datamodel": "http:...",
},
},
},
],
},
},
}

# Store dictionaries to triplestore
save_container(ts, config1, EX.config1)
save_container(ts, config2, EX.config2)
save_container(ts, config3, EX.config3)

# Print content of triplestore
# print(ts.serialize())

# Load dictionaries from triplestore
d1 = load_container(ts, EX.config1)
d2 = load_container(ts, EX.config2)
d3 = load_container(ts, EX.config3)

# Check that we got back what we stored
assert d1 == config1
assert d2 == config2
assert d3 == config3

# Now, test serialising using recognised_keys
save_container(ts, config1, EX.config1b, recognised_keys="basic")
save_container(ts, config2, EX.config2b, recognised_keys="basic")
save_container(ts, config3, EX.config3b, recognised_keys="basic")

d1b = load_container(ts, EX.config1b, recognised_keys="basic")
d2b = load_container(ts, EX.config2b, recognised_keys="basic")
d3b = load_container(ts, EX.config3b, recognised_keys="basic")

assert d1b == config1
assert d2b == config2
assert d3b == config3

# Check custom recognised keys
from tripper.convert import BASIC_RECOGNISED_KEYS

reg_keys = BASIC_RECOGNISED_KEYS.copy()
reg_keys.update(
Simulation=EX.Simulation,
files=EX.files,
command=EX.command,
input=EX.input,
output=EX.output,
)
save_container(ts, config3, EX.config3c, recognised_keys=reg_keys)
d3c = load_container(ts, EX.config3c, recognised_keys=reg_keys)
assert d3c == config3
2 changes: 2 additions & 0 deletions tripper/convert/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tripper sub-package for converting between RDF and other repetations."""

from .convert import (
BASIC_RECOGNISED_KEYS,
from_container,
from_dict,
load_container,
Expand All @@ -10,6 +11,7 @@
)

__all__ = [
"BASIC_RECOGNISED_KEYS",
"from_container",
"save_container",
"load_container",
Expand Down
29 changes: 20 additions & 9 deletions tripper/convert/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,19 @@
Returns:
A Python container object corresponding to `iri`.
"""
# pylint: disable=too-many-branches
if iri == RDF.nil:
return []

if recognised_keys == "basic":
recognised_keys = BASIC_RECOGNISED_KEYS

parents = set(o for s, p, o in ts.triples(subject=iri, predicate=RDF.type))
recognised_iris = (
{v: k for k, v in recognised_keys.items()} # type: ignore
if recognised_keys
else {}
)
parents = set(ts.objects(iri, RDF.type))

def get_obj(value):
"""Return Python object for `value`."""
Expand All @@ -243,14 +249,19 @@

if OTEIO.Dictionary in parents:
container = {}
for _, _, pair in ts.triples(
subject=iri, predicate=OTEIO.hasKeyValuePair
):
key_iri = ts.value(pair, OTEIO.hasDictionaryKey)
key = ts.value(key_iri, EMMO.hasStringValue)
value_iri = ts.value(pair, OTEIO.hasDictionaryValue)
value = ts.value(value_iri, EMMO.hasValue)
container[str(key)] = get_obj(value)
for pred, obj in ts.predicate_objects(iri):
if pred == OTEIO.hasKeyValuePair:
key_iri = ts.value(obj, OTEIO.hasDictionaryKey)
key = ts.value(key_iri, EMMO.hasStringValue)
value_iri = ts.value(obj, OTEIO.hasDictionaryValue)
value = ts.value(value_iri, EMMO.hasValue)
container[str(key)] = get_obj(value)
elif pred in recognised_iris:
container[recognised_iris[pred]] = get_obj(obj)
elif pred not in (RDF.type,):
raise ValueError(

Check warning on line 262 in tripper/convert/convert.py

View check run for this annotation

Codecov / codecov/patch

tripper/convert/convert.py#L262

Added line #L262 was not covered by tests
f"Unrecognised predicate '{pred}' in dict: {iri}"
)

# Recognised IRIs
if recognised_keys:
Expand Down
8 changes: 6 additions & 2 deletions tripper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,16 @@
properties = schema["properties"]
if "uri" in properties and isinstance(properties["uri"], str):
iri = properties["uri"]
if "identity" in properties and isinstance(
elif "identity" in properties and isinstance(

Check warning on line 77 in tripper/utils.py

View check run for this annotation

Codecov / codecov/patch

tripper/utils.py#L77

Added line #L77 was not covered by tests
properties["identity"], str
):
iri = properties["identity"]
if "uuid" in properties and properties["uuid"]:
elif "uuid" in properties and properties["uuid"]:

Check warning on line 81 in tripper/utils.py

View check run for this annotation

Codecov / codecov/patch

tripper/utils.py#L81

Added line #L81 was not covered by tests
iri = str(properties["uuid"])
else:
raise TypeError(

Check warning on line 84 in tripper/utils.py

View check run for this annotation

Codecov / codecov/patch

tripper/utils.py#L84

Added line #L84 was not covered by tests
f"cannot infer IRI from pydantic object: {obj!r}"
)
else:
raise TypeError(f"cannot infer IRI from object: {obj!r}")
return str(iri)
Expand Down