Skip to content

Commit e5dc512

Browse files
yt-msMidnighter
authored andcommitted
refactor: add += to Model for Person and SoftwareSystem
1 parent 5d4d193 commit e5dc512

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

src/structurizr/model/model.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
import logging
20-
from typing import Iterable, List, Optional, ValuesView
20+
from typing import Iterable, List, Optional, Union, ValuesView
2121

2222
from pydantic import Field
2323

@@ -209,6 +209,18 @@ def add_software_system(self, software_system=None, **kwargs) -> SoftwareSystem:
209209
self.software_systems.add(software_system)
210210
return software_system
211211

212+
def __iadd__(self, element: Union[Person, SoftwareSystem]) -> "Model":
213+
"""Add a new Person or SoftwareSystem to the model."""
214+
if isinstance(element, Person):
215+
self.add_person(person=element)
216+
elif isinstance(element, SoftwareSystem):
217+
self.add_software_system(software_system=element)
218+
else:
219+
raise ValueError(
220+
f"Cannot add element with the name {element.name} to Model with += as it is not a Person or a SoftwareSystem."
221+
)
222+
return self
223+
212224
def add_container(self, container: Container) -> Container:
213225
"""
214226
Register a newly constructed container with the model.

tests/unit/model/test_model.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import pytest
1818

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

2121

2222
@pytest.fixture(scope="function")
@@ -76,3 +76,30 @@ def test_model_add_container_must_have_parent(empty_model: Model):
7676
container = Container(name="c1")
7777
with pytest.raises(ValueError, match="Container with name .* has no parent"):
7878
empty_model.add_container(container)
79+
80+
81+
def test_model_add_person_with_plusequals(empty_model: Model):
82+
"""Check that adding a Person to a Model with += works, consistent with += on SoftwareSystem and Container."""
83+
bob = Person(name="Bob")
84+
empty_model += bob
85+
assert bob in empty_model.people
86+
assert bob.id != ""
87+
88+
89+
def test_model_add_software_system_with_plusequals(empty_model: Model):
90+
"""Check that adding a SoftwareSystem to a Model with += works, consistent with += on SoftwareSystem and Container."""
91+
sys = SoftwareSystem(name="Sys")
92+
empty_model += sys
93+
assert sys in empty_model.software_systems
94+
assert sys.id != ""
95+
96+
97+
def test_model_can_only_add_person_or_software_system_with_plusequals(
98+
empty_model: Model,
99+
):
100+
"""Ensure that passing something other than a Person or SoftwareSystem in to += fails."""
101+
c = Container(name="C")
102+
with pytest.raises(
103+
ValueError, match="Cannot add element with the name .* to Model"
104+
):
105+
empty_model += c

0 commit comments

Comments
 (0)