| 1 |
"""Tests for CDSETool's download module.""" |
| 2 |
|
| 3 |
import logging |
| 4 |
import os |
| 5 |
from pathlib import Path |
| 6 |
from typing import Any, Dict, List |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from cdsetool.credentials import Credentials |
| 11 |
from cdsetool.download import ( |
| 12 |
_get_feature_url, |
| 13 |
_get_odata_url, |
| 14 |
download_feature, |
| 15 |
download_file, |
| 16 |
filter_files, |
| 17 |
) |
| 18 |
|
| 19 |
from ..mock_auth import mock_jwks, mock_openid, mock_token |
| 20 |
|
| 21 |
|
| 22 |
def mock_download_file(url: str, path: str, options: Dict[str, Any]) -> bool: |
| 23 |
"""Mock the download_file function to create mock files.""" |
| 24 |
with open(path, "wb") as f: |
| 25 |
f.write(b"dummy data") |
| 26 |
return True |
| 27 |
|
| 28 |
|
| 29 |
def test_get_feature_url() -> None: |
| 30 |
"""Test full product OData download URL generation.""" |
| 31 |
feature = {"Id": "a6215824-704b-46d7-a2ec-efea4e468668"} |
| 32 |
expected_url = ( |
| 33 |
"https://download.dataspace.copernicus.eu/odata/v1/" |
| 34 |
"Products(a6215824-704b-46d7-a2ec-efea4e468668)/$value" |
| 35 |
) |
| 36 |
assert _get_feature_url(feature) == expected_url |
| 37 |
assert _get_feature_url({}) == "" |
| 38 |
assert _get_feature_url({"Id": None}) == "" |
| 39 |
|
| 40 |
|
| 41 |
def test_get_odata_url() -> None: |
| 42 |
"""Test individual file OData download URL generation.""" |
| 43 |
product_id = "a6215824-704b-46d7-a2ec-efea4e468668" |
| 44 |
product_name = "S2B_MSIL1C_20241209T162609_N0511_R040_T17UPV_20241209T195414.SAFE" |
| 45 |
href = "path/to/resource.xml" |
| 46 |
expected_url = ( |
| 47 |
"https://download.dataspace.copernicus.eu/odata/v1/" |
| 48 |
"Products(a6215824-704b-46d7-a2ec-efea4e468668)/" |
| 49 |
f"Nodes({product_name})/" |
| 50 |
"Nodes(path)/Nodes(to)/Nodes(resource.xml)/$value" |
| 51 |
) |
| 52 |
assert _get_odata_url(product_id, product_name, href) == expected_url |
| 53 |
|
| 54 |
|
| 55 |
@pytest.mark.parametrize( |
| 56 |
"manifest_file_path, pattern, expected_files", |
| 57 |
[ |
| 58 |
( |
| 59 |
Path("tests/download/mock/sentinel_1/manifest.safe"), |
| 60 |
"*/calibration-*.xml", |
| 61 |
[ |
| 62 |
Path( |
| 63 |
"annotation/calibration/calibration-s1a-iw-grd-vh-20241217t061735-20241217t061800-057028-07020f-002.xml" |
| 64 |
), |
| 65 |
Path( |
| 66 |
"annotation/calibration/calibration-s1a-iw-grd-vv-20241217t061735-20241217t061800-057028-07020f-001.xml" |
| 67 |
), |
| 68 |
], |
| 69 |
), |
| 70 |
( |
| 71 |
Path("tests/download/mock/sentinel_2/manifest.safe"), |
| 72 |
"*TCI.jp2", |
| 73 |
[ |
| 74 |
Path( |
| 75 |
"GRANULE/L1C_T17UPV_A040535_20241209T162603/IMG_DATA/T17UPV_20241209T162609_TCI.jp2" |
| 76 |
) |
| 77 |
], |
| 78 |
), |
| 79 |
( |
| 80 |
Path("tests/download/mock/sentinel_3/xfdumanifest.xml"), |
| 81 |
"*Oa02_reflectance.nc", |
| 82 |
[Path("Oa02_reflectance.nc")], |
| 83 |
), |
| 84 |
], |
| 85 |
) |
| 86 |
def test_filter_files( |
| 87 |
manifest_file_path: Path, pattern: str, expected_files: List[str] |
| 88 |
) -> None: |
| 89 |
filtered_files = filter_files(manifest_file_path, pattern) |
| 90 |
assert filtered_files == expected_files |
| 91 |
|
| 92 |
|
| 93 |
def test_filter_files_with_exclude() -> None: |
| 94 |
manifest_file_path = Path("tests/download/mock/sentinel_2/manifest.safe") |
| 95 |
filtered_files = filter_files(manifest_file_path, "*.jp2", exclude=True) |
| 96 |
assert filtered_files == [ |
| 97 |
Path("MTD_MSIL1C.xml"), |
| 98 |
Path("INSPIRE.xml"), |
| 99 |
Path("HTML/UserProduct_index.html"), |
| 100 |
Path("HTML/UserProduct_index.xsl"), |
| 101 |
Path("DATASTRIP/DS_2BPS_20241209T195414_S20241209T162603/MTD_DS.xml"), |
| 102 |
Path( |
| 103 |
"DATASTRIP/DS_2BPS_20241209T195414_S20241209T162603/QI_DATA/FORMAT_CORRECTNESS.xml" |
| 104 |
), |
| 105 |
Path( |
| 106 |
"DATASTRIP/DS_2BPS_20241209T195414_S20241209T162603/QI_DATA/GENERAL_QUALITY.xml" |
| 107 |
), |
| 108 |
Path( |
| 109 |
"DATASTRIP/DS_2BPS_20241209T195414_S20241209T162603/QI_DATA/GEOMETRIC_QUALITY.xml" |
| 110 |
), |
| 111 |
Path( |
| 112 |
"DATASTRIP/DS_2BPS_20241209T195414_S20241209T162603/QI_DATA/RADIOMETRIC_QUALITY.xml" |
| 113 |
), |
| 114 |
Path( |
| 115 |
"DATASTRIP/DS_2BPS_20241209T195414_S20241209T162603/QI_DATA/SENSOR_QUALITY.xml" |
| 116 |
), |
| 117 |
Path("GRANULE/L1C_T17UPV_A040535_20241209T162603/AUX_DATA/AUX_CAMSFO"), |
| 118 |
Path("GRANULE/L1C_T17UPV_A040535_20241209T162603/AUX_DATA/AUX_ECMWFT"), |
| 119 |
Path("GRANULE/L1C_T17UPV_A040535_20241209T162603/MTD_TL.xml"), |
| 120 |
Path( |
| 121 |
"GRANULE/L1C_T17UPV_A040535_20241209T162603/QI_DATA/FORMAT_CORRECTNESS.xml" |
| 122 |
), |
| 123 |
Path("GRANULE/L1C_T17UPV_A040535_20241209T162603/QI_DATA/GENERAL_QUALITY.xml"), |
| 124 |
Path( |
| 125 |
"GRANULE/L1C_T17UPV_A040535_20241209T162603/QI_DATA/GEOMETRIC_QUALITY.xml" |
| 126 |
), |
| 127 |
Path("GRANULE/L1C_T17UPV_A040535_20241209T162603/QI_DATA/SENSOR_QUALITY.xml"), |
| 128 |
] |
| 129 |
|
| 130 |
|
| 131 |
def test_filter_files_no_match() -> None: |
| 132 |
manifest_file_path = Path("tests/download/mock/sentinel_2/manifest.safe") |
| 133 |
filtered_files = filter_files(manifest_file_path, "Oa1*.nc") |
| 134 |
assert not filtered_files |
| 135 |
|
| 136 |
|
| 137 |
def test_filter_files_broken_manifest() -> None: |
| 138 |
manifest_file_path = Path("tests/download/mock/sentinel_2/broken_manifest.safe") |
| 139 |
filtered_files = filter_files(manifest_file_path, "*TCI.jp2") |
| 140 |
assert filtered_files is None |
| 141 |
|
| 142 |
|
| 143 |
def test_download_file_success(requests_mock: Any, mocker: Any, tmp_path: Path) -> None: |
| 144 |
mock_openid(requests_mock) |
| 145 |
mock_token(requests_mock) |
| 146 |
mock_jwks(mocker) |
| 147 |
|
| 148 |
mock_url = "http://example.com/file" |
| 149 |
mocker.patch("cdsetool.download._follow_redirect", return_value=mock_url) |
| 150 |
content = b"data" * 5 |
| 151 |
requests_mock.get( |
| 152 |
mock_url, status_code=200, headers={"Content-Length": "100"}, content=content |
| 153 |
) |
| 154 |
mock_file = tmp_path / "mock_file" |
| 155 |
|
| 156 |
result = download_file( |
| 157 |
mock_url, mock_file, {"credentials": Credentials("usr", "pwd")} |
| 158 |
) |
| 159 |
|
| 160 |
assert result is True |
| 161 |
|
| 162 |
# Check that file was written correctly |
| 163 |
with open(mock_file, "rb") as f: |
| 164 |
file_content = f.read() |
| 165 |
assert file_content == content |
| 166 |
|
| 167 |
|
| 168 |
def test_download_file_failure(requests_mock: Any, mocker: Any, tmp_path: Path) -> None: |
| 169 |
mock_openid(requests_mock) |
| 170 |
mock_token(requests_mock) |
| 171 |
mock_jwks(mocker) |
| 172 |
|
| 173 |
mock_url = "http://example.com/file" |
| 174 |
mocker.patch("cdsetool.download._follow_redirect", return_value=mock_url) |
| 175 |
requests_mock.get(mock_url, status_code=404, headers={"Content-Length": "100"}) |
| 176 |
mock_file = tmp_path / "mock_file" |
| 177 |
mocker.patch("time.sleep", return_value=None) # Avoid retry delay |
| 178 |
|
| 179 |
result = download_file( |
| 180 |
mock_url, mock_file, {"credentials": Credentials("usr", "pwd")} |
| 181 |
) |
| 182 |
assert result is False |
| 183 |
|
| 184 |
|
| 185 |
def test_download_feature(mocker: Any, tmp_path: Path) -> None: |
| 186 |
title = "S2B_MSIL1C_20241209T162609_N0511_R040_T17UPV_20241209T195414.SAFE" |
| 187 |
mock_feature = { |
| 188 |
"Id": "a6215824-704b-46d7-a2ec-efea4e468668", |
| 189 |
"Name": title, |
| 190 |
"ContentLength": 1000, |
| 191 |
"Online": True, |
| 192 |
} |
| 193 |
mocker.patch("cdsetool.download.download_file", mock_download_file) |
| 194 |
|
| 195 |
final_dir = str(tmp_path / "test_download_feature") |
| 196 |
filename = download_feature(mock_feature, final_dir) |
| 197 |
assert filename == f"{title}.zip" |
| 198 |
assert os.path.exists(os.path.join(final_dir, f"{title}.zip")) |
| 199 |
|
| 200 |
|
| 201 |
def test_download_feature_failure(mocker: Any, tmp_path: Path) -> None: |
| 202 |
title = "S2B_MSIL1C_20241209T162609_N0511_R040_T17UPV_20241209T195414.SAFE" |
| 203 |
mock_feature = { |
| 204 |
"Id": "a6215824-704b-46d7-a2ec-efea4e468668", |
| 205 |
"Name": title, |
| 206 |
"ContentLength": 1000, |
| 207 |
"Online": True, |
| 208 |
} |
| 209 |
mocker.patch( |
| 210 |
"cdsetool.download.download_file", side_effect=lambda url, path, options: None |
| 211 |
) |
| 212 |
|
| 213 |
final_dir = str(tmp_path / "test_download_feature_failure") |
| 214 |
filename = download_feature(mock_feature, final_dir) |
| 215 |
assert filename is None |
| 216 |
|
| 217 |
|
| 218 |
def test_download_feature_with_filter(mocker: Any, tmp_path: Path) -> None: |
| 219 |
options = {"filter_pattern": "*.jp2"} |
| 220 |
title = "S2B_MSIL1C_20241209T162609_N0511_R040_T17UPV_20241209T195414.SAFE" |
| 221 |
mock_feature = { |
| 222 |
"Id": "a6215824-704b-46d7-a2ec-efea4e468668", |
| 223 |
"Name": title, |
| 224 |
"ContentLength": 1000, |
| 225 |
"Online": True, |
| 226 |
"Collection": "SENTINEL-2", |
| 227 |
} |
| 228 |
mocker.patch( |
| 229 |
"cdsetool.download.filter_files", |
| 230 |
return_value=[Path("./GRANULE/file1.jp2"), Path("./GRANULE/file2.jp2")], |
| 231 |
) |
| 232 |
mocker.patch("cdsetool.download.download_file", mock_download_file) |
| 233 |
|
| 234 |
final_dir = str(tmp_path / "test_download_feature_with_filter") |
| 235 |
product_name = download_feature(mock_feature, final_dir, options) |
| 236 |
assert product_name == mock_feature["Name"] |
| 237 |
assert os.path.exists(os.path.join(final_dir, title, "GRANULE", "file1.jp2")) |
| 238 |
assert os.path.exists(os.path.join(final_dir, title, "GRANULE", "file2.jp2")) |
| 239 |
|
| 240 |
|
| 241 |
def test_download_feature_with_filter_failure(mocker: Any, tmp_path: Path) -> None: |
| 242 |
options = {"filter_pattern": "*.jp2"} |
| 243 |
mock_feature = { |
| 244 |
"Id": "a6215824-704b-46d7-a2ec-efea4e468668", |
| 245 |
"Name": "S2B_MSIL1C_20241209T162609_N0511_R040_T17UPV_20241209T195414.SAFE", |
| 246 |
"ContentLength": 1000, |
| 247 |
"Online": True, |
| 248 |
"Collection": "SENTINEL-2", |
| 249 |
} |
| 250 |
mocker.patch( |
| 251 |
"cdsetool.download.filter_files", |
| 252 |
return_value=["./GRANULE/file1.jp2", "./GRANULE/file2.jp2"], |
| 253 |
) |
| 254 |
mocker.patch( |
| 255 |
"cdsetool.download.download_file", side_effect=lambda url, path, options: None |
| 256 |
) |
| 257 |
|
| 258 |
final_dir = str(tmp_path / "test_download_feature_with_filter_failure") |
| 259 |
product_name = download_feature(mock_feature, final_dir, options) |
| 260 |
assert product_name is None |
| 261 |
|
| 262 |
|
| 263 |
def test_download_feature_with_filter_unsupported_coll( |
| 264 |
caplog: Any, tmp_path: Path |
| 265 |
) -> None: |
| 266 |
options = {"logger": logging.getLogger(__name__), "filter_pattern": "*MTL.txt"} |
| 267 |
mock_feature = { |
| 268 |
"Id": "a6215824-704b-46d7-a2ec-efea4e468668", |
| 269 |
"Name": "L8XXX", |
| 270 |
"ContentLength": 1000, |
| 271 |
"Online": True, |
| 272 |
"Collection": "Landsat8", |
| 273 |
} |
| 274 |
|
| 275 |
final_dir = str(tmp_path / "test_download_feature_with_filter_unsupported_coll") |
| 276 |
product_name = download_feature(mock_feature, final_dir, options) |
| 277 |
assert product_name is None |
| 278 |
assert ( |
| 279 |
"No support for downloading individual files in " |
| 280 |
f"{mock_feature['Collection']} products" |
| 281 |
) in caplog.text |