Passed
Push — main ( 32abc3...844181 )
by Eran
02:16
created

test_builders.test_build_graph_without_title()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
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 "MQ(1)" in diagram
83
    assert "Mg(2)" in diagram
84
    assert "MQ --> Mg" 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 "MQ(Node 1)" in diagram
100
    assert "Mg(Node 2)" in diagram
101
102
103
def test_build_graph_with_edge_labels():
104
    # Arrange
105
    graph = nx.Graph()
106
    graph.add_edge(1, 2, label="Edge 1-2")
107
    builder = DiagramBuilder()
108
109
    # Act
110
    diagram = builder.build(graph)
111
112
    # Assert
113
    assert "MQ -->|Edge 1-2| Mg" in diagram
114
115
def test_build_graph_without_edge_labels():
116
    # Arrange
117
    graph = nx.Graph()
118
    graph.add_edge(1, 2, label="Edge 1-2")
119
    builder = DiagramBuilder()
120
121
    # Act
122
    diagram = builder.build(graph, with_edge_labels=False)
123
124
    # Assert
125
    assert "MQ --> Mg" in diagram
126
    assert "|Edge 1-2|" not in diagram
127
128
129
def test_build_graph_with_node_colors():
130
    # Arrange
131
    graph = nx.Graph()
132
    graph.add_node(1, color="#FF0000")
133
    graph.add_node(2, color="#00FF00")
134
    graph.add_edge(1, 2)
135
    builder = DiagramBuilder()
136
137
    # Act
138
    diagram = builder.build(graph)
139
140
    # Assert
141
    assert "style MQ fill:#FF0000, color:#ffffff" in diagram
142
    assert "style Mg fill:#00FF00, color:#ffffff" in diagram
143
144
145
def test_build_graph_with_custom_node_shape():
146
    # Arrange
147
    graph = nx.Graph()
148
    graph.add_node(1)
149
    graph.add_edge(1, 1)
150
    builder = DiagramBuilder(node_shape=DiagramNodeShape.RECTANGLE)
151
152
    # Act
153
    diagram = builder.build(graph)
154
155
    # Assert
156
    assert "MQ[1]" in diagram
157
158
159
def test_build_graph_with_custom_orientation():
160
    # Arrange
161
    graph = nx.Graph()
162
    graph.add_node(1)
163
    graph.add_edge(1, 1)
164
    builder = DiagramBuilder(orientation=DiagramOrientation.TOP_DOWN)
165
166
    # Act
167
    diagram = builder.build(graph)
168
169
    # Assert
170
    assert "graph TD" in diagram
171
172
173
def test_build_graph_with_custom_layout():
174
    # Arrange
175
    graph = nx.Graph()
176
    graph.add_node(1)
177
    graph.add_edge(1, 1)
178
    builder = DiagramBuilder(layout='elk')
179
180
    # Act
181
    diagram = builder.build(graph)
182
183
    # Assert
184
    assert "layout: elk" in diagram
185
186
187
def test_build_graph_with_custom_look():
188
    # Arrange
189
    graph = nx.Graph()
190
    graph.add_node(1)
191
    graph.add_edge(1, 1)
192
    builder = DiagramBuilder(look='neo')
193
194
    # Act
195
    diagram = builder.build(graph)
196
197
    # Assert
198
    assert "look: neo" in diagram
199
200
201
def test_build_graph_with_custom_theme():
202
    # Arrange
203
    graph = nx.Graph()
204
    graph.add_node(1)
205
    graph.add_edge(1, 1)
206
    builder = DiagramBuilder(theme='neutral')
207
208
    # Act
209
    diagram = builder.build(graph)
210
211
    # Assert
212
    assert "theme: neutral" in diagram
213
214
215
def test_build_graph_with_title():
216
    # Arrange
217
    graph = nx.Graph(name="My Graph")
218
    graph.add_node(1)
