diff options
author | Jeffrey C. Lerman <jeff@sironamedical.com> | 2023-03-12 05:17:39 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-12 13:17:39 +0100 |
commit | a44bd9918ffc4dd6ea68c7c9f74f1c95f7f7e204 (patch) | |
tree | 7eb81a571c6637c55d0edfd8e34cf430a0a9179a /test | |
parent | 8c4854908d3fbd89c57fe40ecd3d11c0a634860f (diff) | |
download | rdflib-a44bd9918ffc4dd6ea68c7c9f74f1c95f7f7e204.tar.gz |
fix: add more type-hinting for SPARQL plugin (#2265)
Here, adding type-hints to some of the SPARQL parser plugin code.
Includes a couple of small consequent changes:
1. Minor refactor of `prettify_parsetree()`, separating the public-facing callable from the internal code that does not need to be public-facing. That allows the public-facing callable to have more informative and restrictive type-hints for its arguments.
2. Added some test-coverage for `expandUnicodeEscapes()` - initially for my own understanding, but seems useful to leave it in place since I didn't see test-coverage for that function.
There should be no backwards-incompatible changes in this PR - at least, not intentionally.
---------
Co-authored-by: Iwan Aucamp <aucampia@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/test_sparql/test_sparql.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/test/test_sparql/test_sparql.py b/test/test_sparql/test_sparql.py index 02406bdf..80768604 100644 --- a/test/test_sparql/test_sparql.py +++ b/test/test_sparql/test_sparql.py @@ -16,7 +16,7 @@ from rdflib.plugins.sparql import prepareQuery, sparql from rdflib.plugins.sparql.algebra import translateQuery from rdflib.plugins.sparql.evaluate import evalPart from rdflib.plugins.sparql.evalutils import _eval -from rdflib.plugins.sparql.parser import parseQuery +from rdflib.plugins.sparql.parser import expandUnicodeEscapes, parseQuery from rdflib.plugins.sparql.parserutils import prettify_parsetree from rdflib.plugins.sparql.sparql import SPARQLError from rdflib.query import Result, ResultRow @@ -957,3 +957,25 @@ def test_sparql_describe( subjects = {s for s in r.graph.subjects() if not isinstance(s, BNode)} assert subjects == expected_subjects assert len(r.graph) == expected_size + + +@pytest.mark.parametrize( + "arg, expected_result, expected_valid", + [ + ("abc", "abc", True), + ("1234", "1234", True), + (r"1234\u0050", "1234P", True), + (r"1234\u00e3", "1234\u00e3", True), + (r"1234\u00e3\u00e5", "1234ãå", True), + (r"1234\u900000e5", "", False), + (r"1234\u010000e5", "", False), + (r"1234\u001000e5", "1234\U001000e5", True), + ], +) +def test_expand_unicode_escapes(arg: str, expected_result: str, expected_valid: bool): + if expected_valid: + actual_result = expandUnicodeEscapes(arg) + assert actual_result == expected_result + else: + with pytest.raises(ValueError, match="Invalid unicode code point"): + _ = expandUnicodeEscapes(arg) |