Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jesper-friis committed Oct 19, 2024
1 parent af45e17 commit f1c0958
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
14 changes: 13 additions & 1 deletion tests/dataset/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def test_get_context():
assert "@version" in context
assert len(context) > 20

# Check for consistency between context online and on disk
online_context = get_context(fromfile=False)
assert online_context == context


def test_get_prefixes():
"""Test get_prefixes()."""
Expand Down Expand Up @@ -223,16 +227,24 @@ def test_save_and_load():
assert newimage2.distribution["@type"] == DCAT.Distribution
assert newimage2.distribution.downloadURL == f"file:{newfile2}"

# Test save anonymous dataset with existing distribution
newfile2.unlink(missing_ok=True)
save(ts, buf, distribution=SEMDATA.newdistr2)
assert newfile2.exists()
assert newfile2.stat().st_size == len(buf)

# Test searching the triplestore
SAMPLE = ts.namespaces["sample"]
assert set(list_dataset_iris(ts)) == {
datasets = list_dataset_iris(ts)
named_datasets = {
SEMDATA["SEM_cement_batch2/77600-23-001/77600-23-001_5kV_400x_m001"],
SEMDATA["SEM_cement_batch2/77600-23-001"],
SEMDATA["SEM_cement_batch2"],
SAMPLE["SEM_cement_batch2/77600-23-001"],
SEMDATA.newimage,
SEMDATA.newimage2,
}
assert not named_datasets.difference(datasets)
assert set(list_dataset_iris(ts, creator="Sigurd Wenner")) == {
SEMDATA["SEM_cement_batch2/77600-23-001/77600-23-001_5kV_400x_m001"],
SEMDATA["SEM_cement_batch2/77600-23-001"],
Expand Down
1 change: 1 addition & 0 deletions tests/input/semdata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,4 @@ generators:
generatorType: application/vnd.dlite-generate
configuration:
driver: hitachi
options: mode=w
9 changes: 9 additions & 0 deletions tripper/context/0.2/context.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
"format": "dcterms:format",
"checksum": "spdx:checksum",

"abstract": "dcterms:abstract",
"bibliographicCitation": "dcterms:bibliographicCitation",
"source": "dcterms:source",
"deprecated": "owl:deprecated",
"comment": "rdfs:comment",
"isDefinedBy": "rdfs:isDefinedBy",
"label": "rdfs:label",
"seeAlso": "rdfs:seeAlso",

"parser": {
"@id": "oteio:parser",
"@type": "@id"
Expand Down
22 changes: 21 additions & 1 deletion tripper/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def save(
class_iri: "Optional[str]" = None,
dataset: "Optional[Union[str, dict]]" = None,
distribution: "Optional[Union[str, dict]]" = None,
generator: "Optional[str]" = None,
prefixes: "Optional[dict]" = None,
use_sparql: "Optional[bool]" = None,
) -> None:
Expand All @@ -123,6 +124,8 @@ def save(
distribution: IRI of distribution for the data to be saved.
Or a dict additional documentation of the distribution,
like media type, parsers, generators etc...
generator: Name of generator to use in case the distribution has
several generators.
prefixes: Dict with prefixes in addition to those included in the
context. Should map namespace prefixes to IRIs.
use_sparql: Whether to access the triplestore with SPARQL.
Expand Down Expand Up @@ -194,6 +197,19 @@ def save(
)
distribution: dict # Tell mypy that this now is a dict

if isinstance(generator, str):
for gen in get(distribution, "generator"):
if gen.get("@id") == generator:
break

Check warning on line 203 in tripper/dataset/dataset.py

View check run for this annotation

Codecov / codecov/patch

tripper/dataset/dataset.py#L201-L203

Added lines #L201 - L203 were not covered by tests
else:
raise ValueError(

Check warning on line 205 in tripper/dataset/dataset.py

View check run for this annotation

Codecov / codecov/patch

tripper/dataset/dataset.py#L205

Added line #L205 was not covered by tests
f"dataset '{dataset}' has no such generator: {generator}"
)
elif "generator" in distribution:
gen = get(distribution, "generator")[0]
else:
gen = None

# __TODO__: Check if `class_iri` already has the value restriction.
# If not, add it to triples

Expand All @@ -210,12 +226,16 @@ def save(
location = (
f"{scheme}://{p.netloc}{p.path}" if p.netloc else f"{scheme}:{p.path}"
)
options = [p.query] if p.query else []
if gen and "configuration" in gen and "options" in gen.configuration:
# __TODO__: allow options to also be a dict
options.append(gen.configuration["options"])
id = (
distribution["accessService"].get("identifier")
if "accessService" in distribution
else None
)
with Protocol(scheme, location, options=p.query) as pr:
with Protocol(scheme, location, options=";".join(options)) as pr:
pr.save(data, id)

# Update triplestore
Expand Down

0 comments on commit f1c0958

Please sign in to comment.