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

Commit 948021b

Browse files
yt-msMidnighter
authored andcommitted
refactor: move model lookups out of instance hyrdation and into DeploymentNode
1 parent 575fc94 commit 948021b

File tree

6 files changed

+27
-41
lines changed

6 files changed

+27
-41
lines changed

src/structurizr/model/container_instance.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
if TYPE_CHECKING: # pragma: no cover
3232
from .deployment_node import DeploymentNode
33-
from .model import Model
3433

3534

3635
__all__ = ("ContainerInstance", "ContainerInstanceIO")
@@ -64,18 +63,16 @@ def container_id(self) -> str:
6463
def hydrate(
6564
cls,
6665
container_instance_io: ContainerInstanceIO,
67-
model: "Model",
66+
container: Container,
6867
parent: "DeploymentNode",
6968
) -> "ContainerInstance":
7069
"""Hydrate a new ContainerInstance instance from its IO.
7170
7271
This will also automatically register with the model.
7372
"""
74-
container = model.get_element(container_instance_io.container_id)
7573
instance = cls(
7674
**cls.hydrate_arguments(container_instance_io),
7775
container=container,
7876
parent=parent,
7977
)
80-
model += instance
8178
return instance

src/structurizr/model/deployment_node.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,16 @@ def hydrate(
302302
node += child_node
303303

304304
for instance_io in deployment_node_io.container_instances:
305-
instance = ContainerInstance.hydrate(instance_io, model=model, parent=node)
305+
container = model.get_element(instance_io.container_id)
306+
instance = ContainerInstance.hydrate(
307+
instance_io, container=container, parent=node
308+
)
306309
node._container_instances.add(instance)
307310

308311
for instance_io in deployment_node_io.software_system_instances:
312+
system = model.get_element(instance_io.software_system_id)
309313
instance = SoftwareSystemInstance.hydrate(
310-
instance_io, model=model, parent=node
314+
instance_io, system=system, parent=node
311315
)
312316
node._software_system_instances.add(instance)
313317

src/structurizr/model/software_system_instance.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
if TYPE_CHECKING: # pragma: no cover
3232
from .deployment_node import DeploymentNode
33-
from .model import Model
3433

3534

3635
__all__ = ("SoftwareSystemInstance", "SoftwareSystemInstanceIO")
@@ -64,11 +63,10 @@ def software_system_id(self) -> str:
6463
def hydrate(
6564
cls,
6665
system_instance_io: SoftwareSystemInstanceIO,
67-
model: "Model",
66+
system: SoftwareSystem,
6867
parent: "DeploymentNode",
6968
) -> "SoftwareSystemInstance":
7069
"""Hydrate a new SoftwareSystemInstance instance from its IO."""
71-
system = model.get_element(system_instance_io.software_system_id)
7270
instance = cls(
7371
**cls.hydrate_arguments(system_instance_io),
7472
software_system=system,

tests/unit/model/test_container_instance.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,13 @@ def test_container_instance_tags(model_with_container):
8383
assert Tags.CONTAINER_INSTANCE in instance.tags
8484

8585

86-
def test_container_instance_hydration_retrieves_container_from_id(model_with_container):
87-
"""Check that the container instance is retrieved from the model on hydration."""
88-
io = ContainerInstanceIO(container_id="19", instance_id=3, environment="Live")
89-
90-
instance = ContainerInstance.hydrate(io, model_with_container, parent=None)
91-
92-
assert instance.instance_id == 3
93-
assert instance.environment == "Live"
94-
assert instance.container is model_with_container.mock_container
95-
assert instance.container_id == "19"
96-
97-
9886
def test_container_instance_name_is_container_name(model_with_container):
9987
"""Ensure container instance takes its name from its container."""
10088
io = ContainerInstanceIO(container_id="19", instance_id=3, environment="Live")
10189

102-
instance = ContainerInstance.hydrate(io, model_with_container, parent=None)
90+
instance = ContainerInstance.hydrate(
91+
io, model_with_container.mock_container, parent=None
92+
)
10393

10494
assert instance.name == "Mock Container"
10595

@@ -114,7 +104,9 @@ def test_container_instance_hyrdates_http_health_checks(model_with_container):
114104
health_checks=[health_check_io],
115105
)
116106

117-
instance = ContainerInstance.hydrate(io, model_with_container, parent=None)
107+
instance = ContainerInstance.hydrate(
108+
io, model_with_container.mock_container, parent=None
109+
)
118110

119111
assert len(instance.health_checks) == 1
120112
assert list(instance.health_checks)[0].name == "name"

tests/unit/model/test_model.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ def test_model_get_software_system_bad_id(empty_model: Model):
165165
assert empty_model.get_software_system_with_id(container.id) is None
166166

167167

168+
def test_model_add_element_twice_is_ignored(empty_model: Model):
169+
"""Test you can't add an element with the same ID as an existing one."""
170+
system1 = empty_model.add_software_system(name="System")
171+
empty_model += system1
172+
assert empty_model.software_systems == {system1}
173+
174+
168175
def test_model_add_element_with_existing_id_raises_error(empty_model: Model):
169176
"""Test you can't add an element with the same ID as an existing one."""
170177
system1 = empty_model.add_software_system(name="System")

tests/unit/model/test_software_system_instance.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,29 +86,15 @@ def test_software_system_instance_tags(model_with_system):
8686
assert Tags.SOFTWARE_SYSTEM_INSTANCE in node.tags
8787

8888

89-
def test_software_system_instance_hydration_retrieves_software_system_from_id(
90-
model_with_system,
91-
):
92-
"""Check that the software_system instance is retrieved on hydration."""
93-
io = SoftwareSystemInstanceIO(
94-
software_system_id="19", instance_id=3, environment="Live"
95-
)
96-
97-
instance = SoftwareSystemInstance.hydrate(io, model_with_system, parent=None)
98-
99-
assert instance.instance_id == 3
100-
assert instance.environment == "Live"
101-
assert instance.software_system is model_with_system.mock_system
102-
assert instance.software_system_id == "19"
103-
104-
10589
def test_software_system_instance_name_is_software_system_name(model_with_system):
10690
"""Ensure software_system instance takes its name from its software system."""
10791
io = SoftwareSystemInstanceIO(
10892
software_system_id="19", instance_id=3, environment="Live"
10993
)
11094

111-
instance = SoftwareSystemInstance.hydrate(io, model_with_system, parent=None)
95+
instance = SoftwareSystemInstance.hydrate(
96+
io, model_with_system.mock_system, parent=None
97+
)
11298

11399
assert instance.name == "Mock System"
114100

@@ -123,7 +109,9 @@ def test_software_system_instance_hyrdates_http_health_checks(model_with_system)
123109
health_checks=[health_check_io],
124110
)
125111

126-
instance = SoftwareSystemInstance.hydrate(io, model_with_system, parent=None)
112+
instance = SoftwareSystemInstance.hydrate(
113+
io, model_with_system.mock_system, parent=None
114+
)
127115

128116
assert len(instance.health_checks) == 1
129117
assert list(instance.health_checks)[0].name == "name"

0 commit comments

Comments
 (0)