summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoern Hees <dev@joernhees.de>2015-03-18 15:49:43 +0100
committerJoern Hees <dev@joernhees.de>2015-03-18 16:33:45 +0100
commit6b383f48a3cc13a94379065d8759ed38533a7f17 (patch)
treed0cba864f37d097b19dd8e13d531b88c3cbb7b30
parenta940565bf559cbfaeca17198da69d8eee6fd0d02 (diff)
downloadrdflib-6b383f48a3cc13a94379065d8759ed38533a7f17.tar.gz
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.
-rw-r--r--rdflib/extras/external_graph_libs.py9
-rw-r--r--setup.cfg1
-rw-r--r--test/test_extras_external_graph_libs.py92
3 files changed, 98 insertions, 4 deletions
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])