|
3 | 3 | import json |
4 | 4 | import zipfile |
5 | 5 | from datetime import datetime, timezone |
| 6 | +from unittest.mock import MagicMock, patch |
6 | 7 |
|
7 | 8 | import pytest |
8 | 9 |
|
@@ -121,3 +122,135 @@ def test_export_opera_products_writes_workbook_and_skips_cloudiness_when_disable |
121 | 122 | assert payload[0][0] == "Dataset" |
122 | 123 | assert payload[1][1] == "granule-1" |
123 | 124 | assert payload[1][5] == "https://example.com/file_B01_WTR.tif" |
| 125 | + |
| 126 | + |
| 127 | +@patch("utils.opera_products.earthaccess.search_data") |
| 128 | +def test_fetch_hls_granule_links(mock_search): |
| 129 | + # Setup mock successful response |
| 130 | + mock_result = MagicMock() |
| 131 | + mock_result.data_links.return_value = ["https://example.com/B04.tif"] |
| 132 | + mock_search.return_value = [mock_result] |
| 133 | + |
| 134 | + # Test 1: Successful fetch |
| 135 | + links = opera_products.fetch_hls_granule_links("HLS.S30.T11SLT.20230101.v2.0") |
| 136 | + assert links == ["https://example.com/B04.tif"] |
| 137 | + mock_search.assert_called_with( |
| 138 | + short_name="HLSS30", granule_name="HLS.S30.T11SLT.20230101.v2.0" |
| 139 | + ) |
| 140 | + |
| 141 | + # Test 2: Error case (API failure) |
| 142 | + mock_search.side_effect = Exception("API Timeout") |
| 143 | + error_links = opera_products.fetch_hls_granule_links("HLS.S30.T11SLT.20230101.v2.0") |
| 144 | + assert error_links is None |
| 145 | + |
| 146 | + |
| 147 | +@patch("utils.opera_products.fetch_hls_granule_links") |
| 148 | +def test_export_hls_band_mapping_and_input_granules(mock_fetch_links, tmp_path): |
| 149 | + # Simulate an S30 and L30 response with their specific band names |
| 150 | + mock_fetch_links.side_effect = [ |
| 151 | + [ |
| 152 | + "https://fake/B02.tif", |
| 153 | + "https://fake/B03.tif", |
| 154 | + "https://fake/B04.tif", |
| 155 | + "https://fake/B8A.tif", |
| 156 | + "https://fake/Fmask.tif", |
| 157 | + ], # S30 response |
| 158 | + [ |
| 159 | + "https://fake/B02.tif", |
| 160 | + "https://fake/B03.tif", |
| 161 | + "https://fake/B04.tif", |
| 162 | + "https://fake/B05.tif", |
| 163 | + "https://fake/Fmask.tif", |
| 164 | + ], # L30 response |
| 165 | + ] |
| 166 | + |
| 167 | + mock_results = { |
| 168 | + "OPERA_L3_DSWX-HLS_V1": { |
| 169 | + "results": [ |
| 170 | + { |
| 171 | + "umm": { |
| 172 | + "GranuleUR": "OPERA_S30", |
| 173 | + "InputGranules": ["HLS.S30.T11SLT"], |
| 174 | + } |
| 175 | + }, |
| 176 | + { |
| 177 | + "umm": { |
| 178 | + "GranuleUR": "OPERA_L30", |
| 179 | + "InputGranules": ["HLS.L30.T11SLT"], |
| 180 | + } |
| 181 | + }, |
| 182 | + ], |
| 183 | + "gdf": None, # Skipping geometry for simplicity |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + # Run the export function |
| 188 | + opera_products.export_opera_products( |
| 189 | + mock_results, tmp_path, compute_cloudiness=False, include_hls=True |
| 190 | + ) |
| 191 | + |
| 192 | + # Read the generated Excel file to verify mappings |
| 193 | + from openpyxl import load_workbook |
| 194 | + |
| 195 | + wb = load_workbook(tmp_path / "opera_products_metadata.xlsx") |
| 196 | + ws = wb.active |
| 197 | + |
| 198 | + # Row 2 is S30 (B8A for NIR) |
| 199 | + assert ws.cell(row=2, column=18).value == "https://fake/B04.tif" # Red |
| 200 | + assert ws.cell(row=2, column=21).value == "https://fake/B8A.tif" # NIR |
| 201 | + assert ws.cell(row=2, column=22).value == "https://fake/Fmask.tif" |
| 202 | + |
| 203 | + # Row 3 is L30 (B05 for NIR) |
| 204 | + assert ws.cell(row=3, column=21).value == "https://fake/B05.tif" # NIR for Landsat |
| 205 | + |
| 206 | + |
| 207 | +@patch("utils.opera_products.earthaccess.search_data") |
| 208 | +def test_export_hls_fallback_cmr_search(mock_cmr_search, tmp_path): |
| 209 | + # Setup mock geometry and mock CMR response |
| 210 | + mock_geom = MagicMock() |
| 211 | + mock_geom.bounds = (-120, 30, -119, 31) |
| 212 | + mock_geom.wkt = "POLYGON((-120 30, -119 30, -119 31, -120 31, -120 30))" |
| 213 | + |
| 214 | + mock_hls_result = MagicMock() |
| 215 | + mock_hls_result.get.return_value = {"GranuleUR": "HLS.S30.T11SLT.123"} |
| 216 | + mock_hls_result.data_links.return_value = ["https://fallback/B04.tif"] |
| 217 | + mock_cmr_search.return_value = [mock_hls_result] |
| 218 | + |
| 219 | + # Provide results missing "InputGranules", forcing the fallback logic |
| 220 | + mock_results = { |
| 221 | + "OPERA_L3_DIST-ALERT-HLS_V1": { |
| 222 | + "results": [ |
| 223 | + { |
| 224 | + "umm": { |
| 225 | + "GranuleUR": "OPERA_L3_DIST-ALERT-HLS_T11SLT_20230101", |
| 226 | + "TemporalExtent": { |
| 227 | + "RangeDateTime": { |
| 228 | + "BeginningDateTime": "2023-01-01T00:00:00Z" |
| 229 | + } |
| 230 | + }, |
| 231 | + } |
| 232 | + } |
| 233 | + ], |
| 234 | + "gdf": FakeFrame([{"geometry": mock_geom}]), |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + opera_products.export_opera_products( |
| 239 | + mock_results, tmp_path, compute_cloudiness=False, include_hls=True |
| 240 | + ) |
| 241 | + |
| 242 | + # Verify fallback search triggered with bounds and date |
| 243 | + mock_cmr_search.assert_called_once() |
| 244 | + kwargs = mock_cmr_search.call_args.kwargs |
| 245 | + assert kwargs["bounding_box"] == (-120, 30, -119, 31) |
| 246 | + assert kwargs["temporal"] == ("2023-01-01T00:00:00", "2023-01-01T23:59:59") |
| 247 | + |
| 248 | + # Verify the fallback successfully mapped the data |
| 249 | + from openpyxl import load_workbook |
| 250 | + |
| 251 | + wb = load_workbook(tmp_path / "opera_products_metadata.xlsx") |
| 252 | + ws = wb.active |
| 253 | + assert ( |
| 254 | + ws.cell(row=2, column=17).value == "HLS.S30.T11SLT.123" |
| 255 | + ) # Extracted Fallback ID |
| 256 | + assert ws.cell(row=2, column=18).value == "https://fallback/B04.tif" |
0 commit comments