Skip to content
This repository was archived by the owner on Jan 21, 2023. It is now read-only.

Commit 4781632

Browse files
yt-msMidnighter
authored andcommitted
refactor: add child_elements property to Element
1 parent d4bbd9a commit 4781632

File tree

8 files changed

+55
-0
lines changed

8 files changed

+55
-0
lines changed

src/structurizr/model/container.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ def components(self) -> Iterable[Component]:
107107
"""Return read-only list of child components."""
108108
return list(self._components)
109109

110+
@property
111+
def child_elements(self) -> Iterable[Component]:
112+
"""Return child elements (from `Element.children`)."""
113+
return self.components
114+
110115
@classmethod
111116
def hydrate(
112117
cls,

src/structurizr/model/deployment_node.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .container import Container
2323
from .container_instance import ContainerInstance, ContainerInstanceIO
2424
from .deployment_element import DeploymentElement, DeploymentElementIO
25+
from .element import Element
2526
from .infrastructure_node import InfrastructureNode, InfrastructureNodeIO
2627
from .software_system import SoftwareSystem
2728
from .software_system_instance import SoftwareSystemInstance, SoftwareSystemInstanceIO
@@ -118,6 +119,16 @@ def children(self) -> Iterable["DeploymentNode"]:
118119
"""Return read-only list of child nodes."""
119120
return list(self._children)
120121

122+
@property
123+
def child_elements(self) -> Iterable[Element]:
124+
"""Return child elements (from `Element.children`)."""
125+
return (
126+
self.children
127+
+ self.container_instances
128+
+ self.software_system_instances
129+
+ self.infrastructure_nodes
130+
)
131+
121132
@property
122133
def container_instances(self) -> Iterable[ContainerInstance]:
123134
"""Return read-only list of container instances."""

src/structurizr/model/element.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ def __repr__(self):
8282
"""Return a string representation of this instance."""
8383
return f"{type(self).__name__}(id={self.id}, name={self.name})"
8484

85+
@property
86+
def child_elements(self) -> Iterable["Element"]:
87+
"""Return the elements that are children of this one.
88+
89+
Subclasses should override this to provide their specific children.
90+
"""
91+
return []
92+
8593
def get_relationships(self) -> Iterator[Relationship]:
8694
"""Return a Iterator over all relationships involving this element."""
8795
return (

src/structurizr/model/software_system.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ def containers(self) -> Iterable[Container]:
7979
"""Return read-only list of child containers."""
8080
return list(self._containers)
8181

82+
@property
83+
def child_elements(self) -> Iterable[Container]:
84+
"""Return child elements (from `Element.children`)."""
85+
return self.containers
86+
8287
def add_container(
8388
self, name: str, description: str = "", technology: str = "", **kwargs
8489
) -> Container:

tests/unit/model/test_container.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ def test_container_add_component_adds_to_component_list(
6767
assert component.parent is empty_container
6868

6969

70+
def test_container_child_elements_property(model_with_container: MockModel):
71+
"""Verify children property works."""
72+
empty_container = model_with_container.empty_container
73+
component = empty_container.add_component(name="Component")
74+
assert component in empty_container.child_elements
75+
76+
7077
def test_container_add_constructed_component(model_with_container: MockModel):
7178
"""Verify behaviour when adding a newly constructed Container."""
7279
empty_container = model_with_container.empty_container

tests/unit/model/test_deployment_node.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,19 @@ def test_deployment_node_add_with_iadd(model_with_node: MockModel):
158158

159159
node += child_node
160160
assert child_node in node.children
161+
assert child_node in node.child_elements
161162

162163
node += system
163164
assert node.software_system_instances[0].software_system is system
165+
assert node.software_system_instances[0] in node.child_elements
164166

165167
child_node += container
166168
assert child_node.container_instances[0].container is container
169+
assert child_node.container_instances[0] in child_node.child_elements
167170

168171
child_node += infra_node
169172
assert infra_node in child_node.infrastructure_nodes
173+
assert child_node.infrastructure_nodes[0] in child_node.child_elements
170174

171175

172176
def test_deployment_node_serialising_container(model_with_node):

tests/unit/model/test_element.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def test_model_reference():
5757
assert element.get_model() is model
5858

5959

60+
def test_element_child_elements_default():
61+
"""Ensure that by default, element has no children."""
62+
element = ConcreteElement(name="Element")
63+
assert element.child_elements == []
64+
65+
6066
def test_element_can_only_add_relationship_to_source():
6167
"""Make sure that nothing adds a relationship to the wrong element."""
6268
element1 = ConcreteElement(name="elt1")

tests/unit/model/test_software_system.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ def test_software_system_add_container_adds_to_container_list(
8484
assert container.parent is empty_system
8585

8686

87+
def test_software_system_child_elements_property(
88+
model_with_system: MockModel,
89+
):
90+
"""Ensure that children propery returns containers."""
91+
empty_system = model_with_system.empty_system
92+
container = empty_system.add_container(name="Container", description="Description")
93+
assert container in empty_system.child_elements
94+
95+
8796
def test_software_system_add_constructed_container(model_with_system: MockModel):
8897
"""Verify behaviour when adding a newly constructed Container."""
8998
empty_system = model_with_system.empty_system

0 commit comments

Comments
 (0)