File: test_scc.py

package info (click to toggle)
python-datamodel-code-generator 0.45.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,052 kB
  • sloc: python: 29,621; makefile: 15
file content (388 lines) | stat: -rw-r--r-- 12,161 bytes parent folder | download
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"""Unit tests for SCC (Strongly Connected Components) detection module."""

from __future__ import annotations

from inline_snapshot import snapshot

from datamodel_code_generator.parser._scc import (
    find_circular_sccs,
    strongly_connected_components,
)


def _to_sorted_result(sccs: list[set[tuple[str, ...]]]) -> list[list[tuple[str, ...]]]:
    """Convert SCCs to sorted nested lists for deterministic snapshot comparison."""
    return [sorted(scc) for scc in sccs]


def test_scc_empty_graph() -> None:
    """Empty graph."""
    graph: dict[tuple[str, ...], set[tuple[str, ...]]] = {}
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([])


def test_scc_single_node_without_edges() -> None:
    """Graph: a (isolated)."""
    graph = {("a",): set()}
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("a",)]])


def test_scc_single_node_with_self_loop() -> None:
    """Graph: a -> a."""
    graph = {("a",): {("a",)}}
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("a",)]])


def test_scc_bidirectional_edge_pair() -> None:
    """Graph: a <-> b."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("a",)},
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("a",), ("b",)]])


def test_scc_triangular_cycle() -> None:
    """Graph: a -> b -> c -> a."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("c",)},
        ("c",): {("a",)},
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("a",), ("b",), ("c",)]])


def test_scc_linear_chain() -> None:
    """Graph: a -> b -> c (acyclic)."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("c",)},
        ("c",): set(),
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("c",)], [("b",)], [("a",)]])


def test_scc_two_independent_cycles() -> None:
    """Graph: a <-> b / x <-> y (disconnected)."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("a",)},
        ("x",): {("y",)},
        ("y",): {("x",)},
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("a",), ("b",)], [("x",), ("y",)]])


def test_scc_edge_only_node() -> None:
    """Graph: a -> b (b only referenced as edge target)."""
    graph = {
        ("a",): {("b",)},
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("b",)], [("a",)]])


def test_scc_nested_cycle() -> None:
    """Graph: a -> b,d / b <-> c / d (isolated)."""
    graph = {
        ("a",): {("b",), ("d",)},
        ("b",): {("c",)},
        ("c",): {("b",)},
        ("d",): set(),
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("b",), ("c",)], [("d",)], [("a",)]])


def test_scc_deterministic_results() -> None:
    """Graph: z <-> y / a <-> b (verify determinism across 5 runs)."""
    graph = {
        ("z",): {("y",)},
        ("y",): {("z",)},
        ("a",): {("b",)},
        ("b",): {("a",)},
    }
    results = [_to_sorted_result(strongly_connected_components(graph)) for _ in range(5)]
    for i in range(1, 5):
        assert results[i] == results[0]


def test_scc_phase1_multiple_unvisited_neighbors() -> None:
    """Graph: a -> b,c,d / b -> a / c -> a / d (isolated)."""
    graph = {
        ("a",): {("b",), ("c",), ("d",)},
        ("b",): {("a",)},
        ("c",): {("a",)},
        ("d",): set(),
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("d",)], [("a",), ("b",), ("c",)]])


