|
1 | 1 | import itertools |
2 | 2 | import logging |
3 | | -import unittest |
4 | | -from pathlib import Path, PurePath |
5 | | -from tempfile import TemporaryDirectory |
| 3 | +import re |
| 4 | +from pathlib import Path |
6 | 5 | from test.testutils import GraphHelper |
7 | 6 | from typing import Tuple, cast |
8 | 7 |
|
@@ -47,55 +46,49 @@ def test_rdf_type(format: str, tuple_index: int, is_keyword: bool) -> None: |
47 | 46 | GraphHelper.assert_triple_sets_equals(graph, parsed_graph) |
48 | 47 |
|
49 | 48 |
|
50 | | -class TestSerialize(unittest.TestCase): |
51 | | - def setUp(self) -> None: |
| 49 | +EG = Namespace("example:") |
52 | 50 |
|
53 | | - graph = Graph() |
54 | | - subject = URIRef("example:subject") |
55 | | - predicate = URIRef("example:predicate") |
56 | | - object = Literal("日本語の表記体系", lang="jpx") |
57 | | - self.triple = ( |
58 | | - subject, |
59 | | - predicate, |
60 | | - object, |
| 51 | + |
| 52 | +@pytest.fixture |
| 53 | +def simple_graph() -> Graph: |
| 54 | + graph = Graph() |
| 55 | + graph.add((EG.subject, EG.predicate, Literal("日本語の表記体系", lang="jpx"))) |
| 56 | + return graph |
| 57 | + |
| 58 | + |
| 59 | +def test_serialize_to_purepath(tmp_path: Path, simple_graph: Graph): |
| 60 | + tfpath = tmp_path / "out.nt" |
| 61 | + simple_graph.serialize(destination=tfpath, format="nt", encoding="utf-8") |
| 62 | + graph_check = Graph() |
| 63 | + graph_check.parse(source=tfpath, format="nt") |
| 64 | + |
| 65 | + GraphHelper.assert_triple_sets_equals(simple_graph, graph_check) |
| 66 | + |
| 67 | + |
| 68 | +def test_serialize_to_path(tmp_path: Path, simple_graph: Graph): |
| 69 | + tfpath = tmp_path / "out.nt" |
| 70 | + simple_graph.serialize(destination=tfpath, format="nt", encoding="utf-8") |
| 71 | + graph_check = Graph() |
| 72 | + graph_check.parse(source=tfpath, format="nt") |
| 73 | + |
| 74 | + GraphHelper.assert_triple_sets_equals(simple_graph, graph_check) |
| 75 | + |
| 76 | + |
| 77 | +def test_serialize_to_neturl(simple_graph: Graph): |
| 78 | + with pytest.raises(ValueError) as raised: |
| 79 | + simple_graph.serialize( |
| 80 | + destination="http://example.com/", format="nt", encoding="utf-8" |
61 | 81 | ) |
62 | | - graph.add(self.triple) |
63 | | - self.graph = graph |
64 | | - return super().setUp() |
65 | | - |
66 | | - def test_serialize_to_purepath(self): |
67 | | - with TemporaryDirectory() as td: |
68 | | - tfpath = PurePath(td) / "out.nt" |
69 | | - self.graph.serialize(destination=tfpath, format="nt", encoding="utf-8") |
70 | | - graph_check = Graph() |
71 | | - graph_check.parse(source=tfpath, format="nt") |
72 | | - |
73 | | - self.assertEqual(self.triple, next(iter(graph_check))) |
74 | | - |
75 | | - def test_serialize_to_path(self): |
76 | | - with TemporaryDirectory() as td: |
77 | | - tfpath = Path(td) / "out.nt" |
78 | | - self.graph.serialize(destination=tfpath, format="nt", encoding="utf-8") |
79 | | - graph_check = Graph() |
80 | | - graph_check.parse(source=tfpath, format="nt") |
81 | | - |
82 | | - self.assertEqual(self.triple, next(iter(graph_check))) |
83 | | - |
84 | | - def test_serialize_to_neturl(self): |
85 | | - with self.assertRaises(ValueError) as raised: |
86 | | - self.graph.serialize( |
87 | | - destination="http://example.com/", format="nt", encoding="utf-8" |
88 | | - ) |
89 | | - self.assertIn("destination", f"{raised.exception}") |
90 | | - |
91 | | - def test_serialize_to_fileurl(self): |
92 | | - with TemporaryDirectory() as td: |
93 | | - tfpath = Path(td) / "out.nt" |
94 | | - tfurl = tfpath.as_uri() |
95 | | - self.assertRegex(tfurl, r"^file:") |
96 | | - self.assertFalse(tfpath.exists()) |
97 | | - self.graph.serialize(destination=tfurl, format="nt", encoding="utf-8") |
98 | | - self.assertTrue(tfpath.exists()) |
99 | | - graph_check = Graph() |
100 | | - graph_check.parse(source=tfpath, format="nt") |
101 | | - self.assertEqual(self.triple, next(iter(graph_check))) |
| 82 | + assert "destination" in f"{raised.value}" |
| 83 | + |
| 84 | + |
| 85 | +def test_serialize_to_fileurl(tmp_path: Path, simple_graph: Graph): |
| 86 | + tfpath = tmp_path / "out.nt" |
| 87 | + tfurl = tfpath.as_uri() |
| 88 | + assert re.match(r"^file:", tfurl) |
| 89 | + assert not tfpath.exists() |
| 90 | + simple_graph.serialize(destination=tfurl, format="nt", encoding="utf-8") |
| 91 | + assert tfpath.exists() |
| 92 | + graph_check = Graph() |
| 93 | + graph_check.parse(source=tfpath, format="nt") |
| 94 | + GraphHelper.assert_triple_sets_equals(simple_graph, graph_check) |
0 commit comments