Skip to content

Commit ad91ffa

Browse files
yt-msMidnighter
authored andcommitted
refactor: use mock rather than real Model
1 parent 48904f2 commit ad91ffa

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

tests/unit/model/test_container.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,29 @@
1616

1717
import pytest
1818

19-
from structurizr.model import Component, Container, Model
19+
from structurizr.model import Component, Container
2020

2121

22-
_model = (
23-
Model()
24-
) # Have to create outside the fixture so it doesn't get garbage-collected.
22+
class MockModel:
23+
"""Implement a mock model for testing."""
24+
25+
def add_component(self, component):
26+
"""Simulate the model assigning IDs to new elements."""
27+
if not component.id:
28+
component.id = "id"
29+
component.set_model(self)
30+
pass
31+
32+
33+
_model = MockModel()
2534

2635

2736
@pytest.fixture(scope="function")
2837
def empty_container() -> Container:
2938
"""Provide an empty Container on demand for test cases to use."""
30-
system = _model.add_software_system(name="Sys")
31-
return system.add_container(name="Container", description="Description")
39+
container = Container(name="Container", description="Description")
40+
container.set_model(_model)
41+
return container
3242

3343

3444
@pytest.mark.parametrize(
@@ -83,9 +93,7 @@ def test_container_adding_component_with_same_name_fails(empty_container: Contai
8393

8494
def test_adding_component_with_existing_parent_fails(empty_container: Container):
8595
"""Check that adding a component with a different parent fails."""
86-
container2 = empty_container.parent.add_container(
87-
name="Container 2", description="Description"
88-
)
96+
container2 = Container(name="Container 2", description="Description")
8997
component = empty_container.add_component(name="Component")
9098
with pytest.raises(ValueError, match="Component with name .* already has parent"):
9199
container2 += component

tests/unit/model/test_software_system.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,28 @@
1919
import pytest
2020

2121
from structurizr.model.container import Container
22-
from structurizr.model.model import Model
2322
from 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")
3240
def 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

Comments
 (0)