Skip to content

Commit 61a565c

Browse files
yt-msMidnighter
authored andcommitted
refactor: remove global _model instances in tests
1 parent 6d3c3aa commit 61a565c

File tree

2 files changed

+52
-35
lines changed

2 files changed

+52
-35
lines changed

tests/unit/model/test_container.py

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
class MockModel:
2323
"""Implement a mock model for testing."""
2424

25+
def __init__(self):
26+
"""Create an empty container for testing."""
27+
self.empty_container = Container(name="Container", description="Description")
28+
self.empty_container.set_model(self)
29+
2530
def __iadd__(self, component):
2631
"""Simulate the model assigning IDs to new elements."""
2732
if not component.id:
@@ -30,15 +35,10 @@ def __iadd__(self, component):
3035
return self
3136

3237

33-
_model = MockModel()
34-
35-
3638
@pytest.fixture(scope="function")
37-
def empty_container() -> Container:
38-
"""Provide an empty Container on demand for test cases to use."""
39-
container = Container(name="Container", description="Description")
40-
container.set_model(_model)
41-
return container
39+
def model_with_container() -> MockModel:
40+
"""Provide a model with an empty Container on demand for test cases to use."""
41+
return MockModel()
4242

4343

4444
@pytest.mark.parametrize(
@@ -55,54 +55,63 @@ def test_container_init(attributes):
5555
assert getattr(container, attr) == expected
5656

5757

58-
def test_container_add_component_adds_to_component_list(empty_container: Container):
58+
def test_container_add_component_adds_to_component_list(
59+
model_with_container: MockModel,
60+
):
5961
"""Verify add_component() adds the new component to Container.components."""
62+
empty_container = model_with_container.empty_container
6063
component = empty_container.add_component(name="Component")
6164
assert component in empty_container.components
6265
assert component.id != ""
63-
assert component.model is _model
66+
assert component.model is model_with_container
6467
assert component.parent is empty_container
6568

6669

67-
def test_container_add_constructed_component(empty_container: Container):
70+
def test_container_add_constructed_component(model_with_container: MockModel):
6871
"""Verify behaviour when adding a newly constructed Container."""
72+
empty_container = model_with_container.empty_container
6973
component = Component(name="Component")
7074
empty_container += component
7175
assert component in empty_container.components
7276
assert component.id != ""
73-
assert component.model is _model
77+
assert component.model is model_with_container
7478
assert component.parent is empty_container
7579

7680

77-
def test_container_adding_component_twice_is_ok(empty_container: Container):
81+
def test_container_adding_component_twice_is_ok(model_with_container: MockModel):
7882
"""Defensive check that adding the same component twice is OK."""
83+
empty_container = model_with_container.empty_container
7984
component = Component(name="Component")
8085
empty_container += component
8186
empty_container += component
8287
assert len(empty_container.components) == 1
8388

8489

85-
def test_container_adding_component_with_same_name_fails(empty_container: Container):
90+
def test_container_adding_component_with_same_name_fails(
91+
model_with_container: MockModel,
92+
):
8693
"""Check that adding a component with the same name as an existing one fails."""
94+
empty_container = model_with_container.empty_container
8795
empty_container.add_component(name="Component")
8896
with pytest.raises(ValueError, match="Component with name .* already exists"):
8997
empty_container.add_component(name="Component")
9098
with pytest.raises(ValueError, match="Component with name .* already exists"):
9199
empty_container += Component(name="Component")
92100

93101

94-
def test_adding_component_with_existing_parent_fails(empty_container: Container):
102+
def test_adding_component_with_existing_parent_fails(model_with_container: MockModel):
95103
"""Check that adding a component with a different parent fails."""
104+
empty_container = model_with_container.empty_container
96105
container2 = Container(name="Container 2", description="Description")
97106
component = empty_container.add_component(name="Component")
98107
with pytest.raises(ValueError, match="Component with name .* already has parent"):
99108
container2 += component
100109

101110

102-
def test_serialisation_of_child_components():
111+
def test_serialisation_of_child_components(model_with_container: MockModel):
103112
"""Make sure that components are serialised even though read-only."""
104113
container = Container(name="Container", description="Description")
105-
container.set_model(_model)
114+
container.set_model(model_with_container)
106115
container.add_component(name="Component")
107116
io = ContainerIO.from_orm(container)
108117

tests/unit/model/test_software_system.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
class 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

7375
def 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

102107
def 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

113119
def 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

Comments
 (0)