Skip to content

Commit c6d6bd6

Browse files
yt-msMidnighter
authored andcommitted
test: more testing around adding relationships
1 parent 8ec212b commit c6d6bd6

File tree

1 file changed

+58
-13
lines changed

1 file changed

+58
-13
lines changed

tests/integration/test_model_element_relationships.py

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616
See https://github.com/Midnighter/structurizr-python/issues/31.
1717
"""
1818

19+
from pathlib import Path
20+
1921
import pytest
2022

23+
from structurizr import Workspace
2124
from structurizr.model import Model
2225

2326

27+
DEFINITIONS = Path(__file__).parent / "data" / "workspace_definition"
28+
29+
2430
@pytest.mark.xfail(strict=True)
2531
def test_adding_relationship_to_element_adds_to_model():
2632
"""
@@ -31,15 +37,31 @@ def test_adding_relationship_to_element_adds_to_model():
3137
sys1 = model.add_software_system(name="sys1")
3238
sys2 = model.add_software_system(name="sys2")
3339

34-
sys1.add_relationship(source=sys1, destination=sys2, description="uses")
35-
assert len(sys1.relationships) == 1
36-
assert len(list(sys1.get_relationships())) == 1 # Currently will fail
37-
assert len(model.get_relationships()) == 1 # Currently will fail
38-
assert len(sys2.relationships) == 1 # Currently will fail
39-
assert len(list(sys2.get_relationships())) == 1 # Currently will fail
40+
r = sys1.add_relationship(source=sys1, destination=sys2, description="uses")
41+
assert list(sys1.relationships) == [r]
42+
assert list(sys1.get_relationships()) == [r] # Currently will fail
43+
assert list(model.get_relationships()) == [r] # Currently will fail
44+
assert list(sys2.relationships) == [] # relationships only contains outbound
45+
assert list(sys2.get_relationships()) == [r] # Currently will fail
46+
47+
48+
def test_adding_relationship_to_model_adds_to_element():
49+
"""
50+
Make sure that when a relationship is added via Element.add_relationship it also
51+
gets added to the model and to the other element.
52+
"""
53+
model = Model()
54+
sys1 = model.add_software_system(name="sys1")
55+
sys2 = model.add_software_system(name="sys2")
56+
57+
r = model.add_relationship(source=sys1, destination=sys2, description="uses")
58+
assert list(sys1.relationships) == [r]
59+
assert list(sys1.get_relationships()) == [r]
60+
assert list(model.get_relationships()) == [r]
61+
assert list(sys2.relationships) == [] # relationships only contains outbound
62+
assert list(sys2.get_relationships()) == [r]
4063

4164

42-
@pytest.mark.xfail(strict=True)
4365
def test_adding_relationship_via_uses_adds_to_elements():
4466
"""
4567
Make sure that when a relationship is added via StaticStructureElement.uses
@@ -49,9 +71,32 @@ def test_adding_relationship_via_uses_adds_to_elements():
4971
sys1 = model.add_software_system(name="sys1")
5072
sys2 = model.add_software_system(name="sys2")
5173

52-
sys1.uses(sys2, "uses")
53-
assert len(sys1.relationships) == 1
54-
assert len(list(sys1.get_relationships())) == 1
55-
assert len(model.get_relationships()) == 1
56-
assert len(sys2.relationships) == 1 # Currently will fail
57-
assert len(list(sys2.get_relationships())) == 1
74+
r = sys1.uses(sys2, "uses")
75+
assert list(sys1.relationships) == [r]
76+
assert list(sys1.get_relationships()) == [r]
77+
assert list(model.get_relationships()) == [r]
78+
assert list(sys2.relationships) == [] # relationships only contains outbound
79+
assert list(sys2.get_relationships()) == [r]
80+
81+
82+
@pytest.mark.parametrize(
83+
"filename",
84+
["BigBank.json"],
85+
)
86+
def test_relationships_after_deserialisation_are_consistent(filename):
87+
"""Make sure that relationships are consistent between the Model and the Element after deserialisation."""
88+
path = DEFINITIONS / filename
89+
workspace = Workspace.load(path)
90+
model = workspace.model
91+
atm = model.get_software_system_with_id("9")
92+
mainframe = model.get_software_system_with_id("4")
93+
customer = model.get_element("1")
94+
95+
assert len(atm.relationships) == 1
96+
assert (
97+
len(list(atm.get_relationships())) == 2
98+
) # One to mainframe, one from personal banking customer
99+
assert len(list(atm.get_afferent_relationships())) == 1
100+
assert list(atm.get_afferent_relationships())[0].source is customer
101+
assert len(list(atm.get_efferent_relationships())) == 1
102+
assert list(atm.get_efferent_relationships())[0].destination is mainframe

0 commit comments

Comments
 (0)