Skip to content

Commit bd62ddc

Browse files
add test
1 parent fb7d78d commit bd62ddc

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

climada/entity/impact_funcs/test/test_tc.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,46 @@ def test_get_countries_per_region(self):
168168
self.assertListEqual(out[2], [124, 840])
169169
self.assertListEqual(out[3], ["CAN", "USA"])
170170

171+
def test_get_region_per_countries(self):
172+
"""Test static get_regions_per_countries()"""
173+
ifs = ImpfSetTropCyclone()
174+
out = ifs.get_regions_per_countries(countries=["CHE"], code_type="ISO3A")
175+
self.assertEqual(out[0][0], 10)
176+
self.assertEqual(out[1][0], "ROW")
177+
self.assertEqual(out[2][0], "Rest of The World")
178+
out = ifs.get_regions_per_countries(countries=[756], code_type="ISO3N")
179+
self.assertEqual(out[0][0], 10)
180+
self.assertEqual(out[1][0], "ROW")
181+
self.assertEqual(out[2][0], "Rest of The World")
182+
with self.assertRaises(ValueError) as context:
183+
ImpfSetTropCyclone.get_regions_per_countries(
184+
countries=["SWE"], code_type="invalid_code"
185+
)
186+
self.assertEqual(
187+
str(context.exception), "code_type must be either 'iso3a' or 'iso3n'"
188+
)
189+
with self.assertRaises(ValueError) as context:
190+
ImpfSetTropCyclone.get_regions_per_countries(
191+
countries=["SWE", 840], code_type="ISO3A"
192+
)
193+
self.assertEqual(
194+
str(context.exception), "All elements in the list must be of the same type."
195+
)
196+
with self.assertRaises(ValueError) as context:
197+
ImpfSetTropCyclone.get_regions_per_countries(
198+
countries=[840, 124], code_type="ISO3A"
199+
)
200+
self.assertEqual(
201+
str(context.exception), "ISO3A code type cannot have integer values."
202+
)
203+
with self.assertRaises(ValueError) as context:
204+
ImpfSetTropCyclone.get_regions_per_countries(
205+
countries=["MEX"], code_type="ISO3N"
206+
)
207+
self.assertEqual(
208+
str(context.exception), "ISO3N code type cannot have string values."
209+
)
210+
171211

172212
# Execute Tests
173213
if __name__ == "__main__":

climada/entity/impact_funcs/trop_cyclone.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,12 @@ def get_regions_per_countries(
480480

481481
if code_type not in {"ISO3A", "ISO3N"}:
482482
raise ValueError("code_type must be either 'iso3a' or 'iso3n'")
483+
elif not all(isinstance(country, type(countries[0])) for country in countries):
484+
raise ValueError("All elements in the list must be of the same type.")
485+
elif code_type == "ISO3A" and isinstance((countries[0]), int):
486+
raise ValueError("ISO3A code type cannot have integer values.")
487+
elif code_type == "ISO3N" and isinstance((countries[0]), str):
488+
raise ValueError("ISO3N code type cannot have string values.")
483489

484490
country_dict = getattr(CountryCode, code_type).value
485491
# Find region

0 commit comments

Comments
 (0)