From f80eab61dd26d52187befae8d97a59e1849d0e4f Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Thu, 29 Feb 2024 22:31:13 +0100 Subject: [PATCH 1/2] Added test for literal triples. --- docs/api_reference/testutils.md | 3 +++ tests/test_triplestore.py | 46 +++++++++++++++++++++++++++++++++ tripper/testutils.py | 12 +++++++++ 3 files changed, 61 insertions(+) create mode 100644 docs/api_reference/testutils.md create mode 100644 tripper/testutils.py diff --git a/docs/api_reference/testutils.md b/docs/api_reference/testutils.md new file mode 100644 index 00000000..ffbfd7dd --- /dev/null +++ b/docs/api_reference/testutils.md @@ -0,0 +1,3 @@ +# testutils + +::: tripper.testutils diff --git a/tests/test_triplestore.py b/tests/test_triplestore.py index c10f6b4b..66259722 100644 --- a/tests/test_triplestore.py +++ b/tests/test_triplestore.py @@ -275,3 +275,49 @@ 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.""" + 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")), + ] + ) diff --git a/tripper/testutils.py b/tripper/testutils.py new file mode 100644 index 00000000..2ac5be64 --- /dev/null +++ b/tripper/testutils.py @@ -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" From 498a47a25f9fc8262b84f21830b1fb4556333273 Mon Sep 17 00:00:00 2001 From: Jesper Friis Date: Sun, 3 Mar 2024 12:44:12 +0100 Subject: [PATCH 2/2] Skip test_find_literal_triples() if rdflib is not installed. --- tests/test_triplestore.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_triplestore.py b/tests/test_triplestore.py index 66259722..463ecbe4 100644 --- a/tests/test_triplestore.py +++ b/tests/test_triplestore.py @@ -280,6 +280,8 @@ def test_backend_sparqlwrapper_methods() -> None: # 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