diff --git a/eodag/api/core.py b/eodag/api/core.py index a0455f1de..91bd01965 100644 --- a/eodag/api/core.py +++ b/eodag/api/core.py @@ -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 @@ -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, diff --git a/eodag/api/product/metadata_mapping.py b/eodag/api/product/metadata_mapping.py index 885d5c10a..7501f8f4f 100644 --- a/eodag/api/product/metadata_mapping.py +++ b/eodag/api/product/metadata_mapping.py @@ -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 @@ -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")}' ] diff --git a/eodag/resources/providers.yml b/eodag/resources/providers.yml index bab25da3d..ffe362a06 100644 --- a/eodag/resources/providers.yml +++ b/eodag/resources/providers.yml @@ -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 diff --git a/tests/units/test_search_plugins.py b/tests/units/test_search_plugins.py index b979cbae5..477d319ab 100644 --- a/tests/units/test_search_plugins.py +++ b/tests/units/test_search_plugins.py @@ -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):