|
| 1 | +from __future__ import unicode_literals |
| 2 | +from ..nbs import NetworkBasedStatistic |
| 3 | +from ....utils.misc import package_check |
| 4 | +import numpy as np |
| 5 | +import networkx as nx |
| 6 | +import pytest |
| 7 | + |
| 8 | +have_cv = True |
| 9 | +try: |
| 10 | + package_check('cviewer') |
| 11 | +except Exception as e: |
| 12 | + have_cv = False |
| 13 | + |
| 14 | +@pytest.fixture() |
| 15 | +def creating_graphs(tmpdir): |
| 16 | + graphlist = [] |
| 17 | + graphnames = ["name"+str(i) for i in range(6)] |
| 18 | + for idx, name in enumerate(graphnames): |
| 19 | + graph = np.random.rand(10,10) |
| 20 | + G = nx.from_numpy_matrix(graph) |
| 21 | + out_file = str(tmpdir) + graphnames[idx] + '.pck' |
| 22 | + # Save as pck file |
| 23 | + nx.write_gpickle(G, out_file) |
| 24 | + graphlist.append(out_file) |
| 25 | + return graphlist |
| 26 | + |
| 27 | + |
| 28 | +@pytest.mark.skipif(have_cv, reason="tests for import error, cviewer available") |
| 29 | +def test_importerror(creating_graphs): |
| 30 | + graphlist = creating_graphs |
| 31 | + group1 = graphlist[:3] |
| 32 | + group2 = graphlist[3:] |
| 33 | + |
| 34 | + nbs = NetworkBasedStatistic() |
| 35 | + nbs.inputs.in_group1 = group1 |
| 36 | + nbs.inputs.in_group2 = group2 |
| 37 | + nbs.inputs.edge_key = "weight" |
| 38 | + |
| 39 | + with pytest.raises(ImportError) as e: |
| 40 | + nbs.run() |
| 41 | + assert "cviewer library is not available" == str(e.value) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.skipif(not have_cv, reason="cviewer has to be available") |
| 45 | +def test_keyerror(creating_graphs): |
| 46 | + graphlist =creating_graphs |
| 47 | + |
| 48 | + group1 = graphlist[:3] |
| 49 | + group2 = graphlist[3:] |
| 50 | + |
| 51 | + nbs = NetworkBasedStatistic() |
| 52 | + nbs.inputs.in_group1 = group1 |
| 53 | + nbs.inputs.in_group2 = group2 |
| 54 | + nbs.inputs.edge_key = "Your_edge" |
| 55 | + |
| 56 | + with pytest.raises(KeyError) as e: |
| 57 | + nbs.run() |
| 58 | + assert "the graph edges do not have Your_edge attribute" in str(e.value) |
0 commit comments