1919import pytest
2020
2121from structurizr .model .model import Model
22+ from structurizr .model .container import Container
2223from 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