From 2547c96bd861a1cc0bf520758dc9fd3f50d5532f Mon Sep 17 00:00:00 2001 From: Tpt Date: Tue, 29 Aug 2023 21:22:46 +0200 Subject: [PATCH] Improves SPARQL test coverage --- tests/test_sparql.py | 59 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 55 insertions(+), 4 deletions(-) diff --git a/tests/test_sparql.py b/tests/test_sparql.py index ef7e163..1567b06 100644 --- a/tests/test_sparql.py +++ b/tests/test_sparql.py @@ -1,7 +1,7 @@ import json import unittest -from rdflib import RDF, ConjunctiveGraph, Graph, Namespace +from rdflib import RDF, ConjunctiveGraph, Dataset, Graph, Namespace EX = Namespace("http://example.com/") @@ -33,7 +33,6 @@ def test_select_query_graph(self): g = Graph("Oxigraph") g.add((EX.foo, RDF.type, EX.Entity)) result = g.query("SELECT ?s WHERE { ?s ?p ?o }") - self.assertEqual(len(result), 1) self.assertEqual( json.loads(result.serialize(format="json").decode("utf-8")), { @@ -46,7 +45,30 @@ def test_select_query_conjunctive(self): g = ConjunctiveGraph("Oxigraph") g.add((EX.foo, RDF.type, EX.Entity)) result = g.query("SELECT ?s WHERE { ?s ?p ?o }") - self.assertEqual(len(result), 1) + self.assertEqual( + json.loads(result.serialize(format="json").decode("utf-8")), + { + "results": {"bindings": [{"s": {"type": "uri", "value": "http://example.com/foo"}}]}, + "head": {"vars": ["s"]}, + }, + ) + + def test_select_query_dataset(self): + g = Dataset("Oxigraph") + g.add((EX.foo, RDF.type, EX.Entity)) + result = g.query("SELECT ?s WHERE { ?s ?p ?o }") + self.assertEqual( + json.loads(result.serialize(format="json").decode("utf-8")), + { + "results": {"bindings": [{"s": {"type": "uri", "value": "http://example.com/foo"}}]}, + "head": {"vars": ["s"]}, + }, + ) + + def test_select_query_dataset_default_union(self): + g = Dataset("Oxigraph", default_union=True) + g.add((EX.foo, RDF.type, EX.Entity, EX.graph)) + result = g.query("SELECT ?s WHERE { ?s ?p ?o }") self.assertEqual( json.loads(result.serialize(format="json").decode("utf-8")), { @@ -59,12 +81,41 @@ def test_construct_query(self): g = ConjunctiveGraph("Oxigraph") g.add((EX.foo, RDF.type, EX.Entity)) result = g.query("CONSTRUCT WHERE { ?s ?p ?o }") - self.assertEqual(len(result), 1) self.assertEqual( result.serialize(format="ntriples").strip(), b" .", ) + def test_insert_where_update_graph(self): + g = Graph("Oxigraph") + g.add((EX.foo, RDF.type, EX.Entity)) + g.update("INSERT { ?s a } WHERE { ?s a }") + self.assertIn((EX.foo, RDF.type, EX.Entity2), g) + + def test_insert_where_update_conjunctive_graph(self): + g = ConjunctiveGraph("Oxigraph") + g.add((EX.foo, RDF.type, EX.Entity, EX.g)) + g.update("INSERT { ?s a } WHERE { ?s a }") + self.assertIn((EX.foo, RDF.type, EX.Entity2), g) + + def test_insert_where_update_dataset_named_graph(self): + g = Dataset("Oxigraph") + g.add((EX.foo, RDF.type, EX.Entity, EX.g)) + g.update("INSERT { ?s a } WHERE { GRAPH ?g { ?s a } }") + self.assertIn((EX.foo, RDF.type, EX.Entity2, g), g) # TODO: Why + + def test_insert_where_update_dataset_default_graph(self): + g = Dataset("Oxigraph") + g.add((EX.foo, RDF.type, EX.Entity)) + g.update("INSERT { ?s a } WHERE { ?s a }") + self.assertIn((EX.foo, RDF.type, EX.Entity2, g.identifier), g) # TODO: Why + + def test_insert_where_update_dataset_default_union(self): + g = Dataset("Oxigraph", default_union=True) + g.add((EX.foo, RDF.type, EX.Entity, EX.g)) + g.update("INSERT { ?s a } WHERE { ?s a }") + self.assertIn((EX.foo, RDF.type, EX.Entity2, g.identifier), g) # TODO: Why + if __name__ == "__main__": unittest.main()