Passed
Push — main ( 40faed...b2501b )
by Eran
01:33
created

TestPlot.test_plot_large_graph()   A

Complexity

Conditions 3

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 29
rs 9.4
c 0
b 0
f 0
cc 3
nop 2
1
import networkx as nx
2
3
4
# Plot a simple graph with default parameters (node labels shown, edge labels hidden)
5 View Code Duplication
def test_plot_with_default_parameters(mocker):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
6
    # Arrange
7
    mock_draw = mocker.patch('graphinate.renderers.matplotlib.draw')
8
    mock_pyplot = mocker.patch('graphinate.renderers.matplotlib.pyplot')
9
    mock_ax = mocker.MagicMock()
10
    mock_fig = mocker.MagicMock()
11
    mock_pyplot.gca.return_value = mock_ax
12
    mock_pyplot.gcf.return_value = mock_fig
13
14
    graph = nx.Graph(name="Test Graph")
15
    graph.add_node(1, label="Node 1")
16
    graph.add_edge(1, 2, label="Edge 1-2")
17
18
    # Act
19
    from graphinate.renderers.matplotlib import plot
20
    plot(graph)
21
22
    # Assert
23
    mock_draw.assert_called_once_with(graph, True, False)
24
    mock_ax.margins.assert_called_once_with(0.10)
25
    mock_fig.suptitle.assert_called_once_with("Test Graph")
26
    mock_fig.tight_layout.assert_called_once()
27
    mock_pyplot.show.assert_called_once()
28
29
30
# Plot a graph with both node and edge labels displayed
31 View Code Duplication
def test_plot_with_node_and_edge_labels(mocker):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
32
    # Arrange
33
    mock_draw = mocker.patch('graphinate.renderers.matplotlib.draw')
34
    mock_pyplot = mocker.patch('graphinate.renderers.matplotlib.pyplot')
35
    mock_ax = mocker.MagicMock()
36
    mock_fig = mocker.MagicMock()
37
    mock_pyplot.gca.return_value = mock_ax
38
    mock_pyplot.gcf.return_value = mock_fig
39
40
    graph = nx.Graph(name="Test Graph")
41
    graph.add_node(1, label="Node 1")
42
    graph.add_edge(1, 2, label="Edge 1-2")
43
44
    # Act
45
    from graphinate.renderers.matplotlib import plot
46
    plot(graph, with_node_labels=True, with_edge_labels=True)
47
48
    # Assert
49
    mock_draw.assert_called_once_with(graph, True, True)
50
    mock_ax.margins.assert_called_once_with(0.10)
51
    mock_fig.suptitle.assert_called_once_with("Test Graph")
52
    mock_fig.tight_layout.assert_called_once()
53
    mock_pyplot.show.assert_called_once()
54
55
56
# Plot a graph with neither node nor edge labels displayed
57 View Code Duplication
def test_plot_without_node_and_edge_labels(mocker):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
58
    # Arrange
59
    mock_draw = mocker.patch('graphinate.renderers.matplotlib.draw')
60
    mock_pyplot = mocker.patch('graphinate.renderers.matplotlib.pyplot')
61
    mock_ax = mocker.MagicMock()
62
    mock_fig = mocker.MagicMock()
63
    mock_pyplot.gca.return_value = mock_ax
64
    mock_pyplot.gcf.return_value = mock_fig
65
66
    graph = nx.Graph(name="Test Graph")
67
    graph.add_node(1, label="Node 1")
68
    graph.add_edge(1, 2, label="Edge 1-2")
69
70
    # Act
71
    from graphinate.renderers.matplotlib import plot
72
    plot(graph, with_node_labels=False, with_edge_labels=False)
73
74
    # Assert
75
    mock_draw.assert_called_once_with(graph, False, False)
76
    mock_ax.margins.assert_called_once_with(0.10)
77
    mock_fig.suptitle.assert_called_once_with("Test Graph")
78
    mock_fig.tight_layout.assert_called_once()
79
    mock_pyplot.show.assert_called_once()
80
81
82
# Plot a graph with custom kwargs that are passed to the draw function
83
def test_plot_with_custom_kwargs(mocker):
84
    # Arrange
85
    mock_draw = mocker.patch('graphinate.renderers.matplotlib.draw')
