Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix turtle serialization of rdf:type in subject, object #1684

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ def serialize(self, stream, base=None, encoding=None, spacious=None, **args):
def preprocessTriple(self, triple):
super(TurtleSerializer, self).preprocessTriple(triple)
for i, node in enumerate(triple):
if node in self.keywords:
if i == VERB and node in self.keywords:
# predicate is a keyword
continue
# Don't use generated prefixes for subjects and objects
self.getQName(node, gen_prefix=(i == VERB))
Expand Down
16 changes: 0 additions & 16 deletions test/test_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,6 @@
reason="missing escaping: PCDATA invalid Char value 12 and 8",
raises=SAXParseException,
),
("n3", "rdf_prefix.jsonld"): pytest.mark.xfail(
reason="missing 'rdf:' prefix",
raises=BadSyntax,
),
("ttl", "rdf_prefix.jsonld"): pytest.mark.xfail(
reason="missing 'rdf:' prefix",
raises=BadSyntax,
),
("trig", "rdf_prefix.jsonld"): pytest.mark.xfail(
reason="missing 'rdf:' prefix",
raises=BadSyntax,
),
("turtle", "rdf_prefix.jsonld"): pytest.mark.xfail(
reason="missing 'rdf:' prefix",
raises=BadSyntax,
),
}

# This is for files which can only be represented properly in one format
Expand Down
51 changes: 46 additions & 5 deletions test/test_serializer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,53 @@
import logging
import unittest
from rdflib import Graph, URIRef, Literal
from rdflib import RDF, Graph, Literal, Namespace, URIRef
from tempfile import TemporaryDirectory
from pathlib import Path, PurePath

from typing import Tuple, cast

import pytest
import itertools

from rdflib.graph import ConjunctiveGraph

from .testutils import GraphHelper


@pytest.mark.parametrize(
"format, tuple_index, is_keyword",
[
(format, tuple_index, keyword)
for format, (tuple_index, keyword) in itertools.product(
["turtle", "n3", "trig"],
[
(0, False),
(1, True),
(2, False),
],
)
]
+ [("trig", 3, False)],
)
def test_rdf_type(format: str, tuple_index: int, is_keyword: bool) -> None:
NS = Namespace("example:")
graph = ConjunctiveGraph()
graph.bind("eg", NS)
nodes = [NS.subj, NS.pred, NS.obj, NS.graph]
nodes[tuple_index] = RDF.type
quad = cast(Tuple[URIRef, URIRef, URIRef, URIRef], tuple(nodes))
graph.add(quad)
data = graph.serialize(format=format)
logging.info("data = %s", data)
assert NS in data
if is_keyword:
assert str(RDF) not in data
else:
assert str(RDF) in data
parsed_graph = ConjunctiveGraph()
parsed_graph.parse(data=data, format=format)
GraphHelper.assert_triple_sets_equals(graph, parsed_graph)


class TestSerialize(unittest.TestCase):
def setUp(self) -> None:
Expand Down Expand Up @@ -56,7 +101,3 @@ def test_serialize_to_fileurl(self):
graph_check = Graph()
graph_check.parse(source=tfpath, format="nt")
self.assertEqual(self.triple, next(iter(graph_check)))


if __name__ == "__main__":
unittest.main()