|
| 1 | +import datetime |
| 2 | +import unittest |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | + |
| 8 | +from climada.entity.exposures import Exposures |
| 9 | +from climada.entity.impact_funcs import ImpactFunc, ImpactFuncSet |
| 10 | +from climada.entity.measures.base import Measure |
| 11 | +from climada.hazard import Hazard |
| 12 | +from climada.trajectories.snapshot import Snapshot |
| 13 | +from climada.util.constants import EXP_DEMO_H5, HAZ_DEMO_H5 |
| 14 | + |
| 15 | + |
| 16 | +class TestSnapshot(unittest.TestCase): |
| 17 | + |
| 18 | + def setUp(self): |
| 19 | + # Create mock objects for testing |
| 20 | + self.mock_exposure = Exposures.from_hdf5(EXP_DEMO_H5) |
| 21 | + self.mock_hazard = Hazard.from_hdf5(HAZ_DEMO_H5) |
| 22 | + self.mock_impfset = ImpactFuncSet( |
| 23 | + [ |
| 24 | + ImpactFunc( |
| 25 | + "TC", |
| 26 | + 3, |
| 27 | + intensity=np.array([0, 20]), |
| 28 | + mdd=np.array([0, 0.5]), |
| 29 | + paa=np.array([0, 1]), |
| 30 | + ) |
| 31 | + ] |
| 32 | + ) |
| 33 | + self.mock_measure = MagicMock(spec=Measure) |
| 34 | + self.mock_measure.name = "Test Measure" |
| 35 | + |
| 36 | + # Setup mock return values for measure.apply |
| 37 | + self.mock_modified_exposure = MagicMock(spec=Exposures) |
| 38 | + self.mock_modified_hazard = MagicMock(spec=Hazard) |
| 39 | + self.mock_modified_impfset = MagicMock(spec=ImpactFuncSet) |
| 40 | + self.mock_measure.apply.return_value = ( |
| 41 | + self.mock_modified_exposure, |
| 42 | + self.mock_modified_impfset, |
| 43 | + self.mock_modified_hazard, |
| 44 | + ) |
| 45 | + |
| 46 | + def test_init_with_int_date(self): |
| 47 | + snapshot = Snapshot( |
| 48 | + exposure=self.mock_exposure, |
| 49 | + hazard=self.mock_hazard, |
| 50 | + impfset=self.mock_impfset, |
| 51 | + date=2023, |
| 52 | + ) |
| 53 | + self.assertEqual(snapshot.date, datetime.date(2023, 1, 1)) |
| 54 | + |
| 55 | + def test_init_with_str_date(self): |
| 56 | + snapshot = Snapshot( |
| 57 | + exposure=self.mock_exposure, |
| 58 | + hazard=self.mock_hazard, |
| 59 | + impfset=self.mock_impfset, |
| 60 | + date="2023-01-01", |
| 61 | + ) |
| 62 | + self.assertEqual(snapshot.date, datetime.date(2023, 1, 1)) |
| 63 | + |
| 64 | + def test_init_with_date_object(self): |
| 65 | + date_obj = datetime.date(2023, 1, 1) |
| 66 | + snapshot = Snapshot( |
| 67 | + exposure=self.mock_exposure, |
| 68 | + hazard=self.mock_hazard, |
| 69 | + impfset=self.mock_impfset, |
| 70 | + date=date_obj, |
| 71 | + ) |
| 72 | + self.assertEqual(snapshot.date, date_obj) |
| 73 | + |
| 74 | + def test_init_with_invalid_date(self): |
| 75 | + with self.assertRaises(ValueError): |
| 76 | + Snapshot( |
| 77 | + exposure=self.mock_exposure, |
| 78 | + hazard=self.mock_hazard, |
| 79 | + impfset=self.mock_impfset, |
| 80 | + date="invalid-date", |
| 81 | + ) |
| 82 | + |
| 83 | + def test_init_with_invalid_type(self): |
| 84 | + with self.assertRaises(TypeError): |
| 85 | + Snapshot( |
| 86 | + exposure=self.mock_exposure, |
| 87 | + hazard=self.mock_hazard, |
| 88 | + impfset=self.mock_impfset, |
| 89 | + date=2023.5, # type: ignore |
| 90 | + ) |
| 91 | + |
| 92 | + def test_properties(self): |
| 93 | + snapshot = Snapshot( |
| 94 | + exposure=self.mock_exposure, |
| 95 | + hazard=self.mock_hazard, |
| 96 | + impfset=self.mock_impfset, |
| 97 | + date=2023, |
| 98 | + ) |
| 99 | + |
| 100 | + # We want a new reference |
| 101 | + self.assertIsNot(snapshot.exposure, self.mock_exposure) |
| 102 | + self.assertIsNot(snapshot.hazard, self.mock_hazard) |
| 103 | + self.assertIsNot(snapshot.impfset, self.mock_impfset) |
| 104 | + |
| 105 | + # But we want equality |
| 106 | + pd.testing.assert_frame_equal(snapshot.exposure.gdf, self.mock_exposure.gdf) |
| 107 | + |
| 108 | + self.assertEqual(snapshot.hazard.haz_type, self.mock_hazard.haz_type) |
| 109 | + self.assertEqual(snapshot.hazard.intensity.nnz, self.mock_hazard.intensity.nnz) |
| 110 | + self.assertEqual(snapshot.hazard.size, self.mock_hazard.size) |
| 111 | + |
| 112 | + self.assertEqual(snapshot.impfset, self.mock_impfset) |
| 113 | + |
| 114 | + def test_apply_measure(self): |
| 115 | + snapshot = Snapshot( |
| 116 | + exposure=self.mock_exposure, |
| 117 | + hazard=self.mock_hazard, |
| 118 | + impfset=self.mock_impfset, |
| 119 | + date=2023, |
| 120 | + ) |
| 121 | + new_snapshot = snapshot.apply_measure(self.mock_measure) |
| 122 | + |
| 123 | + self.assertIsNotNone(new_snapshot.measure) |
| 124 | + self.assertEqual(new_snapshot.measure.name, "Test Measure") # type: ignore |
| 125 | + self.assertEqual(new_snapshot.exposure, self.mock_modified_exposure) |
| 126 | + self.assertEqual(new_snapshot.hazard, self.mock_modified_hazard) |
| 127 | + self.assertEqual(new_snapshot.impfset, self.mock_modified_impfset) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + TESTS = unittest.TestLoader().loadTestsFromTestCase(TestSnapshot) |
| 132 | + unittest.TextTestRunner(verbosity=2).run(TESTS) |
0 commit comments