Skip to content

Commit 9e2e5a2

Browse files
committed
fix: better normalisation of group names
1 parent 10014f4 commit 9e2e5a2

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/structurizr/model/groupable_element.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,26 @@ class GroupableElementIO(ElementIO, ABC):
2828
Define a superclass for all elements that can be included in a group.
2929
3030
Attributes:
31-
group (str): The name of thegroup in which this element should be included.
31+
group (str): The name of thegroup in which this element should be included, or
32+
None if no group.
3233
"""
3334

34-
group: Optional[str] = ""
35+
group: Optional[str]
3536

3637

3738
class GroupableElement(Element, ABC):
3839
"""
3940
Define a superclass for all elements that can be included in a group.
4041
4142
Attributes:
42-
group (str): The name of thegroup in which this element should be included.
43+
group (str): The name of thegroup in which this element should be included, or
44+
None if no group.
4345
"""
4446

45-
def __init__(self, *, group: str = "", **kwargs):
47+
def __init__(self, *, group: Optional[str] = None, **kwargs):
4648
"""Initialise a GroupableElement."""
4749
super().__init__(**kwargs)
50+
group = group.strip() or None if group else None
4851
self.group = group
4952

5053
@classmethod

tests/unit/model/test_groupable_element.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ class ConcreteElement(ChildlessMixin, GroupableElement):
2323
pass
2424

2525

26+
def test_group_name_normalisation():
27+
"""Test that empty group names are normalised to None."""
28+
assert ConcreteElement(name="Name").group is None
29+
assert ConcreteElement(name="Name", group=None).group is None
30+
assert ConcreteElement(name="Name", group="").group is None
31+
assert ConcreteElement(name="Name", group=" ").group is None
32+
assert ConcreteElement(name="Name", group=" g1 ").group == "g1"
33+
34+
2635
def test_group_in_json():
2736
"""Test the group field is output to JSON."""
2837
element = ConcreteElement(name="Name", group="Group 1")

0 commit comments

Comments
 (0)