|
| 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 software_system element.""" |
| 15 | + |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from structurizr.model.http_health_check import HTTPHealthCheck, HTTPHealthCheckIO |
| 20 | +from structurizr.model.software_system_instance import ( |
| 21 | + SoftwareSystemInstance, |
| 22 | + SoftwareSystemInstanceIO, |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +class MockModel: |
| 27 | + """Implement a mock model for testing.""" |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + """Create an mock software_system for testing.""" |
| 31 | + self.mock_system = MockSystem() |
| 32 | + |
| 33 | + def __iadd__(self, instance: SoftwareSystemInstance): |
| 34 | + """Simulate the model assigning IDs to new elements.""" |
| 35 | + if not instance.id: |
| 36 | + instance.id = "id" |
| 37 | + instance.set_model(self) |
| 38 | + return self |
| 39 | + |
| 40 | + def get_element(self, id: str): |
| 41 | + """Simulate fetching the software_system by ID.""" |
| 42 | + return self.mock_system |
| 43 | + |
| 44 | + |
| 45 | +class MockSystem: |
| 46 | + """Implement a mock system for testing.""" |
| 47 | + |
| 48 | + def __init__(self): |
| 49 | + """Create a new mock.""" |
| 50 | + self.id = "19" |
| 51 | + self.name = "Mock System" |
| 52 | + |
| 53 | + |
| 54 | +@pytest.fixture(scope="function") |
| 55 | +def model_with_system() -> MockModel: |
| 56 | + """Provide a mock model for test cases to use.""" |
| 57 | + return MockModel() |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize( |
| 61 | + "attributes", |
| 62 | + [ |
| 63 | + pytest.param({}, marks=pytest.mark.raises(exception=TypeError)), |
| 64 | + { |
| 65 | + "environment": "Development", |
| 66 | + "software_system": MockSystem(), |
| 67 | + "instance_id": "17", |
| 68 | + "properties": {"a": "b"}, |
| 69 | + }, |
| 70 | + ], |
| 71 | +) |
| 72 | +def test_software_system_instance_init(attributes): |
| 73 | + """Expect proper initialization from arguments.""" |
| 74 | + instance = SoftwareSystemInstance(**attributes) |
| 75 | + for attr, expected in attributes.items(): |
| 76 | + assert getattr(instance, attr) == expected |
| 77 | + |
| 78 | + |
| 79 | +def test_software_system_instance_hydration_retrieves_software_system_from_id( |
| 80 | + model_with_system, |
| 81 | +): |
| 82 | + """Check that the software_system instance is retrieved on hydration.""" |
| 83 | + io = SoftwareSystemInstanceIO( |
| 84 | + software_system_id="19", instance_id=3, environment="Live" |
| 85 | + ) |
| 86 | + |
| 87 | + instance = SoftwareSystemInstance.hydrate(io, model_with_system) |
| 88 | + |
| 89 | + assert instance.instance_id == 3 |
| 90 | + assert instance.environment == "Live" |
| 91 | + assert instance.software_system is model_with_system.mock_system |
| 92 | + assert instance.software_system_id == "19" |
| 93 | + |
| 94 | + |
| 95 | +def test_software_system_instance_name_is_software_system_name(model_with_system): |
| 96 | + """Ensure software_system instance takes its name from its software system.""" |
| 97 | + io = SoftwareSystemInstanceIO( |
| 98 | + software_system_id="19", instance_id=3, environment="Live" |
| 99 | + ) |
| 100 | + |
| 101 | + instance = SoftwareSystemInstance.hydrate(io, model_with_system) |
| 102 | + |
| 103 | + assert instance.name == "Mock System" |
| 104 | + |
| 105 | + |
| 106 | +def test_software_system_instance_hyrdates_http_health_checks(model_with_system): |
| 107 | + """Check that health checks are hydrated into the SoftwareSystemInstance.""" |
| 108 | + health_check_io = HTTPHealthCheckIO(name="name", url="http://a.b.com") |
| 109 | + io = SoftwareSystemInstanceIO( |
| 110 | + software_system_id="19", |
| 111 | + instance_id=3, |
| 112 | + environment="Live", |
| 113 | + health_checks=[health_check_io], |
| 114 | + ) |
| 115 | + |
| 116 | + instance = SoftwareSystemInstance.hydrate(io, model_with_system) |
| 117 | + |
| 118 | + assert len(instance.health_checks) == 1 |
| 119 | + assert list(instance.health_checks)[0].name == "name" |
| 120 | + |
| 121 | + |
| 122 | +def test_software_system_instance_serialization(model_with_system): |
| 123 | + """Test that system instances serialise properly.""" |
| 124 | + health_check = HTTPHealthCheck(name="health", url="http://a.b.com") |
| 125 | + instance = SoftwareSystemInstance( |
| 126 | + software_system=model_with_system.mock_system, |
| 127 | + instance_id=7, |
| 128 | + health_checks=[health_check], |
| 129 | + ) |
| 130 | + |
| 131 | + io = SoftwareSystemInstanceIO.from_orm(instance) |
| 132 | + |
| 133 | + assert io.software_system_id == "19" |
| 134 | + assert io.instance_id == 7 |
| 135 | + assert len(io.health_checks) == 1 |
| 136 | + assert io.health_checks[0].name == "health" |
0 commit comments