86
    mock_pyplot = mocker.patch('graphinate.renderers.matplotlib.pyplot')
87
    mock_ax = mocker.MagicMock()
88
    mock_fig = mocker.MagicMock()
89
    mock_pyplot.gca.return_value = mock_ax
90
    mock_pyplot.gcf.return_value = mock_fig
91
92
    graph = nx.Graph(name="Test Graph")
93
    custom_kwargs = {'node_size': 500, 'alpha': 0.8, 'width': 2.0}
94
95
    # Act
96
    from graphinate.renderers.matplotlib import plot
97
    plot(graph, **custom_kwargs)
98
99
    # Assert
100
    mock_draw.assert_called_once_with(graph, True, False, **custom_kwargs)
101
    mock_ax.margins.assert_called_once_with(0.10)
102
    mock_fig.suptitle.assert_called_once_with("Test Graph")
103
    mock_fig.tight_layout.assert_called_once()
104
    mock_pyplot.show.assert_called_once()
105
106
107
# Plot a graph with a custom name that appears in the figure title
108
def test_plot_with_custom_graph_name(mocker):
109
    # Arrange
110
    mock_draw = mocker.patch('graphinate.renderers.matplotlib.draw')
111
    mock_pyplot = mocker.patch('graphinate.renderers.matplotlib.pyplot')
112
    mock_ax = mocker.MagicMock()
113
    mock_fig = mocker.MagicMock()
114
    mock_pyplot.gca.return_value = mock_ax
115
    mock_pyplot.gcf.return_value = mock_fig
116
117
    custom_name = "My Special Graph Visualization"
118
    graph = nx.Graph(name=custom_name)
119
    graph.add_node(1)
120
121
    # Act
122
    from graphinate.renderers.matplotlib import plot
123
    plot(graph)
124
125
    # Assert
126
    mock_draw.assert_called_once_with(graph, True, False)
127
    mock_ax.margins.assert_called_once_with(0.10)
128
    mock_fig.suptitle.assert_called_once_with(custom_name)
129
    mock_fig.tight_layout.assert_called_once()
130
    mock_pyplot.show.assert_called_once()
131
132
133
# Plot an empty graph (no nodes or edges)
134
def test_plot_empty_graph(mocker):
135
    # Arrange
136
    mock_draw = mocker.patch('graphinate.renderers.matplotlib.draw')
137
    mock_pyplot = mocker.patch('graphinate.renderers.matplotlib.pyplot')
138
    mock_ax = mocker.MagicMock()
139
    mock_fig = mocker.MagicMock()
140
    mock_pyplot.gca.return_value = mock_ax
141
    mock_pyplot.gcf.return_value = mock_fig
142
143
    graph = nx.Graph(name="Empty Graph")
144
145
    # Act
146
    from graphinate.renderers.matplotlib import plot
147
    plot(graph)
148
149
    # Assert
150
    mock_draw.assert_called_once_with(graph, True, False)
151
    mock_ax.margins.assert_called_once_with(0.10)
152
    mock_fig.suptitle.assert_called_once_with("Empty Graph")
153
    mock_fig.tight_layout.assert_called_once()
154
    mock_pyplot.show.assert_called_once()
155
156
157
# Plot a graph with no name attribute
158
def test_plot_graph_without_name(mocker):
159
    # Arrange
160
    mock_draw = mocker.patch('graphinate.renderers.matplotlib.draw')
161
    mock_pyplot = mocker.patch('graphinate.renderers.matplotlib.pyplot')
162
    mock_ax = mocker.MagicMock()
163
    mock_fig = mocker.MagicMock()
164
    mock_pyplot.gca.return_value = mock_ax
165
    mock_pyplot.gcf.return_value = mock_fig
166
167
    graph = nx.Graph()  # No name provided
168
    graph.add_node(1)
169
170
    # Act
171
    from graphinate.renderers.matplotlib import plot
172
    plot(graph)
173
174
    # Assert
175
    mock_draw.assert_called_once_with(graph, True, False)
176
    mock_ax.margins.assert_called_once_with(0.10)
177
    mock_fig.suptitle.assert_called_once_with("")  # Empty string expected
