Skip to content

Commit 7e1ce6d

Browse files
committed
test: add unit tests for STAC Zarr URL extraction
Tests asset priority logic (product > zarr > any .zarr) and error handling for missing or malformed STAC items.
1 parent 46dbf72 commit 7e1ce6d

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

tests/unit/test_get_zarr_url.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"""Tests for get_zarr_url.py - STAC asset URL extraction."""
2+
3+
import json
4+
from unittest.mock import mock_open, patch
5+
6+
import pytest
7+
8+
from scripts.get_zarr_url import get_zarr_url
9+
10+
11+
class TestGetZarrUrl:
12+
"""Test Zarr URL extraction from STAC items."""
13+
14+
def test_finds_product_asset_first(self):
15+
"""Product asset has highest priority."""
16+
stac_json = json.dumps(
17+
{
18+
"assets": {
19+
"product": {"href": "s3://bucket/product.zarr"},
20+
"zarr": {"href": "s3://bucket/other.zarr"},
21+
"thumbnail": {"href": "s3://bucket/random.zarr"},
22+
}
23+
}
24+
)
25+
with patch("scripts.get_zarr_url.urlopen", mock_open(read_data=stac_json.encode())):
26+
url = get_zarr_url("https://stac.example.com/item")
27+
assert url == "s3://bucket/product.zarr"
28+
29+
def test_finds_zarr_asset_second(self):
30+
"""Zarr asset used if no product asset."""
31+
stac_json = json.dumps(
32+
{
33+
"assets": {
34+
"thumbnail": {"href": "s3://bucket/thumb.png"},
35+
"zarr": {"href": "s3://bucket/data.zarr"},
36+
"metadata": {"href": "s3://bucket/other.zarr"},
37+
}
38+
}
39+
)
40+
with patch("scripts.get_zarr_url.urlopen", mock_open(read_data=stac_json.encode())):
41+
url = get_zarr_url("https://stac.example.com/item")
42+
assert url == "s3://bucket/data.zarr"
43+
44+
def test_fallback_to_any_zarr_asset(self):
45+
"""Falls back to any asset with .zarr in href."""
46+
stac_json = json.dumps(
47+
{
48+
"assets": {
49+
"thumbnail": {"href": "s3://bucket/thumb.png"},
50+
"data": {"href": "s3://bucket/measurements.zarr"},
51+
}
52+
}
53+
)
54+
with patch("scripts.get_zarr_url.urlopen", mock_open(read_data=stac_json.encode())):
55+
url = get_zarr_url("https://stac.example.com/item")
56+
assert url == "s3://bucket/measurements.zarr"
57+
58+
def test_no_zarr_asset_raises_error(self):
59+
"""Raises RuntimeError if no Zarr asset found."""
60+
stac_json = json.dumps(
61+
{
62+
"assets": {
63+
"thumbnail": {"href": "s3://bucket/thumb.png"},
64+
"metadata": {"href": "s3://bucket/meta.json"},
65+
}
66+
}
67+
)
68+
with (
69+
patch("scripts.get_zarr_url.urlopen", mock_open(read_data=stac_json.encode())),
70+
pytest.raises(RuntimeError, match="No Zarr asset found"),
71+
):
72+
get_zarr_url("https://stac.example.com/item")
73+
74+
def test_empty_assets_raises_error(self):
75+
"""Raises RuntimeError if assets dict is empty."""
76+
stac_json = json.dumps({"assets": {}})
77+
with (
78+
patch("scripts.get_zarr_url.urlopen", mock_open(read_data=stac_json.encode())),
79+
pytest.raises(RuntimeError, match="No Zarr asset found"),
80+
):
81+
get_zarr_url("https://stac.example.com/item")
82+
83+
def test_missing_assets_key_raises_error(self):
84+
"""Raises RuntimeError if no assets key in item."""
85+
stac_json = json.dumps({"id": "test-item"})
86+
with (
87+
patch("scripts.get_zarr_url.urlopen", mock_open(read_data=stac_json.encode())),
88+
pytest.raises(RuntimeError, match="No Zarr asset found"),
89+
):
90+
get_zarr_url("https://stac.example.com/item")
91+
92+
def test_product_asset_without_href(self):
93+
"""Skips product asset if no href, falls back."""
94+
stac_json = json.dumps(
95+
{
96+
"assets": {
97+
"product": {"type": "application/json"},
98+
"data": {"href": "s3://bucket/data.zarr"},
99+
}
100+
}
101+
)
102+
with patch("scripts.get_zarr_url.urlopen", mock_open(read_data=stac_json.encode())):
103+
url = get_zarr_url("https://stac.example.com/item")
104+
assert url == "s3://bucket/data.zarr"
105+
106+
def test_handles_http_zarr_urls(self):
107+
"""Works with HTTP URLs for Zarr."""
108+
stac_json = json.dumps(
109+
{
110+
"assets": {
111+
"product": {"href": "https://example.com/data.zarr"},
112+
}
113+
}
114+
)
115+
with patch("scripts.get_zarr_url.urlopen", mock_open(read_data=stac_json.encode())):
116+
url = get_zarr_url("https://stac.example.com/item")
117+
assert url == "https://example.com/data.zarr"

0 commit comments

Comments
 (0)