diff options
author | Jörn Hees <joernhees@users.noreply.github.com> | 2015-02-25 14:02:20 +0100 |
---|---|---|
committer | Jörn Hees <joernhees@users.noreply.github.com> | 2015-02-25 14:02:20 +0100 |
commit | 1a25c0ce6809608b1c68f947a6443e13a671d59f (patch) | |
tree | 0a0530de9d0f47b3f9b2cf4f0a554e7485eee122 | |
parent | 127c5537a847fd427b1f75c5690b12a992ff0c7c (diff) | |
parent | 46567b44825a58142f039f22460302bbc29a1572 (diff) | |
download | rdflib-1a25c0ce6809608b1c68f947a6443e13a671d59f.tar.gz |
Merge pull request #459 from joernhees/sparql_store_empty_literals
SPARQLStore does not transform Literal('') into Literal('None') anymore, fixes #457
-rw-r--r-- | rdflib/plugins/stores/sparqlstore.py | 10 | ||||
-rw-r--r-- | test/test_issue457.py | 14 | ||||
-rw-r--r-- | test/test_sparqlupdatestore.py | 26 |
3 files changed, 37 insertions, 13 deletions
diff --git a/rdflib/plugins/stores/sparqlstore.py b/rdflib/plugins/stores/sparqlstore.py index 09966771..b3ae46eb 100644 --- a/rdflib/plugins/stores/sparqlstore.py +++ b/rdflib/plugins/stores/sparqlstore.py @@ -129,17 +129,15 @@ def CastToTerm(node): elif node.tag == '{%s}uri' % SPARQL_NS: return URIRef(node.text) elif node.tag == '{%s}literal' % SPARQL_NS: + value = node.text if node.text is not None else '' if 'datatype' in node.attrib: dT = URIRef(node.attrib['datatype']) - if False: # not node.xpath('*'): - return Literal('', datatype=dT) - else: - return Literal(node.text, datatype=dT) + return Literal(value, datatype=dT) elif '{http://www.w3.org/XML/1998/namespace}lang' in node.attrib: - return Literal(node.text, lang=node.attrib[ + return Literal(value, lang=node.attrib[ "{http://www.w3.org/XML/1998/namespace}lang"]) else: - return Literal(node.text) + return Literal(value) else: raise Exception('Unknown answer type') diff --git a/test/test_issue457.py b/test/test_issue457.py new file mode 100644 index 00000000..f9957dbe --- /dev/null +++ b/test/test_issue457.py @@ -0,0 +1,14 @@ +# test for https://github.com/RDFLib/rdflib/issues/457 + +import io +from xml.etree import ElementTree + +import rdflib +from rdflib.plugins.stores.sparqlstore import CastToTerm +from rdflib.py3compat import b + +s = b("""<ns0:literal xmlns:ns0="http://www.w3.org/2005/sparql-results#" />""") +node = ElementTree.parse(io.BytesIO(s)).getroot() +term = CastToTerm(node) +assert term == rdflib.Literal(''), repr(term) + diff --git a/test/test_sparqlupdatestore.py b/test/test_sparqlupdatestore.py index 2d54d0cb..73ac97de 100644 --- a/test/test_sparqlupdatestore.py +++ b/test/test_sparqlupdatestore.py @@ -2,7 +2,7 @@ import unittest import re -from rdflib import ConjunctiveGraph, URIRef +from rdflib import ConjunctiveGraph, URIRef, Literal from rdflib.util import from_n3 # this assumed SPARQL1.1 query/update endpoints @@ -105,23 +105,23 @@ class TestSparql11(unittest.TestCase): def testUpdate(self): self.graph.update("INSERT DATA { GRAPH <urn:graph> { <urn:michel> <urn:likes> <urn:pizza> . } }") - + g = self.graph.get_context(graphuri) self.assertEquals(1, len(g), 'graph contains 1 triples') - + def testUpdateWithInitNs(self): self.graph.update( "INSERT DATA { GRAPH ns:graph { ns:michel ns:likes ns:pizza . } }", initNs={'ns': URIRef('urn:')} ) - + g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), set([(michel,likes,pizza)]), 'only michel likes pizza' ) - + def testUpdateWithInitBindings(self): self.graph.update( "INSERT { GRAPH <urn:graph> { ?a ?b ?c . } } WherE { }", @@ -131,7 +131,7 @@ class TestSparql11(unittest.TestCase): 'c': URIRef('urn:pizza'), } ) - + g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), @@ -150,7 +150,7 @@ class TestSparql11(unittest.TestCase): 'd': URIRef('urn:bob'), } ) - + g = self.graph.get_context(graphuri) self.assertEquals( set(g.triples((None,None,None))), @@ -269,6 +269,18 @@ class TestSparql11(unittest.TestCase): self.assertTrue(empty_graph_iri in [unicode(g.identifier) for g in self.graph.contexts()]) + def testEmptyLiteral(self): + # test for https://github.com/RDFLib/rdflib/issues/457 + # also see test_issue457.py which is sparql store independent! + g = self.graph.get_context(graphuri) + g.add(( + URIRef('http://example.com/s'), + URIRef('http://example.com/p'), + Literal(''))) + + o = tuple(g)[0][2] + self.assertEquals(o, Literal(''), repr(o)) + from nose import SkipTest import urllib2 try: |