Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue #381 #528

Merged
merged 1 commit into from
Sep 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions rdflib/plugins/sparql/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ def expandTriples(terms):
l = len(terms)
for i, t in enumerate(terms):
if t == ',':
res.append(res[i - 3])
res.append(res[i - 2])
res.extend([res[-3], res[-2]])
elif t == ';':
res.append(res[i - 3])
if i+1 == len(terms) or terms[i+1] == ";" or terms[i+1] == ".":
continue # this semicolon is spurious
res.append(res[-3])
elif isinstance(t, list):
# BlankNodePropertyList
# is this bnode the object of previous triples?
Expand Down
119 changes: 119 additions & 0 deletions test/test_issue381.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from rdflib import BNode, Graph, Namespace
from rdflib.compare import isomorphic

NS = Namespace("http://example.org/")

def test_no_spurious_semicolon():
sparql = """
PREFIX : <http://example.org/>
CONSTRUCT {
:a :b :c ; :d :e .
} WHERE {}
"""
expected = Graph()
expected.addN( t+(expected,) for t in [
(NS.a, NS.b, NS.c),
(NS.a, NS.d, NS.e),
])
got = Graph().query(sparql).graph
assert isomorphic(got, expected), got.serialize(format="turtle")

def test_one_spurious_semicolon():
sparql = """
PREFIX : <http://example.org/>
CONSTRUCT {
:a :b :c ; :d :e ; .
} WHERE {}
"""
expected = Graph()
expected.addN( t+(expected,) for t in [
(NS.a, NS.b, NS.c),
(NS.a, NS.d, NS.e),
])
got = Graph().query(sparql).graph
assert isomorphic(got, expected), got.serialize(format="turtle")

def test_one_spurious_semicolon_no_perdiod():
sparql = """
PREFIX : <http://example.org/>
CONSTRUCT {
:a :b :c ; :d :e ;
} WHERE {}
"""
expected = Graph()
expected.addN( t+(expected,) for t in [
(NS.a, NS.b, NS.c),
(NS.a, NS.d, NS.e),
])
got = Graph().query(sparql).graph
assert isomorphic(got, expected), got.serialize(format="turtle")

def test_two_spurious_semicolons_no_period():
sparql = """
PREFIX : <http://example.org/>
CONSTRUCT {
:a :b :c ; :d :e ; ;
} WHERE {}
"""
expected = Graph()
expected.addN( t+(expected,) for t in [
(NS.a, NS.b, NS.c),
(NS.a, NS.d, NS.e),
])
got = Graph().query(sparql).graph
assert isomorphic(got, expected), got.serialize(format="turtle")

def test_one_spurious_semicolons_bnode():
sparql = """
PREFIX : <http://example.org/>
CONSTRUCT {
[ :b :c ; :d :e ; ]
} WHERE {}
"""
expected = Graph()
expected.addN( t+(expected,) for t in [
(BNode("a"), NS.b, NS.c),
(BNode("a"), NS.d, NS.e),
])
got = Graph().query(sparql).graph
assert isomorphic(got, expected), got.serialize(format="turtle")

def test_pathological():
"""
This test did not raise an exception,
but generated a graph completely different from the expected one.
"""
sparql = """
PREFIX : <http://example.org/>
CONSTRUCT {
:a :b :c ; ;
:d :e ; ;
:f :g ;
} WHERE {}
"""
expected = Graph()
expected.addN( t+(expected,) for t in [
(NS.a, NS.b, NS.c),
(NS.a, NS.d, NS.e),
(NS.a, NS.f, NS.g),
])
got = Graph().query(sparql).graph
assert isomorphic(got, expected), got.serialize(format="turtle")

def test_mixing_spurious_semicolons_and_commas():
sparql = """
PREFIX : <http://example.org/>
CONSTRUCT {
:a :b :c ; ;
:d :e, :f
} WHERE {}
"""
expected = Graph()
expected.addN( t+(expected,) for t in [
(NS.a, NS.b, NS.c),
(NS.a, NS.d, NS.e),
(NS.a, NS.d, NS.f),
])
got = Graph().query(sparql).graph
assert isomorphic(got, expected), got.serialize(format="turtle")