-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yang
committed
Oct 18, 2023
1 parent
ba32cc7
commit fccc9af
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
"""Unit test for land cover dataset.""" | ||
|
||
import json | ||
from pathlib import Path | ||
from unittest.mock import patch | ||
import numpy as np | ||
import pytest | ||
import xarray as xr | ||
from zampy.datasets.catalog import LandCover | ||
from zampy.datasets.dataset_protocol import SpatialBounds | ||
from zampy.datasets.dataset_protocol import TimeBounds | ||
from . import data_folder | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def valid_path_config(tmp_path_factory): | ||
"""Create a dummy .zampy_config file.""" | ||
fn = tmp_path_factory.mktemp("usrhome") / "zampy_config.yml" | ||
with open(fn, mode="w", encoding="utf-8") as f: | ||
f.write("cdsapi:\n url: a\n key: 123:abc-def\n") | ||
f.write("adsapi:\n url: a\n key: 123:abc-def") | ||
return fn | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def dummy_dir(tmp_path_factory): | ||
"""Create a dummpy directory for testing.""" | ||
return tmp_path_factory.mktemp("data") | ||
|
||
|
||
class TestLandCover: | ||
"""Test the LandCover class.""" | ||
|
||
@patch("cdsapi.Client.retrieve") | ||
def test_download(self, mock_retrieve, valid_path_config, dummy_dir): | ||
"""Test download functionality. | ||
Here we mock the downloading and save property file to a fake path. | ||
""" | ||
time_bounds = TimeBounds( | ||
np.datetime64("1996-01-01"), np.datetime64("1996-12-31") | ||
) | ||
bbox = SpatialBounds(54, 56, 1, 3) | ||
variable = ["land_cover"] | ||
download_dir = Path(dummy_dir, "download") | ||
|
||
land_cover_dataset = LandCover() | ||
# create a dummy .cdsapirc | ||
patching = patch("zampy.datasets.cds_utils.CONFIG_PATH", valid_path_config) | ||
with patching: | ||
land_cover_dataset.download( | ||
download_dir=download_dir, | ||
time_bounds=time_bounds, | ||
spatial_bounds=bbox, | ||
variable_names=variable, | ||
overwrite=True, | ||
) | ||
|
||
# make sure that the download is called | ||
mock_retrieve.assert_called_once_with( | ||
"satellite-land-cover", | ||
{ | ||
"variable": "all", | ||
"format": "zip", | ||
"year": "1996", | ||
"version": "v2.0.7cds", | ||
}, | ||
) | ||
|
||
# check property file | ||
with (download_dir / "land-cover" / "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 zip data to nc for other tests.""" | ||
land_cover_dataset = LandCover() | ||
land_cover_dataset.ingest(download_dir=data_folder, ingest_dir=Path(temp_dir)) | ||
ds = xr.load_dataset( | ||
Path( | ||
temp_dir, | ||
"land-cover", | ||
"land-cover_LCCS_MAP_300m_1996.nc", | ||
) | ||
) | ||
|
||
return ds, land_cover_dataset | ||
|
||
def test_ingest(self, dummy_dir): | ||
"""Test ingest function.""" | ||
ds, _ = self.ingest_dummy_data(dummy_dir) | ||
assert isinstance(ds, xr.Dataset) |