Skip to content

Commit

Permalink
Improves SPARQL test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tpt committed Sep 6, 2023
1 parent 5d16d00 commit 2547c96
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions tests/test_sparql.py
Original file line number Diff line number Diff line change
@@ -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/")

Expand Down Expand Up @@ -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")),
{
Expand All @@ -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")),
{
Expand All @@ -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"<http://example.com/foo> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Entity> .",
)

def test_insert_where_update_graph(self):
g = Graph("Oxigraph")
g.add((EX.foo, RDF.type, EX.Entity))
g.update("INSERT { ?s a <http://example.com/Entity2> } WHERE { ?s a <http://example.com/Entity> }")
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 <http://example.com/Entity2> } WHERE { ?s a <http://example.com/Entity> }")
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 <http://example.com/Entity2> } WHERE { GRAPH ?g { ?s a <http://example.com/Entity> } }")
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 <http://example.com/Entity2> } WHERE { ?s a <http://example.com/Entity> }")
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 <http://example.com/Entity2> } WHERE { ?s a <http://example.com/Entity> }")
self.assertIn((EX.foo, RDF.type, EX.Entity2, g.identifier), g) # TODO: Why


if __name__ == "__main__":
unittest.main()

0 comments on commit 2547c96

Please sign in to comment.