Skip to content

Commit

Permalink
Merge branch 'RDFLib:master' into remove-deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
Graham Higgins committed Dec 15, 2021
2 parents 460f68f + eb151d3 commit eeb27e4
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 18 deletions.
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
2 changes: 1 addition & 1 deletion rdflib/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def __new__(cls, lexical_or_value, lang=None, datatype=None, normalize=None):
)

if lang and not _is_valid_langtag(lang):
raise Exception("'%s' is not a valid language tag!" % lang)
raise ValueError("'%s' is not a valid language tag!" % lang)

if datatype:
datatype = URIRef(datatype)
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
8 changes: 7 additions & 1 deletion test/test_issue893.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ def test_issue893_ds_unpickle():
picklestring = pickle.dumps(ds1)
ds2 = pickle.loads(picklestring)

for new_graph, graph in zip(ds2.graphs(), ds1.graphs()):
ds1graphs = list(ds1.graphs())
ds2graphs = list(ds2.graphs())

ds1graphs.sort()
ds2graphs.sort()

for new_graph, graph in zip(ds2graphs, ds1graphs):
assert new_graph.identifier == graph.identifier
for new_triple, triple in zip(
new_graph.triples((None, None, None)), graph.triples((None, None, None))
Expand Down
5 changes: 5 additions & 0 deletions test/test_literal.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ def testCantPassLangAndDatatype(self):
TypeError, Literal, "foo", lang="en", datatype=URIRef("http://example.com/")
)

def testCantPassInvalidLang(self):
self.assertRaises(
ValueError, Literal, "foo", lang="999"
)

def testFromOtherLiteral(self):
l = Literal(1)
l2 = Literal(l)
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

0 comments on commit eeb27e4

Please sign in to comment.