Skip to content

Commit f6d0373

Browse files
committed
Automatize Hazard attribute testing
1 parent 9aa7e4b commit f6d0373

File tree

1 file changed

+18
-58
lines changed

1 file changed

+18
-58
lines changed

climada/hazard/test/test_forecast.py

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import numpy.testing as npt
2424
import pandas as pd
2525
import pytest
26+
from scipy.sparse import csr_matrix
2627

2728
from climada.hazard.base import Hazard
2829
from climada.hazard.forecast import HazardForecast
@@ -37,7 +38,7 @@ def haz_kwargs():
3738

3839

3940
@pytest.fixture
40-
def dummy_hazard(haz_kwargs):
41+
def hazard(haz_kwargs):
4142
return Hazard(**haz_kwargs)
4243

4344

@@ -60,71 +61,30 @@ def haz_fc(lead_time, member, haz_kwargs):
6061
)
6162

6263

64+
def assert_hazard_kwargs(hazard: Hazard, **kwargs):
65+
for key, value in kwargs.items():
66+
attr = getattr(hazard, key)
67+
if isinstance(value, (np.ndarray, list)):
68+
npt.assert_array_equal(attr, value)
69+
elif isinstance(value, csr_matrix):
70+
npt.assert_array_equal(attr.todense(), value.todense())
71+
else:
72+
assert attr == value
73+
74+
6375
def test_init_hazard_forecast(haz_fc, member, lead_time, haz_kwargs):
6476
assert isinstance(haz_fc, HazardForecast)
65-
npt.assert_array_equal(
66-
haz_fc.lead_time,
67-
lead_time,
68-
)
77+
npt.assert_array_equal(haz_fc.lead_time, lead_time)
6978
assert haz_fc.lead_time.dtype == lead_time.dtype
7079
npt.assert_array_equal(haz_fc.member, member)
71-
assert haz_fc.haz_type == haz_kwargs["haz_type"]
72-
assert haz_fc.pool == haz_kwargs["pool"]
73-
assert haz_fc.units == haz_kwargs["units"]
74-
assert haz_fc.centroids == haz_kwargs["centroids"]
75-
npt.assert_array_equal(haz_fc.event_id, haz_kwargs["event_id"])
76-
npt.assert_array_equal(haz_fc.frequency, haz_kwargs["frequency"])
77-
assert haz_fc.frequency_unit == haz_kwargs["frequency_unit"]
78-
npt.assert_array_equal(haz_fc.event_name, haz_kwargs["event_name"])
79-
npt.assert_array_equal(haz_fc.date, haz_kwargs["date"])
80-
npt.assert_array_equal(haz_fc.orig, haz_kwargs["orig"])
81-
npt.assert_array_equal(
82-
haz_fc.intensity.todense(), haz_kwargs["intensity"].todense()
83-
)
84-
npt.assert_array_equal(haz_fc.fraction.todense(), haz_kwargs["fraction"].todense())
80+
assert_hazard_kwargs(haz_fc, **haz_kwargs)
8581

8682

87-
def test_from_hazard(lead_time, member, dummy_hazard):
83+
def test_from_hazard(lead_time, member, hazard, haz_kwargs):
8884
haz_fc_from_haz = HazardForecast.from_hazard(
89-
dummy_hazard, lead_time=lead_time, member=member
85+
hazard, lead_time=lead_time, member=member
9086
)
91-
9287
assert isinstance(haz_fc_from_haz, HazardForecast)
9388
npt.assert_array_equal(haz_fc_from_haz.lead_time, lead_time)
9489
npt.assert_array_equal(haz_fc_from_haz.member, member)
95-
assert haz_fc_from_haz.haz_type == dummy_hazard.haz_type
96-
assert haz_fc_from_haz.pool == dummy_hazard.pool
97-
assert haz_fc_from_haz.units == dummy_hazard.units
98-
assert haz_fc_from_haz.centroids == dummy_hazard.centroids
99-
npt.assert_array_equal(haz_fc_from_haz.event_id, dummy_hazard.event_id)
100-
npt.assert_array_equal(haz_fc_from_haz.frequency, dummy_hazard.frequency)
101-
assert haz_fc_from_haz.frequency_unit == dummy_hazard.frequency_unit
102-
npt.assert_array_equal(haz_fc_from_haz.event_name, dummy_hazard.event_name)
103-
npt.assert_array_equal(haz_fc_from_haz.date, dummy_hazard.date)
104-
npt.assert_array_equal(haz_fc_from_haz.orig, dummy_hazard.orig)
105-
npt.assert_array_equal(
106-
haz_fc_from_haz.intensity.todense(), dummy_hazard.intensity.todense()
107-
)
108-
npt.assert_array_equal(
109-
haz_fc_from_haz.fraction.todense(), dummy_hazard.fraction.todense()
110-
)
111-
112-
113-
@pytest.fixture
114-
def hazard():
115-
return Hazard()
116-
117-
118-
def test_empty_hazard(hazard):
119-
assert hazard.size == 0
120-
assert hazard.haz_type == ""
121-
122-
123-
class TestSomething:
124-
125-
@pytest.fixture(autouse=True)
126-
def haz_type(self, hazard):
127-
hazard.haz_type = "foo"
128-
129-
def test_haz_type(self, hazard):
130-
assert hazard.haz_type == "foo"
90+
assert_hazard_kwargs(haz_fc_from_haz, **haz_kwargs)

0 commit comments

Comments
 (0)