diff --git a/lib/oxrdf/src/triple.rs b/lib/oxrdf/src/triple.rs index 4502bc5a..c0328f46 100644 --- a/lib/oxrdf/src/triple.rs +++ b/lib/oxrdf/src/triple.rs @@ -1184,7 +1184,7 @@ impl<'a> QuadRef<'a> { impl fmt::Display for QuadRef<'_> { #[inline] fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if self.graph_name == GraphNameRef::DefaultGraph { + if self.graph_name.is_default_graph() { write!(f, "{} {} {}", self.subject, self.predicate, self.object) } else { write!( diff --git a/lib/src/io/write.rs b/lib/src/io/write.rs index 56eebca6..ab52b6ed 100644 --- a/lib/src/io/write.rs +++ b/lib/src/io/write.rs @@ -236,16 +236,16 @@ impl QuadWriter { writeln!(writer, "{quad} .")?; } QuadWriterKind::TriG(writer) => { - if quad.graph_name == GraphNameRef::DefaultGraph { + if quad.graph_name.is_default_graph() { + writeln!(writer, "{} .", TripleRef::from(quad)) + } else { writeln!( writer, - "GRAPH {} {{ {} }}", + "{} {{ {} }}", quad.graph_name, TripleRef::from(quad) - )?; - } else { - writeln!(writer, "{quad} .")?; - } + ) + }?; } } Ok(()) diff --git a/python/tests/test_io.py b/python/tests/test_io.py index 004dd3e7..a5d47309 100644 --- a/python/tests/test_io.py +++ b/python/tests/test_io.py @@ -8,6 +8,12 @@ from pyoxigraph import * EXAMPLE_TRIPLE = Triple( NamedNode("http://example.com/foo"), NamedNode("http://example.com/p"), Literal("1") ) +EXAMPLE_QUAD = Quad( + NamedNode("http://example.com/foo"), + NamedNode("http://example.com/p"), + Literal("1"), + NamedNode("http://example.com/g"), +) class TestParse(unittest.TestCase): @@ -53,6 +59,18 @@ class TestParse(unittest.TestCase): with TemporaryFile("wb") as fp: list(parse(fp, mime_type="application/n-triples")) + def test_parse_quad(self) -> None: + self.assertEqual( + list( + parse( + StringIO(' {

"1" }'), + "application/trig", + base_iri="http://example.com/", + ) + ), + [EXAMPLE_QUAD], + ) + class TestSerialize(unittest.TestCase): def test_serialize_to_bytes_io(self) -> None: @@ -74,3 +92,11 @@ class TestSerialize(unittest.TestCase): with self.assertRaises(UnsupportedOperation) as _: with TemporaryFile("rb") as fp: serialize([EXAMPLE_TRIPLE], fp, "text/turtle") + + def test_serialize_quad(self) -> None: + output = BytesIO() + serialize([EXAMPLE_QUAD], output, "application/trig") + self.assertEqual( + output.getvalue(), + b' { "1" }\n', + ) diff --git a/python/tests/test_store.py b/python/tests/test_store.py index aab3d648..5b4c409c 100644 --- a/python/tests/test_store.py +++ b/python/tests/test_store.py @@ -265,6 +265,17 @@ class TestStore(unittest.TestCase): b" .\n", ) + def test_dump_trig(self) -> None: + store = Store() + store.add(Quad(foo, bar, baz, graph)) + store.add(Quad(foo, bar, baz)) + output = BytesIO() + store.dump(output, "application/trig") + self.assertEqual( + output.getvalue(), + b" .\n { }\n", + ) + def test_dump_file(self) -> None: with NamedTemporaryFile(delete=False) as fp: file_name = fp.name diff --git a/testsuite/src/sparql_evaluator.rs b/testsuite/src/sparql_evaluator.rs index 96501c14..e0b5fea6 100644 --- a/testsuite/src/sparql_evaluator.rs +++ b/testsuite/src/sparql_evaluator.rs @@ -715,7 +715,7 @@ fn load_graph_to_store<'a>( Ok(()) } -fn load_dataset_to_store<'a>(url: &str, store: &Store) -> Result<()> { +fn load_dataset_to_store(url: &str, store: &Store) -> Result<()> { if let Ok(format) = guess_dataset_format(url) { store.load_dataset(read_file(url)?, format, Some(url)) } else {