|
| 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 | +import unittest |
| 24 | +from unittest.mock import MagicMock, patch |
| 25 | + |
| 26 | +from climada.engine import Impact |
| 27 | +from climada.entity import ImpactFuncSet |
| 28 | +from climada.entity.exposures import Exposures |
| 29 | +from climada.hazard import Hazard |
| 30 | +from climada.trajectories import Snapshot |
| 31 | +from climada.trajectories.impact_calc_strat import ImpactCalcComputation |
| 32 | + |
| 33 | + |
| 34 | +class TestImpactCalcComputation(unittest.TestCase): |
| 35 | + def setUp(self): |
| 36 | + self.mock_snapshot0 = MagicMock(spec=Snapshot) |
| 37 | + self.mock_snapshot0.exposure = MagicMock(spec=Exposures) |
| 38 | + self.mock_snapshot0.hazard = MagicMock(spec=Hazard) |
| 39 | + self.mock_snapshot0.impfset = MagicMock(spec=ImpactFuncSet) |
| 40 | + self.mock_snapshot1 = MagicMock(spec=Snapshot) |
| 41 | + self.mock_snapshot1.exposure = MagicMock(spec=Exposures) |
| 42 | + self.mock_snapshot1.hazard = MagicMock(spec=Hazard) |
| 43 | + self.mock_snapshot1.impfset = MagicMock(spec=ImpactFuncSet) |
| 44 | + |
| 45 | + self.impact_calc_computation = ImpactCalcComputation() |
| 46 | + |
| 47 | + @patch.object(ImpactCalcComputation, "compute_impacts_pre_transfer") |
| 48 | + def test_compute_impacts(self, mock_calculate_impacts_for_snapshots): |
| 49 | + mock_impacts = MagicMock(spec=Impact) |
| 50 | + mock_calculate_impacts_for_snapshots.return_value = mock_impacts |
| 51 | + |
| 52 | + result = self.impact_calc_computation.compute_impacts( |
| 53 | + exp=self.mock_snapshot0.exposure, |
| 54 | + haz=self.mock_snapshot0.hazard, |
| 55 | + vul=self.mock_snapshot0.impfset, |
| 56 | + ) |
| 57 | + |
| 58 | + self.assertEqual(result, mock_impacts) |
| 59 | + mock_calculate_impacts_for_snapshots.assert_called_once_with( |
| 60 | + self.mock_snapshot0.exposure, |
| 61 | + self.mock_snapshot0.hazard, |
| 62 | + self.mock_snapshot0.impfset, |
| 63 | + ) |
| 64 | + |
| 65 | + def test_calculate_impacts_for_snapshots(self): |
| 66 | + mock_imp_E0H0 = MagicMock(spec=Impact) |
| 67 | + |
| 68 | + with patch( |
| 69 | + "climada.trajectories.impact_calc_strat.ImpactCalc" |
| 70 | + ) as mock_impact_calc: |
| 71 | + mock_impact_calc.return_value.impact.side_effect = [mock_imp_E0H0] |
| 72 | + |
| 73 | + result = self.impact_calc_computation.compute_impacts_pre_transfer( |
| 74 | + exp=self.mock_snapshot0.exposure, |
| 75 | + haz=self.mock_snapshot0.hazard, |
| 76 | + vul=self.mock_snapshot0.impfset, |
| 77 | + ) |
| 78 | + |
| 79 | + self.assertEqual(result, mock_imp_E0H0) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + TESTS = unittest.TestLoader().loadTestsFromTestCase(TestImpactCalcComputation) |
| 84 | + unittest.TextTestRunner(verbosity=2).run(TESTS) |
0 commit comments