test_builders   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 370
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 191
dl 0
loc 370
rs 10
c 0
b 0
f 0
wmc 30

27 Functions

Rating   Name   Duplication   Size   Complexity  
A test_build_empty_graph() 0 10 1
A test_build_simple_graph() 0 14 1
A test_invalid_orientation_type() 0 4 2
A test_invalid_node_shape_type() 0 4 2
A test_custom_initialization() 0 16 1
A test_default_initialization() 0 10 1
A test_build_graph_with_edge_labels() 0 11 1
A test_build_graph_with_custom_look() 0 12 1
A test_graph_title_with_name() 0 9 1
A test_graph_title_without_name() 0 9 1
A test_build_graph_with_node_colors() 0 14 1
A test_node_style_with_color() 0 9 1
A test_build_graph_without_edge_labels() 0 12 1
A test_contrast_color_invalid_hex() 0 9 2
A test_build_graph_with_custom_node_shape() 0 12 1
A test_node_style_without_color() 0 9 1
A test_edge_label_without_label() 0 9 1
A test_build_graph_with_custom_theme() 0 12 1
A test_build_graph_with_custom_layout() 0 12 1
A test_build_graph_with_title_overwrite() 0 13 1
A test_edge_label_with_label() 0 9 1
A test_build_graph_with_empty_title_overwrite() 0 12 1
A test_contrast_color_valid_hex() 0 19 1
A test_build_graph_without_title() 0 12 1
A test_build_graph_with_title() 0 12 1
A test_build_graph_with_node_labels() 0 15 1
A test_build_graph_with_custom_orientation() 0 12 1
1
import networkx as nx
2
import pytest
3
4
from networkx_mermaid import DiagramNodeShape, DiagramOrientation
5
from networkx_mermaid.builders import (
6
    DEFAULT_LAYOUT,
7
    DEFAULT_LOOK,
8
    DEFAULT_THEME,
9
    DiagramBuilder,
10
    _contrast_color,
11
    _edge_label,
12
    _graph_title,
13
    _node_style,
14
)
15
16
17
def test_default_initialization():
18
    # Arrange
19
    builder = DiagramBuilder()
20
21
    # Act & Assert
22
    assert builder.orientation == DiagramOrientation.LEFT_RIGHT
23
    assert builder.node_shape == DiagramNodeShape.DEFAULT
24
    assert builder.layout == DEFAULT_LAYOUT
25
    assert builder.look == DEFAULT_LOOK
26
    assert builder.theme == DEFAULT_THEME
27
28
29
def test_custom_initialization():
30
    # Arrange
31
    builder = DiagramBuilder(
32
        orientation=DiagramOrientation.TOP_DOWN,
33
        node_shape=DiagramNodeShape.RECTANGLE,
34
        layout="elk",
35
        look="neo",
36
        theme="neutral"
37
    )
38
39
    # Act & Assert
40
    assert builder.orientation == DiagramOrientation.TOP_DOWN
41
    assert builder.node_shape == DiagramNodeShape.RECTANGLE
42
    assert builder.layout == "elk"
43
    assert builder.look == "neo"
44
    assert builder.theme == "neutral"
45
46
47
def test_invalid_orientation_type():
48
    # Arrange & Act & Assert
49
    with pytest.raises(TypeError):
50
        DiagramBuilder(orientation="invalid")
51
52
53
def test_invalid_node_shape_type():
54
    # Arrange & Act & Assert
55
    with pytest.raises(TypeError):
56
        DiagramBuilder(node_shape="invalid")
57
58
59
def test_build_empty_graph():
60
    # Arrange
61
    graph = nx.Graph()
62
    builder = DiagramBuilder()
63
64
    # Act
65
    diagram = builder.build(graph)
66
67
    # Assert
68
    assert "graph LR" in diagram
69
70
71
def test_build_simple_graph():
72
    # Arrange
73
    graph = nx.Graph()
74
    graph.add_edge(1, 2)
75
    builder = DiagramBuilder()
76
77
    # Act
78
    diagram = builder.build(graph)
