This repository has been archived by the owner on Jan 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Issue 63: StaticView.add_nearest_neighbours() should not duplicate relationships #64
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ac39d76
fix(StaticView): demonstrate duplication of relationships
yt-ms 7946a7d
fix(StaticView): stop add_nearest_neighbour duplicating relationships
yt-ms dab5b86
fix(View): ignore if trying to add the same relationship to a view twice
yt-ms 7a639a4
chore: fix flake8 errors
yt-ms 6ba54f3
style: fix flake8 errors in examples
yt-ms f92e98f
fix(View): demonstrate problem with missing descriptions (issue #40)
yt-ms b5d7f05
fix(View): support blank descriptions in diagrams from the Structuriz…
yt-ms 7629dd2
Fix string concatenation (PR feedback)
yt-ms 3d4aa90
fix: string concatenation (PR feedback)
yt-ms eabef7a
fix: string concatenation (PR feedback)
yt-ms 883dde0
fix: remove flake8 from make qa (PR feedback)
yt-ms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# Copyright (c) 2020 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
"""Ensure the expected behaviour of StaticView.""" | ||
|
||
|
||
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(): | ||
"""Test basic behaviour of add_nearest_neighbours.""" | ||
model = Model() | ||
sys1 = model.add_software_system(name="System 1") | ||
sys2 = model.add_software_system(name="System 2") | ||
person = model.add_person(name="Person 1") | ||
sys1.uses(sys2) | ||
person.uses(sys1) | ||
|
||
# Check neighbours from outbound relationships | ||
view = DerivedView(software_system=sys1, description="") | ||
view.add_nearest_neighbours(sys1, SoftwareSystem) | ||
assert any((elt_view.element is sys1 for elt_view in view.element_views)) | ||
assert any((elt_view.element is sys2 for elt_view in view.element_views)) | ||
assert not any((elt_view.element is person for elt_view in view.element_views)) | ||
assert len(view.relationship_views) == 1 | ||
|
||
# Check neighbours from inbound relationships | ||
view = DerivedView(software_system=sys1, description="") | ||
view.add_nearest_neighbours(sys2, SoftwareSystem) | ||
assert any((elt_view.element is sys1 for elt_view in view.element_views)) | ||
assert any((elt_view.element is sys2 for elt_view in view.element_views)) | ||
assert not any((elt_view.element is person for elt_view in view.element_views)) | ||
assert len(view.relationship_views) == 1 | ||
|
||
|
||
def test_add_nearest_neighbours_doesnt_dupe_relationships(): | ||
"""Test relationships aren't duplicated if neighbours added more than once. | ||
|
||
See https://github.com/Midnighter/structurizr-python/issues/63. | ||
""" | ||
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 should not add any new relationships | ||
view.add_nearest_neighbours(sys1, Person) | ||
assert len(view.relationship_views) == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Copyright (c) 2020 | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
"""Ensure the expected behaviour of View.""" | ||
|
||
from structurizr.model import Model | ||
from structurizr.view.view import View, ViewIO | ||
|
||
|
||
class DerivedView(View): | ||
"""Mock class for testing.""" | ||
|
||
pass | ||
|
||
|
||
def test_add_relationship_doesnt_duplicate(): | ||
"""Test that adding a relationships twice doesn't duplicate it.""" | ||
model = Model() | ||
sys1 = model.add_software_system(name="System 1") | ||
sys2 = model.add_software_system(name="System 2") | ||
rel = sys1.uses(sys2) | ||
|
||
view = DerivedView(software_system=sys1, description="") | ||
view._add_element(sys1, False) | ||
view._add_element(sys2, False) | ||
|
||
rel_view1 = view._add_relationship(rel) | ||
assert len(view.relationship_views) == 1 | ||
rel_view2 = view._add_relationship(rel) | ||
assert len(view.relationship_views) == 1 | ||
assert rel_view2 is rel_view1 | ||
|
||
|
||
def test_add_relationship_for_element_not_in_view(): | ||
"""Ensures relationships for elements outside the view are ignored.""" | ||
model = Model() | ||
sys1 = model.add_software_system(name="System 1") | ||
sys2 = model.add_software_system(name="System 2") | ||
rel = sys1.uses(sys2) | ||
|
||
view = DerivedView(software_system=sys1, description="") | ||
view._add_element(sys1, False) | ||
|
||
# This relationship should be ignored as sys2 isn't in the view | ||
rel_view1 = view._add_relationship(rel) | ||
assert rel_view1 is None | ||
assert view.relationship_views == set() | ||
|
||
|
||
def test_adding_all_relationships(): | ||
"""Test adding all relationships for elements in the view.""" | ||
model = Model() | ||
sys1 = model.add_software_system(name="System 1") | ||
sys2 = model.add_software_system(name="System 2") | ||
sys3 = model.add_software_system(name="System 3") | ||
rel1 = sys1.uses(sys2) | ||
rel2 = sys3.uses(sys1) | ||
|
||
view = DerivedView(software_system=sys1, description="") | ||
view._add_element(sys1, False) | ||
view._add_element(sys2, False) | ||
view._add_element(sys3, False) | ||
assert view.relationship_views == set() | ||
|
||
view._add_relationships(sys1) | ||
assert len(view.relationship_views) == 2 | ||
assert rel1 in [vr.relationship for vr in view.relationship_views] | ||
assert rel2 in [vr.relationship for vr in view.relationship_views] | ||
|
||
|
||
def test_missing_json_description_allowed(): | ||
""" | ||
Ensure that missing descriptions in the JSON form are supported. | ||
|
||
Raised as https://github.com/Midnighter/structurizr-python/issues/40, it is | ||
permitted through the Structurizr UI to create views with a blank description, | ||
which then gets ommitted from the workspace JSON, so this needs to be allowed by | ||
the Pydantic validation also. | ||
""" | ||
|
||
json = """ | ||
{ | ||
"key": "System1-SystemContext" | ||
} | ||
""" | ||
io = ViewIO.parse_raw(json) | ||
assert io is not None |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will remove this line before merging.
make qa
is intended for applying style fixes and not for checking for problems. You can either usetox -e flake8
for that or create a separate commandmake qc
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, sure.