| 1 |
"""Tests for CDSETool's query module.""" |
| 2 |
|
| 3 |
from typing import Any |
| 4 |
|
| 5 |
from cdsetool.query import ( |
| 6 |
_build_attribute_filter, |
| 7 |
_build_odata_filter, |
| 8 |
_fetch_collection_attributes, |
| 9 |
_parse_interval, |
| 10 |
_parse_operator_suffix, |
| 11 |
describe_collection, |
| 12 |
geojson_to_wkt, |
| 13 |
shape_to_wkt, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
def test_build_attribute_filter() -> None: |
| 18 |
"""Test OData attribute filter building""" |
| 19 |
result = _build_attribute_filter("productType", "S2MSI2A", "StringAttribute", "eq") |
| 20 |
assert "Attributes/OData.CSC.StringAttribute/any(" in result |
| 21 |
assert "att/Name eq 'productType'" in result |
| 22 |
assert "att/OData.CSC.StringAttribute/Value eq 'S2MSI2A'" in result |
| 23 |
|
| 24 |
result = _build_attribute_filter("cloudCover", "40.5", "DoubleAttribute", "le") |
| 25 |
assert "Attributes/OData.CSC.DoubleAttribute/any(" in result |
| 26 |
assert "att/Name eq 'cloudCover'" in result |
| 27 |
assert "att/OData.CSC.DoubleAttribute/Value le 40.5" in result |
| 28 |
|
| 29 |
result = _build_attribute_filter( |
| 30 |
"relativeOrbitNumber", "123", "IntegerAttribute", "eq" |
| 31 |
) |
| 32 |
assert "Attributes/OData.CSC.IntegerAttribute/any(" in result |
| 33 |
assert "att/Name eq 'relativeOrbitNumber'" in result |
| 34 |
assert "att/OData.CSC.IntegerAttribute/Value eq 123" in result |
| 35 |
|
| 36 |
result = _build_attribute_filter( |
| 37 |
"processingDate", "2024-01-15T10:30:00Z", "DateTimeOffsetAttribute", "eq" |
| 38 |
) |
| 39 |
assert "Attributes/OData.CSC.DateTimeOffsetAttribute/any(" in result |
| 40 |
assert "att/Name eq 'processingDate'" in result |
| 41 |
assert ( |
| 42 |
"att/OData.CSC.DateTimeOffsetAttribute/Value eq 2024-01-15T10:30:00Z" in result |
| 43 |
) |
| 44 |
|
| 45 |
result = _build_attribute_filter( |
| 46 |
"sliceProductFlag", "true", "BooleanAttribute", "eq" |
| 47 |
) |
| 48 |
assert "Attributes/OData.CSC.BooleanAttribute/any(" in result |
| 49 |
assert "att/Name eq 'sliceProductFlag'" in result |
| 50 |
assert "att/OData.CSC.BooleanAttribute/Value eq true" in result |
| 51 |
|
| 52 |
result = _build_attribute_filter( |
| 53 |
"sliceProductFlag", "False", "BooleanAttribute", "eq" |
| 54 |
) |
| 55 |
assert "att/OData.CSC.BooleanAttribute/Value eq false" in result |
| 56 |
|
| 57 |
|
| 58 |
def test_parse_interval() -> None: |
| 59 |
"""Test interval syntax parsing""" |
| 60 |
result = _parse_interval("[10,22]") |
| 61 |
assert result == ("10", "22", "ge", "le") |
| 62 |
|
| 63 |
result = _parse_interval("(10,22)") |
| 64 |
assert result == ("10", "22", "gt", "lt") |
| 65 |
|
| 66 |
result = _parse_interval("[10,22)") |
| 67 |
assert result == ("10", "22", "ge", "lt") |
| 68 |
|
| 69 |
result = _parse_interval("(10,22]") |
| 70 |
assert result == ("10", "22", "gt", "le") |
| 71 |
|
| 72 |
result = _parse_interval("[ 10 , 22 ]") |
| 73 |
assert result == ("10", "22", "ge", "le") |
| 74 |
|
| 75 |
result = _parse_interval("[2024-01-01,2024-01-31]") |
| 76 |
assert result == ("2024-01-01", "2024-01-31", "ge", "le") |
| 77 |
|
| 78 |
assert _parse_interval("30") is None |
| 79 |
assert _parse_interval("abc") is None |
| 80 |
assert _parse_interval("[10]") is None |
| 81 |
|
| 82 |
|
| 83 |
def test_parse_operator_suffix() -> None: |
| 84 |
"""Test operator suffix parsing""" |
| 85 |
assert _parse_operator_suffix("cloudCoverLt") == ("cloudCover", "lt") |
| 86 |
assert _parse_operator_suffix("cloudCoverLe") == ("cloudCover", "le") |
| 87 |
assert _parse_operator_suffix("cloudCoverGt") == ("cloudCover", "gt") |
| 88 |
assert _parse_operator_suffix("cloudCoverGe") == ("cloudCover", "ge") |
| 89 |
assert _parse_operator_suffix("cloudCover") == ("cloudCover", "eq") |
| 90 |
assert _parse_operator_suffix("orbitNumberLt") == ("orbitNumber", "lt") |
| 91 |
assert _parse_operator_suffix("processingDateGe") == ("processingDate", "ge") |
| 92 |
|
| 93 |
|
| 94 |
def test_build_odata_filter() -> None: |
| 95 |
"""Test full OData filter expression building""" |
| 96 |
result = _build_odata_filter( |
| 97 |
"SENTINEL-2", |
| 98 |
{"contentDateStartGt": "2020-01-01", "contentDateEndLt": "2020-01-10"}, |
| 99 |
) |
| 100 |
assert "Collection/Name eq 'SENTINEL-2'" in result |
| 101 |
assert "ContentDate/Start gt 2020-01-01" in result |
| 102 |
assert "ContentDate/End lt 2020-01-10" in result |
| 103 |
|
| 104 |
result = _build_odata_filter( |
| 105 |
"SENTINEL-2", {"cloudCoverEq": 40, "productType": "S2MSI2A"} |
| 106 |
) |
| 107 |
assert "Collection/Name eq 'SENTINEL-2'" in result |
| 108 |
assert "cloudCover" in result |
| 109 |
assert "Value eq 40.0" in result |
| 110 |
assert "productType" in result |
| 111 |
|
| 112 |
result = _build_odata_filter("SENTINEL-2", {"cloudCoverLe": 30}) |
| 113 |
assert "Collection/Name eq 'SENTINEL-2'" in result |
| 114 |
assert "cloudCover" in result |
| 115 |
assert "Value le 30.0" in result |
| 116 |
|
| 117 |
result = _build_odata_filter("SENTINEL-2", {"cloudCover": "[10,22]"}) |
| 118 |
assert "Collection/Name eq 'SENTINEL-2'" in result |
| 119 |
assert result.count("DoubleAttribute/any(") == 2 |
| 120 |
assert "att/Name eq 'cloudCover'" in result |
| 121 |
assert "Value ge 10.0" in result |
| 122 |
assert "Value le 22.0" in result |
| 123 |
|
| 124 |
result = _build_odata_filter("SENTINEL-2", {"cloudCover": "(10,22)"}) |
| 125 |
assert result.count("DoubleAttribute/any(") == 2 |
| 126 |
assert "Value gt 10.0" in result |
| 127 |
assert "Value lt 22.0" in result |
| 128 |
|
| 129 |
result = _build_odata_filter("SENTINEL-2", {"cloudCover": "[10,22)"}) |
| 130 |
assert result.count("DoubleAttribute/any(") == 2 |
| 131 |
assert "Value ge 10.0" in result |
| 132 |
assert "Value lt 22.0" in result |
| 133 |
|
| 134 |
|
| 135 |
def test_build_odata_filter_new_attribute_types() -> None: |
| 136 |
"""Test OData filter building with new attribute types from ATTRIBUTES""" |
| 137 |
result = _build_odata_filter("SENTINEL-3", {"brightCoverEq": 50.5}) |
| 138 |
assert "Collection/Name eq 'SENTINEL-3'" in result |
| 139 |
assert "Attributes/OData.CSC.DoubleAttribute/any(" in result |
| 140 |
assert "att/Name eq 'brightCover'" in result |
| 141 |
assert "att/OData.CSC.DoubleAttribute/Value eq 50.5" in result |
| 142 |
|
| 143 |
result = _build_odata_filter( |
| 144 |
"SENTINEL-1", {"processingDateEq": "2024-06-15T12:00:00Z"} |
| 145 |
) |
| 146 |
assert "Attributes/OData.CSC.DateTimeOffsetAttribute/any(" in result |
| 147 |
assert "att/Name eq 'processingDate'" in result |
| 148 |
assert "2024-06-15T12:00:00Z" in result |
| 149 |
|
| 150 |
result = _build_odata_filter("SENTINEL-1", {"sliceProductFlag": "true"}) |
| 151 |
assert "Attributes/OData.CSC.BooleanAttribute/any(" in result |
| 152 |
assert "att/Name eq 'sliceProductFlag'" in result |
| 153 |
assert "att/OData.CSC.BooleanAttribute/Value eq true" in result |
| 154 |
|
| 155 |
result = _build_odata_filter("SENTINEL-1", {"cycleNumberEq": 42}) |
| 156 |
assert "Attributes/OData.CSC.IntegerAttribute/any(" in result |
| 157 |
assert "att/Name eq 'cycleNumber'" in result |
| 158 |
assert "att/OData.CSC.IntegerAttribute/Value eq 42" in result |
| 159 |
|
| 160 |
result = _build_odata_filter("SENTINEL-1", {"timeliness": "NRT"}) |
| 161 |
assert "Attributes/OData.CSC.StringAttribute/any(" in result |
| 162 |
assert "att/Name eq 'timeliness'" in result |
| 163 |
assert "att/OData.CSC.StringAttribute/Value eq 'NRT'" in result |
| 164 |
|
| 165 |
|
| 166 |
def test_build_odata_filter_operator_suffixes() -> None: |
| 167 |
"""Test OData filter building with operator suffixes""" |
| 168 |
result = _build_odata_filter("SENTINEL-1", {"orbitNumberLt": 100}) |
| 169 |
assert "att/Name eq 'orbitNumber'" in result |
| 170 |
assert "att/OData.CSC.IntegerAttribute/Value lt 100" in result |
| 171 |
|
| 172 |
result = _build_odata_filter("SENTINEL-1", {"orbitNumberGe": 50}) |
| 173 |
assert "att/Name eq 'orbitNumber'" in result |
| 174 |
assert "att/OData.CSC.IntegerAttribute/Value ge 50" in result |
| 175 |
|
| 176 |
result = _build_odata_filter("SENTINEL-2", {"cloudCoverLe": 25.5}) |
| 177 |
assert "att/Name eq 'cloudCover'" in result |
| 178 |
assert "att/OData.CSC.DoubleAttribute/Value le 25.5" in result |
| 179 |
|
| 180 |
result = _build_odata_filter( |
| 181 |
"SENTINEL-1", {"processingDateGt": "2024-01-01T00:00:00Z"} |
| 182 |
) |
| 183 |
assert "att/Name eq 'processingDate'" in result |
| 184 |
assert ( |
| 185 |
"att/OData.CSC.DateTimeOffsetAttribute/Value gt 2024-01-01T00:00:00Z" in result |
| 186 |
) |
| 187 |
|
| 188 |
|
| 189 |
def test_build_odata_filter_date_intervals() -> None: |
| 190 |
"""Test OData filter building with date interval syntax""" |
| 191 |
result = _build_odata_filter( |
| 192 |
"SENTINEL-2", {"contentDateStart": "[2024-01-01,2024-01-31]"} |
| 193 |
) |
| 194 |
assert "ContentDate/Start ge 2024-01-01" in result |
| 195 |
assert "ContentDate/Start le 2024-01-31" in result |
| 196 |
|
| 197 |
result = _build_odata_filter( |
| 198 |
"SENTINEL-2", {"contentDateEnd": "(2024-01-01,2024-01-31)"} |
| 199 |
) |
| 200 |
assert "ContentDate/End gt 2024-01-01" in result |
| 201 |
assert "ContentDate/End lt 2024-01-31" in result |
| 202 |
|
| 203 |
result = _build_odata_filter( |
| 204 |
"SENTINEL-1", {"processingDate": "[2024-01-01T00:00:00Z,2024-01-31T23:59:59Z]"} |
| 205 |
) |
| 206 |
assert result.count("DateTimeOffsetAttribute/any(") == 2 |
| 207 |
assert "att/Name eq 'processingDate'" in result |
| 208 |
assert "Value ge 2024-01-01T00:00:00Z" in result |
| 209 |
assert "Value le 2024-01-31T23:59:59Z" in result |
| 210 |
|
| 211 |
|
| 212 |
def test_shape_to_wkt() -> None: |
| 213 |
wkt = "POLYGON((10.172406299744779 55.48259118004532, 10.172406299744779 55.38234270718456, 10.42371976928382 55.38234270718456, 10.42371976928382 55.48259118004532, 10.172406299744779 55.48259118004532))" |
| 214 |
assert shape_to_wkt("tests/shape/POLYGON.shp") == wkt |
| 215 |
|
| 216 |
|
| 217 |
def test_geojson_to_wkt() -> None: |
| 218 |
wkt = "POLYGON((10.172406299744779 55.48259118004532, 10.172406299744779 55.38234270718456, 10.42371976928382 55.38234270718456, 10.42371976928382 55.48259118004532, 10.172406299744779 55.48259118004532))" |
| 219 |
geojson = '{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 10.172406299744779, 55.482591180045318 ], [ 10.172406299744779, 55.382342707184563 ], [ 10.423719769283821, 55.382342707184563 ], [ 10.423719769283821, 55.482591180045318 ], [ 10.172406299744779, 55.482591180045318 ] ] ] } }' |
| 220 |
|
| 221 |
assert geojson_to_wkt(geojson) == wkt |
| 222 |
|
| 223 |
geojson = '{ "type": "Polygon", "coordinates": [ [ [ 10.172406299744779, 55.482591180045318 ], [ 10.172406299744779, 55.382342707184563 ], [ 10.423719769283821, 55.382342707184563 ], [ 10.423719769283821, 55.482591180045318 ], [ 10.172406299744779, 55.482591180045318 ] ] ] }' |
| 224 |
|
| 225 |
assert geojson_to_wkt(geojson) == wkt |
| 226 |
|
| 227 |
wkt = "POLYGON((17.58127378553624 59.88489715357605, 17.58127378553624 59.80687027682205, 17.73996723627809 59.80687027682205, 17.73996723627809 59.88489715357605, 17.58127378553624 59.88489715357605))" |
| 228 |
geojson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{ },"geometry":{"coordinates":[[[17.58127378553624,59.88489715357605],[17.58127378553624,59.80687027682205],[17.73996723627809,59.80687027682205],[17.73996723627809,59.88489715357605],[17.58127378553624,59.88489715357605]]],"type":"Polygon" } } ] }' |
| 229 |
|
| 230 |
assert geojson_to_wkt(geojson) == wkt |
| 231 |
|
| 232 |
|
| 233 |
def _mock_sentinel_1_attributes(requests_mock: Any) -> None: |
| 234 |
"""Mock the Attributes endpoint for SENTINEL-1""" |
| 235 |
with open( |
| 236 |
"tests/query/mock/sentinel_1/attributes.json", "r", encoding="utf-8" |
| 237 |
) as f: |
| 238 |
requests_mock.get( |
| 239 |
"https://catalogue.dataspace.copernicus.eu/odata/v1/Attributes(SENTINEL-1)", |
| 240 |
text=f.read(), |
| 241 |
) |
| 242 |
|
| 243 |
|
| 244 |
def test_fetch_collection_attributes(requests_mock: Any) -> None: |
| 245 |
"""Test fetching attributes from OData API""" |
| 246 |
_mock_sentinel_1_attributes(requests_mock) |
| 247 |
|
| 248 |
attrs = _fetch_collection_attributes("SENTINEL-1") |
| 249 |
|
| 250 |
assert attrs is not None |
| 251 |
attr_names = [a["Name"] for a in attrs] |
| 252 |
assert "productType" in attr_names |
| 253 |
assert "orbitNumber" in attr_names |
| 254 |
assert "relativeOrbitNumber" in attr_names |
| 255 |
|
| 256 |
product_type = next(a for a in attrs if a["Name"] == "productType") |
| 257 |
assert product_type["ValueType"] == "String" |
| 258 |
|
| 259 |
orbit_number = next(a for a in attrs if a["Name"] == "orbitNumber") |
| 260 |
assert orbit_number["ValueType"] == "Integer" |
| 261 |
|
| 262 |
|
| 263 |
def test_fetch_collection_attributes_not_found(requests_mock: Any) -> None: |
| 264 |
"""Test fetching attributes for non-existent collection""" |
| 265 |
requests_mock.get( |
| 266 |
"https://catalogue.dataspace.copernicus.eu/odata/v1/Attributes(INVALID)", |
| 267 |
status_code=400, |
| 268 |
) |
| 269 |
assert _fetch_collection_attributes("INVALID") is None |
| 270 |
|
| 271 |
|
| 272 |
def test_describe_collection_returns_all_server_attrs_as_supported( |
| 273 |
requests_mock: Any, |
| 274 |
) -> None: |
| 275 |
"""Test that describe_collection returns all server attributes as supported""" |
| 276 |
_mock_sentinel_1_attributes(requests_mock) |
| 277 |
|
| 278 |
result = describe_collection("SENTINEL-1") |
| 279 |
|
| 280 |
assert result is not None |
| 281 |
assert "contentDateStart" in result |
| 282 |
assert "contentDateEnd" in result |
| 283 |
assert "geometry" in result |
| 284 |
assert "productType" in result |
| 285 |
assert "orbitDirection" in result |
| 286 |
assert "relativeOrbitNumber" in result |
| 287 |
assert "orbitNumber" in result |
| 288 |
assert "datatakeID" in result |
| 289 |
assert "cycleNumber" in result |
| 290 |
assert "sliceNumber" in result |
| 291 |
assert "processorName" in result |
| 292 |
assert "processingDate" in result |
| 293 |
assert "sliceProductFlag" in result |
| 294 |
assert "startTimeFromAscendingNode" in result |