Skip to content

Commit

Permalink
test for #494 (canonicalization sometimes collapses BNodes)
Browse files Browse the repository at this point in the history
  • Loading branch information
joernhees committed Jul 13, 2015
1 parent fe3fa52 commit e1ace26
Showing 1 changed file with 103 additions and 4 deletions.
107 changes: 103 additions & 4 deletions test/test_canonicalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def negative_graph_match_test():
True
],
[ unicode('''@prefix : <http://example.org/ns#> .
:linear_two_step_symmetry_start :related [ :related [ :related :linear_two_step_symmatry_end]],
:linear_two_step_symmetry_start :related [ :related [ :related :linear_two_step_symmatry_end]],
[ :related [ :related :linear_two_step_symmatry_end]].'''),
unicode('''@prefix : <http://example.org/ns#> .
:linear_two_step_symmetry_start :related [ :related [ :related :linear_two_step_symmatry_end]],
:linear_two_step_symmetry_start :related [ :related [ :related :linear_two_step_symmatry_end]],
[ :related [ :related :linear_two_step_symmatry_end]].'''),
True
],
Expand Down Expand Up @@ -68,7 +68,7 @@ def negative_graph_match_test():
].'''),
False
],
# This test fails because the algorithm purposefully breaks the symmetry of symetric
# This test fails because the algorithm purposefully breaks the symmetry of symetric
[ unicode('''@prefix : <http://example.org/ns#> .
_:a :rel [
:rel [
Expand Down Expand Up @@ -148,4 +148,103 @@ def fn(rdf1, rdf2, identical):
print digest2
assert (digest1 == digest2) == identical
for inputs in testInputs:
yield fn, inputs[0], inputs[1], inputs[2]
yield fn, inputs[0], inputs[1], inputs[2]

def test_issue494_collapsing_bnodes():
"""Test for https://github.com/RDFLib/rdflib/issues/494 collapsing BNodes"""
g = Graph()
g += [
(BNode('Na1a8fbcf755f41c1b5728f326be50994'),
RDF['object'],
URIRef(u'source')),
(BNode('Na1a8fbcf755f41c1b5728f326be50994'),
RDF['predicate'],
BNode('vcb3')),
(BNode('Na1a8fbcf755f41c1b5728f326be50994'),
RDF['subject'],
BNode('vcb2')),
(BNode('Na1a8fbcf755f41c1b5728f326be50994'),
RDF['type'],
RDF['Statement']),
(BNode('Na713b02f320d409c806ff0190db324f4'),
RDF['object'],
URIRef(u'target')),
(BNode('Na713b02f320d409c806ff0190db324f4'),
RDF['predicate'],
BNode('vcb0')),
(BNode('Na713b02f320d409c806ff0190db324f4'),
RDF['subject'],
URIRef(u'source')),
(BNode('Na713b02f320d409c806ff0190db324f4'),
RDF['type'],
RDF['Statement']),
(BNode('Ndb804ba690a64b3dbb9063c68d5e3550'),
RDF['object'],
BNode('vr0KcS4')),
(BNode('Ndb804ba690a64b3dbb9063c68d5e3550'),
RDF['predicate'],
BNode('vrby3JV')),
(BNode('Ndb804ba690a64b3dbb9063c68d5e3550'),
RDF['subject'],
URIRef(u'source')),
(BNode('Ndb804ba690a64b3dbb9063c68d5e3550'),
RDF['type'],
RDF['Statement']),
(BNode('Ndfc47fb1cd2d4382bcb8d5eb7835a636'),
RDF['object'],
URIRef(u'source')),
(BNode('Ndfc47fb1cd2d4382bcb8d5eb7835a636'),
RDF['predicate'],
BNode('vcb5')),
(BNode('Ndfc47fb1cd2d4382bcb8d5eb7835a636'),
RDF['subject'],
URIRef(u'target')),
(BNode('Ndfc47fb1cd2d4382bcb8d5eb7835a636'),
RDF['type'],
RDF['Statement']),
(BNode('Nec6864ef180843838aa9805bac835c98'),
RDF['object'],
URIRef(u'source')),
(BNode('Nec6864ef180843838aa9805bac835c98'),
RDF['predicate'],
BNode('vcb4')),
(BNode('Nec6864ef180843838aa9805bac835c98'),
RDF['subject'],
URIRef(u'source')),
(BNode('Nec6864ef180843838aa9805bac835c98'),
RDF['type'],
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:'
g_node_degs = sorted([
len(list(g.triples([node, None, None])))
for node in g.all_nodes()
], reverse=True)
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:'
cg_node_degs = sorted([
len(list(cg.triples([node, None, None])))
for node in cg.all_nodes()
], reverse=True)
print cg_node_degs

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'
assert g_node_degs == cg_node_degs, \
'canonicalization changed node degrees'

0 comments on commit e1ace26

Please sign in to comment.