diff --git a/oxrdflib/serializer.py b/oxrdflib/serializer.py index 1411206..2f46904 100644 --- a/oxrdflib/serializer.py +++ b/oxrdflib/serializer.py @@ -22,21 +22,24 @@ class _OxigraphSerializer(Serializer, ABC): def serialize( self, stream: IO[bytes], - _base: Optional[str] = None, + base: Optional[str] = None, encoding: Optional[str] = None, **kwargs: Any, # noqa: ARG002 ) -> None: if encoding not in (None, "utf-8"): raise ValueError(f"RDF files are always utf-8 encoded, I was passed: {encoding}") - # TODO: base and prefixes + base_iri = base or self.store.base + prefixes = dict(self.store.namespaces()) if isinstance(self.store.store, OxigraphStore): self.store.store._inner.dump( stream, format=self._format, from_graph=None if isinstance(self.store, Dataset) else to_ox(self.store.identifier), + base_iri=base_iri, + prefixes=prefixes, ) else: - serialize((to_ox(q) for q in self.store), stream, format=self._format) + serialize((to_ox(q) for q in self.store), stream, format=self._format, base_iri=base_iri, prefixes=prefixes) @property @abstractmethod diff --git a/pyproject.toml b/pyproject.toml index de8effe..fdcf93d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,7 @@ classifiers = [ "Topic :: Software Development :: Libraries :: Python Modules", "Topic :: Database :: Database Engines/Servers", ] -dependencies = ["pyoxigraph~=0.4.0", "rdflib>=6.3,<8.0"] +dependencies = ["pyoxigraph~=0.4.2", "rdflib>=6.3,<8.0"] dynamic = ["version"] license = { text = "BSD-3-Clause" } name = "oxrdflib" diff --git a/tests/test_parser.py b/tests/test_parser.py index c9b2b3e..91c3ec9 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -47,7 +47,6 @@ def test_parse_graph(self): transactional=transactional, ) self.assertEqual(list(graph), [(s, p, o)]) - # TODO: pyoxigraph 0.4.2: test that prefixes are properly loaded @unittest.skipIf(rdflib_version < (7, 1), "only works in rdflib 7.1+") def test_parse_dataset(self): diff --git a/tests/test_serializer.py b/tests/test_serializer.py index 9562509..a971582 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -12,30 +12,29 @@ class TestSerializer(unittest.TestCase): def test_serialize_graph(self): for store in ("default", "oxigraph"): for fmt, serialization in ( - ("ox-turtle", " .\n"), - ("ox-ttl", " .\n"), + ("ox-turtle", " v:p .\n"), + ("ox-ttl", " v:p .\n"), ("ox-ntriples", " .\n"), - ("ox-n3", " .\n"), + ("ox-n3", " v:p .\n"), ("ox-nquads", " .\n"), ("ox-nt", " .\n"), ("ox-nt11", " .\n"), - ("ox-trig", " .\n"), + ("ox-trig", " v:p .\n"), ( "ox-xml", - """ - - -

+ """ + + """, ), ): with self.subTest(store=store, format=fmt): - graph = Graph(store=store) + graph = Graph(store=store, base="http://example.com/") graph.add((s, p, o)) graph.store.add((o, p, s), context=g) # Should not be serialized - self.assertEqual(graph.serialize(format=fmt), serialization) - # TODO: pyoxigraph 0.4.2: test that prefixes and base are properly used + graph.bind("v", "http://example.com/vocab#") + self.assertTrue(graph.serialize(format=fmt).endswith(serialization)) def test_serialize_dataset(self): for store in ("default", "oxigraph"): @@ -47,14 +46,14 @@ def test_serialize_dataset(self): ), ( "ox-trig", - " {\n\t " - " .\n}\n", + " {\n\t v:p .\n}\n", ), ): with self.subTest(store=store, format=fmt): dataset = Dataset(store=store) dataset.add((s, p, o, Graph(identifier=g))) - self.assertEqual(dataset.serialize(format=fmt), serialization) + dataset.bind("v", "http://example.com/vocab#") + self.assertTrue(dataset.serialize(format=fmt, base="http://example.com/").endswith(serialization)) if __name__ == "__main__":