Skip to content

Commit 56e400c

Browse files
committed
refactor tests to use xarray for mock dataset
1 parent dd96cd7 commit 56e400c

1 file changed

Lines changed: 13 additions & 17 deletions

File tree

tests/cli/test_cli.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
import pytest
5+
import xarray as xr
56
from click.testing import CliRunner
67

78
from virtualship.cli.commands import fetch, init
@@ -10,29 +11,24 @@
1011

1112
@pytest.fixture
1213
def copernicus_no_download(monkeypatch):
13-
"""Mock the download and open_dataset functions."""
14+
"""Mock the copernicusmarine `subset` and `open_dataset` functions, approximating the reanalysis products."""
1415

1516
# mock for copernicusmarine.subset
1617
def fake_download(output_filename, output_directory, **_):
1718
Path(output_directory).joinpath(output_filename).touch()
1819

19-
# mock for copernicusmarine.open_dataset
20-
class DummyTime:
21-
def __getitem__(self, idx):
22-
return self
23-
24-
@property
25-
def values(self):
26-
return np.datetime64("2023-02-01")
27-
28-
class DummyDS(dict):
29-
def __getitem__(self, key):
30-
if key == "time":
31-
return DummyTime()
32-
raise KeyError(key)
33-
3420
def fake_open_dataset(*args, **kwargs):
35-
return DummyDS()
21+
return xr.Dataset(
22+
coords={
23+
"time": (
24+
"time",
25+
[
26+
np.datetime64("1993-01-01"),
27+
np.datetime64("2022-01-01"),
28+
], # mock up rough renanalysis period
29+
)
30+
}
31+
)
3632

3733
monkeypatch.setattr("virtualship.cli._fetch.copernicusmarine.subset", fake_download)
3834
monkeypatch.setattr(

0 commit comments

Comments
 (0)