Skip to content

Commit

Permalink
Implement and test the root property
Browse files Browse the repository at this point in the history
  • Loading branch information
liamhuber committed Jan 16, 2024
1 parent 197497c commit 874aaff
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pyiron_workflow/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,11 @@ def parent(self, new_parent: Composite | None) -> None:
"parent"
)

@property
def root(self) -> Node:
"""The parent-most node in this graph."""
return self if self.parent is None else self.parent.root

@property
def readiness_report(self) -> str:
input_readiness = "\n".join(
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,34 @@ def test_de_activate_strict_connections(self):
msg="Activating should propagate to children"
)

def test_root(self):
top = AComposite("topmost")
top.middle_composite = AComposite("middle_composite")
top.middle_composite.deep_node = Composite.create.SingleValue(plus_one)
top.middle_function = Composite.create.SingleValue(plus_one)

self.assertIs(
top,
top.root,
msg="The parent-most node should be its own root."
)
self.assertIs(
top,
top.middle_composite.root,
msg="The parent-most node should be the root."
)
self.assertIs(
top,
top.middle_function.root,
msg="The parent-most node should be the root."
)
self.assertIs(
top,
top.middle_composite.deep_node.root,
msg="The parent-most node should be the root, recursively accessible from "
"all depths."
)


if __name__ == '__main__':
unittest.main()
8 changes: 8 additions & 0 deletions tests/unit/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,14 @@ def test_run_after_init(self):
msg="With run_after_init, the node should run right away"
)

def test_root(self):
n = ANode("n")
self.assertIs(
n,
n.root,
msg="Lone nodes should be their own root, as there is no parent above."
)


if __name__ == '__main__':
unittest.main()

0 comments on commit 874aaff

Please sign in to comment.