From 6b383f48a3cc13a94379065d8759ed38533a7f17 Mon Sep 17 00:00:00 2001 From: Joern Hees Date: Wed, 18 Mar 2015 15:49:43 +0100 Subject: make external_graph_libs tests optional (only run if ext lib is installed) networkx shouldn't be a dependency of rdflib just to run tests. Also graph_tool heavily depends on C libs and can't easily be installed via pip (or via apt-get on travis). Hence, the doctests are duplicated into proper tests that auto skip if networkx or graph_tool can't be imported. --- rdflib/extras/external_graph_libs.py | 9 ++-- setup.cfg | 1 + test/test_extras_external_graph_libs.py | 92 +++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 test/test_extras_external_graph_libs.py diff --git a/rdflib/extras/external_graph_libs.py b/rdflib/extras/external_graph_libs.py index 8423f4a0..10daf949 100644 --- a/rdflib/extras/external_graph_libs.py +++ b/rdflib/extras/external_graph_libs.py @@ -10,6 +10,10 @@ from __future__ import unicode_literals Currently the following libraries are supported: - networkx: MultiDiGraph, DiGraph, Graph - graph_tool: Graph + +Doctests in this file are all skipped, as we can't run them conditionally if +networkx or graph_tool are available and they would err otherwise. +see ../../test/test_extras_external_graph_libs.py for conditional tests """ import logging @@ -333,13 +337,10 @@ def rdflib_to_graphtool( return g -def main(): # pragma: no cover +if __name__ == '__main__': import sys import logging.config logging.basicConfig(level=logging.DEBUG) import nose nose.run(argv=[sys.argv[0], sys.argv[0], '-v', '--with-doctest']) - -if __name__ == '__main__': - main() diff --git a/setup.cfg b/setup.cfg index a4e33487..74635d08 100644 --- a/setup.cfg +++ b/setup.cfg @@ -4,3 +4,4 @@ attr=!known_issue,!non_core,!performancetest verbosity=1 with-doctest=1 exclude=rdflib.plugins.sparql.paths +exclude=rdflib.extras.external_graph_libs diff --git a/test/test_extras_external_graph_libs.py b/test/test_extras_external_graph_libs.py new file mode 100644 index 00000000..2986d258 --- /dev/null +++ b/test/test_extras_external_graph_libs.py @@ -0,0 +1,92 @@ +from nose import SkipTest +from rdflib import Graph, URIRef, Literal + +def test_rdflib_to_networkx(): + try: + import networkx + except ImportError: + raise SkipTest("couldn't find networkx") + from rdflib.extras.external_graph_libs import rdflib_to_networkx_multidigraph + from rdflib.extras.external_graph_libs import rdflib_to_networkx_digraph + from rdflib.extras.external_graph_libs import rdflib_to_networkx_graph + g = Graph() + a, b, l = URIRef('a'), URIRef('b'), Literal('l') + p, q = URIRef('p'), URIRef('q') + edges = [(a, p, b), (a, q, b), (b, p, a), (b, p, l)] + for t in edges: + g.add(t) + + + mdg = rdflib_to_networkx_multidigraph(g) + assert len(mdg.edges()) == 4 + assert mdg.has_edge(a, b) + assert mdg.has_edge(a, b, key=p) + assert mdg.has_edge(a, b, key=q) + + mdg = rdflib_to_networkx_multidigraph(g, edge_attrs=lambda s, p, o: {}) + assert mdg.has_edge(a, b, key=0) + assert mdg.has_edge(a, b, key=1) + + + dg = rdflib_to_networkx_digraph(g) + assert dg[a][b]['weight'] == 2 + assert sorted(dg[a][b]['triples']) == [(a, p, b), (a, q, b)] + assert len(dg.edges()) == 3 + assert dg.size() == 3 + assert dg.size(weight='weight') == 4.0 + + dg = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s, p, o:{}) + assert 'weight' not in dg[a][b] + assert 'triples' not in dg[a][b] + + + ug = rdflib_to_networkx_graph(g) + assert ug[a][b]['weight'] == 3 + assert sorted(ug[a][b]['triples']) == [(a, p, b), (a, q, b), (b, p, a)] + assert len(ug.edges()) == 2 + assert ug.size() == 2 + assert ug.size(weight='weight') == 4.0 + + ug = rdflib_to_networkx_graph(g, False, edge_attrs=lambda s, p, o:{}) + assert 'weight' not in ug[a][b] + assert 'triples' not in ug[a][b] + + +def test_rdflib_to_graphtool(): + try: + from graph_tool import util as gt_util + except ImportError: + raise SkipTest("couldn't find graph_tool") + from rdflib.extras.external_graph_libs import rdflib_to_graphtool + g = Graph() + a, b, l = URIRef('a'), URIRef('b'), Literal('l') + p, q = URIRef('p'), URIRef('q') + edges = [(a, p, b), (a, q, b), (b, p, a), (b, p, l)] + for t in edges: + g.add(t) + + mdg = rdflib_to_graphtool(g) + assert len(list(mdg.edges())) == 4 + + vpterm = mdg.vertex_properties['term'] + va = gt_util.find_vertex(mdg, vpterm, a)[0] + vb = gt_util.find_vertex(mdg, vpterm, b)[0] + vl = gt_util.find_vertex(mdg, vpterm, l)[0] + assert (va, vb) in [(e.source(), e.target()) for e in list(mdg.edges())] + + epterm = mdg.edge_properties['term'] + assert len(list(gt_util.find_edge(mdg, epterm, p))) == 3 + assert len(list(gt_util.find_edge(mdg, epterm, q))) == 1 + + mdg = rdflib_to_graphtool( + g, + e_prop_names=[str('name')], + transform_p=lambda s, p, o: {str('name'): unicode(p)}) + epterm = mdg.edge_properties['name'] + assert len(list(gt_util.find_edge(mdg, epterm, unicode(p)))) == 3 + assert len(list(gt_util.find_edge(mdg, epterm, unicode(q)))) == 1 + +if __name__ == "__main__": + import sys + import nose + nose.main(defaultTest=sys.argv[0]) -- cgit v1.2.1