Skip to content

Commit ae33e3d

Browse files
committed
Improves SPARQL test coverage
1 parent ec07095 commit ae33e3d

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

tests/test_sparql.py

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def test_select_query_graph(self):
3636
g = Graph("Oxigraph")
3737
g.add((EX.foo, RDF.type, EX.Entity))
3838
result = g.query("SELECT ?s WHERE { ?s ?p ?o }")
39-
self.assertEqual(len(result), 1)
4039
self.assertEqual(
4140
json.loads(result.serialize(format="json").decode("utf-8")),
4241
{
@@ -49,7 +48,6 @@ def test_select_query_conjunctive(self):
4948
g = ConjunctiveGraph("Oxigraph")
5049
g.add((EX.foo, RDF.type, EX.Entity))
5150
result = g.query("SELECT ?s WHERE { ?s ?p ?o }")
52-
self.assertEqual(len(result), 1)
5351
self.assertEqual(
5452
json.loads(result.serialize(format="json").decode("utf-8")),
5553
{
@@ -63,7 +61,18 @@ def test_select_query_dataset(self):
6361
g = Dataset("Oxigraph")
6462
g.add((EX.foo, RDF.type, EX.Entity))
6563
result = g.query("SELECT ?s WHERE { ?s ?p ?o }")
66-
self.assertEqual(len(result), 1)
64+
self.assertEqual(
65+
json.loads(result.serialize(format="json").decode("utf-8")),
66+
{
67+
"results": {"bindings": [{"s": {"type": "uri", "value": "http://example.com/foo"}}]},
68+
"head": {"vars": ["s"]},
69+
},
70+
)
71+
72+
def test_select_query_dataset_default_union(self):
73+
g = Dataset("Oxigraph", default_union=True)
74+
g.add((EX.foo, RDF.type, EX.Entity, EX.graph))
75+
result = g.query("SELECT ?s WHERE { ?s ?p ?o }")
6776
self.assertEqual(
6877
json.loads(result.serialize(format="json").decode("utf-8")),
6978
{
@@ -76,12 +85,63 @@ def test_construct_query(self):
7685
g = ConjunctiveGraph("Oxigraph")
7786
g.add((EX.foo, RDF.type, EX.Entity))
7887
result = g.query("CONSTRUCT WHERE { ?s ?p ?o }")
79-
self.assertEqual(len(result), 1)
8088
self.assertEqual(
8189
result.serialize(format="ntriples").strip(),
8290
b"<http://example.com/foo> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/Entity> .",
8391
)
8492

93+
def test_select_query_init_bindings(self):
94+
g = Graph("Oxigraph")
95+
result = g.query("SELECT ?s WHERE {}", initBindings={"s": EX.foo})
96+
self.assertEqual(
97+
json.loads(result.serialize(format="json").decode("utf-8")),
98+
{
99+
"results": {"bindings": [{"s": {"type": "uri", "value": "http://example.com/foo"}}]},
100+
"head": {"vars": ["s"]},
101+
},
102+
)
103+
104+
def test_select_query_init_namespace(self):
105+
g = Graph("Oxigraph")
106+
result = g.query("SELECT (ex:foo AS ?s) WHERE {}", initNs={"ex": "http://example.com/"})
107+
self.assertEqual(
108+
json.loads(result.serialize(format="json").decode("utf-8")),
109+
{
110+
"results": {"bindings": [{"s": {"type": "uri", "value": "http://example.com/foo"}}]},
111+
"head": {"vars": ["s"]},
112+
},
113+
)
114+
115+
def test_insert_where_update_graph(self):
116+
g = Graph("Oxigraph")
117+
g.add((EX.foo, RDF.type, EX.Entity))
118+
g.update("INSERT { ?s a <http://example.com/Entity2> } WHERE { ?s a <http://example.com/Entity> }")
119+
self.assertIn((EX.foo, RDF.type, EX.Entity2), g)
120+
121+
def test_insert_where_update_conjunctive_graph(self):
122+
g = ConjunctiveGraph("Oxigraph")
123+
g.add((EX.foo, RDF.type, EX.Entity, EX.g))
124+
g.update("INSERT { ?s a <http://example.com/Entity2> } WHERE { ?s a <http://example.com/Entity> }")
125+
self.assertIn((EX.foo, RDF.type, EX.Entity2), g)
126+
127+
def test_insert_where_update_dataset_named_graph(self):
128+
g = Dataset("Oxigraph")
129+
g.add((EX.foo, RDF.type, EX.Entity, EX.g))
130+
g.update("INSERT { ?s a <http://example.com/Entity2> } WHERE { GRAPH ?g { ?s a <http://example.com/Entity> } }")
131+
self.assertIn((EX.foo, RDF.type, EX.Entity2, g), g) # RDFlib issue #2019
132+
133+
def test_insert_where_update_dataset_default_graph(self):
134+
g = Dataset("Oxigraph")
135+
g.add((EX.foo, RDF.type, EX.Entity))
136+
g.update("INSERT { ?s a <http://example.com/Entity2> } WHERE { ?s a <http://example.com/Entity> }")
137+
self.assertIn((EX.foo, RDF.type, EX.Entity2, g.identifier), g) # RDFlib issue #2019
138+
139+
def test_insert_where_update_dataset_default_union(self):
140+
g = Dataset("Oxigraph", default_union=True)
141+
g.add((EX.foo, RDF.type, EX.Entity, EX.g))
142+
g.update("INSERT { ?s a <http://example.com/Entity2> } WHERE { ?s a <http://example.com/Entity> }")
143+
self.assertIn((EX.foo, RDF.type, EX.Entity2, g.identifier), g) # RDFlib issue #2019
144+
85145

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

0 commit comments

Comments
 (0)