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

Quench nt test userwarn #1500

Merged
merged 4 commits into from Dec 15, 2021
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: 1 addition & 2 deletions rdflib/plugins/serializers/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ def serialize(
encoding = encoding or "utf-8"
if encoding not in ("utf-8", "utf-16"):
warnings.warn(
"JSON should be encoded as unicode. "
+ "Given encoding was: %s" % encoding
"JSON should be encoded as unicode. " f"Given encoding was: {encoding}"
)

context_data = kwargs.get("context")
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/serializers/nquads.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def serialize(
warnings.warn("NQuadsSerializer does not support base.")
if encoding is not None and encoding.lower() != self.encoding.lower():
warnings.warn(
"NQuadsSerializer does not use custom encoding."
+ "Given encoding was: %s" % encoding
"NQuadsSerializer does not use custom encoding. "
f"Given encoding was: {encoding}"
)
encoding = self.encoding
for context in self.store.contexts():
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/serializers/nt.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def serialize(
warnings.warn("NTSerializer does not support base.")
if encoding != "utf-8":
warnings.warn(
"NTSerializer always uses UTF-8 encoding."
+ "Given encoding was: %s" % encoding
"NTSerializer always uses UTF-8 encoding. "
f"Given encoding was: {encoding}"
)

for triple in self.store:
Expand Down
5 changes: 2 additions & 3 deletions test/test_finalnewline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ def testFinalNewline():
"""
http://code.google.com/p/rdflib/issues/detail?id=5
"""
import sys

graph = ConjunctiveGraph()
graph.add(
Expand All @@ -19,8 +18,8 @@ def testFinalNewline():

failed = set()
for p in rdflib.plugin.plugins(None, rdflib.plugin.Serializer):
v = graph.serialize(format=p.name, encoding="latin-1")
lines = v.split("\n".encode("latin-1"))
v = graph.serialize(format=p.name, encoding="utf-8")
lines = v.split("\n".encode("utf-8"))
if b"\n" not in v or (lines[-1] != b""):
failed.add(p.name)
# JSON-LD does not require a final newline (because JSON doesn't)
Expand Down
2 changes: 1 addition & 1 deletion test/test_nquads.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_serialize(self):
g.get_context(uri1).add((bob, likes, pizza))
g.get_context(uri2).add((bob, likes, pizza))

s = g.serialize(format="nquads", encoding="latin-1")
s = g.serialize(format="nquads", encoding="utf-8")
self.assertEqual(len([x for x in s.split(b"\n") if x.strip()]), 2)

g2 = ConjunctiveGraph()
Expand Down
14 changes: 8 additions & 6 deletions test/test_serializer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from rdflib import Graph, URIRef
from rdflib import Graph, URIRef, Literal
from tempfile import TemporaryDirectory
from pathlib import Path, PurePath

Expand All @@ -10,7 +10,7 @@ def setUp(self) -> None:
graph = Graph()
subject = URIRef("example:subject")
predicate = URIRef("example:predicate")
object = URIRef("example:object")
object = Literal("日本語の表記体系", lang="jpx")
self.triple = (
subject,
predicate,
Expand All @@ -23,7 +23,7 @@ def setUp(self) -> None:
def test_serialize_to_purepath(self):
with TemporaryDirectory() as td:
tfpath = PurePath(td) / "out.nt"
self.graph.serialize(destination=tfpath, format="nt")
self.graph.serialize(destination=tfpath, format="nt", encoding="utf-8")
graph_check = Graph()
graph_check.parse(source=tfpath, format="nt")

Expand All @@ -32,15 +32,17 @@ def test_serialize_to_purepath(self):
def test_serialize_to_path(self):
with TemporaryDirectory() as td:
tfpath = Path(td) / "out.nt"
self.graph.serialize(destination=tfpath, format="nt")
self.graph.serialize(destination=tfpath, format="nt", encoding="utf-8")
graph_check = Graph()
graph_check.parse(source=tfpath, format="nt")

self.assertEqual(self.triple, next(iter(graph_check)))

def test_serialize_to_neturl(self):
with self.assertRaises(ValueError) as raised:
self.graph.serialize(destination="http://example.com/", format="nt")
self.graph.serialize(
destination="http://example.com/", format="nt", encoding="utf-8"
)
self.assertIn("destination", f"{raised.exception}")

def test_serialize_to_fileurl(self):
Expand All @@ -49,7 +51,7 @@ def test_serialize_to_fileurl(self):
tfurl = tfpath.as_uri()
self.assertRegex(tfurl, r"^file:")
self.assertFalse(tfpath.exists())
self.graph.serialize(destination=tfurl, format="nt")
self.graph.serialize(destination=tfurl, format="nt", encoding="utf-8")
self.assertTrue(tfpath.exists())
graph_check = Graph()
graph_check.parse(source=tfpath, format="nt")
Expand Down