| 1 |
"""Tests for CDSETool's query_features module.""" |
| 2 |
|
| 3 |
import json |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from cdsetool.query import _strip_odata_count, query_features |
| 7 |
|
| 8 |
|
| 9 |
def _mock_describe(requests_mock: Any) -> None: |
| 10 |
url = "https://catalogue.dataspace.copernicus.eu/odata/v1/Attributes(SENTINEL-1)" |
| 11 |
with open( |
| 12 |
"tests/query/mock/sentinel_1/attributes.json", "r", encoding="utf-8" |
| 13 |
) as file: |
| 14 |
requests_mock.get(url, text=file.read()) |
| 15 |
|
| 16 |
|
| 17 |
def _mock_sentinel_1(requests_mock: Any) -> None: |
| 18 |
"""Mock OData API responses for SENTINEL-1 queries""" |
| 19 |
filenames = [ |
| 20 |
"odata_page_1.json", |
| 21 |
"odata_page_2.json", |
| 22 |
"odata_page_3.json", |
| 23 |
"odata_page_4.json", |
| 24 |
"odata_page_5.json", |
| 25 |
] |
| 26 |
|
| 27 |
page_data = [] |
| 28 |
for filename in filenames: |
| 29 |
with open( |
| 30 |
f"tests/query/mock/sentinel_1/{filename}", "r", encoding="utf-8" |
| 31 |
) as f: |
| 32 |
page_data.append(json.loads(f.read())) |
| 33 |
|
| 34 |
# First page URL is built by the client |
| 35 |
first_url = ( |
| 36 |
"https://catalogue.dataspace.copernicus.eu/odata/v1/Products?" |
| 37 |
"$filter=Collection/Name%20eq%20%27SENTINEL-1%27&$top=10&$count=true" |
| 38 |
"&$expand=Attributes&$orderby=ContentDate/Start%20asc" |
| 39 |
) |
| 40 |
requests_mock.get(first_url, json=page_data[0]) |
| 41 |
|
| 42 |
# Subsequent pages: register the nextLink from each page (with $count stripped) |
| 43 |
for i in range(len(page_data) - 1): |
| 44 |
next_link = _strip_odata_count(page_data[i]["@odata.nextLink"]) |
| 45 |
requests_mock.get(next_link, json=page_data[i + 1]) |
| 46 |
|
| 47 |
|
| 48 |
def test_query_features_length(requests_mock: Any) -> None: |
| 49 |
"""Test that query returns correct total count""" |
| 50 |
_mock_sentinel_1(requests_mock) |
| 51 |
query = query_features( |
| 52 |
"SENTINEL-1", {"top": 10}, options={"expand_attributes": True} |
| 53 |
) |
| 54 |
|
| 55 |
assert len(query) == 47 |
| 56 |
|
| 57 |
manual_count = 0 |
| 58 |
for _ in query: |
| 59 |
manual_count += 1 |
| 60 |
|
| 61 |
assert manual_count == 47 |
| 62 |
|
| 63 |
|
| 64 |
def test_query_features_reusable(requests_mock: Any) -> None: |
| 65 |
"""Test that query can be iterated multiple times""" |
| 66 |
_mock_sentinel_1(requests_mock) |
| 67 |
|
| 68 |
query = query_features( |
| 69 |
"SENTINEL-1", {"top": 10}, options={"expand_attributes": True} |
| 70 |
) |
| 71 |
|
| 72 |
assert len(query) == len(query) |
| 73 |
assert len(query) == 47 # query is not exhausted after first len call |
| 74 |
|
| 75 |
assert list(query) == list(query) # query is not exhausted after first iteration |
| 76 |
|
| 77 |
|
| 78 |
def test_query_features_random_access(requests_mock: Any) -> None: |
| 79 |
"""Test random access to products with proper lazy loading""" |
| 80 |
_mock_sentinel_1(requests_mock) |
| 81 |
|
| 82 |
query = query_features( |
| 83 |
"SENTINEL-1", {"top": 10}, options={"expand_attributes": True} |
| 84 |
) |
| 85 |
|
| 86 |
assert query[0]["Name"] == "S1A_AUX_INS_V20140406T010000_G20140409T142540.SAFE" |
| 87 |
assert len(query.features) == 10 |
| 88 |
assert query[9]["Name"] == "S1A_AUX_PP2_V20140406T133000_G20241125T134251.SAFE" |
| 89 |
assert len(query.features) == 10 |
| 90 |
assert query[13]["Name"] == "S1A_AUX_INS_V20140406T133000_G20211028T132414.SAFE" |
| 91 |
assert len(query.features) == 20 |
| 92 |
assert query[2]["Name"] == "S1A_AUX_PP2_V20140406T133000_G20251021T105030.SAFE" |
| 93 |
assert len(query.features) == 20 |
| 94 |
assert ( |
| 95 |
query[34]["Name"] |
| 96 |
== "S1A_OPER_AUX_PROQUA_POD__20210408T165229_V20140409T235944_20140410T235943" |
| 97 |
) |
| 98 |
assert len(query.features) == 40 |