-
Notifications
You must be signed in to change notification settings - Fork 62
/
test_graph_methods.py
354 lines (313 loc) · 12.1 KB
/
test_graph_methods.py
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python
# Copyright (c) 2021, QuantStack and ipycytoscape Contributors
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
import copy
import pytest
from ipycytoscape.cytoscape import Edge, Graph, Node
from ._util import compare_edges, compare_nodes
@pytest.fixture(name="edges", scope="function")
def _make_edges():
ids = ["0", "1", "2"]
edges = [
Edge(data={"source": source, "target": target})
for source, target in zip(ids[:-1], ids[1:])
]
edges_weighted = [
Edge(data={"source": "0", "target": "1", "weight": str(i)}) for i in range(1, 3)
]
edge_inv = Edge(data={"source": "1", "target": "0"})
edges += edges_weighted
edges += [edge_inv]
return edges
class TestGraphRemoveMethods:
def test_remove_edge(self):
"""
Test to ensure that edges will be removed
"""
# only a small test because everything else is covered in remove_edge_by_id()
data = {
"nodes": [
{"data": {"id": "0"}},
{"data": {"id": "1"}},
{"data": {"id": "2"}},
],
"edges": [
{"data": {"source": "0", "target": "1"}},
{"data": {"source": "1", "target": "2"}},
{"data": {"source": "2", "target": "0"}},
],
}
expected_nodes = [
Node(data={"id": "0"}, position={}),
Node(data={"id": "1"}, position={}),
Node(data={"id": "2"}, position={}),
]
expected_edges = [
Edge(classes="", data={"source": "1", "target": "2"}),
Edge(classes="", data={"source": "2", "target": "0"}),
]
graph = Graph()
graph.add_graph_from_json(data)
graph.remove_edge(graph.edges[0])
compare_edges(expected_edges, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
def test_remove_edge_by_id(self):
"""
Test to ensure that edges will be removed given the ids of the nodes
for different graphs
"""
data = {
"nodes": [
{"data": {"id": "0"}},
{"data": {"id": "1"}},
{"data": {"id": "2"}},
],
"edges": [
{"data": {"source": "0", "target": "1", "weight": "1"}},
{"data": {"source": "0", "target": "1", "weight": "2"}},
{"data": {"source": "1", "target": "0"}},
{"data": {"source": "1", "target": "2"}},
{"data": {"source": "2", "target": "0"}},
],
}
expected_nodes = [
Node(data={"id": "0"}, position={}),
Node(data={"id": "1"}, position={}),
Node(data={"id": "2"}, position={}),
]
expected_edges_undirected = [
Edge(classes="", data={"source": "1", "target": "2"}),
Edge(classes="", data={"source": "2", "target": "0"}),
]
expected_edges_directed = [
Edge(classes=" directed ", data={"source": "1", "target": "0"}),
Edge(classes=" directed ", data={"source": "1", "target": "2"}),
Edge(classes=" directed ", data={"source": "2", "target": "0"}),
]
expected_edges_multiple = [
Edge(classes=" multiple_edges ", data={"source": "1", "target": "2"}),
Edge(classes=" multiple_edges ", data={"source": "2", "target": "0"}),
]
graph = Graph()
graph.add_graph_from_json(data)
graph.remove_edge_by_id("0", "1")
compare_edges(expected_edges_undirected, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
graph = Graph()
graph.add_graph_from_json(data, directed=True)
graph.remove_edge_by_id("0", "1")
compare_edges(expected_edges_directed, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
graph.remove_edge_by_id("1", "0")
compare_edges(expected_edges_directed[1:], graph.edges)
compare_nodes(expected_nodes, graph.nodes)
graph = Graph()
graph.add_graph_from_json(data, multiple_edges=True)
graph.remove_edge_by_id("0", "1")
compare_edges(expected_edges_multiple, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
def test_remove_node(self):
"""
Test to ensure that nodes will be removed correctly
"""
data = {
"nodes": [
{"data": {"id": "0"}},
{"data": {"id": "1"}},
{"data": {"id": "2"}},
],
"edges": [
{"data": {"source": "0", "target": "1"}},
{"data": {"source": "1", "target": "2"}},
{"data": {"source": "2", "target": "0"}},
],
}
expected_nodes = [
Node(data={"id": "1"}, position={}),
Node(data={"id": "2"}, position={}),
]
expected_edges = [
Edge(classes="", data={"source": "1", "target": "2"}),
]
graph = Graph()
graph.add_graph_from_json(data)
graph.remove_node(graph.nodes[0])
compare_edges(expected_edges, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
def test_remove_node_by_id(self):
"""
Test to ensure that nodes will be removed given the id
for different graphs with the corresponding edges
"""
data = {
"nodes": [
{"data": {"id": "0"}},
{"data": {"id": "1"}},
{"data": {"id": "2"}},
],
"edges": [
{"data": {"source": "0", "target": "1", "weight": "1"}},
{"data": {"source": "0", "target": "1", "weight": "2"}},
{"data": {"source": "1", "target": "0"}},
{"data": {"source": "1", "target": "2"}},
{"data": {"source": "2", "target": "0"}},
],
}
expected_nodes = [
Node(data={"id": "1"}, position={}),
Node(data={"id": "2"}, position={}),
]
expected_edges_undirected = [
Edge(classes="", data={"source": "1", "target": "2"}),
]
expected_edges_directed = [
Edge(classes=" directed ", data={"source": "1", "target": "2"}),
]
expected_edges_multiple = [
Edge(classes=" multiple_edges ", data={"source": "1", "target": "2"}),
]
graph = Graph()
graph.add_graph_from_json(data)
graph.remove_node_by_id("0")
compare_edges(expected_edges_undirected, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
graph = Graph()
graph.add_graph_from_json(data, directed=True)
graph.remove_node_by_id("0")
compare_edges(expected_edges_directed, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
graph.remove_node_by_id("1")
compare_edges(expected_edges_directed[1:], graph.edges)
compare_nodes(expected_nodes[1:], graph.nodes)
graph = Graph()
graph.add_graph_from_json(data, multiple_edges=True)
graph.remove_node_by_id("0")
compare_edges(expected_edges_multiple, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
graph.remove_node_by_id("1")
compare_edges(expected_edges_multiple[1:], graph.edges)
compare_nodes(expected_nodes[1:], graph.nodes)
class TestGraphAddMethods:
def test_add_nodes(self):
"""
Test to ensure that nodes will be added to the graph
"""
# create some nodes
ids = ["0", "1"]
nodes = [Node(data={"id": i}) for i in ids]
expected_nodes = [
Node(data={"id": "0"}, position={}),
Node(data={"id": "1"}, position={}),
]
expected_edges = []
graph = Graph()
graph.add_nodes(nodes)
compare_edges(expected_edges, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
def test_add_node(self):
"""
Test to ensure that a single node will be added to the graph
"""
# will call add_edges, so no extensive test are necessary
node = Node(data={"id": "0"})
expected_nodes = [
Node(data={"id": "0"}, position={}),
]
expected_edges = []
graph = Graph()
graph.add_node(node)
compare_edges(expected_edges, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
def test_add_edges(self, edges):
"""
Test to ensure that edges with the corresponding nodes will be
added to the graph.
"""
expected_nodes = [
Node(data={"id": "0"}, position={}),
Node(data={"id": "1"}, position={}),
Node(data={"id": "2"}, position={}),
]
expected_edges_undirected = [
Edge(classes="", data={"source": "0", "target": "1"}),
Edge(classes="", data={"source": "1", "target": "2"}),
]
graph = Graph()
graph.add_edges(edges)
compare_edges(expected_edges_undirected, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
def test_add_edges_directed(self, edges):
"""
Test to ensure that edges with the corresponding nodes will be added
to the graph for directed edges
"""
expected_nodes = [
Node(data={"id": "0"}, position={}),
Node(data={"id": "1"}, position={}),
Node(data={"id": "2"}, position={}),
]
expected_edges_directed = [
Edge(classes=" directed ", data={"source": "0", "target": "1"}),
Edge(classes=" directed ", data={"source": "1", "target": "2"}),
Edge(classes=" directed ", data={"source": "1", "target": "0"}),
]
graph = Graph()
graph.add_edges(edges, directed=True)
compare_edges(expected_edges_directed, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
def test_add_edges_multiple_edges(self, edges):
"""
Test to ensure that edges with the corresponding nodes
will be added to the graph with multiple_edges.
"""
expected_nodes = [
Node(data={"id": "0"}, position={}),
Node(data={"id": "1"}, position={}),
Node(data={"id": "2"}, position={}),
]
expected_edges_multiple = [
Edge(classes=" multiple_edges ", data={"source": "0", "target": "1"}),
Edge(classes=" multiple_edges ", data={"source": "1", "target": "2"}),
Edge(
classes=" multiple_edges ",
data={"source": "0", "target": "1", "weight": "1"},
),
Edge(
classes=" multiple_edges ",
data={"source": "0", "target": "1", "weight": "2"},
),
Edge(classes=" multiple_edges ", data={"source": "1", "target": "0"}),
]
graph = Graph()
graph.add_edges(edges, multiple_edges=True)
compare_edges(expected_edges_multiple, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
def test_add_edges_2(self):
"""
Test to ensure that an edge with the corresponding nodes will be
added to the graph.
"""
edge = Edge(data={"source": "0", "target": "1"})
edge_inv = Edge(data={"source": "1", "target": "0"})
expected_nodes = [
Node(data={"id": "0"}, position={}),
Node(data={"id": "1"}, position={}),
]
expected_edges = [
Edge(classes="", data={"source": "0", "target": "1"}),
]
expected_edges_multiple = [
Edge(classes="", data={"source": "0", "target": "1"}),
Edge(classes=" multiple_edges ", data={"source": "1", "target": "0"}),
]
graph = Graph()
graph.add_edge(copy.copy(edge))
compare_edges(expected_edges, graph.edges)
compare_nodes(expected_nodes, graph.nodes)
# test for passing arguments
graph.add_edge(copy.copy(edge_inv), multiple_edges=True)
compare_edges(expected_edges_multiple, graph.edges)
compare_nodes(expected_nodes, graph.nodes)