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

test: convert test/test_serializers/test_serializer.py to pytest #1861

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 46 additions & 53 deletions test/test_serializers/test_serializer.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import itertools
import logging
import unittest
from pathlib import Path, PurePath
from tempfile import TemporaryDirectory
import re
from pathlib import Path
from test.testutils import GraphHelper
from typing import Tuple, cast

Expand Down Expand Up @@ -47,55 +46,49 @@ def test_rdf_type(format: str, tuple_index: int, is_keyword: bool) -> None:
GraphHelper.assert_triple_sets_equals(graph, parsed_graph)


class TestSerialize(unittest.TestCase):
def setUp(self) -> None:
EG = Namespace("example:")

graph = Graph()
subject = URIRef("example:subject")
predicate = URIRef("example:predicate")
object = Literal("日本語の表記体系", lang="jpx")
self.triple = (
subject,
predicate,
object,

@pytest.fixture
def simple_graph() -> Graph:
graph = Graph()
graph.add((EG.subject, EG.predicate, Literal("日本語の表記体系", lang="jpx")))
return graph


def test_serialize_to_purepath(tmp_path: Path, simple_graph: Graph):
tfpath = tmp_path / "out.nt"
simple_graph.serialize(destination=tfpath, format="nt", encoding="utf-8")
graph_check = Graph()
graph_check.parse(source=tfpath, format="nt")

GraphHelper.assert_triple_sets_equals(simple_graph, graph_check)


def test_serialize_to_path(tmp_path: Path, simple_graph: Graph):
tfpath = tmp_path / "out.nt"
simple_graph.serialize(destination=tfpath, format="nt", encoding="utf-8")
graph_check = Graph()
graph_check.parse(source=tfpath, format="nt")

GraphHelper.assert_triple_sets_equals(simple_graph, graph_check)


def test_serialize_to_neturl(simple_graph: Graph):
with pytest.raises(ValueError) as raised:
simple_graph.serialize(
destination="http://example.com/", format="nt", encoding="utf-8"
)
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)))
assert "destination" in f"{raised.value}"


def test_serialize_to_fileurl(tmp_path: Path, simple_graph: Graph):
tfpath = tmp_path / "out.nt"
tfurl = tfpath.as_uri()
assert re.match(r"^file:", tfurl)
assert not tfpath.exists()
simple_graph.serialize(destination=tfurl, format="nt", encoding="utf-8")
assert tfpath.exists()
graph_check = Graph()
graph_check.parse(source=tfpath, format="nt")
GraphHelper.assert_triple_sets_equals(simple_graph, graph_check)