|
| 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 | +"""Test the behaviour when replicating relationships to element instances.""" |
| 15 | + |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from structurizr.model import InteractionStyle, Model |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(scope="function") |
| 23 | +def empty_model() -> Model: |
| 24 | + """Provide an new empty model on demand for test cases to use.""" |
| 25 | + return Model() |
| 26 | + |
| 27 | + |
| 28 | +def test_replication_adds_relationships_in_the_same_environment(empty_model: Model): |
| 29 | + """Ensure within the same environment, relationships are replicated.""" |
| 30 | + # Adapted from the Java ModelTests |
| 31 | + model = empty_model |
| 32 | + system1 = model.add_software_system("Software System 1") |
| 33 | + container1 = system1.add_container("Container 1") |
| 34 | + |
| 35 | + system2 = model.add_software_system("Software System 2") |
| 36 | + container2 = system2.add_container("Container 2") |
| 37 | + |
| 38 | + system3 = model.add_software_system("Software System 3") |
| 39 | + container3 = system3.add_container("Container 3") |
| 40 | + |
| 41 | + system4 = model.add_software_system("Software System 4") |
| 42 | + |
| 43 | + container1.uses( |
| 44 | + container2, |
| 45 | + "Uses 1", |
| 46 | + "Technology 1", |
| 47 | + interaction_style=InteractionStyle.Synchronous, |
| 48 | + ) |
| 49 | + container2.uses( |
| 50 | + container3, |
| 51 | + "Uses 2", |
| 52 | + "Technology 2", |
| 53 | + interaction_style=InteractionStyle.Asynchronous, |
| 54 | + ) |
| 55 | + container3.uses(system4) |
| 56 | + |
| 57 | + dev_deployment_node = model.add_deployment_node( |
| 58 | + "Deployment Node", environment="Development" |
| 59 | + ) |
| 60 | + container_instance1 = dev_deployment_node.add_container(container1) |
| 61 | + container_instance2 = dev_deployment_node.add_container(container2) |
| 62 | + container_instance3 = dev_deployment_node.add_container(container3) |
| 63 | + system_instance = dev_deployment_node.add_software_system(system4) |
| 64 | + |
| 65 | + # The following live element instances should not affect the relationships of the |
| 66 | + # development element instances |
| 67 | + live_deployment_node = model.add_deployment_node( |
| 68 | + "Deployment Node", environment="Live" |
| 69 | + ) |
| 70 | + live_deployment_node.add_container(container1) |
| 71 | + live_deployment_node.add_container(container2) |
| 72 | + live_deployment_node.add_container(container3) |
| 73 | + live_deployment_node.add_software_system(system4) |
| 74 | + |
| 75 | + assert len(container_instance1.relationships) == 1 |
| 76 | + relationship = next(iter(container_instance1.relationships)) |
| 77 | + assert relationship.source is container_instance1 |
| 78 | + assert relationship.destination is container_instance2 |
| 79 | + assert relationship.description == "Uses 1" |
| 80 | + assert relationship.technology == "Technology 1" |
| 81 | + assert relationship.interaction_style == InteractionStyle.Synchronous |
| 82 | + assert relationship.tags == set() |
| 83 | + |
| 84 | + assert len(container_instance2.relationships) == 1 |
| 85 | + relationship = next(iter(container_instance2.relationships)) |
| 86 | + assert relationship.source is container_instance2 |
| 87 | + assert relationship.destination is container_instance3 |
| 88 | + assert relationship.description == "Uses 2" |
| 89 | + assert relationship.technology == "Technology 2" |
| 90 | + assert relationship.interaction_style == InteractionStyle.Asynchronous |
| 91 | + assert relationship.tags == set() |
| 92 | + |
| 93 | + assert len(container_instance3.relationships) == 1 |
| 94 | + relationship = next(iter(container_instance3.relationships)) |
| 95 | + assert relationship.source is container_instance3 |
| 96 | + assert relationship.destination is system_instance |
| 97 | + assert relationship.description == "Uses" |
| 98 | + assert relationship.technology == "" |
| 99 | + assert relationship.interaction_style == InteractionStyle.Synchronous |
| 100 | + assert relationship.tags == set() |
| 101 | + |
| 102 | + |
| 103 | +def test_not_replicating_doesnt_add_relationships(empty_model: Model): |
| 104 | + """Ensure within the same environment, relationships are replicated.""" |
| 105 | + # Adapted from the Java ModelTests |
| 106 | + model = empty_model |
| 107 | + system1 = model.add_software_system("Software System 1") |
| 108 | + container1 = system1.add_container("Container 1") |
| 109 | + |
| 110 | + system2 = model.add_software_system("Software System 2") |
| 111 | + container2 = system2.add_container("Container 2") |
| 112 | + |
| 113 | + system3 = model.add_software_system("Software System 3") |
| 114 | + container3 = system3.add_container("Container 3") |
| 115 | + |
| 116 | + system4 = model.add_software_system("Software System 4") |
| 117 | + |
| 118 | + container1.uses( |
| 119 | + container2, |
| 120 | + "Uses 1", |
| 121 | + "Technology 1", |
| 122 | + interaction_style=InteractionStyle.Synchronous, |
| 123 | + ) |
| 124 | + container2.uses( |
| 125 | + container3, |
| 126 | + "Uses 2", |
| 127 | + "Technology 2", |
| 128 | + interaction_style=InteractionStyle.Asynchronous, |
| 129 | + ) |
| 130 | + container3.uses(system4) |
| 131 | + |
| 132 | + dev_deployment_node = model.add_deployment_node( |
| 133 | + "Deployment Node", environment="Development" |
| 134 | + ) |
| 135 | + container_instance1 = dev_deployment_node.add_container( |
| 136 | + container1, replicate_relationships=False |
| 137 | + ) |
| 138 | + container_instance2 = dev_deployment_node.add_container( |
| 139 | + container2, replicate_relationships=False |
| 140 | + ) |
| 141 | + container_instance3 = dev_deployment_node.add_container( |
| 142 | + container3, replicate_relationships=False |
| 143 | + ) |
| 144 | + |
| 145 | + assert container_instance1.relationships == set() |
| 146 | + assert container_instance2.relationships == set() |
| 147 | + assert container_instance3.relationships == set() |
0 commit comments