Hi all,

I found a bug in the JSON sparql serializer. When a query contains an
OPTIONAL not matching all bindings in the result, the serializer adds
an unnecessary "," to the end of the list of bound variables. The eval
method in the javascript implementation of Opera had problems with
this, firefox handled it somehow. Anyway, I think the test case
explains it better (see attached file).

I'm also not sure if the selected variables are reported correctly. In
the test case "x", "name" and "friend" are selected, but in the head
only "x" and "name" are listed. If the optional wouldnt find any of
the variables listed in the optional then this is correct, I think...
but now when the "friend" variable is bound, should it also be listed
in head?

Cheers,
Mikael
from rdflib import ConjunctiveGraph, plugin
from rdflib.store import Store
from StringIO import StringIO
import unittest

test_data = """ 
@prefix foaf:       <http://xmlns.com/foaf/0.1/> .
@prefix rdf:        <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .

<http://example.org/alice>  foaf:name       "Alice" .
<http://example.org/alice>  foaf:knows      <http://example.org/bob> .
<http://example.org/bob>  foaf:name       "Bob" .
"""

test_query = """
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?name ?x ?friend
WHERE { ?x foaf:name ?name .
        OPTIONAL { ?x foaf:knows ?friend . }
}
"""

correct = """"name" : {"type": "literal", "xml:lang" : "None", "value" : "Bob"},\n                   "x" : {"type": "uri", "value" : "http://example.org/bob"}\n                }"""

class JSONTest(unittest.TestCase):

    def testOPTIONALSimple(self):
        graph = ConjunctiveGraph(plugin.get('IOMemory',Store)())
        graph.parse(StringIO(test_data), format="n3")
        results = graph.query(test_query)
        result_json = results.serialize(format='json')
        self.failUnless(result_json.find(correct) > 0)

if __name__ == "__main__":
    unittest.main()
_______________________________________________
Dev mailing list
Dev@rdflib.net
http://rdflib.net/mailman/listinfo/dev

Reply via email to