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: raise an exception when serialization format does not support quads
- fixes <RDFLib#2393> checkpoint checkpoint checkpoint
- Loading branch information
Showing
10 changed files
with
265 additions
and
10 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
File renamed without changes.
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,5 @@ | ||
<http://example.com/subject> <http://example.com/predicate> "日本語の表記体系"@jpx . | ||
<urn:example:subject> <example:predicate> <example:subject> . | ||
<example:object> <http://example.com/predicate> "XSD string" . | ||
<example:subject> <example:predicate> <example:object> . | ||
<example:subject> <example:predicate> "12"^^<http://www.w3.org/2001/XMLSchema#integer> . |
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,15 @@ | ||
@prefix eghttp: <http://example.com/> . | ||
@prefix egurn: <urn:example:> . | ||
@prefix egschema: <example:> . | ||
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | ||
|
||
{ | ||
eghttp:subject eghttp:predicate "日本語の表記体系"@jpx . | ||
|
||
egurn:subject egschema:predicate egschema:subject . | ||
} | ||
|
||
egschema:object eghttp:predicate "XSD string"^^xsd:string . | ||
|
||
egschema:subject egschema:predicate egschema:object, | ||
12 . |
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 itertools | ||
import logging | ||
from test.utils.graph import GRAPH_FORMATS, GraphType | ||
from test.utils.variants import load_pyvariant | ||
from typing import Dict, Iterable, Type | ||
|
||
import pytest | ||
from _pytest.mark.structures import ParameterSet | ||
from _pytest.outcomes import Failed | ||
|
||
from rdflib.graph import ConjunctiveGraph, Dataset, Graph | ||
|
||
|
||
def make_quads_in_triples_cases() -> Iterable[ParameterSet]: | ||
""" | ||
Generate test cases for serializing named graphs (i.e. quads) into a format | ||
that does not support named graphs. | ||
""" | ||
triple_only_formats = [ | ||
graph_format | ||
for graph_format in GRAPH_FORMATS | ||
if graph_format.info.graph_types == {GraphType.TRIPLE} | ||
] | ||
for graph_type, graph_format in itertools.product( | ||
(ConjunctiveGraph, Dataset), triple_only_formats | ||
): | ||
for serializer in graph_format.info.serializers: | ||
yield pytest.param( | ||
graph_type, serializer, marks=pytest.mark.xfail(raises=Failed) | ||
) | ||
|
||
|
||
CONJUNCTIVE_GRAPH_WITH_QUADS = load_pyvariant("diverse_quads", ConjunctiveGraph) | ||
DATASET_WITH_QUADS = load_pyvariant("diverse_quads", Dataset) | ||
|
||
GRAPHS: Dict[Type[Graph], Graph] = { | ||
ConjunctiveGraph: CONJUNCTIVE_GRAPH_WITH_QUADS, | ||
Dataset: DATASET_WITH_QUADS, | ||
} | ||
|
||
|
||
@pytest.mark.parametrize(["graph_type", "serializer"], make_quads_in_triples_cases()) | ||
def test_quads_in_triples(graph_type: Type[ConjunctiveGraph], serializer: str) -> None: | ||
""" | ||
Serializing named graphs (i.e. quads) inside a `ConjunctiveGraph` into a | ||
format that does not support named graphs should result in an exception. | ||
""" | ||
graph = GRAPHS[graph_type] | ||
assert type(graph) is graph_type | ||
with pytest.raises(Exception) as caught: | ||
graph.serialize(format=serializer) | ||
|
||
logging.debug("caught.value = %r", caught.value, exc_info=caught.value) |
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,34 @@ | ||
""" | ||
Functionality for interacting with graph variants in `test/data/variants`. | ||
""" | ||
|
||
from functools import lru_cache | ||
from importlib import import_module | ||
from typing import Type | ||
|
||
from rdflib.graph import Graph, _GraphT | ||
|
||
|
||
def parse_pyvariant(variant_name: str, target: Graph) -> None: | ||
""" | ||
Parse the graph variant with the given name into the target graph. | ||
:param variant_name: the name of the graph variant to parse | ||
:param target: the graph to parse the variant into | ||
""" | ||
module_name = f"test.data.variants.{variant_name}" | ||
module = import_module(module_name) | ||
module.populate_graph(target) | ||
|
||
|
||
@lru_cache(maxsize=None) | ||
def load_pyvariant(variant_name: str, graph_type: Type[_GraphT]) -> _GraphT: | ||
""" | ||
Load the graph variant with the given name. | ||
:param variant_name: the name of the graph variant to load | ||
:return: the loaded graph variant | ||
""" | ||
target = graph_type() | ||
parse_pyvariant(variant_name, target) | ||
return target |