Spaces:
Build error
Build error
File size: 1,539 Bytes
3382f47 |
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 |
from agbenchmark.utils.dependencies.graphs import is_circular
def test_is_circular():
cyclic_graph = {
"nodes": [
{"id": "A", "data": {"category": []}},
{"id": "B", "data": {"category": []}},
{"id": "C", "data": {"category": []}},
{"id": "D", "data": {"category": []}}, # New node
],
"edges": [
{"from": "A", "to": "B"},
{"from": "B", "to": "C"},
{"from": "C", "to": "D"},
{"from": "D", "to": "A"}, # This edge creates a cycle
],
}
result = is_circular(cyclic_graph)
assert result is not None, "Expected a cycle, but none was detected"
assert all(
(
(result[i], result[i + 1])
in [(x["from"], x["to"]) for x in cyclic_graph["edges"]]
)
for i in range(len(result) - 1)
), "The detected cycle path is not part of the graph's edges"
def test_is_not_circular():
acyclic_graph = {
"nodes": [
{"id": "A", "data": {"category": []}},
{"id": "B", "data": {"category": []}},
{"id": "C", "data": {"category": []}},
{"id": "D", "data": {"category": []}}, # New node
],
"edges": [
{"from": "A", "to": "B"},
{"from": "B", "to": "C"},
{"from": "C", "to": "D"},
# No back edge from D to any node, so it remains acyclic
],
}
assert is_circular(acyclic_graph) is None, "Detected a cycle in an acyclic graph"
|