diff --git a/test/test_canonicalization.py b/test/test_canonicalization.py index 2745f4903..30fb0bb1e 100644 --- a/test/test_canonicalization.py +++ b/test/test_canonicalization.py @@ -37,10 +37,10 @@ def negative_graph_match_test(): True ], [ unicode('''@prefix : . - :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 : . - :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 ], @@ -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 : . _:a :rel [ :rel [ @@ -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] \ No newline at end of file + 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'