|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | + |
| 14 | +"""Ensure the expected behaviour of the model.""" |
| 15 | + |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from structurizr.model import Model |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope="function") |
| 23 | +def empty_model() -> Model: |
| 24 | + """Provide an empty Model on demand for test cases to use.""" |
| 25 | + return Model() |
| 26 | + |
| 27 | + |
| 28 | +def test_model_get_relationship_by_id(empty_model: Model): |
| 29 | + """Test retrieving relationships by their IDs.""" |
| 30 | + sys1 = empty_model.add_software_system(name="sys1") |
| 31 | + sys2 = empty_model.add_software_system(name="sys2") |
| 32 | + r = empty_model.add_relationship(source=sys1, destination=sys2, id="r1") |
| 33 | + assert empty_model.get_relationship("r1") is r |
| 34 | + |
| 35 | + |
| 36 | +def test_model_add_relationship_twice_ignored(empty_model: Model): |
| 37 | + """Make sure that adding an existing relationship to the Model makes no difference.""" |
| 38 | + sys1 = empty_model.add_software_system(name="sys1") |
| 39 | + sys2 = empty_model.add_software_system(name="sys2") |
| 40 | + r = empty_model.add_relationship(source=sys1, destination=sys2) |
| 41 | + assert list(empty_model.get_relationships()) == [r] |
| 42 | + empty_model.add_relationship(r) |
| 43 | + assert list(empty_model.get_relationships()) == [r] |
| 44 | + |
| 45 | + |
| 46 | +def test_model_cannot_add_relationship_with_same_id_as_existing(empty_model: Model): |
| 47 | + """Ensure you can't add two relationships with the same ID.""" |
| 48 | + sys1 = empty_model.add_software_system(name="sys1") |
| 49 | + sys2 = empty_model.add_software_system(name="sys2") |
| 50 | + r1 = empty_model.add_relationship(source=sys1, destination=sys2, id="r1") |
| 51 | + with pytest.raises( |
| 52 | + ValueError, match="Relationship.* has the same ID as Relationship.*" |
| 53 | + ): |
| 54 | + r2 = empty_model.add_relationship(source=sys1, destination=sys2, id="r1") |
| 55 | + |
| 56 | + |
| 57 | +def test_model_cannot_add_relationship_with_same_id_as_element(empty_model: Model): |
| 58 | + """Ensure you can't add a relationship with the same ID as an element.""" |
| 59 | + sys1 = empty_model.add_software_system(name="sys1") |
| 60 | + sys2 = empty_model.add_software_system(name="sys2") |
| 61 | + with pytest.raises( |
| 62 | + ValueError, match="Relationship.* has the same ID as SoftwareSystem.*" |
| 63 | + ): |
| 64 | + empty_model.add_relationship(source=sys1, destination=sys2, id=sys1.id) |
0 commit comments