Skip to content

Commit 1b851d5

Browse files
committed
Add helper to compare generalized nquads in test runner
1 parent a694d23 commit 1b851d5

3 files changed

Lines changed: 55 additions & 43 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- Literals with invalid `@language` no longer output triples. Fixes [toRdf#twf05](https://w3c.github.io/json-ld-api/tests/toRdf-manifest#twf05).
2828
- IRI validation now detects multiple # fragments so these IRI's are skipped during toRDF. Fixes [toRdf#te111](https://w3c.github.io/json-ld-api/tests/toRdf-manifest#te111) and [toRdf#te112](https://w3c.github.io/json-ld-api/tests/toRdf-manifest#te112).
2929
- In `_create_node_map()`, ignored `@id: None` subjects are now dropped before they enter the node map to prevent `_graph_to_rdf()` from crashing when sorting mixed `None` and string keys. Fixes [toRdf#te122](https://w3c.github.io/json-ld-api/tests/toRdf-manifest#te122).
30+
- Use a helper for generalized N-Quads normalization in the test runner, since rdflib cannot parse blank-node predicates. Fixes [toRdf#te075](https://w3c.github.io/json-ld-api/tests/toRdf-manifest#te075).
3031

3132
### Changed
3233
- **BREAKING**: Migrated `jsonld.to_rdf()`, `jsonld.from_rdf()`, and N-Quads parsing/serialization to use `rdflib.Dataset` and RDFLib terms directly.

lib/pyld/options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ class NormalizeOptions(ProcessingOptions, total=False):
146146
processingMode: Literal['json-ld-1.0', 'json-ld-1.1']
147147
"""Either `json-ld-1.0` or `json-ld-1.1` (default: `json-ld-1.1`)."""
148148

149-
algorithm: Literal['URDNA2015', 'URGNA2012']
150-
"""The algorithm to use (default: `URGNA2012`)."""
149+
algorithm: Literal['URDNA2015', 'URGNA2012', 'RDFC10']
150+
"""The algorithm to use (default: `RDFC10`)."""
151151

152152
inputFormat: Literal['application/n-quads']
153153
"""The format if input is not JSON-LD: `application/n-quads` for N-Quads."""

tests/runtests.py

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -455,23 +455,29 @@ def runTest(self):
455455
if self.is_syntax and not self.pending:
456456
self.assertTrue(True)
457457
elif self.test_type == 'jld:ToRDFTest':
458-
# Test normalized results
459-
result = jsonld.normalize(
460-
result,
461-
{
462-
'algorithm': 'URGNA2012',
463-
'inputFormat': 'application/n-quads',
464-
'format': 'application/n-quads',
465-
},
466-
)
467-
expect = jsonld.normalize(
468-
expect,
469-
{
470-
'algorithm': 'URGNA2012',
471-
'inputFormat': 'application/n-quads',
472-
'format': 'application/n-quads',
473-
},
474-
)
458+
# avoid normalization when produceGeneralizedRdf is enabled,
459+
# since the rdflib parser cannot parse blank-node predicates.
460+
if data.get('option', {}).get('produceGeneralizedRdf'):
461+
result = _normalize_generalized_nquads_text(result)
462+
expect = _normalize_generalized_nquads_text(expect)
463+
else:
464+
# Test normalized results
465+
result = jsonld.normalize(
466+
result,
467+
{
468+
'algorithm': 'RDFC10',
469+
'inputFormat': 'application/n-quads',
470+
'format': 'application/n-quads',
471+
},
472+
)
473+
expect = jsonld.normalize(
474+
expect,
475+
{
476+
'algorithm': 'RDFC10',
477+
'inputFormat': 'application/n-quads',
478+
'format': 'application/n-quads',
479+
},
480+
)
475481
if _running_under_pytest():
476482
assert result == expect
477483
else:
@@ -535,6 +541,26 @@ def _running_under_pytest():
535541
return 'pytest' in sys.modules
536542

537543

544+
def _normalize_generalized_nquads_text(nquads):
545+
"""Normalize generalized N-Quads text for test-suite comparisons."""
546+
# Remove string datatype
547+
nquads = nquads.replace(
548+
'^^<http://www.w3.org/2001/XMLSchema#string> .',
549+
'.',
550+
)
551+
552+
# Re-label blank nodes
553+
bnodes = {}
554+
555+
# Generalized N-Quads expected results only need deterministic comparison,
556+
# not preservation of input blank node labels.
557+
nquads = re.sub(r'_:([A-Za-z][A-Za-z0-9]*)', lambda m: bnodes.setdefault(m[0], f'_:b{len(bnodes)}'), nquads)
558+
lines = (re.sub(r'\s*\.$', '.', line.strip()) for line in nquads.splitlines())
559+
560+
# Sort nquads
561+
return '\n'.join(sorted(line for line in lines if line))
562+
563+
538564
# Compare values with order-insensitive array tests
539565
def equal_unordered(result, expect):
540566
"""
@@ -925,7 +951,6 @@ def write(self, filename):
925951
# skip tests where behavior changed for a 1.1 processor
926952
# see JSON-LD 1.0 Errata
927953
'specVersion': ['json-ld-1.0'],
928-
'idRegex': [],
929954
},
930955
'fn': 'compact',
931956
'params': [
@@ -957,7 +982,6 @@ def write(self, filename):
957982
# skip tests where behavior changed for a 1.1 processor
958983
# see JSON-LD 1.0 Errata
959984
'specVersion': ['json-ld-1.0'],
960-
'idRegex': [],
961985
},
962986
'fn': 'expand',
963987
'params': [read_test_url('input'), create_test_options()],
@@ -968,7 +992,6 @@ def write(self, filename):
968992
# skip tests where behavior changed for a 1.1 processor
969993
# see JSON-LD 1.0 Errata
970994
'specVersion': ['json-ld-1.0'],
971-
'idRegex': [],
972995
},
973996
'fn': 'flatten',
974997
'params': [
@@ -983,7 +1006,6 @@ def write(self, filename):
9831006
# skip tests where behavior changed for a 1.1 processor
9841007
# see JSON-LD 1.0 Errata
9851008
'specVersion': ['json-ld-1.0'],
986-
'idRegex': [],
9871009
},
9881010
'fn': 'frame',
9891011
'params': [
@@ -995,7 +1017,6 @@ def write(self, filename):
9951017
'jld:FromRDFTest': {
9961018
'skip': {
9971019
'specVersion': ['json-ld-1.0'],
998-
'idRegex': [],
9991020
},
10001021
'fn': 'from_rdf',
10011022
'params': [
@@ -1012,17 +1033,11 @@ def write(self, filename):
10121033
],
10131034
},
10141035
'jld:ToRDFTest': {
1015-
'pending': {
1016-
'idRegex': [
1017-
# blank node property
1018-
'.*toRdf-manifest#te075$',
1019-
]
1020-
},
1036+
'pending': {},
10211037
'skip': {
10221038
# skip tests where behavior changed for a 1.1 processor
10231039
# see JSON-LD 1.0 Errata
10241040
'specVersion': ['json-ld-1.0'],
1025-
'idRegex': [],
10261041
},
10271042
'fn': 'to_rdf',
10281043
'params': [
@@ -1031,8 +1046,8 @@ def write(self, filename):
10311046
],
10321047
},
10331048
'rdfn:Urgna2012EvalTest': {
1034-
'pending': {'idRegex': []},
1035-
'skip': {'idRegex': []},
1049+
'pending': {},
1050+
'skip': {},
10361051
'fn': 'normalize',
10371052
'params': [
10381053
read_test_property('action'),
@@ -1046,8 +1061,8 @@ def write(self, filename):
10461061
],
10471062
},
10481063
'rdfn:Urdna2015EvalTest': {
1049-
'pending': {'idRegex': []},
1050-
'skip': {'idRegex': []},
1064+
'pending': {},
1065+
'skip': {},
10511066
'fn': 'normalize',
10521067
'params': [
10531068
read_test_property('action'),
@@ -1061,8 +1076,8 @@ def write(self, filename):
10611076
],
10621077
},
10631078
'rdfc:RDFC10EvalTest': {
1064-
'pending': {'idRegex': []},
1065-
'skip': {'idRegex': []},
1079+
'pending': {},
1080+
'skip': {},
10661081
'fn': 'normalize',
10671082
'params': [
10681083
read_test_property('action'),
@@ -1074,12 +1089,8 @@ def write(self, filename):
10741089
]
10751090
},
10761091
'rdfc:RDFC10MapTest': {
1077-
'pending': {
1078-
'idRegex': []
1079-
},
1080-
'skip': {
1081-
'idRegex': []
1082-
},
1092+
'pending': {},
1093+
'skip': {},
10831094
'fn': 'normalize',
10841095
'params': [
10851096
read_test_property('action'),

0 commit comments

Comments
 (0)