-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #528 from pchampin/fix-issue-381
SPARQL parser can deal with spurious ";", fixes #381
- Loading branch information
Showing
2 changed files
with
123 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|