Skip to content

Commit

Permalink
Added test for finding literal triples (#183)
Browse files Browse the repository at this point in the history
# Description:
Added test for finding literal triples. 

That works, but it turned out that we need to support untyped literals.
See issue #182

## Type of change:
- [ ] Bug fix and code cleanup.
- [ ] New feature.
- [ ] Documentation update.
- [x] Testing.
  • Loading branch information
jesper-friis authored Mar 4, 2024
1 parent c18a9f1 commit 4314f40
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/api_reference/testutils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# testutils

::: tripper.testutils
48 changes: 48 additions & 0 deletions tests/test_triplestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,51 @@ def test_backend_sparqlwrapper_methods() -> None:
(EX.a, SKOS.prefLabel, Literal("An a class.", lang="en")),
]
)


# if True:
def test_find_literal_triples() -> None:
"""Test finding literals."""
pytest.importorskip("rdflib")

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#")
ts.parse(ontodir / "family.ttl")
ts.add_triples(
[
(FAM.Ola, RDF.type, FAM.Son),
(FAM.Ola, FAM.hasName, Literal("Ola")),
(FAM.Ola, FAM.hasAge, Literal(18)),
(FAM.Ola, FAM.hasWeight, Literal(68.5)),
(FAM.Kari, RDF.type, FAM.Dauther),
(FAM.Kari, FAM.hasName, Literal("Kari")),
(FAM.Kari, FAM.hasAge, Literal(18)),
(FAM.Kari, FAM.hasWeight, Literal(66.2)),
(FAM.Per, RDF.type, FAM.Father),
(FAM.Per, FAM.hasName, Literal("Per")),
(FAM.Per, FAM.hasAge, Literal(49)),
(FAM.Per, FAM.hasWeight, Literal(83.8)),
(FAM.Per, FAM.hasChild, FAM.Ola),
(FAM.Per, FAM.hasChild, FAM.Kari),
]
)
assert set(
ts.subjects(
predicate=FAM.hasAge,
object=Literal(18, datatype=XSD.integer),
)
) == set([FAM.Ola, FAM.Kari])

assert set(
ts.triples(
predicate=FAM.hasName,
object=Literal("Per", datatype=XSD.string),
)
) == set(
[
(FAM.Per, FAM.hasName, Literal("Per")),
]
)
12 changes: 12 additions & 0 deletions tripper/testutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""Motule primarly intended to be imported by 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"
ontodir = testdir / "ontologies"
outdir = testdir / "output"

0 comments on commit 4314f40

Please sign in to comment.