Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(providers): usage of hydrological_year in provider wekeo_ecmwf #1313

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion eodag/api/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
from eodag.plugins.manager import PluginManager
from eodag.plugins.search import PreparedSearch
from eodag.plugins.search.build_search_result import BuildPostSearchResult
from eodag.plugins.search.qssearch import PostJsonSearch
from eodag.types import model_fields_to_annotated
from eodag.types.queryables import CommonQueryables
from eodag.types.whoosh import EODAGQueryParser
Expand Down Expand Up @@ -1561,7 +1562,7 @@ def _search_by_id(
"max_items_per_page", DEFAULT_MAX_ITEMS_PER_PAGE
)
kwargs.update(items_per_page=items_per_page)
if isinstance(plugin, BuildPostSearchResult):
if isinstance(plugin, PostJsonSearch):
kwargs.update(
items_per_page=items_per_page,
_dc_qs=_dc_qs,
Expand Down
3 changes: 2 additions & 1 deletion eodag/api/product/metadata_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import orjson
import pyproj
from dateutil.parser import isoparse
from dateutil.relativedelta import relativedelta
from dateutil.tz import UTC, tzutc
from jsonpath_ng.jsonpath import Child, JSONPath
from lxml import etree
Expand Down Expand Up @@ -831,7 +832,7 @@ def convert_get_dates_from_string(text: str, split_param="-"):
def convert_get_hydrological_year(date: str):
utc_date = MetadataFormatter.convert_to_iso_utc_datetime(date)
date_object = datetime.strptime(utc_date, "%Y-%m-%dT%H:%M:%S.%fZ")
date_object_second_year = date_object + timedelta(days=365)
date_object_second_year = date_object + relativedelta(years=1)
return [
f'{date_object.strftime("%Y")}_{date_object_second_year.strftime("%y")}'
]
Expand Down
3 changes: 3 additions & 0 deletions eodag/resources/providers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4886,6 +4886,9 @@
statistic:
- '{{"statistic": {statistic}}}'
- '$.null'
hydrological_year:
- '{{"hydrological_year": {hydrological_year}}}'
- '$.null'
products:
SATELLITE_CARBON_DIOXIDE:
productType: EO:ECMWF:DAT:SATELLITE_CARBON_DIOXIDE
Expand Down
99 changes: 99 additions & 0 deletions tests/units/test_search_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,105 @@ def test_plugins_search_postjsonsearch_default_dates(
verify=True,
)

@mock.patch("eodag.plugins.search.qssearch.PostJsonSearch._request", autospec=True)
def test_plugins_search_postjsonsearch_query_params_wekeo(self, mock__request):
"""A query with PostJsonSearch (here wekeo) must generate query params corresponding to the
search criteria"""
provider = "wekeo_ecmwf"
product_type = "GRIDDED_GLACIERS_MASS_CHANGE"
search_plugin = self.get_search_plugin(product_type, provider)
auth_plugin = self.get_auth_plugin(provider)

mock__request.return_value = mock.Mock()

def _test_query_params(search_criteria, raw_result, expected_query_params):
mock__request.reset_mock()
mock__request.return_value.json.side_effect = [raw_result]
results, _ = search_plugin.query(
prep=PreparedSearch(
page=1,
items_per_page=10,
auth_plugin=auth_plugin,
),
**search_criteria,
)
self.assertDictEqual(
mock__request.call_args_list[0].args[1].query_params,
expected_query_params,
)

raw_result = {
"properties": {"itemsPerPage": 1, "startIndex": 0, "totalResults": 1},
"features": [
{
"type": "Feature",
"properties": {
"startdate": "1975-01-01T00:00:00Z",
"enddate": "2024-09-30T00:00:00Z",
},
"id": "derived-gridded-glacier-mass-change-576f8a153a25a83d9d3a5cfb03c4a759",
}
],
}

# Test #1: using the datetime
search_criteria = {
"productType": product_type,
"startTimeFromAscendingNode": "1980-01-01",
"completionTimeFromAscendingNode": "1981-12-31",
"variable": "glacier_mass_change",
"format": "zip",
"version": "wgms_fog_2022_09",
}
expected_query_params = {
"dataset_id": "EO:ECMWF:DAT:DERIVED_GRIDDED_GLACIER_MASS_CHANGE",
"hydrological_year": ["1980_81"],
"variable": "glacier_mass_change",
"format": "zip",
"product_version": "wgms_fog_2022_09",
"itemsPerPage": 10,
"startIndex": 0,
}
_test_query_params(search_criteria, raw_result, expected_query_params)

# Test #2: using parameter hydrological_year (single value)
search_criteria = {
"productType": product_type,
"variable": "glacier_mass_change",
"format": "zip",
"version": "wgms_fog_2022_09",
"hydrological_year": ["2020_21"],
}
expected_query_params = {
"dataset_id": "EO:ECMWF:DAT:DERIVED_GRIDDED_GLACIER_MASS_CHANGE",
"hydrological_year": ["2020_21"],
"variable": "glacier_mass_change",
"format": "zip",
"product_version": "wgms_fog_2022_09",
"itemsPerPage": 10,
"startIndex": 0,
}
_test_query_params(search_criteria, raw_result, expected_query_params)

# Test #3: using parameter hydrological_year (multiple values)
search_criteria = {
"productType": product_type,
"variable": "glacier_mass_change",
"format": "zip",
"version": "wgms_fog_2022_09",
"hydrological_year": ["1990_91", "2020_21"],
}
expected_query_params = {
"dataset_id": "EO:ECMWF:DAT:DERIVED_GRIDDED_GLACIER_MASS_CHANGE",
"hydrological_year": ["1990_91", "2020_21"],
"variable": "glacier_mass_change",
"format": "zip",
"product_version": "wgms_fog_2022_09",
"itemsPerPage": 10,
"startIndex": 0,
}
_test_query_params(search_criteria, raw_result, expected_query_params)


class TestSearchPluginODataV4Search(BaseSearchPluginTest):
def setUp(self):
Expand Down