Skip to content

Commit e81249f

Browse files
authored
Fix the exception tests for the latest version of xarray (#827)
* Match error messages for latest xarray version * Make tests compatible to older and newer xarray versions * Use more elegant assertRaisesRegex method for testing exceptions
1 parent 0b06f3f commit e81249f

File tree

1 file changed

+3
-12
lines changed

1 file changed

+3
-12
lines changed

climada/hazard/test/test_base_xarray.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,14 @@ def test_missing_dims(self):
359359

360360
# Now drop variable altogether, should raise an error
361361
ds = ds.drop_vars("time")
362-
with self.assertRaises(RuntimeError) as cm:
362+
with self.assertRaisesRegex(RuntimeError, "time"):
363363
Hazard.from_xarray_raster(ds, "", "")
364-
self.assertIn(
365-
"Dataset is missing dimension/coordinate: time", str(cm.exception)
366-
)
367364

368365
# Expand time again
369366
ds = ds.expand_dims(time=[np.datetime64("2022-01-01")])
370367
hazard = Hazard.from_xarray_raster(ds, "", "")
371368
self._assert_default_types(hazard)
372-
np.testing.assert_array_equal(
373-
hazard.event_name, ["2022-01-01"]
374-
)
369+
np.testing.assert_array_equal(hazard.event_name, ["2022-01-01"])
375370
np.testing.assert_array_equal(
376371
hazard.date, [dt.datetime(2022, 1, 1).toordinal()]
377372
)
@@ -568,17 +563,13 @@ def test_errors(self):
568563
self.assertIn("Unknown coordinates passed: '['bar']'.", str(cm.exception))
569564

570565
# Correctly specified, but the custom dimension does not exist
571-
with self.assertRaises(RuntimeError) as cm:
566+
with self.assertRaisesRegex(RuntimeError, "lalalatitude"):
572567
Hazard.from_xarray_raster_file(
573568
self.netcdf_path,
574569
"",
575570
"",
576571
coordinate_vars=dict(latitude="lalalatitude"),
577572
)
578-
self.assertIn(
579-
"Dataset is missing dimension/coordinate: lalalatitude.", str(cm.exception)
580-
)
581-
582573

583574
# Execute Tests
584575
if __name__ == "__main__":

0 commit comments

Comments
 (0)