Skip to content

Commit 9aec92f

Browse files
committed
test: adds lots of unittests, and placeholders
1 parent 9711379 commit 9aec92f

File tree

6 files changed

+1049
-0
lines changed

6 files changed

+1049
-0
lines changed

climada/test/test_trajectories.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# 100% Coverage goal:
2+
## Integration test for risk period with periods and freq defined
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
import numpy as np
27+
from scipy.sparse import csr_matrix
28+
29+
from climada.trajectories.impact_calc_strat import (
30+
Impact,
31+
ImpactCalcComputation,
32+
Snapshot,
33+
)
34+
35+
36+
class TestImpactCalcComputation(unittest.TestCase):
37+
def setUp(self):
38+
self.mock_snapshot0 = MagicMock(spec=Snapshot)
39+
self.mock_snapshot1 = MagicMock(spec=Snapshot)
40+
41+
self.impact_calc_computation = ImpactCalcComputation()
42+
43+
@patch.object(ImpactCalcComputation, "_calculate_impacts_for_snapshots")
44+
@patch.object(ImpactCalcComputation, "_apply_risk_transfer")
45+
def test_compute_impacts(
46+
self, mock_apply_risk_transfer, mock_calculate_impacts_for_snapshots
47+
):
48+
mock_impacts = (
49+
MagicMock(spec=Impact),
50+
MagicMock(spec=Impact),
51+
MagicMock(spec=Impact),
52+
MagicMock(spec=Impact),
53+
)
54+
mock_calculate_impacts_for_snapshots.return_value = mock_impacts
55+
56+
result = self.impact_calc_computation.compute_impacts(
57+
self.mock_snapshot0, self.mock_snapshot1, 0.1, 0.9, False
58+
)
59+
60+
self.assertEqual(result, mock_impacts)
61+
mock_calculate_impacts_for_snapshots.assert_called_once_with(
62+
self.mock_snapshot0, self.mock_snapshot1
63+
)
64+
mock_apply_risk_transfer.assert_called_once_with(mock_impacts, 0.1, 0.9, False)
65+
66+
def test_calculate_impacts_for_snapshots(self):
67+
mock_imp_E0H0 = MagicMock(spec=Impact)
68+
mock_imp_E1H0 = MagicMock(spec=Impact)
69+
mock_imp_E0H1 = MagicMock(spec=Impact)
70+
mock_imp_E1H1 = MagicMock(spec=Impact)
71+
72+
with patch(
73+
"climada.trajectories.impact_calc_strat.ImpactCalc"
74+
) as mock_impact_calc:
75+
mock_impact_calc.return_value.impact.side_effect = [
76+
mock_imp_E0H0,
77+
mock_imp_E1H0,
78+
mock_imp_E0H1,
79+
mock_imp_E1H1,
80+
]
81+
82+
result = self.impact_calc_computation._calculate_impacts_for_snapshots(
83+
self.mock_snapshot0, self.mock_snapshot1
84+
)
85+
86+
self.assertEqual(
87+
result, (mock_imp_E0H0, mock_imp_E1H0, mock_imp_E0H1, mock_imp_E1H1)
88+
)
89+
90+
def test_apply_risk_transfer(self):
91+
mock_imp_E0H0 = MagicMock(spec=Impact)
92+
mock_imp_E0H0.imp_mat = MagicMock(spec=csr_matrix)
93+
94+
mock_imp_E1H0 = MagicMock(spec=Impact)
95+
mock_imp_E1H0.imp_mat = MagicMock(spec=csr_matrix)
96+
97+
mock_imp_E0H1 = MagicMock(spec=Impact)
98+
mock_imp_E0H1.imp_mat = MagicMock(spec=csr_matrix)
99+
100+
mock_imp_E1H1 = MagicMock(spec=Impact)
101+
mock_imp_E1H1.imp_mat = MagicMock(spec=csr_matrix)
102+
103+
mock_impacts = (mock_imp_E0H0, mock_imp_E1H0, mock_imp_E0H1, mock_imp_E1H1)
104+
105+
mock_imp_resi = MagicMock(spec=csr_matrix)
106+
107+
with patch.object(
108+
self.impact_calc_computation,
109+
"calculate_residual_or_risk_transfer_impact_matrix",
110+
) as mock_calc_risk_transfer:
111+
mock_calc_risk_transfer.return_value = mock_imp_resi
112+
self.impact_calc_computation._apply_risk_transfer(
113+
mock_impacts, 0.1, 0.9, False
114+
)
115+
116+
self.assertIs(mock_impacts[0].imp_mat, mock_imp_resi)
117+
self.assertIs(mock_impacts[1].imp_mat, mock_imp_resi)
118+
self.assertIs(mock_impacts[2].imp_mat, mock_imp_resi)
119+
self.assertIs(mock_impacts[3].imp_mat, mock_imp_resi)
120+
121+
def test_calculate_residual_or_risk_transfer_impact_matrix(self):
122+
imp_mat = MagicMock()
123+
imp_mat.sum.return_value.A1 = np.array([100, 200, 300])
124+
imp_mat.multiply.return_value = "rescaled_matrix"
125+
126+
result = self.impact_calc_computation.calculate_residual_or_risk_transfer_impact_matrix(
127+
imp_mat, 0.1, 0.9, True
128+
)
129+
self.assertEqual(result, "rescaled_matrix")
130+
131+
result = self.impact_calc_computation.calculate_residual_or_risk_transfer_impact_matrix(
132+
imp_mat, 0.1, 0.9, False
133+
)
134+
self.assertEqual(result, "rescaled_matrix")
135+
136+
137+
if __name__ == "__main__":
138+
unittest.main()
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 interpolation
20+
21+
"""
22+
23+
import unittest
24+
from unittest.mock import MagicMock
25+
26+
import numpy as np
27+
from scipy.sparse import csr_matrix
28+
29+
from climada.trajectories.interpolation import (
30+
ExponentialInterpolation,
31+
LinearInterpolation,
32+
)
33+
34+
35+
class TestLinearInterpolation(unittest.TestCase):
36+
def setUp(self):
37+
# Create mock impact matrices for testing
38+
self.imp0 = MagicMock()
39+
self.imp1 = MagicMock()
40+
self.imp0.imp_mat = csr_matrix(np.array([[1, 2], [3, 4]]))
41+
self.imp1.imp_mat = csr_matrix(np.array([[5, 6], [7, 8]]))
42+
self.time_points = 5
43+
44+
# Create an instance of LinearInterpolation
45+
self.linear_interpolation = LinearInterpolation()
46+
47+
def test_interpolate(self):
48+
result = self.linear_interpolation.interpolate(
49+
self.imp0, self.imp1, self.time_points
50+
)
51+
self.assertEqual(len(result), self.time_points)
52+
for mat in result:
53+
self.assertIsInstance(mat, csr_matrix)
54+
55+
dense = np.array([r.todense() for r in result])
56+
expected = np.array(
57+
[
58+
[[1.0, 2.0], [3.0, 4.0]],
59+
[[2.0, 3.0], [4.0, 5.0]],
60+
[[3.0, 4.0], [5.0, 6.0]],
61+
[[4.0, 5.0], [6.0, 7.0]],
62+
[[5.0, 6.0], [7.0, 8.0]],
63+
]
64+
)
65+
np.testing.assert_array_equal(dense, expected)
66+
67+
def test_interpolate_inconsistent_shape(self):
68+
imp0 = MagicMock()
69+
imp1 = MagicMock()
70+
imp0.imp_mat = csr_matrix(np.array([[1, 2], [3, 4]]))
71+
imp1.imp_mat = csr_matrix(np.array([[5, 6, 7], [8, 9, 10]])) # Different shape
72+
73+
with self.assertRaises(ValueError):
74+
self.linear_interpolation.interpolate(imp0, imp1, self.time_points)
75+
76+
77+
class TestExponentialInterpolation(unittest.TestCase):
78+
def setUp(self):
79+
# Create mock impact matrices for testing
80+
self.imp0 = MagicMock()
81+
self.imp1 = MagicMock()
82+
self.imp0.imp_mat = csr_matrix(np.array([[1, 2], [3, 4]]))
83+
self.imp1.imp_mat = csr_matrix(np.array([[5, 6], [7, 8]]))
84+
self.time_points = 5
85+
86+
# Create an instance of ExponentialInterpolation
87+
self.exponential_interpolation = ExponentialInterpolation()
88+
89+
def test_interpolate(self):
90+
result = self.exponential_interpolation.interpolate(
91+
self.imp0, self.imp1, self.time_points
92+
)
93+
self.assertEqual(len(result), self.time_points)
94+
for mat in result:
95+
self.assertIsInstance(mat, csr_matrix)
96+
97+
dense = np.array([r.todense() for r in result])
98+
expected = np.array(
99+
[
100+
[[1.0, 2.0], [3.0, 4.0]],
101+
[[1.49534878, 2.63214803], [3.70779275, 4.75682846]],
102+
[[2.23606798, 3.46410162], [4.58257569, 5.65685425]],
103+
[[3.34370152, 4.55901411], [5.66374698, 6.72717132]],
104+
[[5.0, 6.0], [7.0, 8.0]],
105+
]
106+
)
107+
np.testing.assert_array_almost_equal(dense, expected)
108+
109+
def test_interpolate_inconsistent_shape(self):
110+
imp0 = MagicMock()
111+
imp1 = MagicMock()
112+
imp0.imp_mat = csr_matrix(np.array([[1, 2], [3, 4]]))
113+
imp1.imp_mat = csr_matrix(np.array([[5, 6, 7], [8, 9, 10]])) # Different shape
114+
115+
with self.assertRaises(ValueError):
116+
self.exponential_interpolation.interpolate(imp0, imp1, self.time_points)
117+
118+
119+
if __name__ == "__main__":
120+
unittest.main()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
unit tests for risk_trajectory
20+
21+
"""

0 commit comments

Comments
 (0)