2525class MockModel :
2626 """Implement a mock model for testing."""
2727
28+ def __init__ (self ):
29+ """Set up an empty system for testing."""
30+ self .empty_system = SoftwareSystem (name = "Sys" )
31+ self .empty_system .set_model (self )
32+
2833 def add_container (self , container ):
2934 """Simulate the model assigning IDs to new elements."""
3035 if not container .id :
@@ -33,15 +38,10 @@ def add_container(self, container):
3338 pass
3439
3540
36- _model = MockModel ()
37-
38-
3941@pytest .fixture (scope = "function" )
40- def empty_system () -> SoftwareSystem :
41- """Provide an empty SoftwareSystem on demand for test cases to use."""
42- software_system = SoftwareSystem (name = "Sys" )
43- software_system .set_model (_model )
44- return software_system
42+ def model_with_system () -> MockModel :
43+ """Provide a model and empty SoftwareSystem on demand for test cases to use."""
44+ return MockModel ()
4545
4646
4747@pytest .mark .parametrize (
@@ -58,51 +58,57 @@ def test_software_system_init(attributes):
5858 assert getattr (system , attr ) == expected
5959
6060
61- def test_add_container_accepts_additional_args (empty_system : SoftwareSystem ):
61+ def test_add_container_accepts_additional_args (model_with_system : MockModel ):
6262 """Test keyword arguments (e.g. id) are allowed when adding a new container."""
63+ empty_system = model_with_system .empty_system
6364 container = empty_system .add_container ("container" , "description" , id = "id1" )
6465 assert container .id == "id1"
6566
6667
67- def test_add_container_technology_is_optional (empty_system : SoftwareSystem ):
68+ def test_add_container_technology_is_optional (model_with_system : MockModel ):
6869 """Ensure that you don't have to specify the technology."""
70+ empty_system = model_with_system .empty_system
6971 container = empty_system .add_container (name = "Container" , description = "Description" )
7072 assert container .technology == ""
7173
7274
7375def test_software_system_add_container_adds_to_container_list (
74- empty_system : SoftwareSystem ,
76+ model_with_system : MockModel ,
7577):
7678 """Ensure that add_container() adds the container and sets up other properties."""
79+ empty_system = model_with_system .empty_system
7780 container = empty_system .add_container (name = "Container" , description = "Description" )
7881 assert container in empty_system .containers
7982 assert container .id != ""
80- assert container .model is _model
83+ assert container .model is model_with_system
8184 assert container .parent is empty_system
8285
8386
84- def test_software_system_add_constructed_container (empty_system : SoftwareSystem ):
87+ def test_software_system_add_constructed_container (model_with_system : MockModel ):
8588 """Verify behaviour when adding a newly constructed Container."""
89+ empty_system = model_with_system .empty_system
8690 container = Container (name = "Container" )
8791 empty_system += container
8892 assert container in empty_system .containers
8993 assert container .id != ""
90- assert container .model is _model
94+ assert container .model is model_with_system
9195 assert container .parent is empty_system
9296
9397
94- def test_software_system_adding_container_twice_is_ok (empty_system : SoftwareSystem ):
98+ def test_software_system_adding_container_twice_is_ok (model_with_system : MockModel ):
9599 """Defensive check that adding the same container twice is OK."""
100+ empty_system = model_with_system .empty_system
96101 container = Container (name = "Container" )
97102 empty_system += container
98103 empty_system += container
99104 assert len (empty_system .containers ) == 1
100105
101106
102107def test_software_system_adding_container_with_same_name_fails (
103- empty_system : SoftwareSystem ,
108+ model_with_system : MockModel ,
104109):
105110 """Check that adding a container with the same name as an existing one fails."""
111+ empty_system = model_with_system .empty_system
106112 empty_system .add_container (name = "Container" , description = "Description" )
107113 with pytest .raises (ValueError , match = "Container with name .* already exists" ):
108114 empty_system .add_container (name = "Container" , description = "Description2" )
@@ -111,9 +117,10 @@ def test_software_system_adding_container_with_same_name_fails(
111117
112118
113119def test_software_system_adding_container_with_existing_parent_fails (
114- empty_system : SoftwareSystem ,
120+ model_with_system : MockModel ,
115121):
116122 """Check that adding a container with a (different) parent fails."""
123+ empty_system = model_with_system .empty_system
117124 system2 = SoftwareSystem (name = "System 2" , description = "Description" )
118125 system2 .set_model (empty_system .model )
119126
@@ -122,8 +129,9 @@ def test_software_system_adding_container_with_existing_parent_fails(
122129 system2 += container
123130
124131
125- def test_software_system_get_container_with_name (empty_system : SoftwareSystem ):
132+ def test_software_system_get_container_with_name (model_with_system : MockModel ):
126133 """Test getting containers by name."""
134+ empty_system = model_with_system .empty_system
127135 container = empty_system .add_container (name = "Test" , description = "Description" )
128136 assert empty_system .get_container_with_name ("Test" ) is container
129137 assert empty_system .get_container_with_name ("FooBar" ) is None
0 commit comments