|
1
|
|
|
import networkx as nx |
|
2
|
|
|
|
|
3
|
|
|
from graphinate.renderers.matplotlib import draw |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class TestDraw: |
|
7
|
|
|
|
|
8
|
|
|
# Drawing a graph with default parameters (node labels on, edge labels off) |
|
9
|
|
|
def test_draw_with_default_parameters(self, mocker): |
|
10
|
|
|
# Arrange |
|
11
|
|
|
mock_nx_draw = mocker.patch('networkx.draw') |
|
12
|
|
|
mock_nx_draw_edge_labels = mocker.patch('networkx.draw_networkx_edge_labels') |
|
13
|
|
|
mock_spring_layout = mocker.patch('networkx.spring_layout', return_value='mock_pos') |
|
14
|
|
|
mock_is_planar = mocker.patch('networkx.is_planar', return_value=False) |
|
15
|
|
|
mock_node_color_mapping = mocker.patch('graphinate.renderers.matplotlib.node_color_mapping', |
|
16
|
|
|
return_value={0: 'red', 1: 'blue'}) |
|
17
|
|
|
|
|
18
|
|
|
graph = nx.Graph() |
|
19
|
|
|
graph.add_node(0, label='Node0') |
|
20
|
|
|
graph.add_node(1, label='Node1') |
|
21
|
|
|
graph.add_edge(0, 1) |
|
22
|
|
|
|
|
23
|
|
|
# Act |
|
24
|
|
|
draw(graph) |
|
25
|
|
|
|
|
26
|
|
|
# Assert |
|
27
|
|
|
mock_is_planar.assert_called_once_with(graph) |
|
28
|
|
|
mock_spring_layout.assert_called_once_with(graph) |
|
29
|
|
|
mock_node_color_mapping.assert_called_once_with(graph) |
|
30
|
|
|
mock_nx_draw.assert_called_once() |
|
31
|
|
|
mock_nx_draw_edge_labels.assert_not_called() |
|
32
|
|
|
|
|
33
|
|
|
# Drawing a graph with node labels turned off |
|
34
|
|
View Code Duplication |
def test_draw_with_node_labels_off(self, mocker): |
|
|
|
|
|
|
35
|
|
|
# Arrange |
|
36
|
|
|
mock_nx_draw = mocker.patch('networkx.draw') |
|
37
|
|
|
mock_spring_layout = mocker.patch('networkx.spring_layout', return_value='mock_pos') |
|
38
|
|
|
mock_is_planar = mocker.patch('networkx.is_planar', return_value=False) |
|
39
|
|
|
mock_node_color_mapping = mocker.patch('graphinate.renderers.matplotlib.node_color_mapping', |
|
40
|
|
|
return_value={0: 'red', 1: 'blue'}) |
|
41
|
|
|
|
|
42
|
|
|
graph = nx.Graph() |
|
43
|
|
|
graph.add_node(0, label='Node0') |
|
44
|
|
|
graph.add_node(1, label='Node1') |
|
45
|
|
|
graph.add_edge(0, 1) |
|
46
|
|
|
|
|
47
|
|
|
# Act |
|
48
|
|
|
draw(graph, with_node_labels=False) |
|
49
|
|
|
|
|
50
|
|
|
# Assert |
|
51
|
|
|
mock_is_planar.assert_called_once_with(graph) |
|
52
|
|
|
mock_spring_layout.assert_called_once_with(graph) |
|
53
|
|
|
mock_node_color_mapping.assert_called_once_with(graph) |
|
54
|
|
|
mock_nx_draw.assert_called_once() |
|
55
|
|
|
# Check that 'with_labels' is not in the draw parameters |
|
56
|
|
|
args, kwargs = mock_nx_draw.call_args |
|
57
|
|
|
assert 'with_labels' not in kwargs |
|
58
|
|
|
|
|
59
|
|
|
# Drawing a graph with edge labels turned on |
|
60
|
|
|
def test_draw_with_edge_labels_on(self, mocker): |
|
61
|
|
|
# Arrange |
|
62
|
|
|
mock_nx_draw = mocker.patch('networkx.draw') |
|
63
|
|
|
mock_nx_draw_edge_labels = mocker.patch('networkx.draw_networkx_edge_labels') |
|
64
|
|
|
mock_spring_layout = mocker.patch('networkx.spring_layout', return_value='mock_pos') |
|
65
|
|
|
mock_is_planar = mocker.patch('networkx.is_planar', return_value=False) |
|
66
|
|
|
mock_node_color_mapping = mocker.patch('graphinate.renderers.matplotlib.node_color_mapping', |
|
67
|
|
|
return_value={0: 'red', 1: 'blue'}) |
|
68
|
|
|
|
|
69
|
|
|
graph = nx.Graph() |
|
70
|
|
|
graph.add_node(0, label='Node0') |
|
71
|
|
|
graph.add_node(1, label='Node1') |
|
72
|
|
|
graph.add_edge(0, 1, label='Edge0-1') |
|
73
|
|
|
|
|
74
|
|
|
# Act |
|
75
|
|
|
draw(graph, with_edge_labels=True) |
|
76
|
|
|
|
|
77
|
|
|
# Assert |
|
78
|
|
|
mock_is_planar.assert_called_once_with(graph) |
|
79
|
|
|
mock_spring_layout.assert_called_once_with(graph) |
|
80
|
|
|
mock_node_color_mapping.assert_called_once_with(graph) |
|
81
|
|
|
mock_nx_draw.assert_called_once() |
|
82
|
|
|
mock_nx_draw_edge_labels.assert_called_once() |
|
83
|
|
|
|
|
84
|
|
|
# Drawing a graph with both node and edge labels turned on |
|
85
|
|
View Code Duplication |
def test_draw_with_both_labels_on(self, mocker): |
|
|
|
|
|
|
86
|
|
|
# Arrange |
|
87
|
|
|
mock_nx_draw = mocker.patch('networkx.draw') |
|
88
|
|
|
mock_nx_draw_edge_labels = mocker.patch('networkx.draw_networkx_edge_labels') |
|
89
|
|
|
mock_spring_layout = mocker.patch('networkx.spring_layout', return_value='mock_pos') |
|
90
|
|
|
mock_is_planar = mocker.patch('networkx.is_planar', return_value=False) |
|
91
|
|
|
mock_node_color_mapping = mocker.patch('graphinate.renderers.matplotlib.node_color_mapping', |
|
92
|
|
|
return_value={0: 'red', 1: 'blue'}) |
|
93
|
|
|
|
|
94
|
|
|
graph = nx.Graph() |
|
95
|
|
|
graph.add_node(0, label='Node0') |
|
96
|
|
|
graph.add_node(1, label='Node1') |
|
97
|
|
|
graph.add_edge(0, 1, label='Edge0-1') |
|
98
|
|
|
|
|
99
|
|
|
# Act |
|
100
|
|
|
draw(graph, with_node_labels=True, with_edge_labels=True) |
|
101
|
|
|
|
|
102
|
|
|
# Assert |
|
103
|
|
|
mock_is_planar.assert_called_once_with(graph) |
|
104
|
|
|
mock_spring_layout.assert_called_once_with(graph) |
|
105
|
|
|
mock_node_color_mapping.assert_called_once_with(graph) |
|
106
|
|
|
mock_nx_draw.assert_called_once() |
|
107
|
|
|
mock_nx_draw_edge_labels.assert_called_once() |
|
108
|
|
|
|
|
109
|
|
|
# Check that node labels are enabled |
|
110
|
|
|
args, kwargs = mock_nx_draw.call_args |
|
111
|
|
|
assert kwargs.get('with_labels') is True |
|
112
|
|
|
|
|
113
|
|
|
# Drawing a planar graph uses planar_layout first |
|
114
|
|
View Code Duplication |
def test_draw_planar_graph(self, mocker): |
|
|
|
|
|
|
115
|
|
|
# Arrange |
|
116
|
|
|
mock_nx_draw = mocker.patch('networkx.draw') |
|
117
|
|
|
mock_planar_layout = mocker.patch('networkx.planar_layout', return_value='planar_pos') |
|
118
|
|
|
mock_spring_layout = mocker.patch('networkx.spring_layout', return_value='spring_pos') |
|
119
|
|
|
mock_is_planar = mocker.patch('networkx.is_planar', return_value=True) |
|
120
|
|
|
mock_node_color_mapping = mocker.patch('graphinate.renderers.matplotlib.node_color_mapping', |
|
121
|
|
|
return_value={0: 'red', 1: 'blue'}) |
|
122
|
|
|
|
|
123
|
|
|
graph = nx.Graph() |
|
124
|
|
|
graph.add_node(0) |
|
125
|
|
|
graph.add_node(1) |
|
126
|
|
|
graph.add_edge(0, 1) |
|
127
|
|
|
|
|
128
|
|
|
# Act |
|
129
|
|
|
draw(graph) |
|
130
|
|
|
|
|
131
|
|
|
# Assert |
|
132
|
|
|
mock_is_planar.assert_called_once_with(graph) |
|
133
|
|
|
mock_planar_layout.assert_called_once_with(graph) |
|
134
|
|
|
mock_spring_layout.assert_called_once_with(graph, pos='planar_pos') |
|
135
|
|
|
mock_node_color_mapping.assert_called_once_with(graph) |
|
136
|
|
|
mock_nx_draw.assert_called_once() |
|
137
|
|
|
|
|
138
|
|
|
# Drawing a non-planar graph uses spring_layout directly |
|
139
|
|
View Code Duplication |
def test_draw_non_planar_graph(self, mocker): |
|
|
|
|
|
|
140
|
|
|
# Arrange |
|
141
|
|
|
mock_nx_draw = mocker.patch('networkx.draw') |
|
142
|
|
|
mock_planar_layout = mocker.patch('networkx.planar_layout') |
|
143
|
|
|
mock_spring_layout = mocker.patch('networkx.spring_layout', return_value='spring_pos') |
|
144
|
|
|
mock_is_planar = mocker.patch('networkx.is_planar', return_value=False) |
|
145
|
|
|
mock_node_color_mapping = mocker.patch('graphinate.renderers.matplotlib.node_color_mapping', |
|
146
|
|
|
return_value={0: 'red', 1: 'blue'}) |
|
147
|
|
|
|
|
148
|
|
|
graph = nx.Graph() |
|
149
|
|
|
graph.add_node(0) |
|
150
|
|
|
graph.add_node(1) |
|
151
|
|
|
graph.add_edge(0, 1) |
|
152
|
|
|
|
|
153
|
|
|
# Act |
|
154
|
|
|
draw(graph) |
|
155
|
|
|
|
|
156
|
|
|
# Assert |
|
157
|
|
|
mock_is_planar.assert_called_once_with(graph) |
|
158
|
|
|
mock_planar_layout.assert_not_called() |
|
159
|
|
|
mock_spring_layout.assert_called_once_with(graph) |
|
160
|
|
|
mock_node_color_mapping.assert_called_once_with(graph) |
|
161
|
|
|
mock_nx_draw.assert_called_once() |
|
162
|
|
|
|
|
163
|
|
|
# Drawing an empty graph |
|
164
|
|
|
def test_draw_empty_graph(self, mocker): |
|
165
|
|
|
# Arrange |
|
166
|
|
|
mock_nx_draw = mocker.patch('networkx.draw') |
|
167
|
|
|
mock_spring_layout = mocker.patch('networkx.spring_layout', return_value={}) |
|
168
|
|
|
mock_is_planar = mocker.patch('networkx.is_planar', return_value=True) |
|
169
|
|
|
mock_node_color_mapping = mocker.patch('graphinate.renderers.matplotlib.node_color_mapping', |
|
170
|
|
|
return_value={}) |
|
171
|
|
|
|
|
172
|
|
|
graph = nx.Graph() |
|
173
|
|
|
|
|
174
|
|
|
# Act |
|
175
|
|
|
draw(graph) |
|
176
|
|
|
|
|
177
|
|
|
# Assert |
|
178
|
|
|
mock_is_planar.assert_called_once_with(graph) |
|
179
|
|
|
mock_spring_layout.assert_called_once() |
|
180
|
|
|
mock_node_color_mapping.assert_called_once_with(graph) |
|
181
|
|
|
mock_nx_draw.assert_called_once() |
|
182
|
|
|
|
|
183
|
|
|
# Check that node_color is an empty list for empty graph |
|
184
|
|
|
args, kwargs = mock_nx_draw.call_args |
|
185
|
|
|
assert kwargs.get('node_color') == [] |
|
186
|
|
|
|
|
187
|
|
|
# Drawing a graph with no node attributes for labels when with_node_labels=True |
|
188
|
|
View Code Duplication |
def test_draw_no_node_labels_attribute(self, mocker): |
|
|
|
|
|
|
189
|
|
|
# Arrange |
|
190
|
|
|
mock_nx_draw = mocker.patch('networkx.draw') |
|
191
|
|
|
mock_get_node_attributes = mocker.patch('networkx.get_node_attributes', return_value={}) |
|
192
|
|
|
mock_spring_layout = mocker.patch('networkx.spring_layout', return_value='mock_pos') |
|
193
|
|
|
mock_is_planar = mocker.patch('networkx.is_planar', return_value=False) |
|
194
|
|
|
mock_node_color_mapping = mocker.patch('graphinate.renderers.matplotlib.node_color_mapping', |
|
195
|
|
|
return_value={0: 'red', 1: 'blue'}) |
|
196
|
|
|
|
|
197
|
|
|
graph = nx.Graph() |
|
198
|
|
|
graph.add_node(0) # No label attribute |
|
199
|
|
|
graph.add_node(1) # No label attribute |
|
200
|
|
|
graph.add_edge(0, 1) |
|
201
|
|
|
|
|
202
|
|
|
# Act |
|
203
|
|
|
draw(graph, with_node_labels=True) |
|
204
|
|
|
|
|
205
|
|
|
# Assert |
|
206
|
|
|
mock_is_planar.assert_called_once_with(graph) |
|
207
|
|
|
mock_spring_layout.assert_called_once_with(graph) |
|
208
|
|
|
mock_node_color_mapping.assert_called_once_with(graph) |
|
209
|
|
|
mock_get_node_attributes.assert_called_once_with(graph, 'label') |
|
210
|
|
|
mock_nx_draw.assert_called_once() |
|
211
|
|
|
|
|
212
|
|
|
# Check that labels parameter is empty dict |
|
213
|
|
|
args, kwargs = mock_nx_draw.call_args |
|
214
|
|
|
assert kwargs.get('labels') == {} |
|
215
|
|
|
|
|
216
|
|
|
# Drawing a graph with no edge attributes for labels when with_edge_labels=True |
|
217
|
|
View Code Duplication |
def test_draw_no_edge_labels_attribute(self, mocker): |
|
|
|
|
|
|
218
|
|
|
# Arrange |
|
219
|
|
|
mock_nx_draw = mocker.patch('networkx.draw') |
|
220
|
|
|
mock_nx_draw_edge_labels = mocker.patch('networkx.draw_networkx_edge_labels') |
|
221
|
|
|
mock_get_edge_attributes = mocker.patch('networkx.get_edge_attributes', return_value={}) |
|
222
|
|
|
mock_spring_layout = mocker.patch('networkx.spring_layout', return_value='mock_pos') |
|
223
|
|
|
mock_is_planar = mocker.patch('networkx.is_planar', return_value=False) |
|
224
|
|
|
mock_node_color_mapping = mocker.patch('graphinate.renderers.matplotlib.node_color_mapping', |
|
225
|
|
|
return_value={0: 'red', 1: 'blue'}) |
|
226
|
|
|
|
|
227
|
|
|
graph = nx.Graph() |
|
228
|
|
|
graph.add_node(0) |
|
229
|
|
|
graph.add_node(1) |
|
230
|
|
|
graph.add_edge(0, 1) # No label attribute |
|
231
|
|
|
|
|
232
|
|
|
# Act |
|
233
|
|
|
draw(graph, with_edge_labels=True) |
|
234
|
|
|
|
|
235
|
|
|
# Assert |
|
236
|
|
|
mock_is_planar.assert_called_once_with(graph) |
|
237
|
|
|
mock_spring_layout.assert_called_once_with(graph) |
|
238
|
|
|
mock_node_color_mapping.assert_called_once_with(graph) |
|
239
|
|
|
mock_get_edge_attributes.assert_called_once_with(graph, 'label') |
|
240
|
|
|
mock_nx_draw.assert_called_once() |
|
241
|
|
|
mock_nx_draw_edge_labels.assert_called_once() |
|
242
|
|
|
|
|
243
|
|
|
# Check that edge_labels parameter is empty dict |
|
244
|
|
|
args, kwargs = mock_nx_draw_edge_labels.call_args |
|
245
|
|
|
assert kwargs.get('edge_labels') == {} |
|
246
|
|
|
|