Skip to content

Add some basic tests #227

Add some basic tests

Add some basic tests #227

This check has been archived and is scheduled for deletion. Learn more about checks retention
GitHub Actions / Unit Test Results Python 3.10 failed Dec 12, 2023 in 0s

2 fail, 632 pass in 6m 27s

634 tests  +4   632 ✔️ +4   6m 27s ⏱️ -1s
    1 suites ±0       0 💤 ±0 
    1 files   ±0       2 ±0 

Results for commit 61f3c6b. ± Comparison against earlier commit f6b72ed.

Annotations

Check warning on line 0 in climada.hazard.test.test_base_xarray.TestReadDefaultNetCDF

See this annotation in the file changed.

@github-actions github-actions / Unit Test Results Python 3.10

test_missing_dims (climada.hazard.test.test_base_xarray.TestReadDefaultNetCDF) failed

tests_xml/tests.xml [took 0s]
Raw output
AssertionError: 'Dataset is missing dimension/coordinate: time' not found in "Dataset is missing dimension/coordinate: No variable named 'time'. Variables on the dataset include ['intensity', 'latitude', 'longitude']. Dataset dimensions: ['latitude', 'longitude']"
self = <climada.hazard.test.test_base_xarray.TestReadDefaultNetCDF testMethod=test_missing_dims>

    def test_missing_dims(self):
        """Test if missing coordinates are expanded and correct errors are thrown"""
        # Drop time as dimension, but not as coordinate!
        with xr.open_dataset(self.netcdf_path) as ds:
            ds = ds.isel(time=0).squeeze()
            hazard = Hazard.from_xarray_raster(ds, "", "")
            self._assert_default_types(hazard)
            np.testing.assert_array_equal(
                hazard.event_name, [self.time[0].strftime("%Y-%m-%d")]
            )
            np.testing.assert_array_equal(hazard.date, [self.time[0].toordinal()])
            np.testing.assert_array_equal(hazard.centroids.lat, [0, 0, 0, 1, 1, 1])
            np.testing.assert_array_equal(hazard.centroids.lon, [0, 1, 2, 0, 1, 2])
            self.assertEqual(hazard.centroids.geometry.crs, DEF_CRS)
            np.testing.assert_array_equal(
                hazard.intensity.toarray(), [[0, 1, 2, 3, 4, 5]]
            )
            self.assertEqual(hazard.fraction.nnz, 0)
            np.testing.assert_array_equal(
                hazard.fraction.toarray(), [[0, 0, 0, 0, 0, 0]]
            )
    
            # Now drop variable altogether, should raise an error
            ds = ds.drop_vars("time")
            with self.assertRaises(RuntimeError) as cm:
                Hazard.from_xarray_raster(ds, "", "")
>           self.assertIn(
                "Dataset is missing dimension/coordinate: time", str(cm.exception)
            )
E           AssertionError: 'Dataset is missing dimension/coordinate: time' not found in "Dataset is missing dimension/coordinate: No variable named 'time'. Variables on the dataset include ['intensity', 'latitude', 'longitude']. Dataset dimensions: ['latitude', 'longitude']"

climada/hazard/test/test_base_xarray.py:364: AssertionError

Check warning on line 0 in climada.hazard.test.test_base_xarray.TestReadDimsCoordsNetCDF

See this annotation in the file changed.

@github-actions github-actions / Unit Test Results Python 3.10

test_errors (climada.hazard.test.test_base_xarray.TestReadDimsCoordsNetCDF) failed

tests_xml/tests.xml [took 0s]
Raw output
AssertionError: 'Dataset is missing dimension/coordinate: lalalatitude.' not found in "Dataset is missing dimension/coordinate: No variable named 'lalalatitude'. Variables on the dataset include ['intensity', 'fraction', 'time', 'x', 'y', 'lon', 'lat', 'years', 'latitude', 'longitude']. Dataset dimensions: ['time', 'y', 'x']"
self = <climada.hazard.test.test_base_xarray.TestReadDimsCoordsNetCDF testMethod=test_errors>

    def test_errors(self):
        """Check if expected errors are thrown"""
        # Wrong coordinate key
        with self.assertRaises(ValueError) as cm:
            Hazard.from_xarray_raster_file(
                self.netcdf_path,
                "",
                "",
                coordinate_vars=dict(bar="latitude", longitude="baz"),
            )
        self.assertIn("Unknown coordinates passed: '['bar']'.", str(cm.exception))
    
        # Correctly specified, but the custom dimension does not exist
        with self.assertRaises(RuntimeError) as cm:
            Hazard.from_xarray_raster_file(
                self.netcdf_path,
                "",
                "",
                coordinate_vars=dict(latitude="lalalatitude"),
            )
>       self.assertIn(
            "Dataset is missing dimension/coordinate: lalalatitude.", str(cm.exception)
        )
E       AssertionError: 'Dataset is missing dimension/coordinate: lalalatitude.' not found in "Dataset is missing dimension/coordinate: No variable named 'lalalatitude'. Variables on the dataset include ['intensity', 'fraction', 'time', 'x', 'y', 'lon', 'lat', 'years', 'latitude', 'longitude']. Dataset dimensions: ['time', 'y', 'x']"

climada/hazard/test/test_base_xarray.py:578: AssertionError