Fixes TriG serialization

pull/373/head
Tpt 2 years ago committed by Thomas Tanon
parent 7fcf9e1051
commit 2d19a19320
  1. 2
      lib/oxrdf/src/triple.rs
  2. 12
      lib/src/io/write.rs
  3. 26
      python/tests/test_io.py
  4. 11
      python/tests/test_store.py
  5. 2
      testsuite/src/sparql_evaluator.rs

@ -1184,7 +1184,7 @@ impl<'a> QuadRef<'a> {
impl fmt::Display for QuadRef<'_> { impl fmt::Display for QuadRef<'_> {
#[inline] #[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 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) write!(f, "{} {} {}", self.subject, self.predicate, self.object)
} else { } else {
write!( write!(

@ -236,16 +236,16 @@ impl<W: Write> QuadWriter<W> {
writeln!(writer, "{quad} .")?; writeln!(writer, "{quad} .")?;
} }
QuadWriterKind::TriG(writer) => { QuadWriterKind::TriG(writer) => {
if quad.graph_name == GraphNameRef::DefaultGraph { if quad.graph_name.is_default_graph() {
writeln!(writer, "{} .", TripleRef::from(quad))
} else {
writeln!( writeln!(
writer, writer,
"GRAPH {} {{ {} }}", "{} {{ {} }}",
quad.graph_name, quad.graph_name,
TripleRef::from(quad) TripleRef::from(quad)
)?; )
} else { }?;
writeln!(writer, "{quad} .")?;
}
} }
} }
Ok(()) Ok(())

@ -8,6 +8,12 @@ from pyoxigraph import *
EXAMPLE_TRIPLE = Triple( EXAMPLE_TRIPLE = Triple(
NamedNode("http://example.com/foo"), NamedNode("http://example.com/p"), Literal("1") 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): class TestParse(unittest.TestCase):
@ -53,6 +59,18 @@ class TestParse(unittest.TestCase):
with TemporaryFile("wb") as fp: with TemporaryFile("wb") as fp:
list(parse(fp, mime_type="application/n-triples")) list(parse(fp, mime_type="application/n-triples"))
def test_parse_quad(self) -> None:
self.assertEqual(
list(
parse(
StringIO('<g> { <foo> <p> "1" }'),
"application/trig",
base_iri="http://example.com/",
)
),
[EXAMPLE_QUAD],
)
class TestSerialize(unittest.TestCase): class TestSerialize(unittest.TestCase):
def test_serialize_to_bytes_io(self) -> None: def test_serialize_to_bytes_io(self) -> None:
@ -74,3 +92,11 @@ class TestSerialize(unittest.TestCase):
with self.assertRaises(UnsupportedOperation) as _: with self.assertRaises(UnsupportedOperation) as _:
with TemporaryFile("rb") as fp: with TemporaryFile("rb") as fp:
serialize([EXAMPLE_TRIPLE], fp, "text/turtle") 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'<http://example.com/g> { <http://example.com/foo> <http://example.com/p> "1" }\n',
)

@ -265,6 +265,17 @@ class TestStore(unittest.TestCase):
b"<http://foo> <http://bar> <http://baz> <http://graph> .\n", b"<http://foo> <http://bar> <http://baz> <http://graph> .\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"<http://foo> <http://bar> <http://baz> .\n<http://graph> { <http://foo> <http://bar> <http://baz> }\n",
)
def test_dump_file(self) -> None: def test_dump_file(self) -> None:
with NamedTemporaryFile(delete=False) as fp: with NamedTemporaryFile(delete=False) as fp:
file_name = fp.name file_name = fp.name

@ -715,7 +715,7 @@ fn load_graph_to_store<'a>(
Ok(()) 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) { if let Ok(format) = guess_dataset_format(url) {
store.load_dataset(read_file(url)?, format, Some(url)) store.load_dataset(read_file(url)?, format, Some(url))
} else { } else {

Loading…
Cancel
Save