Skip to content

Commit

Permalink
Updated dataset
Browse files Browse the repository at this point in the history
  • Loading branch information
jesper-friis committed Oct 15, 2024
1 parent b0a429c commit 15bda64
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 148 deletions.
3 changes: 0 additions & 3 deletions docs/api_reference/testutils.md

This file was deleted.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ good-names = [
"s", "p", "o",
# Namespaces
"EX",
# dict, file, ...
"d", "f",
# dict, value, file, ...
"d", "v", "f",
]

[tool.pytest.ini_options]
Expand Down
100 changes: 47 additions & 53 deletions tests/dataset/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,78 +12,72 @@
inputdir = testdir / "input"


if True:
# def test_save_and_load_dataset():
"""Test save_dataset() and load_dataset()."""
# pylint: disable=too-many-locals,invalid-name
def test_get_context():
"""Test get_context()."""
from tripper.dataset import get_context

from tripper import DCAT, RDF, Triplestore
from tripper.dataset import load_datadoc, load_dataset, save_dataset
context = get_context()
assert isinstance(context, dict)
assert "@version" in context
assert len(context) > 20

ts = Triplestore("rdflib")

# Load data documentation
# datadoc = load_datadoc(inputdir / "datasets.yaml")
datadoc = load_datadoc(inputdir / "semdata.yaml")
assert isinstance(datadoc, dict)
assert "@context" in datadoc
def test_get_prefixes():
"""Test get_prefixes()."""
from tripper.dataset import get_prefixes

prefixes = datadoc["prefixes"]
for dataset in datadoc["datasets"]:
ds = save_dataset(ts, dataset, prefixes=prefixes)
repr1 = set(ts.triples())
prefixes = get_prefixes()
assert prefixes["dcat"] == "http://www.w3.org/ns/dcat#"
assert prefixes["emmo"] == "https://w3id.org/emmo#"

# Load back dict representation from the triplestore
SEMDATA = ts.namespaces["semdata"]
d = load_dataset(ts, iri=SEMDATA["sample3/pos1_01_grid_200x"])

# Should the prefix be expanded?
assert ds["@id"] == "semdata:sample3/pos1_01_grid_200x"
def test_get_shortnames():
"""Test get_shortnames()."""
from tripper.dataset import get_shortnames

# Short names that are not equal to the last component of the IRI
exceptions = (
"datasets",
"datamodel",
"prefixes",
"configuration",
"statements",
"@type",
)

shortnames = get_shortnames()
assert (
"semdata:sample3/pos1_01_grid_200x",
RDF.type,
DCAT.Dataset,
) in repr1
assert d["@id"] == (
"http://sintef.no/data/matchmaker/SEM/sample3/pos1_01_grid_200x"
shortnames["https://w3id.org/emmo/domain/oteio#hasDataSet"]
== "datasets"
)
# # Store the new dict representation to another triplestore
# ts2 = Triplestore("rdflib")
# ds2 = save_dataset(ts2, d)
# repr2 = set(ts.triples())
#
# # Ensure that both dict and triplestore representations are equal
# assert ds2 == ds
# assert repr2 == repr1
#
# # Load dataset using SPARQL
# dd = load_dataset_sparql(ts, iri=EX.mydata)
# assert dd == d

for k, v in shortnames.items():
if v not in exceptions:
assert k.rsplit("#", 1)[-1].rsplit("/", 1)[-1] == v


# if True:
def test_datadoc():
"""Test storing data documentation to triplestore."""
# pylint: disable=unused-variable
import io
import json
def test_save_and_load_dataset():
"""Test save_dataset() and load_dataset()."""
# pylint: disable=too-many-locals,invalid-name

from tripper import Triplestore
from tripper.dataset import load_datadoc
from tripper.dataset import load_dataset, save_datadoc

ts = Triplestore("rdflib")
d = load_datadoc(inputdir / "datasets.yaml")

f = io.StringIO(json.dumps(d))
ts2 = Triplestore(backend="rdflib")
ts2.parse(f, format="json-ld")
# Load data documentation into triplestore
datadoc = save_datadoc(ts, inputdir / "semdata.yaml")
assert isinstance(datadoc, dict)
assert "@context" in datadoc

