|
| 1 | +"""Integration tests for STAC API workflows. |
| 2 | +
|
| 3 | +These tests verify end-to-end scenarios with mocked external dependencies. |
| 4 | +""" |
| 5 | + |
| 6 | +from unittest.mock import MagicMock, patch |
| 7 | + |
| 8 | +import pytest |
| 9 | +from pystac import Asset, Item |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def test_pystac_item(): |
| 14 | + """Sample pystac Item for testing.""" |
| 15 | + item = Item( |
| 16 | + id="S2B_test", |
| 17 | + geometry=None, |
| 18 | + bbox=None, |
| 19 | + datetime="2025-01-01T00:00:00Z", |
| 20 | + properties={}, |
| 21 | + collection="sentinel-2-l2a", |
| 22 | + ) |
| 23 | + item.add_asset( |
| 24 | + "TCI_10m", |
| 25 | + Asset( |
| 26 | + href="https://example.com/data.zarr/TCI", |
| 27 | + media_type="application/vnd+zarr", |
| 28 | + roles=["visual"], |
| 29 | + ), |
| 30 | + ) |
| 31 | + return item |
| 32 | + |
| 33 | + |
| 34 | +@pytest.fixture |
| 35 | +def test_item_dict(): |
| 36 | + """Sample STAC item dictionary for registration tests.""" |
| 37 | + return { |
| 38 | + "type": "Feature", |
| 39 | + "stac_version": "1.0.0", |
| 40 | + "id": "test-item", |
| 41 | + "collection": "test-collection", |
| 42 | + "geometry": None, |
| 43 | + "bbox": None, |
| 44 | + "properties": {"datetime": "2025-01-01T00:00:00Z"}, |
| 45 | + "assets": {}, |
| 46 | + "links": [], |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def mock_stac_client(): |
| 52 | + """Mock pystac_client.Client for registration tests.""" |
| 53 | + mock_client = MagicMock() |
| 54 | + mock_collection = MagicMock() |
| 55 | + mock_client.get_collection.return_value = mock_collection |
| 56 | + |
| 57 | + # Mock the StacApiIO session (httpx client) |
| 58 | + mock_session = MagicMock() |
| 59 | + mock_stac_io = MagicMock() |
| 60 | + mock_stac_io.session = mock_session |
| 61 | + mock_stac_io.timeout = 30 |
| 62 | + mock_client._stac_io = mock_stac_io |
| 63 | + |
| 64 | + return mock_client |
| 65 | + |
| 66 | + |
| 67 | +@pytest.mark.integration |
| 68 | +def test_register_creates_new_item(test_item_dict, mock_stac_client): |
| 69 | + """Test registration creates new STAC item when it doesn't exist.""" |
| 70 | + from scripts.register_stac import register_item |
| 71 | + |
| 72 | + # Mock item doesn't exist |
| 73 | + mock_stac_client.get_collection().get_item.side_effect = Exception("Not found") |
| 74 | + |
| 75 | + # Mock successful POST |
| 76 | + mock_response = MagicMock() |
| 77 | + mock_response.status_code = 201 |
| 78 | + mock_response.raise_for_status = MagicMock() |
| 79 | + mock_stac_client._stac_io.session.post.return_value = mock_response |
| 80 | + |
| 81 | + with patch("pystac_client.Client.open", return_value=mock_stac_client): |
| 82 | + register_item( |
| 83 | + stac_url="https://stac.example.com", |
| 84 | + collection_id="test-collection", |
| 85 | + item_dict=test_item_dict, |
| 86 | + mode="create-or-skip", |
| 87 | + ) |
| 88 | + |
| 89 | + # Verify POST was called to create item |
| 90 | + assert mock_stac_client._stac_io.session.post.called |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.integration |
| 94 | +def test_register_skips_existing_item(test_item_dict, mock_stac_client): |
| 95 | + """Test registration skips when item already exists.""" |
| 96 | + from scripts.register_stac import register_item |
| 97 | + |
| 98 | + # Mock item exists |
| 99 | + mock_stac_client.get_collection().get_item.return_value = MagicMock() |
| 100 | + |
| 101 | + with patch("pystac_client.Client.open", return_value=mock_stac_client): |
| 102 | + register_item( |
| 103 | + stac_url="https://stac.example.com", |
| 104 | + collection_id="test-collection", |
| 105 | + item_dict=test_item_dict, |
| 106 | + mode="create-or-skip", |
| 107 | + ) |
| 108 | + |
| 109 | + # Verify no POST/PUT/DELETE was called |
| 110 | + assert not mock_stac_client._stac_io.session.post.called |
| 111 | + assert not mock_stac_client._stac_io.session.put.called |
| 112 | + assert not mock_stac_client._stac_io.session.delete.called |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.integration |
| 116 | +def test_register_updates_existing_item(test_item_dict, mock_stac_client): |
| 117 | + """Test registration updates existing item in upsert mode.""" |
| 118 | + from scripts.register_stac import register_item |
| 119 | + |
| 120 | + # Mock item exists |
| 121 | + mock_stac_client.get_collection().get_item.return_value = MagicMock() |
| 122 | + |
| 123 | + # Mock successful DELETE and POST |
| 124 | + mock_delete_response = MagicMock() |
| 125 | + mock_delete_response.status_code = 204 |
| 126 | + mock_delete_response.raise_for_status = MagicMock() |
| 127 | + mock_stac_client._stac_io.session.delete.return_value = mock_delete_response |
| 128 | + |
| 129 | + mock_post_response = MagicMock() |
| 130 | + mock_post_response.status_code = 201 |
| 131 | + mock_post_response.raise_for_status = MagicMock() |
| 132 | + mock_stac_client._stac_io.session.post.return_value = mock_post_response |
| 133 | + |
| 134 | + with patch("pystac_client.Client.open", return_value=mock_stac_client): |
| 135 | + register_item( |
| 136 | + stac_url="https://stac.example.com", |
| 137 | + collection_id="test-collection", |
| 138 | + item_dict=test_item_dict, |
| 139 | + mode="upsert", |
| 140 | + ) |
| 141 | + |
| 142 | + # Verify DELETE then POST was called |
| 143 | + assert mock_stac_client._stac_io.session.delete.called |
| 144 | + assert mock_stac_client._stac_io.session.post.called |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.integration |
| 148 | +def test_augmentation_adds_visualization_links(test_pystac_item): |
| 149 | + """Test augmentation workflow adds visualization links.""" |
| 150 | + from scripts.augment_stac_item import add_visualization |
| 151 | + |
| 152 | + # Add visualization links |
| 153 | + add_visualization( |
| 154 | + item=test_pystac_item, |
| 155 | + raster_base="https://titiler.example.com", |
| 156 | + collection_id="sentinel-2-l2a", |
| 157 | + ) |
| 158 | + |
| 159 | + # Verify XYZ tile link added |
| 160 | + xyz_links = [link for link in test_pystac_item.links if link.rel == "xyz"] |
| 161 | + assert len(xyz_links) > 0 |
| 162 | + assert "titiler.example.com" in xyz_links[0].target |
| 163 | + |
| 164 | + # Verify viewer link added |
| 165 | + viewer_links = [link for link in test_pystac_item.links if link.rel == "viewer"] |
| 166 | + assert len(viewer_links) > 0 |
| 167 | + |
| 168 | + |
| 169 | +@pytest.mark.integration |
| 170 | +def test_augmentation_adds_projection(test_pystac_item): |
| 171 | + """Test augmentation extracts projection information.""" |
| 172 | + from scripts.augment_stac_item import add_projection |
| 173 | + |
| 174 | + # Mock zarr group with spatial_ref |
| 175 | + # Note: This test validates the interface, actual zarr integration tested separately |
| 176 | + add_projection(item=test_pystac_item) |
| 177 | + |
| 178 | + # Projection properties should be attempted (may not be set without real zarr) |
| 179 | + # This validates the function can be called and handles missing data gracefully |
| 180 | + assert test_pystac_item.properties is not None |
| 181 | + |
| 182 | + |
| 183 | +@pytest.mark.integration |
| 184 | +def test_full_augmentation_pipeline(test_pystac_item): |
| 185 | + """Test complete augmentation pipeline.""" |
| 186 | + from scripts.augment_stac_item import augment |
| 187 | + |
| 188 | + # Run full augmentation |
| 189 | + result = augment( |
| 190 | + item=test_pystac_item, |
| 191 | + raster_base="https://titiler.example.com", |
| 192 | + collection_id="sentinel-2-l2a", |
| 193 | + verbose=False, |
| 194 | + ) |
| 195 | + |
| 196 | + # Verify item returned |
| 197 | + assert result.id == "S2B_test" |
| 198 | + |
| 199 | + # Verify links added |
| 200 | + assert len(result.links) > 0 |
| 201 | + xyz_count = sum(1 for link in result.links if link.rel == "xyz") |
| 202 | + assert xyz_count > 0 |
0 commit comments