summaryrefslogtreecommitdiff
path: root/test/test_serializers
diff options
context:
space:
mode:
authorIwan Aucamp <aucampia@gmail.com>2022-04-23 21:53:52 +0200
committerIwan Aucamp <aucampia@gmail.com>2022-05-16 17:47:44 +0200
commitb4f779b261c60b28d40dbfae90851bbff7d2c52f (patch)
tree1991b360381a5603f6a55e9dbf3ed20096b36e06 /test/test_serializers
parent06a4395aaf0743f9e3449d852020f96416f59ad8 (diff)
downloadrdflib-b4f779b261c60b28d40dbfae90851bbff7d2c52f.tar.gz
fix: two issues with the N3 serializer
This patch fixes two issues with the N3 serializer: - The N3 serializer incorrectly considered a subject as already serialized if it has been serialized inside a quoted graph. - The N3 serializer does not consider that the predicate of a triple can also be a graph. Other changes included in this patch: - Changed `test.testutils.GraphHelper` to support nested/quoted graphs. - Moved the tests from `test/test_n3_formula.py` into `test/test_serializers/test_serializer_n3.py`. - Include positive syntax tests from the N3 test suite that is smaller than 1024KB and that is not using new N3 syntax into round trip tests. This is mainly to check that there is no regressions after the changes made. Fixes: - https://github.com/RDFLib/rdflib/issues/1807 - https://github.com/RDFLib/rdflib/issues/1701
Diffstat (limited to 'test/test_serializers')
-rw-r--r--test/test_serializers/test_serializer_n3.py150
1 files changed, 150 insertions, 0 deletions
diff --git a/test/test_serializers/test_serializer_n3.py b/test/test_serializers/test_serializer_n3.py
new file mode 100644
index 00000000..4a008105
--- /dev/null
+++ b/test/test_serializers/test_serializer_n3.py
@@ -0,0 +1,150 @@
+import logging
+from test.utils import GraphHelper
+
+import rdflib
+import rdflib.term
+from rdflib import Graph
+from rdflib.graph import QuotedGraph
+from rdflib.namespace import Namespace
+from rdflib.plugins.parsers.notation3 import LOG_implies_URI
+from rdflib.term import BNode, URIRef
+
+logger = logging.getLogger(__name__)
+
+
+def test_implies():
+ test_n3 = """@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+ @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
+ @prefix : <http://test/> .
+ {:a :b :c;a :foo} => {:a :d :c,?y} .
+ _:foo a rdfs:Class .
+ :a :d :c ."""
+ graph1 = rdflib.Graph()
+ graph1.parse(data=test_n3, format="n3")
+
+ if logger.isEnabledFor(logging.DEBUG):
+ logging.debug("sorted(list(graph1)) = \n%s", sorted(list(graph1)))
+
+ """
+ >>> sorted(list(graph1))
+ [
+ (
+ rdflib.term.BNode('fde0470d85a044b6780f0c6804b119063b1'),
+ rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
+ rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#Class')
+ ),
+ (
+ rdflib.term.URIRef('http://test/a'),
+ rdflib.term.URIRef('http://test/d'),
+ rdflib.term.URIRef('http://test/c')
+ ),
+ (
+ <Graph identifier=_:Formula2 (<class 'rdflib.graph.QuotedGraph'>)>,
+ rdflib.term.URIRef('http://www.w3.org/2000/10/swap/log#implies'),
+ <Graph identifier=_:Formula3 (<class 'rdflib.graph.QuotedGraph'>)>
+ )
+ ]
+ """
+
+ graph2 = rdflib.Graph()
+ graph2.parse(data=graph1.serialize(format="n3"), format="n3")
+ assert (
+ rdflib.term.URIRef('http://test/a'),
+ rdflib.term.URIRef('http://test/d'),
+ rdflib.term.URIRef('http://test/c'),
+ ) in graph2
+
+
+EG = Namespace("http://example.com/")
+
+LOG_implies = URIRef(LOG_implies_URI)
+
+
+def test_merging() -> None:
+ data_a = """
+ @prefix : <http://example.com/>.
+ :a :b :c.
+ """
+ data_b = """
+ @prefix : <http://example.com/>.
+ {:a :b :c} => {:d :e :f}.
+ """
+ graph = Graph()
+ assert (EG.a, EG.b, EG.c) not in graph
+
+ graph.parse(data=data_a, format="n3")
+ assert (EG.a, EG.b, EG.c) in graph
+
+ graph.parse(data=data_b, format="n3")
+ assert (EG.a, EG.b, EG.c) in graph
+ assert len(set(graph.triples((None, LOG_implies, None)))) == 1
+
+ data_s = graph.serialize(format="n3")
+ logging.debug("data_s = %s", data_s)
+
+ graph = Graph()
+ graph.parse(data=data_s, format="n3")
+ quad_set = GraphHelper.triple_set(graph)
+
+ assert (EG.a, EG.b, EG.c) in graph
+ assert len(set(graph.triples((None, LOG_implies, None)))) == 1
+
+ logging.debug("quad_set = %s", quad_set)
+
+
+def test_single_simple_triple() -> None:
+ data_a = """
+ @prefix : <http://example.com/>.
+ :a :b :c.
+ """
+ graph = Graph()
+ assert (EG.a, EG.b, EG.c) not in graph
+
+ graph.parse(data=data_a, format="n3")
+ assert (EG.a, EG.b, EG.c) in graph
+
+ data_s = graph.serialize(format="n3")
+ logging.debug("data_s = %s", data_s)
+
+ graph = Graph()
+ graph.parse(data=data_s, format="n3")
+ quad_set = GraphHelper.triple_set(graph)
+
+ assert (EG.a, EG.b, EG.c) in graph
+
+ logging.debug("quad_set = %s", quad_set)
+
+
+def test_implies_nothing() -> None:
+ triple_a = (EG.a, EG.b, EG.c)
+ graph = Graph()
+ qgraph_a = QuotedGraph(graph.store, BNode())
+ qgraph_a.add(triple_a)
+ qgraph_b = QuotedGraph(graph.store, BNode())
+ graph.add((qgraph_a, LOG_implies, qgraph_b))
+ graph.add(triple_a)
+
+ data_s = graph.serialize(format="n3")
+ logging.debug("data_s = %s", data_s)
+
+ rgraph = Graph()
+ rgraph.parse(data=data_s, format="n3")
+ graph_qs, qgraph_a_qs, qgraph_b_qs = GraphHelper.triple_sets(
+ (rgraph, qgraph_a, qgraph_b)
+ )
+ logging.debug("graph_qs = %s", graph_qs)
+ logging.debug("qgraph_a_qs = %s", qgraph_a_qs)
+ logging.debug("qgraph_b_qs = %s", qgraph_b_qs)
+
+ assert len(graph_qs) == 2
+ assert len(qgraph_a_qs) == 1
+ assert len(qgraph_b_qs) == 0
+
+ triple_b = (qgraph_a_qs, LOG_implies, qgraph_b_qs)
+
+ logging.debug("triple_a = %s", triple_a)
+ logging.debug("triple_b = %s", triple_b)
+
+ assert triple_a in graph_qs
+ assert triple_a in rgraph
+ assert triple_b in graph_qs