Skip to content

Commit 6e39634

Browse files
yt-msMidnighter
authored andcommitted
test: improve coverage of Model around adding relationships
1 parent 1cad20d commit 6e39634

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/structurizr/model/model.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ def add_relationship(
348348
Relationship
349349
350350
"""
351-
352351
if relationship is None:
353352
relationship = Relationship(**kwargs)
354353
# Check

tests/unit/model/test_model.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# https://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
13+
14+
"""Ensure the expected behaviour of the model."""
15+
16+
17+
import pytest
18+
19+
from structurizr.model import Model
20+
21+
22+
@pytest.fixture(scope="function")
23+
def empty_model() -> Model:
24+
"""Provide an empty Model on demand for test cases to use."""
25+
return Model()
26+
27+
28+
def test_model_get_relationship_by_id(empty_model: Model):
29+
"""Test retrieving relationships by their IDs."""
30+
sys1 = empty_model.add_software_system(name="sys1")
31+
sys2 = empty_model.add_software_system(name="sys2")
32+
r = empty_model.add_relationship(source=sys1, destination=sys2, id="r1")
33+
assert empty_model.get_relationship("r1") is r
34+
35+
36+
def test_model_add_relationship_twice_ignored(empty_model: Model):
37+
"""Make sure that adding an existing relationship to the Model makes no difference."""
38+
sys1 = empty_model.add_software_system(name="sys1")
39+
sys2 = empty_model.add_software_system(name="sys2")
40+
r = empty_model.add_relationship(source=sys1, destination=sys2)
41+
assert list(empty_model.get_relationships()) == [r]
42+
empty_model.add_relationship(r)
43+
assert list(empty_model.get_relationships()) == [r]
44+
45+
46+
def test_model_cannot_add_relationship_with_same_id_as_existing(empty_model: Model):
47+
"""Ensure you can't add two relationships with the same ID."""
48+
sys1 = empty_model.add_software_system(name="sys1")
49+
sys2 = empty_model.add_software_system(name="sys2")
50+
r1 = empty_model.add_relationship(source=sys1, destination=sys2, id="r1")
51+
with pytest.raises(
52+
ValueError, match="Relationship.* has the same ID as Relationship.*"
53+
):
54+
r2 = empty_model.add_relationship(source=sys1, destination=sys2, id="r1")
55+
56+
57+
def test_model_cannot_add_relationship_with_same_id_as_element(empty_model: Model):
58+
"""Ensure you can't add a relationship with the same ID as an element."""
59+
sys1 = empty_model.add_software_system(name="sys1")
60+
sys2 = empty_model.add_software_system(name="sys2")
61+
with pytest.raises(
62+
ValueError, match="Relationship.* has the same ID as SoftwareSystem.*"
63+
):
64+
empty_model.add_relationship(source=sys1, destination=sys2, id=sys1.id)

0 commit comments

Comments
 (0)