diff --git a/src/zampy/datasets/dataset_protocol.py b/src/zampy/datasets/dataset_protocol.py index eb0dafd..2803cff 100644 --- a/src/zampy/datasets/dataset_protocol.py +++ b/src/zampy/datasets/dataset_protocol.py @@ -38,12 +38,12 @@ def __post_init__(self) -> None: """Validate the initialized SpatialBounds class.""" if self.south > self.north: raise ValueError( - "Value of southern bound is greater than norther bound." + "Value of southern bound is greater than northern bound." "\nPlease check the spatial bounds input." ) if self.west > self.east: raise ValueError( - "Value of western bound is greater than east bound." + "Value of western bound is greater than eastern bound." "\nPlease check the spatial bounds input." ) diff --git a/tests/test_dataset_protocol.py b/tests/test_dataset_protocol.py index 2b29693..66a29b8 100644 --- a/tests/test_dataset_protocol.py +++ b/tests/test_dataset_protocol.py @@ -90,7 +90,7 @@ def test_invalid_spatial_bounds_north_south(): def test_invalid_spatial_bounds_east_west(): - with pytest.raises(ValueError, match="greater than east bound"): + with pytest.raises(ValueError, match="greater than eastern bound"): SpatialBounds(54, 6, 51, 20) diff --git a/tests/test_datasets/test_eth_canopy_height.py b/tests/test_datasets/test_eth_canopy_height.py index 392b1b3..0beca68 100644 --- a/tests/test_datasets/test_eth_canopy_height.py +++ b/tests/test_datasets/test_eth_canopy_height.py @@ -2,9 +2,9 @@ import json from pathlib import Path -from tempfile import TemporaryDirectory from unittest.mock import patch import numpy as np +import pytest import xarray as xr from zampy.datasets import eth_canopy_height from zampy.datasets.dataset_protocol import SpatialBounds @@ -12,39 +12,44 @@ from . import data_folder +@pytest.fixture(scope="function") +def dummy_dir(tmp_path_factory): + """Create a dummpy directory for testing.""" + return tmp_path_factory.mktemp("data") + + class TestEthCanopyHeight: """Test the EthCanopyHeight class.""" @patch("urllib.request.urlretrieve") - def test_download(self, mock_urlretrieve): + def test_download(self, mock_urlretrieve, dummy_dir): """Test download functionality. Here we mock the downloading and save property file to a fake path. """ - with TemporaryDirectory() as temp_dir: - times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-12-31")) - bbox = SpatialBounds(54, 6, 51, 3) - variable = ["height_of_vegetation"] - download_dir = Path(temp_dir, "download") - - canopy_height_dataset = eth_canopy_height.EthCanopyHeight() - canopy_height_dataset.download( - download_dir=download_dir, - time_bounds=times, - spatial_bounds=bbox, - variable_names=variable, - ) + times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-12-31")) + bbox = SpatialBounds(54, 6, 51, 3) + variable = ["height_of_vegetation"] + download_dir = Path(dummy_dir, "download") + + canopy_height_dataset = eth_canopy_height.EthCanopyHeight() + canopy_height_dataset.download( + download_dir=download_dir, + time_bounds=times, + spatial_bounds=bbox, + variable_names=variable, + ) - # make sure that the download is called - assert mock_urlretrieve.called + # make sure that the download is called + assert mock_urlretrieve.called - # check property file - with (download_dir / "eth-canopy-height" / "properties.json").open( - mode="r", encoding="utf-8" - ) as file: - json_dict = json.load(file) - # check property - assert json_dict["variable_names"] == variable + # check property file + with (download_dir / "eth-canopy-height" / "properties.json").open( + mode="r", encoding="utf-8" + ) as file: + json_dict = json.load(file) + # check property + assert json_dict["variable_names"] == variable def ingest_dummy_data(self, temp_dir): """Ingest dummy tif data to nc for other tests.""" @@ -62,44 +67,41 @@ def ingest_dummy_data(self, temp_dir): return ds, canopy_height_dataset - def test_ingest(self): + def test_ingest(self, dummy_dir): """Test ingest function.""" - with TemporaryDirectory() as temp_dir: - ds, _ = self.ingest_dummy_data(temp_dir) + ds, _ = self.ingest_dummy_data(dummy_dir) - assert type(ds) == xr.Dataset + assert type(ds) == xr.Dataset - def test_load(self): + def test_load(self, dummy_dir): """Test load function.""" - with TemporaryDirectory() as temp_dir: - _, canopy_height_dataset = self.ingest_dummy_data(temp_dir) - - times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-12-31")) - bbox = SpatialBounds(54, 6, 51, 3) - variable = ["height_of_vegetation"] - - ds = canopy_height_dataset.load( - ingest_dir=Path(temp_dir), - time_bounds=times, - spatial_bounds=bbox, - variable_names=variable, - resolution=1.0, - regrid_method="flox", - ) + _, canopy_height_dataset = self.ingest_dummy_data(dummy_dir) + + times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-12-31")) + bbox = SpatialBounds(54, 6, 51, 3) + variable = ["height_of_vegetation"] + + ds = canopy_height_dataset.load( + ingest_dir=Path(dummy_dir), + time_bounds=times, + spatial_bounds=bbox, + variable_names=variable, + resolution=1.0, + regrid_method="flox", + ) - # we assert the regridded coordinates - expected_lat = [51.0, 52.0, 53.0, 54.0] - expected_lon = [3.0, 4.0, 5.0, 6.0] + # we assert the regridded coordinates + expected_lat = [51.0, 52.0, 53.0, 54.0] + expected_lon = [3.0, 4.0, 5.0, 6.0] - np.testing.assert_allclose(ds.latitude.values, expected_lat) - np.testing.assert_allclose(ds.longitude.values, expected_lon) + np.testing.assert_allclose(ds.latitude.values, expected_lat) + np.testing.assert_allclose(ds.longitude.values, expected_lon) - def test_convert(self): + def test_convert(self, dummy_dir): """Test convert function.""" - with TemporaryDirectory() as temp_dir: - _, canopy_height_dataset = self.ingest_dummy_data(temp_dir) - canopy_height_dataset.convert(ingest_dir=Path(temp_dir), convention="ALMA") - # TODO: finish this test when the function is complete. + _, canopy_height_dataset = self.ingest_dummy_data(dummy_dir) + canopy_height_dataset.convert(ingest_dir=Path(dummy_dir), convention="ALMA") + # TODO: finish this test when the function is complete. def test_get_filenames(): @@ -155,20 +157,19 @@ def test_parse_tiff_file(): assert type(dummy_ds) == xr.Dataset -def test_convert_tiff_to_netcdf(): +def test_convert_tiff_to_netcdf(dummy_dir): """Test tiff to netcdf conversion function.""" - with TemporaryDirectory() as temp_dir: - dummy_data = Path( - data_folder, - "eth-canopy-height", - "ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.tif", - ) - eth_canopy_height.convert_tiff_to_netcdf( - ingest_folder=Path(temp_dir), - file=dummy_data, - ) + dummy_data = Path( + data_folder, + "eth-canopy-height", + "ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.tif", + ) + eth_canopy_height.convert_tiff_to_netcdf( + ingest_folder=Path(dummy_dir), + file=dummy_data, + ) - ds = xr.load_dataset( - Path(temp_dir, "ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.nc") - ) - assert type(ds) == xr.Dataset + ds = xr.load_dataset( + Path(dummy_dir, "ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.nc") + ) + assert type(ds) == xr.Dataset