def test_scc_phase1_on_stack_neighbor() -> None:
    """Graph: a -> b -> c,d / c -> a / d -> b."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("c",), ("d",)},
        ("c",): {("a",)},
        ("d",): {("b",)},
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("a",), ("b",), ("c",), ("d",)]])


def test_scc_deep_graph_iterative() -> None:
    """100-node chain with terminal cycle n98 <-> n99."""
    graph: dict[tuple[str, ...], set[tuple[str, ...]]] = {}
    for i in range(99):
        graph[f"n{i}",] = {(f"n{i + 1}",)}
    graph["n99",] = {("n98",)}

    result = strongly_connected_components(graph)
    multi_node_sccs = [scc for scc in result if len(scc) > 1]
    assert _to_sorted_result(multi_node_sccs) == snapshot([[("n98",), ("n99",)]])


def test_scc_realistic_module_path_tuples() -> None:
    """Graph: (pkg, __init__) <-> (pkg, issuing)."""
    graph = {
        ("pkg", "__init__"): {("pkg", "issuing")},
        ("pkg", "issuing"): {("pkg", "__init__")},
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([
        [("pkg", "__init__"), ("pkg", "issuing")]
    ])


def test_scc_phase0_skips_indexed_neighbors() -> None:
    """Graph: a -> b,c / b -> c / c (isolated)."""
    graph = {
        ("a",): {("b",), ("c",)},
        ("b",): {("c",)},
        ("c",): set(),
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("c",)], [("b",)], [("a",)]])


def test_scc_phase1_scc_root_detection() -> None:
    """Graph: a -> b,c / b -> d / c -> d / d -> a."""
    graph = {
        ("a",): {("b",), ("c",)},
        ("b",): {("d",)},
        ("c",): {("d",)},
        ("d",): {("a",)},
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("a",), ("b",), ("c",), ("d",)]])


def test_scc_phase1_later_on_stack_neighbor() -> None:
    """Graph: a -> b,c,d / b -> c / c -> a / d (isolated)."""
    graph = {
        ("a",): {("b",), ("c",), ("d",)},
        ("b",): {("c",)},
        ("c",): {("a",)},
        ("d",): set(),
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("d",)], [("a",), ("b",), ("c",)]])


def test_scc_phase0_visited_not_on_stack_neighbor() -> None:
    """Graph: a -> x / b -> a,x / x (isolated)."""
    graph = {
        ("a",): {("x",)},
        ("b",): {("a",), ("x",)},
        ("x",): set(),
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("x",)], [("a",)], [("b",)]])


def test_scc_phase0_exhausts_neighbors_finds_root() -> None:
    """Graph: a -> b / b (isolated)."""
    graph = {
        ("a",): {("b",)},
        ("b",): set(),
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("b",)], [("a",)]])


def test_scc_multi_node_scc_pops_all_members() -> None:
    """Graph: a -> b -> c -> d -> a (4-node cycle)."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("c",)},
        ("c",): {("d",)},
        ("d",): {("a",)},
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([[("a",), ("b",), ("c",), ("d",)]])


def test_scc_phase1_extraction_with_multiple_pops() -> None:
    """Graph: a -> b -> c -> d -> e -> a (5-node cycle via phase 1 return)."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("c",)},
        ("c",): {("d",)},
        ("d",): {("e",)},
        ("e",): {("a",)},
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([
        [("a",), ("b",), ("c",), ("d",), ("e",)]
    ])


def test_scc_phase1_multiple_returns_in_call_stack() -> None:
    """Graph: a -> b,c / b -> d / c -> d / d -> e / e -> a."""
    graph = {
        ("a",): {("b",), ("c",)},
        ("b",): {("d",)},
        ("c",): {("d",)},
        ("d",): {("e",)},
        ("e",): {("a",)},
    }
    assert _to_sorted_result(strongly_connected_components(graph)) == snapshot([
        [("a",), ("b",), ("c",), ("d",), ("e",)]
    ])


def test_circular_empty_graph() -> None:
    """Empty graph."""
    graph: dict[tuple[str, ...], set[tuple[str, ...]]] = {}
    assert _to_sorted_result(find_circular_sccs(graph)) == snapshot([])


def test_circular_acyclic_graph() -> None:
    """Graph: a -> b -> c (acyclic)."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("c",)},
        ("c",): set(),
    }
    assert _to_sorted_result(find_circular_sccs(graph)) == snapshot([])


def test_circular_self_loop_detected() -> None:
    """Graph: a -> a."""
    graph = {("a",): {("a",)}}
    assert _to_sorted_result(find_circular_sccs(graph)) == snapshot([[("a",)]])


def test_circular_single_node_without_self_loop() -> None:
    """Graph: a (isolated)."""
    graph = {("a",): set()}
    assert _to_sorted_result(find_circular_sccs(graph)) == snapshot([])


