Skip to content

Commit 14599b3

Browse files
yt-msMidnighter
authored andcommitted
test: add tests around adding containers to systems ahead of some refactoring.
1 parent 5ac343f commit 14599b3

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

src/structurizr/model/container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Container(StaticStructureElement):
7979
def __init__(
8080
self,
8181
*,
82-
parent: "SoftwareSystem",
82+
parent: "SoftwareSystem" = None,
8383
technology: str = "",
8484
components: Iterable[Component] = (),
8585
**kwargs,

tests/unit/model/test_software_system.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import pytest
2020

2121
from structurizr.model.model import Model
22+
from structurizr.model.container import Container
2223
from structurizr.model.software_system import SoftwareSystem
2324

2425

@@ -28,6 +29,12 @@ def empty_model() -> Model:
2829
return Model()
2930

3031

32+
@pytest.fixture(scope="function")
33+
def empty_system() -> SoftwareSystem:
34+
"""Provide an empty SoftwareSystem on demand for test cases to use."""
35+
return Model().add_software_system("Sys")
36+
37+
3138
@pytest.mark.parametrize(
3239
"attributes",
3340
[
@@ -55,3 +62,52 @@ def test_add_container_technology_is_optional(empty_model: Model):
5562
system = empty_model.add_software_system(name="sys")
5663
container = system.add_container(name="Container", description="Description")
5764
assert container.technology == ""
65+
66+
67+
@pytest.mark.xfail(strict=True)
68+
def test_software_system_add_container_adds_to_container_list(empty_system: SoftwareSystem):
69+
"""Ensure that add_container() adds the new container to SoftwareSystem.containers and sets up other properties."""
70+
container = empty_system.add_container(name="Container", description="Description")
71+
assert container in empty_system.containers
72+
assert container.id != ""
73+
assert container.model is empty_system.model
74+
assert container.parent is empty_system
75+
76+
77+
@pytest.mark.xfail(strict=True)
78+
def test_software_system_add_constructed_container(empty_system: SoftwareSystem):
79+
"""Verify behaviour when adding a newly constructed Container rather than calling add_container()."""
80+
container = Container(name="Container")
81+
empty_system += container
82+
assert container in empty_system.containers
83+
assert container.id != ""
84+
assert container.model is empty_system.model
85+
assert container.parent is empty_system
86+
87+
88+
@pytest.mark.xfail(strict=True)
89+
def test_software_system_adding_container_twice_is_ok(empty_system: SoftwareSystem):
90+
"""Defensive check that adding the same container twice is OK."""
91+
container = Container(name="Container")
92+
empty_system += container
93+
empty_system += container
94+
assert len(empty_system.containers) == 1
95+
96+
97+
@pytest.mark.xfail(strict=True)
98+
def test_software_system_adding_container_with_same_name_fails(empty_system: SoftwareSystem):
99+
"""Defensive check that adding a container with the same name as an existing one fails."""
100+
empty_system.add_container(name="Container")
101+
with pytest.raises(ValueError, match="Container with name .* already exists"):
102+
empty_system.add_container(name="Container")
103+
with pytest.raises(ValueError, match="Container with name .* already exists"):
104+
empty_system += Container(name="Container")
105+
106+
107+
@pytest.mark.xfail(strict=True)
108+
def test_software_system_adding_container_with_existing_parent_fails(empty_system: SoftwareSystem):
109+
"""Defensive check that if a container already has a (different) parent then it can't be added."""
110+
system2 = empty_system.model.add_software_system(name="System 2", description="Description")
111+
container = empty_system.add_container(name="Container")
112+
with pytest.raises(ValueError, match="Container with name .* already has parent"):
113+
system2 += container

0 commit comments

Comments
 (0)