1818
1919import pytest
2020
21- from structurizr .model .model import Model
2221from structurizr .model .container import Container
22+ from structurizr .model .model import Model
2323from structurizr .model .software_system import SoftwareSystem
2424
2525
26- @pytest .fixture (scope = "function" )
27- def empty_model () -> Model :
28- """Provide an empty Model on demand for test cases to use."""
29- return Model ()
26+ _model = (
27+ Model ()
28+ ) # Have to create outside the fixture so it doesn't get garbage-collected.
3029
3130
3231@pytest .fixture (scope = "function" )
3332def empty_system () -> SoftwareSystem :
3433 """Provide an empty SoftwareSystem on demand for test cases to use."""
35- return Model ().add_software_system ("Sys" )
34+ software_system = _model .add_software_system (name = "Sys" )
35+ return software_system
3636
3737
3838@pytest .mark .parametrize (
@@ -57,35 +57,33 @@ def test_add_container_accepts_additional_args():
5757 assert container .id == "id1"
5858
5959
60- def test_add_container_technology_is_optional (empty_model : Model ):
60+ def test_add_container_technology_is_optional (empty_system : SoftwareSystem ):
6161 """Ensure that you don't have to specify the technology."""
62- system = empty_model .add_software_system (name = "sys" )
63- container = system .add_container (name = "Container" , description = "Description" )
62+ container = empty_system .add_container (name = "Container" , description = "Description" )
6463 assert container .technology == ""
6564
66-
67- @pytest .mark .xfail (strict = True )
68- def test_software_system_add_container_adds_to_container_list (empty_system : SoftwareSystem ):
65+
66+ def test_software_system_add_container_adds_to_container_list (
67+ empty_system : SoftwareSystem ,
68+ ):
6969 """Ensure that add_container() adds the new container to SoftwareSystem.containers and sets up other properties."""
7070 container = empty_system .add_container (name = "Container" , description = "Description" )
7171 assert container in empty_system .containers
7272 assert container .id != ""
73- assert container .model is empty_system . model
73+ assert container .model is _model
7474 assert container .parent is empty_system
7575
7676
77- @pytest .mark .xfail (strict = True )
7877def test_software_system_add_constructed_container (empty_system : SoftwareSystem ):
7978 """Verify behaviour when adding a newly constructed Container rather than calling add_container()."""
8079 container = Container (name = "Container" )
8180 empty_system += container
8281 assert container in empty_system .containers
8382 assert container .id != ""
84- assert container .model is empty_system . model
83+ assert container .model is _model
8584 assert container .parent is empty_system
8685
8786
88- @pytest .mark .xfail (strict = True )
8987def test_software_system_adding_container_twice_is_ok (empty_system : SoftwareSystem ):
9088 """Defensive check that adding the same container twice is OK."""
9189 container = Container (name = "Container" )
@@ -94,20 +92,31 @@ def test_software_system_adding_container_twice_is_ok(empty_system: SoftwareSyst
9492 assert len (empty_system .containers ) == 1
9593
9694
97- @pytest .mark .xfail (strict = True )
98- def test_software_system_adding_container_with_same_name_fails (empty_system : SoftwareSystem ):
95+ def test_software_system_adding_container_with_same_name_fails (
96+ empty_system : SoftwareSystem ,
97+ ):
9998 """Defensive check that adding a container with the same name as an existing one fails."""
100- empty_system .add_container (name = "Container" )
99+ empty_system .add_container (name = "Container" , description = "Description" )
101100 with pytest .raises (ValueError , match = "Container with name .* already exists" ):
102- empty_system .add_container (name = "Container" )
101+ empty_system .add_container (name = "Container" , description = "Description2" )
103102 with pytest .raises (ValueError , match = "Container with name .* already exists" ):
104- empty_system += Container (name = "Container" )
103+ empty_system += Container (name = "Container" , description = "Description2" )
105104
106105
107- @pytest .mark .xfail (strict = True )
108- def test_software_system_adding_container_with_existing_parent_fails (empty_system : SoftwareSystem ):
106+ def test_software_system_adding_container_with_existing_parent_fails (
107+ empty_system : SoftwareSystem ,
108+ ):
109109 """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" )
110+ system2 = empty_system .model .add_software_system (
111+ name = "System 2" , description = "Description"
112+ )
113+ container = empty_system .add_container (name = "Container" , description = "Description" )
112114 with pytest .raises (ValueError , match = "Container with name .* already has parent" ):
113115 system2 += container
116+
117+
118+ def test_software_system_get_container_with_name (empty_system : SoftwareSystem ):
119+ """Test getting containers by name."""
120+ container = empty_system .add_container (name = "Test" , description = "Description" )
121+ assert empty_system .get_container_with_name ("Test" ) is container
122+ assert empty_system .get_container_with_name ("FooBar" ) is None
0 commit comments