Skip to content

Commit 46dbf72

Browse files
committed
test: add 91% coverage for get_conversion_params
20 tests: pattern matching, S1/S2 configs, CLI output formats
1 parent 5315397 commit 46dbf72

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
"""Tests for get_conversion_params.py - Collection registry logic."""
2+
3+
import json
4+
5+
import pytest
6+
7+
from scripts.get_conversion_params import (
8+
_match_collection_config,
9+
get_conversion_params,
10+
main,
11+
)
12+
13+
14+
class TestMatchCollectionConfig:
15+
"""Test pattern matching logic."""
16+
17+
def test_exact_match_s2(self):
18+
"""Exact collection ID matches S2 pattern."""
19+
config = _match_collection_config("sentinel-2-l2a")
20+
assert config is not None
21+
assert config["pattern"] == "sentinel-2-l2a*"
22+
23+
def test_pattern_match_s2_with_suffix(self):
24+
"""S2 collection with suffix matches pattern."""
25+
config = _match_collection_config("sentinel-2-l2a-dp-test")
26+
assert config is not None
27+
assert config["conversion"]["groups"] == "/quality/l2a_quicklook/r10m"
28+
29+
def test_exact_match_s1(self):
30+
"""Exact collection ID matches S1 pattern."""
31+
config = _match_collection_config("sentinel-1-l1-grd")
32+
assert config is not None
33+
assert config["pattern"] == "sentinel-1-l1-grd*"
34+
35+
def test_pattern_match_s1_with_suffix(self):
36+
"""S1 collection with suffix matches pattern."""
37+
config = _match_collection_config("sentinel-1-l1-grd-dp-production")
38+
assert config is not None
39+
assert config["conversion"]["groups"] == "/measurements"
40+
assert "--gcp-group" in config["conversion"]["extra_flags"]
41+
42+
def test_no_match_unknown_collection(self):
43+
"""Unknown collection returns None."""
44+
config = _match_collection_config("sentinel-3-olci")
45+
assert config is None
46+
47+
def test_no_match_empty_string(self):
48+
"""Empty collection ID returns None."""
49+
config = _match_collection_config("")
50+
assert config is None
51+
52+
53+
class TestGetConversionParams:
54+
"""Test parameter retrieval with fallback."""
55+
56+
def test_s2_parameters(self):
57+
"""S2 L2A returns correct conversion parameters."""
58+
params = get_conversion_params("sentinel-2-l2a")
59+
assert params["groups"] == "/quality/l2a_quicklook/r10m"
60+
assert params["extra_flags"] == "--crs-groups /quality/l2a_quicklook/r10m"
61+
assert params["spatial_chunk"] == 4096
62+
assert params["tile_width"] == 512
63+
64+
def test_s1_parameters(self):
65+
"""S1 GRD returns correct conversion parameters."""
66+
params = get_conversion_params("sentinel-1-l1-grd")
67+
assert params["groups"] == "/measurements"
68+
assert params["extra_flags"] == "--gcp-group /conditions/gcp"
69+
assert params["spatial_chunk"] == 2048
70+
assert params["tile_width"] == 512
71+
72+
def test_s2_with_suffix_uses_same_config(self):
73+
"""S2 variants use same config."""
74+
params1 = get_conversion_params("sentinel-2-l2a")
75+
params2 = get_conversion_params("sentinel-2-l2a-dp-test")
76+
assert params1 == params2
77+
78+
def test_s1_with_suffix_uses_same_config(self):
79+
"""S1 variants use same config."""
80+
params1 = get_conversion_params("sentinel-1-l1-grd")
81+
params2 = get_conversion_params("sentinel-1-l1-grd-production")
82+
assert params1 == params2
83+
84+
def test_unknown_collection_falls_back_to_default(self):
85+
"""Unknown collection falls back to S2 default."""
86+
params = get_conversion_params("sentinel-3-olci")
87+
# Should use sentinel-2-l2a as default
88+
assert params["groups"] == "/quality/l2a_quicklook/r10m"
89+
assert params["spatial_chunk"] == 4096
90+
91+
92+
class TestMainCLI:
93+
"""Test CLI interface."""
94+
95+
def test_shell_format_default(self, capsys):
96+
"""Default shell output format."""
97+
result = main(["--collection", "sentinel-2-l2a"])
98+
assert result == 0
99+
captured = capsys.readouterr()
100+
assert "ZARR_GROUPS='/quality/l2a_quicklook/r10m'" in captured.out
101+
assert "EXTRA_FLAGS='--crs-groups /quality/l2a_quicklook/r10m'" in captured.out
102+
assert "CHUNK=4096" in captured.out
103+
assert "TILE_WIDTH=512" in captured.out
104+
105+
def test_shell_format_s1(self, capsys):
106+
"""Shell output for S1."""
107+
result = main(["--collection", "sentinel-1-l1-grd", "--format", "shell"])
108+
assert result == 0
109+
captured = capsys.readouterr()
110+
assert "ZARR_GROUPS='/measurements'" in captured.out
111+
assert "EXTRA_FLAGS='--gcp-group /conditions/gcp'" in captured.out
112+
assert "CHUNK=2048" in captured.out
113+
114+
def test_json_format(self, capsys):
115+
"""JSON output format."""
116+
result = main(["--collection", "sentinel-2-l2a", "--format", "json"])
117+
assert result == 0
118+
captured = capsys.readouterr()
119+
data = json.loads(captured.out)
120+
assert data["groups"] == "/quality/l2a_quicklook/r10m"
121+
assert data["spatial_chunk"] == 4096
122+
123+
def test_single_param_groups(self, capsys):
124+
"""Get single parameter: groups."""
125+
result = main(["--collection", "sentinel-1-l1-grd", "--param", "groups"])
126+
assert result == 0
127+
captured = capsys.readouterr()
128+
assert captured.out.strip() == "/measurements"
129+
130+
def test_single_param_extra_flags(self, capsys):
131+
"""Get single parameter: extra_flags."""
132+
result = main(["--collection", "sentinel-1-l1-grd", "--param", "extra_flags"])
133+
assert result == 0
134+
captured = capsys.readouterr()
135+
assert "--gcp-group /conditions/gcp" in captured.out
136+
137+
def test_single_param_spatial_chunk(self, capsys):
138+
"""Get single parameter: spatial_chunk."""
139+
result = main(["--collection", "sentinel-2-l2a", "--param", "spatial_chunk"])
140+
assert result == 0
141+
captured = capsys.readouterr()
142+
assert captured.out.strip() == "4096"
143+
144+
def test_single_param_tile_width(self, capsys):
145+
"""Get single parameter: tile_width."""
146+
result = main(["--collection", "sentinel-2-l2a", "--param", "tile_width"])
147+
assert result == 0
148+
captured = capsys.readouterr()
149+
assert captured.out.strip() == "512"
150+
151+
def test_missing_collection_arg(self, capsys):
152+
"""Missing --collection argument fails."""
153+
with pytest.raises(SystemExit):
154+
main([])
155+
156+
def test_unknown_collection_uses_default(self, capsys):
157+
"""Unknown collection uses default config."""
158+
result = main(["--collection", "sentinel-99-unknown"])
159+
assert result == 0
160+
captured = capsys.readouterr()
161+
# Should fall back to S2 default
162+
assert "ZARR_GROUPS='/quality/l2a_quicklook/r10m'" in captured.out

0 commit comments

Comments
 (0)