Skip to content

Commit

Permalink
Merge pull request #387 from bcogrel/ttl-bool
Browse files Browse the repository at this point in the history
Bad boolean list serialization to Turtle
  • Loading branch information
gromgull committed Apr 27, 2014
2 parents ecf69aa + f8cf5e3 commit f6690c5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def isValidList(self, l):
def doList(self, l):
while l:
item = self.store.value(l, RDF.first)
if item:
if item is not None:
self.path(item, OBJECT)
self.subjectDone(l)
l = self.store.value(l, RDF.rest)
Expand Down
32 changes: 29 additions & 3 deletions test/test_turtle_serialize.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
import rdflib
from rdflib import Graph, URIRef, BNode, RDF, Literal
from rdflib.collection import Collection
from rdflib.py3compat import b


def testTurtleFinalDot():
"""
https://github.com/RDFLib/rdflib/issues/282
"""

g = rdflib.Graph()
u = rdflib.URIRef("http://ex.org/bob.")
g = Graph()
u = URIRef("http://ex.org/bob.")
g.bind("ns", "http://ex.org/")
g.add( (u, u, u) )
s=g.serialize(format='turtle')
assert b("ns:bob.") not in s


def testTurtleBoolList():
subject = URIRef("http://localhost/user")
predicate = URIRef("http://localhost/vocab#hasList")
g1 = Graph()
list_item1 = BNode()
list_item2 = BNode()
list_item3 = BNode()
g1.add((subject, predicate, list_item1))
g1.add((list_item1, RDF.first, Literal(True)))
g1.add((list_item1, RDF.rest, list_item2))
g1.add((list_item2, RDF.first, Literal(False)))
g1.add((list_item2, RDF.rest, list_item3))
g1.add((list_item3, RDF.first, Literal(True)))
g1.add((list_item3, RDF.rest, RDF.nil))

ttl_dump = g1.serialize(format="turtle")
g2 = Graph()
g2.parse(data=ttl_dump, format="turtle")

list_id = g2.value(subject, predicate)
bool_list = [i.toPython() for i in Collection(g2, list_id)]
assert bool_list == [True, False, True]


if __name__ == "__main__":
import nose, sys
nose.main(defaultTest=sys.argv[0])

0 comments on commit f6690c5

Please sign in to comment.