1919import pytest
2020
2121from structurizr .model .container import Container
22- from structurizr .model .model import Model
2322from structurizr .model .software_system import SoftwareSystem
2423
2524
26- _model = (
27- Model ()
28- ) # Have to create outside the fixture so it doesn't get garbage-collected.
25+ class MockModel :
26+ """Implement a mock model for testing."""
27+
28+ def add_container (self , container ):
29+ """Simulate the model assigning IDs to new elements."""
30+ if not container .id :
31+ container .id = "id"
32+ container .set_model (self )
33+ pass
34+
35+
36+ _model = MockModel ()
2937
3038
3139@pytest .fixture (scope = "function" )
3240def empty_system () -> SoftwareSystem :
3341 """Provide an empty SoftwareSystem on demand for test cases to use."""
34- software_system = _model .add_software_system (name = "Sys" )
42+ software_system = SoftwareSystem (name = "Sys" )
43+ software_system .set_model (_model )
3544 return software_system
3645
3746
@@ -49,11 +58,9 @@ def test_software_system_init(attributes):
4958 assert getattr (system , attr ) == expected
5059
5160
52- def test_add_container_accepts_additional_args ():
61+ def test_add_container_accepts_additional_args (empty_system : SoftwareSystem ):
5362 """Test keyword arguments (e.g. id) are allowed when adding a new container."""
54- model = Model ()
55- system = model .add_software_system (name = "Banking System" )
56- container = system .add_container ("container" , "description" , id = "id1" )
63+ container = empty_system .add_container ("container" , "description" , id = "id1" )
5764 assert container .id == "id1"
5865
5966
@@ -107,9 +114,9 @@ def test_software_system_adding_container_with_existing_parent_fails(
107114 empty_system : SoftwareSystem ,
108115):
109116 """Check that adding a container with a (different) parent fails."""
110- system2 = empty_system . model . add_software_system (
111- name = "System 2" , description = "Description"
112- )
117+ system2 = SoftwareSystem ( name = "System 2" , description = "Description" )
118+ system2 . set_model ( empty_system . model )
119+
113120 container = empty_system .add_container (name = "Container" , description = "Description" )
114121 with pytest .raises (ValueError , match = "Container with name .* already has parent" ):
115122 system2 += container
0 commit comments