|
5 | 5 | import pytest |
6 | 6 | import xarray as xr |
7 | 7 |
|
8 | | -from xcengine.util import clear_directory, write_stac |
| 8 | +from xcengine.util import clear_directory, write_stac, save_datasets |
9 | 9 |
|
10 | 10 |
|
11 | 11 | @pytest.fixture |
@@ -38,23 +38,42 @@ def test_clear_directory(tmp_path): |
38 | 38 | assert os.listdir(tmp_path) == [] |
39 | 39 |
|
40 | 40 |
|
41 | | -def test_write_stac(tmp_path, dataset): |
42 | | - write_stac({"ds1": dataset, "ds2": dataset}, tmp_path) |
| 41 | +@pytest.mark.parametrize("write_zarrs", [False, True]) |
| 42 | +def test_write_stac(tmp_path, dataset, write_zarrs): |
| 43 | + datasets = {"ds1": dataset, "ds2": dataset} |
| 44 | + if write_zarrs: |
| 45 | + output_path = tmp_path / "output" |
| 46 | + output_path.mkdir() |
| 47 | + for ds_id, ds in datasets.items(): |
| 48 | + ds.to_zarr(output_path / (ds_id + ".zarr")) |
| 49 | + |
| 50 | + write_stac(datasets, tmp_path) |
43 | 51 | catalog = pystac.Catalog.from_file(tmp_path / "catalog.json") |
44 | 52 | items = set(catalog.get_items(recursive=True)) |
45 | | - assert {item.id for item in items} == {"ds1", "ds2"} |
| 53 | + assert {item.id for item in items} == datasets.keys() |
46 | 54 | catalog.make_all_asset_hrefs_absolute() |
47 | 55 | data_asset_hrefs = { |
48 | | - item.id: [ |
49 | | - a.href # (Path(item.self_href) / a.href).resolve(strict=False) |
50 | | - for a in item.assets.values() |
51 | | - if "data" in a.roles |
52 | | - ] |
| 56 | + item.id: [a.href for a in item.assets.values() if "data" in a.roles] |
53 | 57 | for item in items |
54 | 58 | } |
55 | 59 | assert data_asset_hrefs == { |
56 | | - ds: [ |
57 | | - str(Path(tmp_path / "output" / f"{ds}.zarr").resolve(strict=False)) |
| 60 | + ds_id: [ |
| 61 | + str(Path(tmp_path / ds_id / f"{ds_id}.zarr").resolve(strict=False)) |
58 | 62 | ] |
59 | | - for ds in {"ds1", "ds2"} |
| 63 | + for ds_id in datasets.keys() |
60 | 64 | } |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.parametrize("eoap_mode", [False, True]) |
| 68 | +def test_save_datasets(tmp_path, dataset, eoap_mode): |
| 69 | + datasets = {"ds1": dataset, "ds2": dataset} |
| 70 | + save_datasets(datasets, tmp_path, eoap_mode) |
| 71 | + for ds_id in datasets.keys(): |
| 72 | + assert ( |
| 73 | + tmp_path / (ds_id if eoap_mode else "output") / (ds_id + ".zarr") |
| 74 | + ).is_dir() |
| 75 | + catalogue_path = tmp_path / "catalog.json" |
| 76 | + if eoap_mode: |
| 77 | + assert catalogue_path.is_file() |
| 78 | + else: |
| 79 | + assert not catalogue_path.exists() |
0 commit comments