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<'_> {
#[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!(

@ -236,16 +236,16 @@ impl<W: Write> QuadWriter<W> {
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(())

@ -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('<g> { <foo> <p> "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'<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",
)
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:
with NamedTemporaryFile(delete=False) as fp:
file_name = fp.name

@ -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 {

Loading…
Cancel
Save