summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoern Hees <dev@joernhees.de>2017-03-30 00:37:29 +0200
committerJoern Hees <dev@joernhees.de>2017-03-30 00:47:14 +0200
commit6ab6174c7ec81bdd1bee18f2526c34fbb345e81a (patch)
tree62179fe9874b0f39f6babd4ee646ab3bc9a4a3e4
parentaf230076e7796c368ec4c912404fe3baf44761e5 (diff)
downloadrdflib-6ab6174c7ec81bdd1bee18f2526c34fbb345e81a.tar.gz
test for issue #725: canonicalization collapses BNodes
-rw-r--r--test/test_canonicalization.py134
1 files changed, 122 insertions, 12 deletions
diff --git a/test/test_canonicalization.py b/test/test_canonicalization.py
index b64059e7..2cdeda2c 100644
--- a/test/test_canonicalization.py
+++ b/test/test_canonicalization.py
@@ -1,3 +1,4 @@
+from collections import Counter
from rdflib import Graph, RDF, BNode, URIRef, Namespace, ConjunctiveGraph, Literal
from rdflib.compare import to_isomorphic, to_canonical_graph
from rdflib.plugins.memory import IOMemory
@@ -220,28 +221,28 @@ def test_issue494_collapsing_bnodes():
RDF['Statement']),
]
- print('graph length: %d, nodes: %d' % (len(g), len(g.all_nodes())))
- print('triple_bnode degrees:')
- for triple_bnode in g.subjects(RDF['type'], RDF['Statement']):
- print(len(list(g.triples([triple_bnode, None, None]))))
- print('all node degrees:')
+ # print('graph length: %d, nodes: %d' % (len(g), len(g.all_nodes())))
+ # print('triple_bnode degrees:')
+ # for triple_bnode in g.subjects(RDF['type'], RDF['Statement']):
+ # print(len(list(g.triples([triple_bnode, None, None]))))
+ # print('all node degrees:')
g_node_degs = sorted([
len(list(g.triples([node, None, None])))
for node in g.all_nodes()
], reverse=True)
- print(g_node_degs)
+ # print(g_node_degs)
cg = to_canonical_graph(g)
- print('graph length: %d, nodes: %d' % (len(cg), len(cg.all_nodes())))
- print('triple_bnode degrees:')
- for triple_bnode in cg.subjects(RDF['type'], RDF['Statement']):
- print(len(list(cg.triples([triple_bnode, None, None]))))
- print('all node degrees:')
+ # print('graph length: %d, nodes: %d' % (len(cg), len(cg.all_nodes())))
+ # print('triple_bnode degrees:')
+ # for triple_bnode in cg.subjects(RDF['type'], RDF['Statement']):
+ # print(len(list(cg.triples([triple_bnode, None, None]))))
+ # print('all node degrees:')
cg_node_degs = sorted([
len(list(cg.triples([node, None, None])))
for node in cg.all_nodes()
], reverse=True)
- print(cg_node_degs)
+ # print(cg_node_degs)
assert len(g) == len(cg), \
'canonicalization changed number of triples in graph'
@@ -253,6 +254,24 @@ def test_issue494_collapsing_bnodes():
assert g_node_degs == cg_node_degs, \
'canonicalization changed node degrees'
+ # counter for subject, predicate and object nodes
+ g_pos_counts = Counter(), Counter(), Counter()
+ for t in g:
+ for i, node in enumerate(t):
+ g_pos_counts[i][t] += 1
+ g_count_signature = [sorted(c.values()) for c in g_pos_counts]
+
+ cg = to_canonical_graph(g)
+ cg_pos_counts = Counter(), Counter(), Counter()
+ for t in cg:
+ for i, node in enumerate(t):
+ cg_pos_counts[i][t] += 1
+ cg_count_signature = [sorted(c.values()) for c in cg_pos_counts]
+
+ assert g_count_signature == cg_count_signature, \
+ 'canonicalization changed node position counts'
+
+
def test_issue682_signing_named_graphs():
ns = Namespace("http://love.com#")
@@ -282,3 +301,94 @@ def test_issue682_signing_named_graphs():
assert len(ig) == len(g)
assert len(igmary) < len(ig)
assert ig.graph_digest() != igmary.graph_digest()
+
+
+def test_issue725_collapsing_bnodes():
+ g = Graph()
+ g += [
+ (BNode('N0a76d42406b84fe4b8029d0a7fa04244'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#object'),
+ BNode('v2')),
+ (BNode('N0a76d42406b84fe4b8029d0a7fa04244'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate'),
+ BNode('v0')),
+ (BNode('N0a76d42406b84fe4b8029d0a7fa04244'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject'),
+ URIRef(u'urn:gp_learner:fixed_var:target')),
+ (BNode('N0a76d42406b84fe4b8029d0a7fa04244'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement')),
+ (BNode('N2f62af5936b94a8eb4b1e4bfa8e11d95'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#object'),
+ BNode('v1')),
+ (BNode('N2f62af5936b94a8eb4b1e4bfa8e11d95'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate'),
+ BNode('v0')),
+ (BNode('N2f62af5936b94a8eb4b1e4bfa8e11d95'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject'),
+ URIRef(u'urn:gp_learner:fixed_var:target')),
+ (BNode('N2f62af5936b94a8eb4b1e4bfa8e11d95'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement')),
+ (BNode('N5ae541f93e1d4e5880450b1bdceb6404'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#object'),
+ BNode('v5')),
+ (BNode('N5ae541f93e1d4e5880450b1bdceb6404'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate'),
+ BNode('v4')),
+ (BNode('N5ae541f93e1d4e5880450b1bdceb6404'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject'),
+ URIRef(u'urn:gp_learner:fixed_var:target')),
+ (BNode('N5ae541f93e1d4e5880450b1bdceb6404'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement')),
+ (BNode('N86ac7ca781f546ae939b8963895f672e'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#object'),
+ URIRef(u'urn:gp_learner:fixed_var:source')),
+ (BNode('N86ac7ca781f546ae939b8963895f672e'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate'),
+ BNode('v0')),
+ (BNode('N86ac7ca781f546ae939b8963895f672e'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject'),
+ URIRef(u'urn:gp_learner:fixed_var:target')),
+ (BNode('N86ac7ca781f546ae939b8963895f672e'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement')),
+ (BNode('Nac82b883ca3849b5ab6820b7ac15e490'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#object'),
+ BNode('v1')),
+ (BNode('Nac82b883ca3849b5ab6820b7ac15e490'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate'),
+ BNode('v3')),
+ (BNode('Nac82b883ca3849b5ab6820b7ac15e490'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject'),
+ URIRef(u'urn:gp_learner:fixed_var:target')),
+ (BNode('Nac82b883ca3849b5ab6820b7ac15e490'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
+ URIRef(u'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement'))
+ ]
+
+ cg = to_canonical_graph(g)
+ assert len(g) == len(cg), \
+ 'canonicalization changed number of triples in graph'
+ assert len(g.all_nodes()) == len(cg.all_nodes()), \
+ 'canonicalization changed number of nodes in graph'
+ assert len(list(g.subjects(RDF['type'], RDF['Statement']))) == \
+ len(list(cg.subjects(RDF['type'], RDF['Statement']))), \
+ 'canonicalization changed number of statements'
+
+ # counter for subject, predicate and object nodes
+ g_pos_counts = Counter(), Counter(), Counter()
+ for t in g:
+ for i, node in enumerate(t):
+ g_pos_counts[i][t] += 1
+ g_count_signature = [sorted(c.values()) for c in g_pos_counts]
+
+ cg_pos_counts = Counter(), Counter(), Counter()
+ for t in cg:
+ for i, node in enumerate(t):
+ cg_pos_counts[i][t] += 1
+ cg_count_signature = [sorted(c.values()) for c in cg_pos_counts]
+
+ assert g_count_signature == cg_count_signature, \
+ 'canonicalization changed node position counts'