Skip to content

Commit

Permalink
Merge pull request #592 from joernhees/fix_554
Browse files Browse the repository at this point in the history
SPARQL select nothing no longer returns a `None` row
  • Loading branch information
joernhees committed Feb 15, 2016
2 parents 133dcbe + 3efd0a2 commit 0d9ede7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
8 changes: 5 additions & 3 deletions rdflib/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,14 @@ def __iter__(self):

if self._genbindings:
for b in self._genbindings:
self._bindings.append(b)
yield ResultRow(b, self.vars)
if b: # don't add a result row in case of empty binding {}
self._bindings.append(b)
yield ResultRow(b, self.vars)
self._genbindings = None
else:
for b in self._bindings:
yield ResultRow(b, self.vars)
if b: # don't add a result row in case of empty binding {}
yield ResultRow(b, self.vars)

def __getattr__(self, name):
if self.type in ("CONSTRUCT", "DESCRIBE") and self.graph is not None:
Expand Down
4 changes: 2 additions & 2 deletions test/test_initbindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ def testDistinctOrderByLimit():
def testPrepare():
q = prepareQuery('SELECT ?target WHERE { }')
r = list(g.query(q))
e = [(None,)] # TODO: https://github.com/RDFLib/rdflib/issues/554
e = []
assert r == e, 'prepare: %r != %r'%(r,e)

r = list(g.query(q, initBindings={'target': Literal('example')}))
e = [(Literal('example'),)]
assert r == e, 'prepare: %r != %r'%(r, e)

r = list(g.query(q))
e = [(None,)] # TODO: https://github.com/RDFLib/rdflib/issues/554
e = []
assert r == e, 'prepare: %r != %r'%(r,e)


Expand Down
14 changes: 14 additions & 0 deletions test/test_issue554.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# test for https://github.com/RDFLib/rdflib/issues/554

import rdflib

def test_sparql_empty_no_row():
g = rdflib.Graph()
q = 'select ?whatever { }'
r = list(g.query(q))
assert r == [], \
'sparql query %s should return empty list but returns %s' % (q, r)


if __name__ == '__main__':
test_sparql_empty_no_row()

0 comments on commit 0d9ede7

Please sign in to comment.