Skip to content

Commit 9c4ca00

Browse files
committed
Improve test coverage of util.write_stac
1 parent f571311 commit 9c4ca00

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

test/test_util.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,39 @@ def test_clear_directory(tmp_path):
5050

5151

5252
@pytest.mark.parametrize("write_datasets", [False, True])
53-
def test_write_stac(tmp_path, dataset, write_datasets):
53+
@pytest.mark.parametrize("pre_existing_catalog", [False, True])
54+
def test_write_stac(tmp_path, dataset, write_datasets, pre_existing_catalog):
5455
datasets = {"ds1": dataset, "ds2": dataset.copy()}
5556
datasets["ds2"].attrs["xcengine_output_format"] = "netcdf"
5657
if write_datasets:
5758
output_path = tmp_path / "output"
5859
output_path.mkdir()
5960
datasets["ds1"].to_zarr(output_path / ("ds1.zarr"))
6061
datasets["ds2"].to_netcdf(output_path / ("ds2.nc"))
61-
62+
catalog_path = tmp_path / "catalog.json"
63+
if pre_existing_catalog:
64+
catalog_path.touch()
6265
write_stac(datasets, tmp_path)
63-
catalog = pystac.Catalog.from_file(tmp_path / "catalog.json")
64-
items = set(catalog.get_items(recursive=True))
65-
assert {item.id for item in items} == datasets.keys()
66-
catalog.make_all_asset_hrefs_absolute()
67-
data_asset_hrefs = {
68-
item.id: [a.href for a in item.assets.values() if "data" in a.roles]
69-
for item in items
70-
}
71-
assert data_asset_hrefs == {
72-
"ds1": [str((tmp_path / "ds1" / "ds1.zarr").resolve(strict=False))],
73-
"ds2": [str((tmp_path / "ds2" / "ds2.nc").resolve(strict=False))],
74-
}
66+
if pre_existing_catalog:
67+
# Check that our fake catalogue was not overwritten
68+
assert catalog_path.stat().st_size == 0
69+
else:
70+
catalog = pystac.Catalog.from_file(catalog_path)
71+
items = set(catalog.get_items(recursive=True))
72+
assert {item.id for item in items} == datasets.keys()
73+
catalog.make_all_asset_hrefs_absolute()
74+
data_asset_hrefs = {
75+
item.id: [
76+
a.href for a in item.assets.values() if "data" in a.roles
77+
]
78+
for item in items
79+
}
80+
assert data_asset_hrefs == {
81+
"ds1": [
82+
str((tmp_path / "ds1" / "ds1.zarr").resolve(strict=False))
83+
],
84+
"ds2": [str((tmp_path / "ds2" / "ds2.nc").resolve(strict=False))],
85+
}
7586

7687

7788
@pytest.mark.parametrize("eoap_mode", [False, True])

0 commit comments

Comments
 (0)