def test_circular_bidirectional_pair_detected() -> None:
    """Graph: a <-> b."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("a",)},
    }
    assert _to_sorted_result(find_circular_sccs(graph)) == snapshot([[("a",), ("b",)]])


def test_circular_multiple_independent_cycles_detected() -> None:
    """Graph: a <-> b / x <-> y (disconnected)."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("a",)},
        ("x",): {("y",)},
        ("y",): {("x",)},
    }
    assert _to_sorted_result(find_circular_sccs(graph)) == snapshot([[("a",), ("b",)], [("x",), ("y",)]])


def test_circular_results_sorted_by_minimum_element() -> None:
    """Graph: z <-> y / a <-> b (verify sorted by min element)."""
    graph = {
        ("z",): {("y",)},
        ("y",): {("z",)},
        ("a",): {("b",)},
        ("b",): {("a",)},
    }
    result = find_circular_sccs(graph)
    assert min(result[0]) < min(result[1])
    assert _to_sorted_result(result) == snapshot([[("a",), ("b",)], [("y",), ("z",)]])


def test_circular_filters_acyclic_sccs() -> None:
    """Graph: a <-> b / c -> d (mixed cyclic and acyclic)."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("a",)},
        ("c",): {("d",)},
        ("d",): set(),
    }
    assert _to_sorted_result(find_circular_sccs(graph)) == snapshot([[("a",), ("b",)]])


def test_circular_edge_only_node_not_circular() -> None:
    """Graph: a -> b (b only referenced as edge)."""
    graph = {
        ("a",): {("b",)},
    }
    assert _to_sorted_result(find_circular_sccs(graph)) == snapshot([])


def test_circular_stripe_api_like_pattern() -> None:
    """Graph: () <-> (issuing,)."""
    graph = {
        (): {("issuing",)},
        ("issuing",): {()},
    }
    assert _to_sorted_result(find_circular_sccs(graph)) == snapshot([[(), ("issuing",)]])


def test_circular_triangular_cycle_with_external_edge() -> None:
    """Graph: a -> b,x / b -> c / c -> a / x (isolated)."""
    graph = {
        ("a",): {("b",), ("x",)},
        ("b",): {("c",)},
        ("c",): {("a",)},
        ("x",): set(),
    }
    assert _to_sorted_result(find_circular_sccs(graph)) == snapshot([[("a",), ("b",), ("c",)]])


def test_circular_iteration_over_multiple_sccs() -> None:
    """Graph: a <-> b / c (isolated) / d -> d / e -> f -> g -> e."""
    graph = {
        ("a",): {("b",)},
        ("b",): {("a",)},
        ("c",): set(),
        ("d",): {("d",)},
        ("e",): {("f",)},
        ("f",): {("g",)},
        ("g",): {("e",)},
    }
    result = find_circular_sccs(graph)
    sizes = sorted([len(scc) for scc in result])
    assert sizes == snapshot([1, 2, 3])
    assert _to_sorted_result(result) == snapshot([[("a",), ("b",)], [("d",)], [("e",), ("f",), ("g",)]])


def test_circular_many_single_node_sccs_with_self_loops() -> None:
    """Graph: a -> a / b -> b / c -> c / d -> d (multiple self-loop SCCs)."""
    graph = {
        ("a",): {("a",)},
        ("b",): {("b",)},
        ("c",): {("c",)},
        ("d",): {("d",)},
    }
    result = find_circular_sccs(graph)
    assert len(result) == snapshot(4)
    assert _to_sorted_result(result) == snapshot([[("a",)], [("b",)], [("c",)], [("d",)]])


def test_circular_mixed_scc_sizes_iteration() -> None:
    """Graph: a (isolated) / b -> b / c <-> d / e -> f -> g -> h -> e."""
    graph = {
        ("a",): set(),
        ("b",): {("b",)},
        ("c",): {("d",)},
        ("d",): {("c",)},
        ("e",): {("f",)},
        ("f",): {("g",)},
        ("g",): {("h",)},
        ("h",): {("e",)},
    }
    result = find_circular_sccs(graph)
    assert len(result) == snapshot(3)
    sizes = sorted([len(scc) for scc in result])
    assert sizes == snapshot([1, 2, 4])