Passed
Push — main ( 88ed15...38252d )
by Eran
01:38
created

TestPlot.test_plot_with_custom_kwargs()   A

Complexity

Conditions 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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