Skip to content

Commit a59cb70

Browse files
yt-msMidnighter
authored andcommitted
feat(DeploymentNode): add containers and systems with +=
1 parent c7c3066 commit a59cb70

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/structurizr/model/deployment_node.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
"""Provide a deployment node model."""
1717

18-
from typing import TYPE_CHECKING, Iterable, List
18+
from typing import TYPE_CHECKING, Iterable, List, Union
1919

2020
from pydantic import Field
2121

@@ -148,7 +148,7 @@ def add_deployment_node(
148148
node = DeploymentNode(
149149
name=name, description=description, technology=technology, **kwargs
150150
)
151-
self += node
151+
self._add_child_deployment_node(node)
152152
return node
153153

154154
def add_container(
@@ -231,8 +231,20 @@ def add_infrastructure_node(self, name: str, **kwargs) -> InfrastructureNode:
231231
model += infra_node
232232
return infra_node
233233

234-
def __iadd__(self, node: "DeploymentNode") -> "DeploymentNode":
235-
"""Add a newly constructed chile deployment node to this node."""
234+
def __iadd__(
235+
self, child: Union["DeploymentNode", Container, SoftwareSystem]
236+
) -> "DeploymentNode":
237+
"""Add a sub-node, container or system to this node."""
238+
if isinstance(child, SoftwareSystem):
239+
self.add_software_system(child)
240+
elif isinstance(child, Container):
241+
self.add_container(child)
242+
else:
243+
self._add_child_deployment_node(child)
244+
return self
245+
246+
def _add_child_deployment_node(self, node: "DeploymentNode"):
247+
"""Add a newly constructed child deployment node to this node."""
236248
if node in self._children:
237249
return self
238250

@@ -252,7 +264,6 @@ def __iadd__(self, node: "DeploymentNode") -> "DeploymentNode":
252264
self._children.add(node)
253265
model = self.model
254266
model += node
255-
return self
256267

257268
@classmethod
258269
def hydrate(

tests/unit/model/test_deployment_node.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import pytest
1818

19+
from structurizr.model import Container, SoftwareSystem
1920
from structurizr.model.deployment_node import DeploymentNode, DeploymentNodeIO
2021

2122

@@ -40,6 +41,10 @@ def get_element(self, id: str):
4041
assert id == self.mock_element.id
4142
return self.mock_element
4243

44+
def get_elements(self):
45+
"""Simulate get_elements."""
46+
return []
47+
4348

4449
class MockElement:
4550
"""Implement a mock element for testing."""
@@ -140,6 +145,25 @@ def test_deployment_node_add_container(model_with_node):
140145
assert instance.instance_id == 1
141146

142147

148+
def test_deployment_node_add_with_iadd(model_with_node: MockModel):
149+
"""Test adding things to a node using += rather than add_container."""
150+
node = model_with_node.empty_node
151+
system = SoftwareSystem(name="system")
152+
model_with_node += system
153+
container = Container(name="container")
154+
system += container
155+
child_node = DeploymentNode(name="child")
156+
157+
node += child_node
158+
assert child_node in node.children
159+
160+
node += system
161+
assert node.software_system_instances[0].software_system is system
162+
163+
child_node += container
164+
assert child_node.container_instances[0].container is container
165+
166+
143167
def test_deployment_node_serialising_container(model_with_node):
144168
"""Test serialisation and deserialisation includes container instances."""
145169
node = model_with_node.empty_node

0 commit comments

Comments
 (0)