178
    mock_fig.tight_layout.assert_called_once()
179
    mock_pyplot.show.assert_called_once()
180
181
182
# Plot a very large graph with many nodes and edges
183
def test_plot_large_graph(mocker):
184
    # Arrange
185
    mock_draw = mocker.patch('graphinate.renderers.matplotlib.draw')
186
    mock_pyplot = mocker.patch('graphinate.renderers.matplotlib.pyplot')
187
    mock_ax = mocker.MagicMock()
188
    mock_fig = mocker.MagicMock()
189
    mock_pyplot.gca.return_value = mock_ax
190
    mock_pyplot.gcf.return_value = mock_fig
191
192
    # Create a large graph
193
    graph = nx.complete_graph(100)  # 100 nodes, fully connected
194
    graph.name = "Large Complete Graph"
195
196
    # Add labels to nodes and edges
197
    for node in graph.nodes():
198
        graph.nodes[node]['label'] = f"Node {node}"
199
    for edge in graph.edges():
200
        graph.edges[edge]['label'] = f"Edge {edge[0]}-{edge[1]}"
201
202
    # Act
203
    from graphinate.renderers.matplotlib import plot
204
    plot(graph)
205
206
    # Assert
207
    mock_draw.assert_called_once_with(graph, True, False)
208
    mock_ax.margins.assert_called_once_with(0.10)
209
    mock_fig.suptitle.assert_called_once_with("Large Complete Graph")
210
    mock_fig.tight_layout.assert_called_once()
211
    mock_pyplot.show.assert_called_once()
212
213
214
# Plot a graph with custom node attributes that aren't 'label'
215 View Code Duplication
def test_plot_with_custom_node_attributes(mocker):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
216
    # Arrange
217
    mock_draw = mocker.patch('graphinate.renderers.matplotlib.draw')
218
    mock_pyplot = mocker.patch('graphinate.renderers.matplotlib.pyplot')
219
    mock_ax = mocker.MagicMock()
220
    mock_fig = mocker.MagicMock()
221
    mock_pyplot.gca.return_value = mock_ax
222
    mock_pyplot.gcf.return_value = mock_fig
223
224
    graph = nx.Graph(name="Graph with Custom Node Attributes")
225
    graph.add_node(1, label="Node 1", weight=10, category="A")
226
    graph.add_node(2, label="Node 2", weight=5, category="B")
227
    graph.add_edge(1, 2)
228
229
    # Act
230
    from graphinate.renderers.matplotlib import plot
231
    plot(graph)
232
233
    # Assert
234
    mock_draw.assert_called_once_with(graph, True, False)
235
    mock_ax.margins.assert_called_once_with(0.10)
236
    mock_fig.suptitle.assert_called_once_with("Graph with Custom Node Attributes")
237
    mock_fig.tight_layout.assert_called_once()
238
    mock_pyplot.show.assert_called_once()
239
240
241
# Plot a graph with custom edge attributes that aren't 'label'
242 View Code Duplication
def test_plot_with_custom_edge_attributes(mocker):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
243
    # Arrange
244
    mock_draw = mocker.patch('graphinate.renderers.matplotlib.draw')
245
    mock_pyplot = mocker.patch('graphinate.renderers.matplotlib.pyplot')
246
    mock_ax = mocker.MagicMock()
247
    mock_fig = mocker.MagicMock()
248
    mock_pyplot.gca.return_value = mock_ax
249
    mock_pyplot.gcf.return_value = mock_fig
250
251
    graph = nx.Graph(name="Graph with Custom Edge Attributes")
252
    graph.add_node(1, label="Node 1")
253
    graph.add_node(2, label="Node 2")
254
    graph.add_edge(1, 2, label="Edge 1-2", weight=5, type="connection")
255
256
    # Act
257
    from graphinate.renderers.matplotlib import plot
258
    plot(graph, with_edge_labels=True)
259
260
    # Assert
261
    mock_draw.assert_called_once_with(graph, True, True)
262
    mock_ax.margins.assert_called_once_with(0.10)
263
    mock_fig.suptitle.assert_called_once_with("Graph with Custom Edge Attributes")
264
    mock_fig.tight_layout.assert_called_once()
265
    mock_pyplot.show.assert_called_once()
266