1616
1717import pytest
1818
19- from structurizr .model import Container , Model , SoftwareSystem
19+ from structurizr .model import Component , Container , Model , SoftwareSystem
2020
2121
2222_model = Model ()
2323_system = _model .add_software_system (name = "Sys" )
2424
2525
26+ @pytest .fixture (scope = "function" )
27+ def empty_container () -> Container :
28+ """Provide an empty Container on demand for test cases to use."""
29+ return _system .add_container (name = "Container" , description = "Description" )
30+
31+
2632@pytest .mark .parametrize (
2733 "attributes" ,
2834 [
@@ -35,3 +41,50 @@ def test_container_init(attributes):
3541 container = Container (** attributes )
3642 for attr , expected in attributes .items ():
3743 assert getattr (container , attr ) == expected
44+
45+
46+ def test_container_add_component_adds_to_component_list (empty_container : Container ):
47+ """Ensure that add_component() adds the new component to Container.components and sets up other properties."""
48+ component = empty_container .add_component (name = "Component" )
49+ assert component in empty_container .components
50+ assert component .id != ""
51+ assert component .model is _model
52+ assert component .parent is empty_container
53+
54+
55+ @pytest .mark .xfail (strict = True )
56+ def test_container_add_constructed_component (empty_container : Container ):
57+ """Verify behaviour when adding a newly constructed Container rather than calling add_container()."""
58+ component = Component (name = "Component" )
59+ empty_container .add (component )
60+ assert component in empty_container .components
61+ assert component .id != "" # Currently failing
62+ assert component .model is _model # Currently failing
63+ assert component .parent is empty_container # Currently failing
64+
65+
66+ def test_container_adding_component_twice_is_ok (empty_container : Container ):
67+ """Defensive check that adding the same component twice is OK."""
68+ component = Component (name = "Component" )
69+ empty_container .add (component )
70+ empty_container .add (component )
71+ assert len (empty_container .components ) == 1
72+
73+
74+ @pytest .mark .xfail (strict = True )
75+ def test_container_adding_component_with_same_name_fails (empty_container : Container ):
76+ """Defensive check that adding a component with the same name as an existing one fails."""
77+ empty_container .add_component (name = "Component" )
78+ with pytest .raises (ValueError , match = "Component with name .* already exists" ):
79+ empty_container .add_component (name = "Component" )
80+ with pytest .raises (ValueError , match = "Component with name .* already exists" ):
81+ empty_container .add (Component (name = "Component" )) # Doesn't currently raise
82+
83+
84+ @pytest .mark .xfail (strict = True )
85+ def test_adding_component_with_existing_parent_fails (empty_container : Container ):
86+ """Defensive check that if a component already has a (different) parent then it can't be added."""
87+ container2 = _system .add_container (name = "Container 2" , description = "Description" )
88+ component = empty_container .add_component (name = "Component" )
89+ with pytest .raises (ValueError ):
90+ container2 .add (component ) # Doesn't currently raise
0 commit comments