summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornicholascar <nicholas.car@surroundaustralia.com>2021-12-06 12:24:54 +1000
committernicholascar <nicholas.car@surroundaustralia.com>2021-12-06 12:24:54 +1000
commit16aa03f28b029c8e2a03cb1aeab09da29fdb4122 (patch)
tree91ac3fca3359dd8aa4d505f08236563de31883ac
parenta91dde6c5738d7c3a26c3f5a9d16412b27a31fcb (diff)
downloadrdflib-16aa03f28b029c8e2a03cb1aeab09da29fdb4122.tar.gz
some roundftripling tests; better default graph IRI ('""')
-rw-r--r--rdflib/plugins/parsers/hext.py1
-rw-r--r--rdflib/plugins/serializers/hext.py2
-rw-r--r--test/test_parser_hext.py3
-rw-r--r--test/test_parser_hext_01.ndjson2
-rw-r--r--test/test_serialize_hext.py16
5 files changed, 17 insertions, 7 deletions
diff --git a/rdflib/plugins/parsers/hext.py b/rdflib/plugins/parsers/hext.py
index b83ff721..9f34ac83 100644
--- a/rdflib/plugins/parsers/hext.py
+++ b/rdflib/plugins/parsers/hext.py
@@ -68,6 +68,7 @@ class HextuplesParser(Parser):
cg = ConjunctiveGraph(store=graph.store, identifier=graph.identifier)
cg.default_context = graph
+ # handle different source types - only file and string (data) for now
if hasattr(source, "file"):
with open(source.file.name) as fp:
for l in fp:
diff --git a/rdflib/plugins/serializers/hext.py b/rdflib/plugins/serializers/hext.py
index 820a49e8..3dfabb1b 100644
--- a/rdflib/plugins/serializers/hext.py
+++ b/rdflib/plugins/serializers/hext.py
@@ -60,7 +60,7 @@ def _hex_line(triple, context):
_literal(triple[2]) if type(triple[2]) == Literal else _iri_or_bn(triple[2]),
(f'"{triple[2].datatype}"' if triple[2].datatype is not None else '"http://www.w3.org/2001/XMLSchema#string"') if type(triple[2]) == Literal else '""',
(f'"{triple[2].language}"' if triple[2].language is not None else '""') if type(triple[2]) == Literal else '""',
- _iri_or_bn(context)
+ _iri_or_bn(context) if not str(context).startswith(("_", "file://")) else '""'
)
diff --git a/test/test_parser_hext.py b/test/test_parser_hext.py
index 0f9a2f48..ec211475 100644
--- a/test/test_parser_hext.py
+++ b/test/test_parser_hext.py
@@ -10,7 +10,7 @@ def test_small_string():
["http://example.com/s01", "http://example.com/label", "This is a Label", "http://www.w3.org/2001/XMLSchema#string", "en", ""]
["http://example.com/s01", "http://example.com/comment", "This is a comment", "http://www.w3.org/2001/XMLSchema#string", "", ""]
["http://example.com/s01", "http://example.com/creationDate", "2021-12-01", "http://www.w3.org/2001/XMLSchema#date", "", ""]
- ["http://example.com/s01", "http://example.com/creationTime", "2021-12-01T12:13", "http://www.w3.org/2001/XMLSchema#dateTime", "", ""]
+ ["http://example.com/s01", "http://example.com/creationTime", "2021-12-01T12:13:00", "http://www.w3.org/2001/XMLSchema#dateTime", "", ""]
["http://example.com/s01", "http://example.com/age", 42, "http://www.w3.org/2001/XMLSchema#integer", "", ""]
["http://example.com/s01", "http://example.com/trueFalse", false, "http://www.w3.org/2001/XMLSchema#boolean", "", ""]
["http://example.com/s01", "http://example.com/op1", "http://example.com/o1", "", "", ""]
@@ -23,7 +23,6 @@ def test_small_string():
def test_small_file():
d = Dataset().parse(Path(__file__).parent / "test_parser_hext_01.ndjson", format="hext")
- print(d.serialize())
assert len(d) == 10
diff --git a/test/test_parser_hext_01.ndjson b/test/test_parser_hext_01.ndjson
index c0a7c6ee..5d8f67c8 100644
--- a/test/test_parser_hext_01.ndjson
+++ b/test/test_parser_hext_01.ndjson
@@ -2,7 +2,7 @@
["http://example.com/s01", "http://example.com/label", "This is a Label", "http://www.w3.org/2001/XMLSchema#string", "en", ""]
["http://example.com/s01", "http://example.com/comment", "This is a comment", "http://www.w3.org/2001/XMLSchema#string", "", ""]
["http://example.com/s01", "http://example.com/creationDate", "2021-12-01", "http://www.w3.org/2001/XMLSchema#date", "", ""]
-["http://example.com/s01", "http://example.com/creationTime", "2021-12-01T12:13", "http://www.w3.org/2001/XMLSchema#dateTime", "", ""]
+["http://example.com/s01", "http://example.com/creationTime", "2021-12-01T12:13:00", "http://www.w3.org/2001/XMLSchema#dateTime", "", ""]
["http://example.com/s01", "http://example.com/age", 42, "http://www.w3.org/2001/XMLSchema#integer", "", ""]
["http://example.com/s01", "http://example.com/trueFalse", false, "http://www.w3.org/2001/XMLSchema#boolean", "", ""]
["http://example.com/s01", "http://example.com/op1", "http://example.com/o1", "", "", ""]
diff --git a/test/test_serialize_hext.py b/test/test_serialize_hext.py
index dc5c8d1c..2f1072e1 100644
--- a/test/test_serialize_hext.py
+++ b/test/test_serialize_hext.py
@@ -139,6 +139,16 @@ def test_hext_json_representation():
assert isinstance(j, list)
+def test_roundtrip():
+ d = Dataset().parse(Path(__file__).parent / "test_parser_hext_01.ndjson", format="hext")
+ with open(str(Path(__file__).parent / "test_parser_hext_01.ndjson")) as i:
+ ordered_input = "".join(sorted(i.readlines())).strip()
+
+ ordered_output = "\n".join(sorted(d.serialize(format="hext").split("\n"))).strip()
+
+ assert ordered_output == ordered_input
+
+
# def _make_large_graph():
# import random
#
@@ -162,6 +172,6 @@ def test_hext_json_representation():
# g.serialize(destination="large.ndjson", format="hext")
#
#
-# if __name__ == "__main__":
-# import cProfile
-# cProfile.run("test_hext_scaling()", sort=1)
+if __name__ == "__main__":
+ test_roundtrip()
+