Skip to content

Commit 9c023aa

Browse files
committed
wip
1 parent a9955b4 commit 9c023aa

File tree

11 files changed

+945
-56
lines changed

11 files changed

+945
-56
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,7 @@ filterwarnings = [
7878
]
7979
# log_cli = true
8080
# log_cli_level = "DEBUG"
81+
log_format = "%(asctime)s %(levelname)-8s %(name)-12s %(filename)s:%(lineno)s:%(funcName)s %(message)s"
82+
log_date_format = "%Y-%m-%dT%H:%M:%S"
8183
log_cli_format = "%(asctime)s %(levelname)-8s %(name)-12s %(filename)s:%(lineno)s:%(funcName)s %(message)s"
8284
log_cli_date_format = "%Y-%m-%dT%H:%M:%S"

rdflib/plugins/sparql/results/csvresults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,7 @@ def serialize(self, stream: IO, encoding: str = "utf-8", **kwargs):
8383
def serializeTerm(self, term, encoding):
8484
if term is None:
8585
return ""
86+
elif isinstance(term, BNode):
87+
return f"_:{term}"
8688
else:
8789
return term

rdflib/term.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,12 @@ def __new__(
597597
datatype: Optional[str] = None,
598598
normalize: Optional[bool] = None,
599599
) -> "Literal":
600+
# logging.debug(
601+
# "lexical_or_value = %r, datatype = %s, lang = %s",
602+
# lexical_or_value,
603+
# datatype,
604+
# lang,
605+
# )
600606

601607
if lang == "":
602608
lang = None # no empty lang-tags in RDF
@@ -674,6 +680,13 @@ def __new__(
674680
inst._value = value
675681
inst._ill_formed = ill_formed
676682

683+
# logging.debug(
684+
# "inst = %s, value = %s, ill_formed = %s",
685+
# inst,
686+
# value,
687+
# ill_formed,
688+
# )
689+
677690
return inst
678691

679692
def normalize(self) -> "Literal":

test/data/suites/w3c/sparql11/construct/constructwhere04.rq

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ PREFIX : <http://example.org/>
22

33
CONSTRUCT
44
FROM <data.ttl>
5-
WHERE { ?s ?p ?o }
5+
WHERE { ?s ?p ?o }

test/test_sparql/test_result.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
Iterator,
1717
Mapping,
1818
Optional,
19+
Pattern,
1920
Sequence,
2021
Set,
2122
TextIO,
@@ -29,8 +30,9 @@
2930
from pyparsing import ParseException
3031

3132
from rdflib.graph import Graph
33+
from rdflib.namespace import Namespace
3234
from rdflib.query import Result, ResultRow
33-
from rdflib.term import Identifier, Literal, Node, Variable
35+
from rdflib.term import BNode, Identifier, Literal, Node, Variable
3436

3537
BindingsType = Sequence[Mapping[Variable, Identifier]]
3638
ParseOutcomeType = Union[BindingsType, Type[Exception]]
@@ -75,6 +77,43 @@ def test_select_result_parse(
7577
assert parse_outcome == parsed_result.bindings
7678

7779

80+
EGSCHEME = Namespace("example:")
81+
82+
83+
@pytest.mark.parametrize(
84+
("node", "format", "expected_result"),
85+
[
86+
(BNode(), "csv", re.compile(r"^_:.*$")),
87+
(BNode("a"), "csv", "_:a"),
88+
(Literal("x11"), "csv", "x11"),
89+
],
90+
)
91+
def test_xsv_serialize(
92+
node: Identifier, format: str, expected_result: Union[Pattern[str], str]
93+
) -> None:
94+
graph = Graph()
95+
graph.add((EGSCHEME.checkSubject, EGSCHEME.checkPredicate, node))
96+
result = graph.query(
97+
f"""
98+
PREFIX egscheme: <{EGSCHEME}>
99+
SELECT ?o {{
100+
egscheme:checkSubject egscheme:checkPredicate ?o
101+
}}
102+
"""
103+
)
104+
assert len(result.bindings) == 1
105+
with BytesIO() as bio:
106+
result.serialize(bio, format=format)
107+
result_text = bio.getvalue().decode("utf-8")
108+
result_lines = result_text.splitlines()
109+
assert len(result_lines) == 2
110+
logging.debug("result_lines[1] = %r", result_lines[1])
111+
if isinstance(expected_result, str):
112+
assert expected_result == result_lines[1]
113+
else:
114+
assert expected_result.match(result_lines[1])
115+
116+
78117
@pytest.fixture(scope="module")
79118
def select_result(rdfs_graph: Graph) -> Result:
80119
query = """

test/test_w3c_spec/test_sparql_w3c.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import print_function
22

3+
import logging
4+
import os
35
import sys
46
from io import TextIOWrapper
57
from pathlib import PurePath
@@ -379,7 +381,10 @@ def skip(reason="(none)"):
379381
return
380382

381383
# eval test - carry out query
384+
logging.debug("cwd = %s, base = %s", os.getcwd(), urljoin(query, "."))
385+
logging.debug("g = %s", g.serialize(format="turtle"))
382386
res2 = g.query(bopen_read_close(query_path), base=urljoin(query, "."))
387+
logging.debug("g = %s", g.serialize(format="turtle"))
383388

384389
if resfile.endswith("ttl"):
385390
resg = Graph()
@@ -411,7 +416,7 @@ def skip(reason="(none)"):
411416

412417
else:
413418
res = Result.parse(f, format="xml")
414-
419+
logging.debug("here ...")
415420
if not DETAILEDASSERT:
416421
eq(res.type, res2.type, "Types do not match")
417422
if res.type == "SELECT":
@@ -422,6 +427,7 @@ def skip(reason="(none)"):
422427
elif res.type == "ASK":
423428
eq(res.askAnswer, res2.askAnswer, "Ask answer does not match")
424429
elif res.type in ("DESCRIBE", "CONSTRUCT"):
430+
logging.debug("res2.graph = %s", res2.graph.serialize(format="turtle"))
425431
assert isomorphic(res.graph, res2.graph), "graphs are not isomorphic!"
426432
else:
427433
raise Exception("Unknown result type: %s" % res.type)
@@ -452,6 +458,8 @@ def skip(reason="(none)"):
452458
% (res.askAnswer, res2.askAnswer),
453459
)
454460
elif res.type in ("DESCRIBE", "CONSTRUCT"):
461+
logging.debug("res.graph = %s", res2.graph.serialize(format="turtle"))
462+
logging.debug("res2.graph = %s", res2.graph.serialize(format="turtle"))
455463
assert isomorphic(res.graph, res2.graph), "graphs are not isomorphic!"
456464
else:
457465
raise Exception("Unknown result type: %s" % res.type)

0 commit comments

Comments
 (0)