|
| 1 | +"""Unit tests for augment_stac_item.py.""" |
| 2 | + |
| 3 | +from datetime import UTC, datetime |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | +from pystac import Asset, Item |
| 8 | + |
| 9 | +from scripts.augment_stac_item import add_projection, add_visualization, augment, main |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def item(): |
| 14 | + """Create test STAC item.""" |
| 15 | + return Item("test", geometry=None, bbox=None, datetime=datetime.now(UTC), properties={}) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def mock_httpx_success(): |
| 20 | + """Mock successful httpx requests.""" |
| 21 | + with patch("scripts.augment_stac_item.httpx.Client") as mock_client: |
| 22 | + mock_ctx = MagicMock() |
| 23 | + mock_response = MagicMock() |
| 24 | + mock_response.status_code = 200 |
| 25 | + mock_ctx.get.return_value = mock_response |
| 26 | + mock_ctx.put.return_value = mock_response |
| 27 | + mock_client.return_value.__enter__.return_value = mock_ctx |
| 28 | + mock_client.return_value.__exit__.return_value = None |
| 29 | + yield mock_ctx |
| 30 | + |
| 31 | + |
| 32 | +def test_add_projection_extracts_epsg(item): |
| 33 | + """Test projection extraction from zarr.""" |
| 34 | + item.add_asset("product", Asset(href="s3://test.zarr", media_type="application/vnd+zarr")) |
| 35 | + |
| 36 | + mock_store = MagicMock() |
| 37 | + # The actual code reads spatial_ref dict which contains "spatial_ref" key with EPSG value |
| 38 | + mock_store.attrs.get.return_value = {"spatial_ref": "32632", "crs_wkt": "PROJCS[...]"} |
| 39 | + |
| 40 | + with patch("scripts.augment_stac_item.zarr.open", return_value=mock_store): |
| 41 | + add_projection(item) |
| 42 | + |
| 43 | + # Projection extension sets proj:code based on EPSG |
| 44 | + assert ( |
| 45 | + item.properties.get("proj:code") == "EPSG:32632" |
| 46 | + or item.properties.get("proj:epsg") == 32632 |
| 47 | + ) |
| 48 | + assert "proj:wkt2" in item.properties |
| 49 | + |
| 50 | + |
| 51 | +def test_add_projection_handles_errors(item): |
| 52 | + """Test add_projection error handling.""" |
| 53 | + item.add_asset("product", Asset(href="s3://test.zarr", media_type="application/vnd+zarr")) |
| 54 | + with patch("scripts.augment_stac_item.zarr.open", side_effect=Exception): |
| 55 | + add_projection(item) # Should not raise |
| 56 | + assert "proj:epsg" not in item.properties |
| 57 | + |
| 58 | + |
| 59 | +def test_add_projection_no_zarr_assets(item): |
| 60 | + """Test add_projection with no zarr assets.""" |
| 61 | + add_projection(item) |
| 62 | + assert "proj:epsg" not in item.properties |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize( |
| 66 | + "collection,expected_asset", |
| 67 | + [ |
| 68 | + ("sentinel-2-l2a", "TCI_10m"), |
| 69 | + ], |
| 70 | +) |
| 71 | +def test_add_visualization(item, collection, expected_asset): |
| 72 | + """Test visualization links for S1/S2.""" |
| 73 | + add_visualization(item, "https://raster.api", collection) |
| 74 | + |
| 75 | + links = {link.rel: link for link in item.links} |
| 76 | + assert all(rel in links for rel in ["viewer", "xyz", "tilejson", "via"]) |
| 77 | + |
| 78 | + # Verify asset in xyz URL |
| 79 | + assert expected_asset in links["xyz"].href |
| 80 | + |
| 81 | + # Verify proper URL encoding (/ should be %2F, : should be %3A) |
| 82 | + assert "%2F" in links["xyz"].href # Forward slashes are encoded |
| 83 | + assert "%3A" in links["xyz"].href # Colons are encoded |
| 84 | + |
| 85 | + # Verify titles are present |
| 86 | + assert links["xyz"].title is not None |
| 87 | + assert links["tilejson"].title is not None |
| 88 | + assert links["viewer"].title is not None |
| 89 | + |
| 90 | + |
| 91 | +def test_augment_verbose(item): |
| 92 | + """Test augment with verbose output.""" |
| 93 | + with ( |
| 94 | + patch("scripts.augment_stac_item.add_projection"), |
| 95 | + patch("scripts.augment_stac_item.add_visualization"), |
| 96 | + patch("builtins.print") as mock_print, |
| 97 | + ): |
| 98 | + augment(item, raster_base="https://api", collection_id="col", verbose=True) |
| 99 | + mock_print.assert_called_once() |
| 100 | + |
| 101 | + |
| 102 | +def test_main_success(mock_httpx_success): |
| 103 | + """Test main() success flow.""" |
| 104 | + item_dict = Item( |
| 105 | + "test", geometry=None, bbox=None, datetime=datetime.now(UTC), properties={} |
| 106 | + ).to_dict() |
| 107 | + item_dict["collection"] = "test-col" |
| 108 | + mock_httpx_success.get.return_value.json.return_value = item_dict |
| 109 | + |
| 110 | + with patch("scripts.augment_stac_item.augment") as mock_aug: |
| 111 | + mock_aug.return_value = Item.from_dict(item_dict) |
| 112 | + exit_code = main( |
| 113 | + ["--stac", "https://stac", "--collection", "test-col", "--item-id", "test"] |
| 114 | + ) |
| 115 | + |
| 116 | + assert exit_code == 0 |
| 117 | + |
| 118 | + |
| 119 | +def test_main_get_failure(): |
| 120 | + """Test main() GET failure.""" |
| 121 | + with patch("scripts.augment_stac_item.httpx.Client") as mock: |
| 122 | + mock.return_value.__enter__.return_value.get.side_effect = Exception("Failed") |
| 123 | + exit_code = main(["--stac", "https://stac", "--collection", "col", "--item-id", "test"]) |
| 124 | + |
| 125 | + assert exit_code == 1 |
| 126 | + |
| 127 | + |
| 128 | +def test_main_put_failure(mock_httpx_success): |
| 129 | + """Test main() PUT failure.""" |
| 130 | + item_dict = Item( |
| 131 | + "test", geometry=None, bbox=None, datetime=datetime.now(UTC), properties={} |
| 132 | + ).to_dict() |
| 133 | + mock_httpx_success.get.return_value.json.return_value = item_dict |
| 134 | + mock_httpx_success.put.side_effect = Exception("Failed") |
| 135 | + |
| 136 | + with patch("scripts.augment_stac_item.augment", return_value=Item.from_dict(item_dict)): |
| 137 | + exit_code = main(["--stac", "https://stac", "--collection", "col", "--item-id", "test"]) |
| 138 | + |
| 139 | + assert exit_code == 1 |
| 140 | + |
| 141 | + |
| 142 | +def test_main_with_bearer_token(mock_httpx_success): |
| 143 | + """Test main() with bearer token.""" |
| 144 | + item_dict = Item( |
| 145 | + "test", geometry=None, bbox=None, datetime=datetime.now(UTC), properties={} |
| 146 | + ).to_dict() |
| 147 | + item_dict["collection"] = "col" |
| 148 | + mock_httpx_success.get.return_value.json.return_value = item_dict |
| 149 | + |
| 150 | + with patch("scripts.augment_stac_item.augment", return_value=Item.from_dict(item_dict)): |
| 151 | + main( |
| 152 | + [ |
| 153 | + "--stac", |
| 154 | + "https://stac", |
| 155 | + "--collection", |
| 156 | + "col", |
| 157 | + "--item-id", |
| 158 | + "test", |
| 159 | + "--bearer", |
| 160 | + "token", |
| 161 | + ] |
| 162 | + ) |
| 163 | + |
| 164 | + call = mock_httpx_success.get.call_args |
| 165 | + assert call.kwargs["headers"]["Authorization"] == "Bearer token" |
0 commit comments