# Add triples from temporary triplestore
ts.add_triples(ts2.triples())
# ts2.close() # explicit close ts2
# Load back dict representation from the triplestore
SEMDATA = ts.namespaces["semdata"]
iri = SEMDATA["SEM_cement_batch2/77600-23-001/77600-23-001_5kV_400x_m001"]
d = load_dataset(ts, iri)

print(ts.serialize())
# Should the prefix be expanded?
assert d["@id"] == iri


def test_fuseki():
Expand Down
5 changes: 2 additions & 3 deletions tripper/testutils.py → tests/paths.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"""Motule primarly intended to be imported by tests.
"""Defines paths for tests.
It defines some directories and some utility functions that can be used
with or without conftest.
"""

from pathlib import Path

rootdir = Path(__file__).resolve().parent.parent
testdir = rootdir / "tests"
testdir = Path(__file__).resolve().parent
ontodir = testdir / "ontologies"
outdir = testdir / "output"
6 changes: 4 additions & 2 deletions tests/test_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
def test_namespaces() -> None:
"""Test namespaces."""
pytest.importorskip("rdflib")
from paths import ontodir

from tripper import RDF, Namespace
from tripper.errors import NoSuchIRIError
from tripper.testutils import ontodir

assert str(RDF) == "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
assert RDF.type == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
Expand Down Expand Up @@ -72,9 +73,10 @@ def test_namespaces() -> None:
def test_triplestore_arg() -> None:
"""Test triplestore argument of Namespace.__init__()."""
pytest.importorskip("rdflib")
from paths import ontodir

from tripper import RDF, Namespace, Triplestore
from tripper.errors import NamespaceError, NoSuchIRIError
from tripper.testutils import ontodir

assert str(RDF) == "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
assert RDF.type == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
Expand Down
3 changes: 2 additions & 1 deletion tests/test_triplestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,9 @@ def test_find_literal_triples() -> None:
"""Test finding literals."""
pytest.importorskip("rdflib")

from paths import ontodir

from tripper import RDF, XSD, Literal, Triplestore
from tripper.testutils import ontodir

ts = Triplestore("rdflib")
FAM = ts.bind("ex", "http://onto-ns.com/ontologies/examples/family#")
Expand Down
25 changes: 24 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,10 @@ def test_random_string():
def test_extend_namespace():
"""Test extend namespace()"""
pytest.importorskip("rdflib")
from paths import ontodir

from tripper import Namespace
from tripper.errors import NoSuchIRIError
from tripper.testutils import ontodir
from tripper.utils import extend_namespace

FOOD = Namespace(
Expand Down Expand Up @@ -327,3 +328,25 @@ def test_extend_namespace():
EX = Namespace("http://example.com#")
with pytest.raises(TypeError):
extend_namespace(EX, {"Item": EX + "Item"})


def test_AttrDict():
"""Test AttrDict."""
from tripper.utils import AttrDict

d = AttrDict(a=1, b=2)
assert d.a == 1

with pytest.raises(KeyError):
d.c # pylint: disable=pointless-statement

d.c = 3
assert d.c == 3

d.get = 4
assert d["get"] == 4
assert d.get("get") == 4 # pylint: disable=not-callable

d2 = AttrDict({"a": "A"})
assert d2.a == "A"
assert d2 == {"a": "A"}
4 changes: 2 additions & 2 deletions tripper/context/0.2/context.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
"datamodel": "oteio:hasDatamodel",
"hasDataSink": "oteio:hasDataSink",
"storeURL": "oteio:storeURL",
"mappingURL": "oteio.mappingURL",
"mappingFormat": "oteio.mappingFormat",
"mappingURL": "oteio:mappingURL",
"mappingFormat": "oteio:mappingFormat",

"subject": "rdf:subject",
"predicate": "rdf:predicate",
Expand Down
6 changes: 3 additions & 3 deletions tripper/dataset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from .dataset import (
get_context,
get_prefixes,
load_datadoc,
get_shortnames,
load_dataset,
prepare_datadoc,
read_datadoc,
save_datadoc,
save_dataset,
update_dataset,
)
Loading

0 comments on commit 15bda64

Please sign in to comment.