-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pathlib.PurePath support for Graph.{serialize,parse}
Graph.parse did already support pathlib.Path but there is no good reason to not support PurePath AFAICT.
- Loading branch information
Showing
3 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from unittest import TestCase | ||
from rdflib import Graph, URIRef | ||
from tempfile import NamedTemporaryFile, TemporaryDirectory | ||
from pathlib import Path, PurePath | ||
|
||
|
||
class TestSerialize(TestCase): | ||
def setUp(self) -> None: | ||
|
||
graph = Graph() | ||
subject = URIRef("example:subject") | ||
predicate = URIRef("example:predicate") | ||
object = URIRef("example:object") | ||
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") | ||
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 NamedTemporaryFile() as tf: | ||
tfpath = Path(tf.name) | ||
self.graph.serialize(destination=tfpath, format="nt") | ||
graph_check = Graph() | ||
graph_check.parse(source=tfpath, format="nt") | ||
|
||
self.assertEqual(self.triple, next(iter(graph_check))) |