File: test_utils.py

package info (click to toggle)
python-networkx 1.9%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,052 kB
  • ctags: 3,986
  • sloc: python: 52,132; makefile: 176
file content (129 lines) | stat: -rw-r--r-- 3,701 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from nose.tools import *
import networkx as nx
from networkx.testing import *

# thanks to numpy for this GenericTest class (numpy/testing/test_utils.py)
class _GenericTest(object):
    def _test_equal(self, a, b):
        self._assert_func(a, b)

    def _test_not_equal(self, a, b):
        try:
            self._assert_func(a, b)
            passed = True
        except AssertionError:
            pass
        else:
            raise AssertionError("a and b are found equal but are not")


class TestNodesEqual(_GenericTest):
    def setUp(self):
        self._assert_func = assert_nodes_equal

    def test_nodes_equal(self):
        a = [1,2,5,4]
        b = [4,5,1,2]
        self._test_equal(a,b)

    def test_nodes_not_equal(self):
        a = [1,2,5,4]
        b = [4,5,1,3]
        self._test_not_equal(a,b)

    def test_nodes_with_data_equal(self):
        G = nx.Graph()
        G.add_nodes_from([1,2,3],color='red')
        H = nx.Graph()
        H.add_nodes_from([1,2,3],color='red')
        self._test_equal(G.nodes(data=True), H.nodes(data=True))

    def test_edges_with_data_not_equal(self):
        G = nx.Graph()
        G.add_nodes_from([1,2,3],color='red')
        H = nx.Graph()
        H.add_nodes_from([1,2,3],color='blue')
        self._test_not_equal(G.nodes(data=True), H.nodes(data=True))


class TestEdgesEqual(_GenericTest):
    def setUp(self):
        self._assert_func = assert_edges_equal

    def test_edges_equal(self):
        a = [(1,2),(5,4)]
        b = [(4,5),(1,2)]
        self._test_equal(a,b)

    def test_edges_not_equal(self):
        a = [(1,2),(5,4)]
        b = [(4,5),(1,3)]
        self._test_not_equal(a,b)

    def test_edges_with_data_equal(self):
        G = nx.MultiGraph()
        G.add_path([0,1,2],weight=1)
        H = nx.MultiGraph()
        H.add_path([0,1,2],weight=1)
        self._test_equal(G.edges(data=True, keys=True),
                         H.edges(data=True, keys=True))

    def test_edges_with_data_not_equal(self):
        G = nx.MultiGraph()
        G.add_path([0,1,2],weight=1)
        H = nx.MultiGraph()
        H.add_path([0,1,2],weight=2)
        self._test_not_equal(G.edges(data=True, keys=True),
                             H.edges(data=True, keys=True))

class TestGraphsEqual(_GenericTest):
    def setUp(self):
        self._assert_func = assert_graphs_equal

    def test_graphs_equal(self):
        G = nx.path_graph(4)
        H = nx.Graph()
        H.add_path(range(4))
        H.name='path_graph(4)'
        self._test_equal(G,H)

    def test_digraphs_equal(self):
        G = nx.path_graph(4, create_using=nx.DiGraph())
        H = nx.DiGraph()
        H.add_path(range(4))
        H.name='path_graph(4)'
        self._test_equal(G,H)

    def test_multigraphs_equal(self):
        G = nx.path_graph(4, create_using=nx.MultiGraph())
        H = nx.MultiGraph()
        H.add_path(range(4))
        H.name='path_graph(4)'
        self._test_equal(G,H)

    def test_multigraphs_equal(self):
        G = nx.path_graph(4, create_using=nx.MultiDiGraph())
        H = nx.MultiDiGraph()
        H.add_path(range(4))
        H.name='path_graph(4)'
        self._test_equal(G,H)

    def test_graphs_not_equal(self):
        G = nx.path_graph(4)
        H = nx.Graph()
        H.add_cycle(range(4))
        self._test_not_equal(G,H)

    def test_graphs_not_equal2(self):
        G = nx.path_graph(4)
        H = nx.Graph()
        H.add_path(range(3))
        H.name='path_graph(4)'
        self._test_not_equal(G,H)

    def test_graphs_not_equal3(self):
        G = nx.path_graph(4)
        H = nx.Graph()
        H.add_path(range(4))
        H.name='path_graph(foo)'
        self._test_not_equal(G,H)