|
| 1 | +# Copyright (c) 2020 |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +"""Ensure the expected behaviour of StaticView.""" |
| 17 | + |
| 18 | + |
| 19 | +import pytest |
| 20 | + |
| 21 | +from structurizr.model import Model, Person, SoftwareSystem |
| 22 | +from structurizr.view.static_view import StaticView |
| 23 | + |
| 24 | + |
| 25 | +class DerivedView(StaticView): |
| 26 | + """Mock class for testing.""" |
| 27 | + |
| 28 | + def add_all_elements(self) -> None: |
| 29 | + """Stub method because base is abstract.""" |
| 30 | + pass |
| 31 | + |
| 32 | + |
| 33 | +def test_add_nearest_neighbours(): |
| 34 | + """Test basic behaviour of add_nearest_neighbours.""" |
| 35 | + model = Model() |
| 36 | + sys1 = model.add_software_system(name="System 1") |
| 37 | + sys2 = model.add_software_system(name="System 2") |
| 38 | + person = model.add_person(name="Person 1") |
| 39 | + sys1.uses(sys2) |
| 40 | + person.uses(sys1) |
| 41 | + |
| 42 | + # Check neighbours from outbound relationships |
| 43 | + view = DerivedView(software_system=sys1, description="") |
| 44 | + view.add_nearest_neighbours(sys1, SoftwareSystem) |
| 45 | + assert any((elt_view.element is sys1 for elt_view in view.element_views)) |
| 46 | + assert any((elt_view.element is sys2 for elt_view in view.element_views)) |
| 47 | + assert not any((elt_view.element is person for elt_view in view.element_views)) |
| 48 | + assert len(view.relationship_views) == 1 |
| 49 | + |
| 50 | + # Check neighbours from inbound relationships |
| 51 | + view = DerivedView(software_system=sys1, description="") |
| 52 | + view.add_nearest_neighbours(sys2, SoftwareSystem) |
| 53 | + assert any((elt_view.element is sys1 for elt_view in view.element_views)) |
| 54 | + assert any((elt_view.element is sys2 for elt_view in view.element_views)) |
| 55 | + assert not any((elt_view.element is person for elt_view in view.element_views)) |
| 56 | + assert len(view.relationship_views) == 1 |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.xfail(strict=True) |
| 60 | +def test_add_nearest_neighbours_doesnt_dupe_relationships(): |
| 61 | + """Test relationships aren't duplicated if neighbours added more than once. |
| 62 | +
|
| 63 | + See https://github.com/Midnighter/structurizr-python/issues/63. |
| 64 | + """ |
| 65 | + |
| 66 | + model = Model() |
| 67 | + sys1 = model.add_software_system(name="System 1") |
| 68 | + sys2 = model.add_software_system(name="System 2") |
| 69 | + sys1.uses(sys2) |
| 70 | + view = DerivedView(software_system=sys1, description="") |
| 71 | + view.add_nearest_neighbours(sys1, SoftwareSystem) |
| 72 | + assert len(view.relationship_views) == 1 |
| 73 | + |
| 74 | + # The next line will currently dupe the relationship |
| 75 | + view.add_nearest_neighbours(sys1, Person) |
| 76 | + assert len(view.relationship_views) == 1 # This fails as it's 2 |
0 commit comments