forked from RDFLib/rdflib
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: propagate exceptions from SPARQL TSV result parser
This patch changes the `TSVResultParser` to not eat exceptions, as this results in silent failures that are hard to debug, and in general all functions should either do what they say they will or create an error indication to the caller, which in python is done with exceptions. Fixes: - RDFLib#1477
- Loading branch information
Showing
2 changed files
with
71 additions
and
24 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,53 @@ | ||
import inspect | ||
import logging | ||
from io import StringIO | ||
from typing import Mapping, Sequence, Type, Union | ||
|
||
import pytest | ||
from pyparsing import ParseException | ||
|
||
from rdflib.query import Result | ||
from rdflib.term import Identifier, Literal, Variable | ||
|
||
BindingsType = Sequence[Mapping[Variable, Identifier]] | ||
ParseOutcomeType = Union[BindingsType, Type[Exception]] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("data", "format", "parse_outcome"), | ||
[ | ||
pytest.param( | ||
"a\n1", | ||
"csv", | ||
[{Variable("a"): Literal("1")}], | ||
id="csv-okay-1c1r", | ||
), | ||
pytest.param( | ||
'?a\n"1"', | ||
"tsv", | ||
[{Variable("a"): Literal("1")}], | ||
id="tsv-okay-1c1r", | ||
), | ||
pytest.param( | ||
"1,2,3\nhttp://example.com", | ||
"tsv", | ||
ParseException, | ||
id="tsv-invalid", | ||
), | ||
], | ||
) | ||
def test_select_result_parse( | ||
data: str, format: str, parse_outcome: ParseOutcomeType | ||
) -> None: | ||
""" | ||
Round tripping of a select query through the serializer and parser of a | ||
specific format results in an equivalent result object. | ||
""" | ||
logging.debug("data = %s", data) | ||
|
||
if inspect.isclass(parse_outcome) and issubclass(parse_outcome, Exception): | ||
with pytest.raises(parse_outcome): | ||
parsed_result = Result.parse(StringIO(data), format=format) | ||
else: | ||
parsed_result = Result.parse(StringIO(data), format=format) | ||
assert parse_outcome == parsed_result.bindings |