1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
import unittest
from rdflib import Graph, URIRef, Literal
from tempfile import TemporaryDirectory
from pathlib import Path, PurePath
class TestSerialize(unittest.TestCase):
def setUp(self) -> None:
graph = Graph()
subject = URIRef("example:subject")
predicate = URIRef("example:predicate")
object = Literal("日本語の表記体系", lang="jpx")
self.triple = (
subject,
predicate,
object,
)
graph.add(self.triple)
self.graph = graph
return super().setUp()
def test_serialize_to_purepath(self):
with TemporaryDirectory() as td:
tfpath = PurePath(td) / "out.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_path(self):
with TemporaryDirectory() as td:
tfpath = Path(td) / "out.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", encoding="utf-8"
)
self.assertIn("destination", f"{raised.exception}")
def test_serialize_to_fileurl(self):
with TemporaryDirectory() as td:
tfpath = Path(td) / "out.nt"
tfurl = tfpath.as_uri()
self.assertRegex(tfurl, r"^file:")
self.assertFalse(tfpath.exists())
self.graph.serialize(destination=tfurl, format="nt", encoding="utf-8")
self.assertTrue(tfpath.exists())
graph_check = Graph()
graph_check.parse(source=tfpath, format="nt")
self.assertEqual(self.triple, next(iter(graph_check)))
if __name__ == "__main__":
unittest.main()
|