Skip to content

Commit

Permalink
Fix the exception tests for the latest version of xarray (#827)
Browse files Browse the repository at this point in the history
* Match error messages for latest xarray version

* Make tests compatible to older and newer xarray versions

* Use more elegant assertRaisesRegex method for testing exceptions
  • Loading branch information
peanutfun authored Dec 13, 2023
1 parent 0b06f3f commit e81249f
Showing 1 changed file with 3 additions and 12 deletions.
15 changes: 3 additions & 12 deletions climada/hazard/test/test_base_xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,19 +359,14 @@ def test_missing_dims(self):

# Now drop variable altogether, should raise an error
ds = ds.drop_vars("time")
with self.assertRaises(RuntimeError) as cm:
with self.assertRaisesRegex(RuntimeError, "time"):
Hazard.from_xarray_raster(ds, "", "")
self.assertIn(
"Dataset is missing dimension/coordinate: time", str(cm.exception)
)

# Expand time again
ds = ds.expand_dims(time=[np.datetime64("2022-01-01")])
hazard = Hazard.from_xarray_raster(ds, "", "")
self._assert_default_types(hazard)
np.testing.assert_array_equal(
hazard.event_name, ["2022-01-01"]
)
np.testing.assert_array_equal(hazard.event_name, ["2022-01-01"])
np.testing.assert_array_equal(
hazard.date, [dt.datetime(2022, 1, 1).toordinal()]
)
Expand Down Expand Up @@ -568,17 +563,13 @@ def test_errors(self):
self.assertIn("Unknown coordinates passed: '['bar']'.", str(cm.exception))

# Correctly specified, but the custom dimension does not exist
with self.assertRaises(RuntimeError) as cm:
with self.assertRaisesRegex(RuntimeError, "lalalatitude"):
Hazard.from_xarray_raster_file(
self.netcdf_path,
"",
"",
coordinate_vars=dict(latitude="lalalatitude"),
)
self.assertIn(
"Dataset is missing dimension/coordinate: lalalatitude.", str(cm.exception)
)


# Execute Tests
if __name__ == "__main__":
Expand Down

0 comments on commit e81249f

Please sign in to comment.