mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Add Tree.get_node_by_id
The thinking here is that a user of a Tree may want to relate notes to other parts of their UI, or other data in their application. While this could be done by keeping a reference to the node itself, it could also be beneficial to just track the ID. Given that ID is a public property of a TreeNode, but given it doesn't currently have any other (public) purpose, this seems to add some useful symmetry.
This commit is contained in:
16
tests/tree/test_tree_get_node_by_id.py
Normal file
16
tests/tree/test_tree_get_node_by_id.py
Normal file
@@ -0,0 +1,16 @@
|
||||
import pytest
|
||||
from typing import cast
|
||||
from textual.widgets import Tree
|
||||
from textual.widgets._tree import NodeID
|
||||
|
||||
|
||||
def test_get_tree_node_by_id() -> None:
|
||||
"""It should be possible to get a TreeNode by its ID."""
|
||||
tree = Tree[None]("Anakin")
|
||||
child = tree.root.add("Leia")
|
||||
grandchild = child.add("Ben")
|
||||
assert tree.get_node_by_id(tree.root.id).id == tree.root.id
|
||||
assert tree.get_node_by_id(child.id).id == child.id
|
||||
assert tree.get_node_by_id(grandchild.id).id == grandchild.id
|
||||
with pytest.raises(KeyError):
|
||||
tree.get_node_by_id(cast(NodeID, grandchild.id + 1000))
|
||||
Reference in New Issue
Block a user