Refactored instances management
This commit is contained in:
@@ -311,7 +311,7 @@ class TestFromParentChildList:
|
||||
nodes, edges = from_parent_child_list(items)
|
||||
|
||||
assert len(nodes) == 1
|
||||
assert nodes[0] == {"id": "root", "label": "Root"}
|
||||
assert nodes[0] == {'color': '#ff9999', 'id': 'root', 'label': 'Root'}
|
||||
assert len(edges) == 0
|
||||
|
||||
def test_i_can_convert_simple_parent_child_relationship(self):
|
||||
@@ -323,7 +323,7 @@ class TestFromParentChildList:
|
||||
nodes, edges = from_parent_child_list(items)
|
||||
|
||||
assert len(nodes) == 2
|
||||
assert {"id": "root", "label": "Root"} in nodes
|
||||
assert {'color': '#ff9999', 'id': 'root', 'label': 'Root'} in nodes
|
||||
assert {"id": "child", "label": "Child"} in nodes
|
||||
|
||||
assert len(edges) == 1
|
||||
@@ -513,3 +513,136 @@ class TestFromParentChildList:
|
||||
|
||||
ghost_node = [n for n in nodes if n["id"] == "ghost_parent"][0]
|
||||
assert ghost_node["label"] == "ghost_parent"
|
||||
|
||||
def test_i_can_apply_root_color_to_single_root(self):
|
||||
"""Test that a single root node receives the root_color."""
|
||||
items = [{"id": "root", "label": "Root"}]
|
||||
nodes, edges = from_parent_child_list(items, root_color="#ff0000")
|
||||
|
||||
assert len(nodes) == 1
|
||||
assert nodes[0]["color"] == "#ff0000"
|
||||
|
||||
def test_i_can_apply_root_color_to_multiple_roots(self):
|
||||
"""Test root_color is assigned to all nodes without parent."""
|
||||
items = [
|
||||
{"id": "root1", "label": "Root 1"},
|
||||
{"id": "root2", "label": "Root 2"},
|
||||
{"id": "child", "parent": "root1", "label": "Child"}
|
||||
]
|
||||
nodes, edges = from_parent_child_list(items, root_color="#aa0000")
|
||||
|
||||
root_nodes = [n for n in nodes if n["id"] in ("root1", "root2")]
|
||||
assert all(n.get("color") == "#aa0000" for n in root_nodes)
|
||||
|
||||
# child must NOT have root_color
|
||||
child_node = next(n for n in nodes if n["id"] == "child")
|
||||
assert "color" not in child_node
|
||||
|
||||
def test_i_can_handle_root_with_parent_none(self):
|
||||
"""Test that root_color is applied when parent=None."""
|
||||
items = [
|
||||
{"id": "r1", "parent": None, "label": "R1"}
|
||||
]
|
||||
nodes, edges = from_parent_child_list(items, root_color="#112233")
|
||||
|
||||
assert nodes[0]["color"] == "#112233"
|
||||
|
||||
def test_i_can_handle_root_with_parent_empty_string(self):
|
||||
"""Test that root_color is applied when parent=''."""
|
||||
items = [
|
||||
{"id": "r1", "parent": "", "label": "R1"}
|
||||
]
|
||||
nodes, edges = from_parent_child_list(items, root_color="#334455")
|
||||
|
||||
assert nodes[0]["color"] == "#334455"
|
||||
|
||||
def test_i_do_not_apply_root_color_to_non_roots(self):
|
||||
"""Test that only real roots receive root_color."""
|
||||
items = [
|
||||
{"id": "root", "label": "Root"},
|
||||
{"id": "child", "parent": "root", "label": "Child"}
|
||||
]
|
||||
nodes, edges = from_parent_child_list(items, root_color="#ff0000")
|
||||
|
||||
# Only one root → only this one has the color
|
||||
root_node = next(n for n in nodes if n["id"] == "root")
|
||||
assert root_node["color"] == "#ff0000"
|
||||
|
||||
child_node = next(n for n in nodes if n["id"] == "child")
|
||||
assert "color" not in child_node
|
||||
|
||||
def test_i_do_not_override_ghost_color_with_root_color(self):
|
||||
"""Ghost nodes must keep ghost_color, not root_color."""
|
||||
items = [
|
||||
{"id": "child", "parent": "ghost_parent", "label": "Child"}
|
||||
]
|
||||
nodes, edges = from_parent_child_list(
|
||||
items,
|
||||
root_color="#ff0000",
|
||||
ghost_color="#00ff00"
|
||||
)
|
||||
|
||||
ghost_node = next(n for n in nodes if n["id"] == "ghost_parent")
|
||||
assert ghost_node["color"] == "#00ff00"
|
||||
|
||||
# child is not root → no color
|
||||
child_node = next(n for n in nodes if n["id"] == "child")
|
||||
assert "color" not in child_node
|
||||
|
||||
def test_i_can_use_custom_root_color(self):
|
||||
"""Test that a custom root_color is applied instead of default."""
|
||||
items = [{"id": "root", "label": "Root"}]
|
||||
nodes, edges = from_parent_child_list(items, root_color="#123456")
|
||||
|
||||
assert nodes[0]["color"] == "#123456"
|
||||
|
||||
def test_i_can_mix_root_nodes_and_ghost_nodes(self):
|
||||
"""Ensure root_color applies only to roots and ghost nodes keep ghost_color."""
|
||||
items = [
|
||||
{"id": "root", "label": "Root"},
|
||||
{"id": "child", "parent": "ghost_parent", "label": "Child"}
|
||||
]
|
||||
nodes, edges = from_parent_child_list(
|
||||
items,
|
||||
root_color="#ff0000",
|
||||
ghost_color="#00ff00"
|
||||
)
|
||||
|
||||
root_node = next(n for n in nodes if n["id"] == "root")
|
||||
ghost_node = next(n for n in nodes if n["id"] == "ghost_parent")
|
||||
|
||||
assert root_node["color"] == "#ff0000"
|
||||
assert ghost_node["color"] == "#00ff00"
|
||||
|
||||
def test_i_do_not_mark_node_as_root_if_parent_field_exists(self):
|
||||
"""Node with parent key but non-empty value should NOT get root_color."""
|
||||
items = [
|
||||
{"id": "root", "label": "Root"},
|
||||
{"id": "child", "parent": "root", "label": "Child"},
|
||||
{"id": "other", "parent": "unknown_parent", "label": "Other"}
|
||||
]
|
||||
nodes, edges = from_parent_child_list(
|
||||
items,
|
||||
root_color="#ff0000",
|
||||
ghost_color="#00ff00"
|
||||
)
|
||||
|
||||
# "root" is the only real root
|
||||
root_node = next(n for n in nodes if n["id"] == "root")
|
||||
assert root_node["color"] == "#ff0000"
|
||||
|
||||
# "other" is NOT root, even though its parent is missing
|
||||
other_node = next(n for n in nodes if n["id"] == "other")
|
||||
assert "color" not in other_node
|
||||
|
||||
# ghost parent must have ghost_color
|
||||
ghost_node = next(n for n in nodes if n["id"] == "unknown_parent")
|
||||
assert ghost_node["color"] == "#00ff00"
|
||||
|
||||
def test_i_do_no_add_root_color_when_its_none(self):
|
||||
"""Test that a single root node receives the root_color."""
|
||||
items = [{"id": "root", "label": "Root"}]
|
||||
nodes, edges = from_parent_child_list(items, root_color=None)
|
||||
|
||||
assert len(nodes) == 1
|
||||
assert "color" not in nodes[0]
|
||||
|
||||
Reference in New Issue
Block a user