Skip to content

Commit

Permalink
Merge pull request #1002 from sa-bpelakh/1001-construct-init-bindings
Browse files Browse the repository at this point in the history
CONSTRUCT resolve with initBindings fixes #1001
  • Loading branch information
nicholascar committed Apr 16, 2020
2 parents 604d63e + 1dbe7ac commit acb9d10
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion rdflib/plugins/sparql/sparql.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ def __getitem__(self, key):
if not type(key) in (BNode, Variable):
return key

return self._d[key]
if key not in self._d:
return self.ctx.initBindings[key]
else:
return self._d[key]

def project(self, vars):
return FrozenBindings(
Expand Down
40 changes: 40 additions & 0 deletions test/test_sparql_construct_bindings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from rdflib import Graph, URIRef, Literal, BNode
from rdflib.plugins.sparql import prepareQuery
from rdflib.compare import isomorphic

import unittest
from nose.tools import eq_

class TestConstructInitBindings(unittest.TestCase):

def test_construct_init_bindings(self):
"""
This is issue https://github.com/RDFLib/rdflib/issues/1001
"""

g1 = Graph()

q_str = ("""
PREFIX : <urn:ns1:>
CONSTRUCT {
?uri :prop1 ?val1;
:prop2 ?c .
}
WHERE {
bind(uri(concat("urn:ns1:", ?a)) as ?uri)
bind(?b as ?val1)
}
""")
q_prepared = prepareQuery(q_str)

expected = [
(URIRef('urn:ns1:A'),URIRef('urn:ns1:prop1'), Literal('B')),
(URIRef('urn:ns1:A'),URIRef('urn:ns1:prop2'), Literal('C'))
]
results = g1.query(q_prepared, initBindings={
'a': Literal('A'),
'b': Literal('B'),
'c': Literal('C')
})

eq_(sorted(results, key=lambda x: str(x[1])), expected)

0 comments on commit acb9d10

Please sign in to comment.