Skip to content

Commit

Permalink
Add tests for update query with blank nodes
Browse files Browse the repository at this point in the history
- check if the bnode is parsed correctly from the query
- check of the bnode is serialized to ntriples correctly after an update
and if it can be parsed again
- add these two tests for the SPARQL store and the inmemory Graph()
  • Loading branch information
white-gecko committed Dec 11, 2018
1 parent c23a72b commit 0460579
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
31 changes: 30 additions & 1 deletion test/test_sparql.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from rdflib import Graph, URIRef, Literal
from rdflib import Graph, URIRef, Literal, BNode
from rdflib.plugins.sparql import prepareQuery
from rdflib.compare import isomorphic

Expand Down Expand Up @@ -79,6 +79,35 @@ def test_complex_sparql_construct():
g.query(q)


def test_sparql_update_with_bnode():
"""
Test if the blank node is inserted correctly.
"""
graph = Graph()
graph.update(
"INSERT DATA { _:blankA <urn:type> <urn:Blank> }")
for t in graph.triples((None, None, None)):
assert isinstance(t[0], BNode)
eq_(t[1].n3(), "<urn:type>")
eq_(t[2].n3(), "<urn:Blank>")


def test_sparql_update_with_bnode_serialize_parse():
"""
Test if the blank node is inserted correctly, can be serialized and parsed.
"""
graph = Graph()
graph.update(
"INSERT DATA { _:blankA <urn:type> <urn:Blank> }")
string = graph.serialize(format='ntriples').decode('utf-8')
raised = False
try:
Graph().parse(data=string, format="ntriples")
except Exception as e:
raised = True
assert not raised


if __name__ == '__main__':
import nose
nose.main(defaultTest=__name__)
23 changes: 22 additions & 1 deletion test/test_sparqlupdatestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest
import re

from rdflib import ConjunctiveGraph, URIRef, Literal
from rdflib import ConjunctiveGraph, URIRef, Literal, BNode, Graph
from six import text_type
from six.moves.urllib.request import urlopen

Expand Down Expand Up @@ -165,6 +165,27 @@ def testUpdateWithInitBindings(self):
'only michel likes pizza'
)

def testUpdateWithBlankNode(self):
self.graph.update(
"INSERT DATA { GRAPH <urn:graph> { _:blankA <urn:type> <urn:Blank> } }")
g = self.graph.get_context(graphuri)
for t in g.triples((None, None, None)):
self.assertTrue(isinstance(t[0], BNode))
self.assertEqual(t[1].n3(), "<urn:type>")
self.assertEqual(t[2].n3(), "<urn:Blank>")

def testUpdateWithBlankNodeSerializeAndParse(self):
self.graph.update(
"INSERT DATA { GRAPH <urn:graph> { _:blankA <urn:type> <urn:Blank> } }")
g = self.graph.get_context(graphuri)
string = g.serialize(format='ntriples').decode('utf-8')
raised = False
try:
Graph().parse(data=string, format="ntriples")
except Exception as e:
raised = True
self.assertFalse(raised, 'Exception raised when parsing: ' + string)

def testMultipleUpdateWithInitBindings(self):
self.graph.update(
"INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WHERE { };"
Expand Down

0 comments on commit 0460579

Please sign in to comment.