219
    graph.add_edge(1, 1)
220
    builder = DiagramBuilder()
221
222
    # Act
223
    diagram = builder.build(graph)
224
225
    # Assert
226
    assert "title: My Graph" in diagram
227
228
229
def test_build_graph_without_title():
230
    # Arrange
231
    graph = nx.Graph()
232
    graph.add_node(1)
233
    graph.add_edge(1, 1)
234
    builder = DiagramBuilder()
235
236
    # Act
237
    diagram = builder.build(graph)
238
239
    # Assert
240
    assert "title" not in diagram
241
242
def test_build_graph_with_title_overwrite():
243
    # Arrange
244
    graph = nx.Graph(name="My Graph")
245
    graph.add_node(1)
246
    graph.add_edge(1, 1)
247
    builder = DiagramBuilder(title="Custom Title")
248
249
    # Act
250
    diagram = builder.build(graph)
251
252
    # Assert
253
    assert "title: Custom Title" in diagram
254
    assert "title: My Graph" not in diagram
255
256
257
def test_build_graph_with_empty_title_overwrite():
258
    # Arrange
259
    graph = nx.Graph(name="My Graph")
260
    graph.add_node(1)
261
    graph.add_edge(1, 1)
262
    builder = DiagramBuilder(title="")
263
264
    # Act
265
    diagram = builder.build(graph)
266
267
    # Assert
268
    assert "title" not in diagram
269
270
271
def test_edge_label_with_label():
272
    # Arrange
273
    data = {"label": "Edge Label"}
274
275
    # Act
276
    result = _edge_label(data)
277
278
    # Assert
279
    assert result == "|Edge Label|"
280
281
282
def test_edge_label_without_label():
283
    # Arrange
284
    data = {}
285
286
    # Act
287
    result = _edge_label(data)
288
289
    # Assert
290
    assert result == ""
291
292
293
@pytest.mark.parametrize(
294
    ('color', 'expected_contrast'),
295
    [
296
        ("#FFFFFF", "#000000"),
297
        ("#000000", "#ffffff"),
298
        ("#FF0000", "#ffffff"),
299
        ("#00FF00", "#ffffff"),
300
        ("#0000FF", "#ffffff"),
301
        ("#ABCDEF", "#000000"),
302
        ("#123456", "#ffffff"),
303
    ],
304
)
305
def test_contrast_color_valid_hex(color, expected_contrast):
306
    # Arrange (done by parametrizing)
307
    # Act
308
    result = _contrast_color(color)
309
310
    # Assert
311
    assert result == expected_contrast
312
313
314
@pytest.mark.parametrize(
315
    "color",
316
    ["FFFFFF", "#FFFFF", "#GGGGGG", 123456, None],
317
)
318
def test_contrast_color_invalid_hex(color):
319
    # Arrange (done by parametrizing)
320
    # Act & Assert
321
    with pytest.raises(ValueError): # noqa: PT011
322
        _contrast_color(color)
323
324
325
def test_node_style_with_color():
326
    # Arrange
327
    data = {"color": "#FF0000"}
328
329
    # Act
330
    result = _node_style("1", data)
331
332
    # Assert
333
    assert result == "\nstyle 1 fill:#FF0000, color:#ffffff"
334
335
336
def test_node_style_without_color():
337
    # Arrange
338
    data = {}
339
340
    # Act
341
    result = _node_style("1", data)
342
343
    # Assert
344
    assert result == ""
345
346
347
def test_graph_title_with_name():
348
    # Arrange
349
    graph = nx.Graph(name="My Graph")
350
351
    # Act
352
    result = _graph_title(graph)
353
354
    # Assert
355
    assert result == "title: My Graph\n"
356
357
358
def test_graph_title_without_name():
359
    # Arrange
360
    graph = nx.Graph()
361
362
    # Act
363
    result = _graph_title(graph)
364
365
    # Assert
366
    assert result == ""
367