79
80
    # Assert
81
    assert "graph LR" in diagram
82
    assert "A(1)" in diagram
83
    assert "B(2)" in diagram
84
    assert "A --> B" in diagram
85
86
87
def test_build_graph_with_node_labels():
88
    # Arrange
89
    graph = nx.Graph()
90
    graph.add_node(1, label="Node 1")
91
    graph.add_node(2, label="Node 2")
92
    graph.add_edge(1, 2)
93
    builder = DiagramBuilder()
94
95
    # Act
96
    diagram = builder.build(graph)
97
98
    # Assert
99
    assert "A(Node 1)" in diagram
100
    assert "B(Node 2)" in diagram
101
    assert "A --> B" in diagram
102
103
104
def test_build_graph_with_edge_labels():
105
    # Arrange
106
    graph = nx.Graph()
107
    graph.add_edge(1, 2, label="Edge 1-2")
108
    builder = DiagramBuilder()
109
110
    # Act
111
    diagram = builder.build(graph)
112
113
    # Assert
114
    assert "A -->|Edge 1-2| B" in diagram
115
116
117
def test_build_graph_without_edge_labels():
118
    # Arrange
119
    graph = nx.Graph()
120
    graph.add_edge(1, 2, label="Edge 1-2")
121
    builder = DiagramBuilder()
122
123
    # Act
124
    diagram = builder.build(graph, with_edge_labels=False)
125
126
    # Assert
127
    assert "A --> B" in diagram
128
    assert "|Edge 1-2|" not in diagram
129
130
131
def test_build_graph_with_node_colors():
132
    # Arrange
133
    graph = nx.Graph()
134
    graph.add_node(1, color="#FF0000")
135
    graph.add_node(2, color="#00FF00")
136
    graph.add_edge(1, 2)
137
    builder = DiagramBuilder()
138
139
    # Act
140
    diagram = builder.build(graph)
141
142
    # Assert
143
    assert "style A fill:#FF0000, color:#ffffff" in diagram
144
    assert "style B fill:#00FF00, color:#ffffff" in diagram
145
146
147
def test_build_graph_with_custom_node_shape():
148
    # Arrange
149
    graph = nx.Graph()
150
    graph.add_node(1)
151
    graph.add_edge(1, 1)
152
    builder = DiagramBuilder(node_shape=DiagramNodeShape.RECTANGLE)
153
154
    # Act
155
    diagram = builder.build(graph)
156
157
    # Assert
158
    assert "A[1]" in diagram
159
160
161
def test_build_graph_with_custom_orientation():
162
    # Arrange
163
    graph = nx.Graph()
164
    graph.add_node(1)
165
    graph.add_edge(1, 1)
166
    builder = DiagramBuilder(orientation=DiagramOrientation.TOP_DOWN)
167
168
    # Act
169
    diagram = builder.build(graph)
170
171
    # Assert
172
    assert "graph TD" in diagram
173
174
175
def test_build_graph_with_custom_layout():
176
    # Arrange
177
    graph = nx.Graph()
178
    graph.add_node(1)
179
    graph.add_edge(1, 1)
180
    builder = DiagramBuilder(layout='elk')
181
182
    # Act
183
    diagram = builder.build(graph)
184
185
    # Assert
186
    assert "layout: elk" in diagram
187
188
189
def test_build_graph_with_custom_look():
190
    # Arrange
191
    graph = nx.Graph()
192
    graph.add_node(1)
193
    graph.add_edge(1, 1)
194
    builder = DiagramBuilder(look='neo')
195
196
    # Act
197
    diagram = builder.build(graph)
198
199
    # Assert
200
    assert "look: neo" in diagram
201
202
203
def test_build_graph_with_custom_theme():
204
    # Arrange
205
    graph = nx.Graph()
206
    graph.add_node(1)
207
    graph.add_edge(1, 1)
208
    builder = DiagramBuilder(theme='neutral')
209
210
    # Act
211
    diagram = builder.build(graph)
212
213
    # Assert
214
    assert "theme: neutral" in diagram
