Midnighter/structurizr-python

StaticView.add_nearest_neighbours should not duplicate relationships

yt-ms opened this issue · 0 comments

yt-ms commented

Problem description

If add_nearest_neighbours() is called more than once for the same element (e.g. with different neighbour types) then it can currently duplicate relationships.

Code Sample

Create a minimal, complete, verifiable example.

from structurizr.model import Model, Person, SoftwareSystem
from structurizr.view.static_view import StaticView


class DerivedView(StaticView):
    """Mock class for testing."""
    def add_all_elements(self) -> None:
        """Stub method because base is abstract."""
        pass


def test_add_nearest_neighbours_doesnt_dupe_relationships():
    """Test relationships aren't duplicated if neighbours added more than once."""
    model = Model()
    sys1 = model.add_software_system(name="System 1")
    sys2 = model.add_software_system(name="System 2")
    sys1.uses(sys2)
    view = DerivedView(software_system=sys1, description="")
    view.add_nearest_neighbours(sys1, SoftwareSystem)
    assert len(view.relationship_views) == 1

    # The next line will currently dupe the relationship
    view.add_nearest_neighbours(sys1, Person)
    assert len(view.relationship_views) == 1  # This fails as it's 2

Context

System Information
==================
OS         Windows
OS-release      10
Python       3.7.5

Package Versions
================
depinfo                              1.5.4
httpx                               0.16.1
importlib_metadata                   1.7.0
ordered-set                            3.1
pip                                 20.2.4
pydantic                             1.7.1
python-dotenv                       0.14.0
setuptools                          41.2.0
structurizr-python 0.2.1+29.gf1b13ef.dirty