|
| 1 | +""" |
| 2 | +This file is part of CLIMADA. |
| 3 | +
|
| 4 | +Copyright (C) 2017 ETH Zurich, CLIMADA contributors listed in AUTHORS. |
| 5 | +
|
| 6 | +CLIMADA is free software: you can redistribute it and/or modify it under the |
| 7 | +terms of the GNU General Public License as published by the Free |
| 8 | +Software Foundation, version 3. |
| 9 | +
|
| 10 | +CLIMADA is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 12 | +PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 | +
|
| 14 | +You should have received a copy of the GNU General Public License along |
| 15 | +with CLIMADA. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +
|
| 17 | +--- |
| 18 | +
|
| 19 | +Tests for impact_calc_strat |
| 20 | +
|
| 21 | +""" |
| 22 | + |
| 23 | +from unittest.mock import MagicMock, patch |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +from climada.engine import Impact |
| 28 | +from climada.entity import ImpactFuncSet |
| 29 | +from climada.entity.exposures import Exposures |
| 30 | +from climada.hazard import Hazard |
| 31 | +from climada.trajectories import Snapshot |
| 32 | +from climada.trajectories.impact_calc_strat import ( |
| 33 | + ImpactCalcComputation, |
| 34 | + ImpactComputationStrategy, |
| 35 | +) |
| 36 | + |
| 37 | +# --- Fixtures --- |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture |
| 41 | +def mock_snapshot(): |
| 42 | + """Provides a snapshot with mocked exposure, hazard, and impact functions.""" |
| 43 | + snap = MagicMock(spec=Snapshot) |
| 44 | + snap.exposure = MagicMock(spec=Exposures) |
| 45 | + snap.hazard = MagicMock(spec=Hazard) |
| 46 | + snap.impfset = MagicMock(spec=ImpactFuncSet) |
| 47 | + return snap |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def strategy(): |
| 52 | + """Provides an instance of the ImpactCalcComputation strategy.""" |
| 53 | + return ImpactCalcComputation() |
| 54 | + |
| 55 | + |
| 56 | +# --- Tests --- |
| 57 | +def test_interface_compliance(strategy): |
| 58 | + """Ensure the class correctly inherits from the Abstract Base Class.""" |
| 59 | + assert isinstance(strategy, ImpactComputationStrategy) |
| 60 | + assert isinstance(strategy, ImpactCalcComputation) |
| 61 | + |
| 62 | + |
| 63 | +def test_compute_impacts(strategy, mock_snapshot): |
| 64 | + """Test that compute_impacts calls the pre-transfer method correctly.""" |
| 65 | + mock_impacts = MagicMock(spec=Impact) |
| 66 | + |
| 67 | + # We patch the ImpactCalc within trajectories |
| 68 | + with patch("climada.trajectories.impact_calc_strat.ImpactCalc") as mock_ImpactCalc: |
| 69 | + mock_ImpactCalc.return_value.impact.return_value = mock_impacts |
| 70 | + result = strategy.compute_impacts( |
| 71 | + exp=mock_snapshot.exposure, |
| 72 | + haz=mock_snapshot.hazard, |
| 73 | + vul=mock_snapshot.impfset, |
| 74 | + ) |
| 75 | + mock_ImpactCalc.assert_called_once_with( |
| 76 | + exposures=mock_snapshot.exposure, |
| 77 | + impfset=mock_snapshot.impfset, |
| 78 | + hazard=mock_snapshot.hazard, |
| 79 | + ) |
| 80 | + mock_ImpactCalc.return_value.impact.assert_called_once() |
| 81 | + assert result == mock_impacts |
| 82 | + |
| 83 | + |
| 84 | +def test_cannot_instantiate_abstract_base_class(): |
| 85 | + """Ensure ImpactComputationStrategy cannot be instantiated directly.""" |
| 86 | + with pytest.raises(TypeError, match="Can't instantiate abstract class"): |
| 87 | + ImpactComputationStrategy() # type: ignore |
| 88 | + |
| 89 | + |
| 90 | +@pytest.mark.parametrize("invalid_input", [None, 123, "string"]) |
| 91 | +def test_compute_impacts_type_errors(strategy, invalid_input): |
| 92 | + """ |
| 93 | + Smoke test: Ensure that if ImpactCalc raises errors due to bad input, |
| 94 | + the strategy correctly propagates them. |
| 95 | + """ |
| 96 | + with pytest.raises(AttributeError): |
| 97 | + strategy.compute_impacts(invalid_input, invalid_input, invalid_input) |
0 commit comments