215
216
217
def test_build_graph_with_title():
218
    # Arrange
219
    graph = nx.Graph(name="My Graph")
220
    graph.add_node(1)
221
    graph.add_edge(1, 1)
222
    builder = DiagramBuilder()
223
224
    # Act
225
    diagram = builder.build(graph)
226
227
    # Assert
228
    assert "title: My Graph" in diagram
229
230
231
def test_build_graph_without_title():
232
    # Arrange
233
    graph = nx.Graph()
234
    graph.add_node(1)
235
    graph.add_edge(1, 1)
236
    builder = DiagramBuilder()
237
238
    # Act
239
    diagram = builder.build(graph)
240
241
    # Assert
242
    assert "title" not in diagram
243
244
245
def test_build_graph_with_title_overwrite():
246
    # Arrange
247
    graph = nx.Graph(name="My Graph")
248
    graph.add_node(1)
249
    graph.add_edge(1, 1)
250
    builder = DiagramBuilder()
251
252
    # Act
253
    diagram = builder.build(graph, title="Custom Title")
254
255
    # Assert
256
    assert "title: Custom Title" in diagram
257
    assert "title: My Graph" not in diagram
258
259
260
def test_build_graph_with_empty_title_overwrite():
261
    # Arrange
262
    graph = nx.Graph(name="My Graph")
263
    graph.add_node(1)
264
    graph.add_edge(1, 1)
265
    builder = DiagramBuilder()
266
267
    # Act
268
    diagram = builder.build(graph, title="")
269
270
    # Assert
271
    assert "title" not in diagram
272
273
274
def test_edge_label_with_label():
275
    # Arrange
276
    data = {"label": "Edge Label"}
277
278
    # Act
279
    result = _edge_label(data)
280
281
    # Assert
282
    assert result == "|Edge Label|"
283
284
285
def test_edge_label_without_label():
286
    # Arrange
287
    data = {}
288
289
    # Act
290
    result = _edge_label(data)
291
292
    # Assert
293
    assert result == ""
294
295
296
@pytest.mark.parametrize(
297
    ('color', 'expected_contrast'),
298
    [
299
        ("#FFFFFF", "#000000"),
300
        ("#000000", "#ffffff"),
301
        ("#FF0000", "#ffffff"),
302
        ("#00FF00", "#ffffff"),
303
        ("#0000FF", "#ffffff"),
304
        ("#ABCDEF", "#000000"),
305
        ("#123456", "#ffffff"),
306
    ],
307
)
308
def test_contrast_color_valid_hex(color, expected_contrast):
309
    # Arrange (done by parametrizing)
310
    # Act
311
    result = _contrast_color(color)
312
313
    # Assert
314
    assert result == expected_contrast
315
316
317
@pytest.mark.parametrize(
318
    "color",
319
    ["FFFFFF", "#FFFFF", "#GGGGGG", 123456, None],
320
)
321
def test_contrast_color_invalid_hex(color):
322
    # Arrange (done by parametrizing)
323
    # Act & Assert
324
    with pytest.raises(ValueError):  # noqa: PT011
325
        _contrast_color(color)
326
327
328
def test_node_style_with_color():
329
    # Arrange
330
    data = {"color": "#FF0000"}
331
332
    # Act
333
    result = _node_style("1", data)
334
335
    # Assert
336
    assert result == "\nstyle 1 fill:#FF0000, color:#ffffff"
337
338
339
def test_node_style_without_color():
340
    # Arrange
341
    data = {}
342
343
    # Act
344
    result = _node_style("1", data)
345
346
    # Assert
347
    assert result == ""
348
349
350
def test_graph_title_with_name():
351
    # Arrange
352
    graph = nx.Graph(name="My Graph")
353
354
    # Act
355
    result = _graph_title(graph)
356
357
    # Assert
358
    assert result == "title: My Graph\n"
359
360
361
def test_graph_title_without_name():
362
    # Arrange
363
    graph = nx.Graph()
364
365
    # Act
366
    result = _graph_title(graph)
367
368
    # Assert
369
    assert result == ""
370