Skip to content

Commit 4665cd1

Browse files
committed
fix: add and edit comments, error messages and docstrings
1 parent bcc1a3e commit 4665cd1

File tree

5 files changed

+48
-22
lines changed

5 files changed

+48
-22
lines changed

eodag/api/product/metadata_mapping.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,11 @@ def format_query_params(
12841284
config.metadata_mapping,
12851285
**config.products.get(product_type, {}).get("metadata_mapping", {}),
12861286
)
1287+
# Initialize the configuration parameter which indicates if an error must be raised while there is a search
1288+
# parameter which is not queryable. It is useful when the provider does not return any result if the search
1289+
# parameter is not a queryable without raising an error by itself.
1290+
# This parameter is set to False by default and the configuration at product type level has priority over the
1291+
# one at provider level
12871292
raise_mtd_discovery_error = (
12881293
getattr(config, "discover_metadata", {})
12891294
.get("search_param", {})
@@ -1457,10 +1462,11 @@ def _get_queryables(
14571462
for eodag_search_key, user_input in search_params.items():
14581463
if user_input is not None:
14591464
md_mapping = metadata_mapping.get(eodag_search_key, (None, NOT_MAPPED))
1460-
# raise an eodag error if the provider prevent from having result with a metadata
1461-
# which is not supposed to be a queryable without raising an error by itself
1465+
# raise an error when a query param not allowed by the provider is found
14621466
if not isinstance(md_mapping, list) and raise_mtd_discovery_error:
1463-
raise ValidationError("Unknown parameters not allowed")
1467+
raise ValidationError(
1468+
f"Search parameters which are not queryable are disallowed: {eodag_search_key}"
1469+
)
14641470
_, md_value = md_mapping
14651471
# query param from defined metadata_mapping
14661472
if md_mapping is not None and isinstance(md_mapping, list):

eodag/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ class DiscoverMetadata(TypedDict):
255255
auto_discovery: bool
256256
#: Metadata regex pattern used for discovery in search result properties
257257
metadata_pattern: str
258-
#: Configuration/template that will be used to query for a discovered parameter,
259-
#: or indication whether metadata discovery may fail the search request or not
258+
#: Configuration/template that will be used to query for a discovered parameter, or indication if
259+
#: using a search parameter which is not queryable will fail the search request or not
260260
search_param: str | dict[str, Any]
261261
#: Path to the metadata in search result
262262
metadata_path: str

eodag/plugins/search/data_request_search.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
)
3333
from eodag.plugins.search import PreparedSearch
3434
from eodag.plugins.search.base import Search
35+
from eodag.types.queryables import Queryables
3536
from eodag.utils import (
3637
DEFAULT_ITEMS_PER_PAGE,
3738
DEFAULT_MISSION_START_DATE,
@@ -363,9 +364,13 @@ def _create_data_request(
363364
request_body = format_query_params(
364365
eodag_product_type, self.config, kwargs
365366
)
366-
except ValidationError:
367+
except ValidationError as context:
368+
not_queryable_search_param = Queryables.get_queryable_from_alias(
369+
str(context.message).split(":")[-1].strip()
370+
)
367371
raise ValidationError(
368-
f"Unknown parameters not allowed for {self.provider} with {product_type}"
372+
f"Search parameters which are not queryable are disallowed for {product_type} with "
373+
f"{self.provider}: please remove '{not_queryable_search_param}' from your search parameters"
369374
)
370375
logger.debug(
371376
f"Sending search job request to {url} with {str(request_body)}"

eodag/plugins/search/qssearch.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
from eodag.plugins.search import PreparedSearch
7272
from eodag.plugins.search.base import Search
7373
from eodag.types import json_field_definition_to_python, model_fields_to_annotated
74+
from eodag.types.queryables import Queryables
7475
from eodag.types.search_args import SortByList
7576
from eodag.utils import (
7677
DEFAULT_MISSION_START_DATE,
@@ -533,7 +534,7 @@ def discover_product_types_per_page(
533534

534535
prep.info_message = "Fetching product types: {}".format(prep.url)
535536
prep.exception_message = (
536-
"Skipping error while fetching product types for " "{} {} instance:"
537+
"Skipping error while fetching product types for {} {} instance:"
537538
).format(self.provider, self.__class__.__name__)
538539

539540
# Query using appropriate method
@@ -815,9 +816,13 @@ def build_query_string(
815816
logger.debug("Building the query string that will be used for search")
816817
try:
817818
query_params = format_query_params(product_type, self.config, kwargs)
818-
except ValidationError:
819+
except ValidationError as context:
820+
not_queryable_search_param = Queryables.get_queryable_from_alias(
821+
str(context.message).split(":")[-1].strip()
822+
)
819823
raise ValidationError(
820-
f"Unknown parameters not allowed for {self.provider} with {product_type}"
824+
f"Search parameters which are not queryable are disallowed for {product_type} with "
825+
f"{self.provider}: please remove '{not_queryable_search_param}' from your search parameters"
821826
)
822827

823828
# Build the final query string, in one go without quoting it
@@ -1865,9 +1870,13 @@ def build_query_string(
18651870

18661871
try:
18671872
query_params = format_query_params(product_type, self.config, kwargs)
1868-
except ValidationError:
1873+
except ValidationError as context:
1874+
not_queryable_search_param = Queryables.get_queryable_from_alias(
1875+
str(context.message).split(":")[-1].strip()
1876+
)
18691877
raise ValidationError(
1870-
f"Unknown parameters not allowed for {self.provider} with {product_type}"
1878+
f"Search parameters which are not queryable are disallowed for {product_type} with "
1879+
f"{self.provider}: please remove '{not_queryable_search_param}' from your search parameters"
18711880
)
18721881

18731882
# Build the final query string, in one go without quoting it

tests/units/test_search_plugins.py

+16-10
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ def test_plugins_search_querystringsearch_search_cloudcover_peps(
401401
self.assertNotIn("cloudCover", mock__request.call_args_list[-1][0][1].url)
402402

403403
def test_plugins_search_querystringsearch_search_peps_ko(self):
404-
"""A query with an unknown parameter must raise an error if the provider does not allow it""" # noqa
404+
"""A query with a parameter which is not queryable must
405+
raise an error if the provider does not allow it""" # noqa
405406
# with raising error parameter in the global config of the provider
406407
provider_search_plugin_config = deepcopy(self.peps_search_plugin.config)
407408
self.peps_search_plugin.config.discover_metadata = {
@@ -418,8 +419,9 @@ def test_plugins_search_querystringsearch_search_peps_ko(self):
418419
**{**self.search_criteria_s2_msi_l1c, **{"foo": "bar"}},
419420
)
420421
self.assertEqual(
421-
f"Unknown parameters not allowed for {self.peps_search_plugin.provider} "
422-
f"with {self.search_criteria_s2_msi_l1c['productType']}",
422+
"Search parameters which are not queryable are disallowed for "
423+
f"{self.search_criteria_s2_msi_l1c['productType']} with {self.peps_search_plugin.provider}: "
424+
"please remove 'foo' from your search parameters",
423425
str(context.exception),
424426
)
425427

@@ -446,8 +448,9 @@ def test_plugins_search_querystringsearch_search_peps_ko(self):
446448
**{**self.search_criteria_s2_msi_l1c, **{"foo": "bar"}},
447449
)
448450
self.assertEqual(
449-
f"Unknown parameters not allowed for {self.peps_search_plugin.provider} "
450-
f"with {self.search_criteria_s2_msi_l1c['productType']}",
451+
"Search parameters which are not queryable are disallowed for "
452+
f"{self.search_criteria_s2_msi_l1c['productType']} with {self.peps_search_plugin.provider}: "
453+
"please remove 'foo' from your search parameters",
451454
str(context.exception),
452455
)
453456

@@ -3299,7 +3302,8 @@ def test_plugins_search_postjsonsearchwithstacqueryables_search_wekeomain(
32993302
mock_build_qs_stacsearch.assert_not_called()
33003303

33013304
def test_plugins_search_postjsonsearchwithstacqueryables_search_wekeomain_ko(self):
3302-
"""A query with an unknown parameter must raise an error if the provider does not allow it""" # noqa
3305+
"""A query with a parameter which is not queryable must
3306+
raise an error if the provider does not allow it""" # noqa
33033307
# with raising error parameter in the global config of the provider
33043308
provider_search_plugin_config = deepcopy(self.wekeomain_search_plugin.config)
33053309
self.wekeomain_search_plugin.config.discover_metadata = {
@@ -3316,8 +3320,9 @@ def test_plugins_search_postjsonsearchwithstacqueryables_search_wekeomain_ko(sel
33163320
**{**self.search_criteria_s2_msi_l1c, **{"foo": "bar"}},
33173321
)
33183322
self.assertEqual(
3319-
f"Unknown parameters not allowed for {self.wekeomain_search_plugin.provider} "
3320-
f"with {self.search_criteria_s2_msi_l1c['productType']}",
3323+
"Search parameters which are not queryable are disallowed for "
3324+
f"{self.search_criteria_s2_msi_l1c['productType']} with {self.wekeomain_search_plugin.provider}: "
3325+
"please remove 'foo' from your search parameters",
33213326
str(context.exception),
33223327
)
33233328

@@ -3344,8 +3349,9 @@ def test_plugins_search_postjsonsearchwithstacqueryables_search_wekeomain_ko(sel
33443349
**{**self.search_criteria_s2_msi_l1c, **{"foo": "bar"}},
33453350
)
33463351
self.assertEqual(
3347-
f"Unknown parameters not allowed for {self.wekeomain_search_plugin.provider} "
3348-
f"with {self.search_criteria_s2_msi_l1c['productType']}",
3352+
"Search parameters which are not queryable are disallowed for "
3353+
f"{self.search_criteria_s2_msi_l1c['productType']} with {self.wekeomain_search_plugin.provider}: "
3354+
"please remove 'foo' from your search parameters",
33493355
str(context.exception),
33503356
)
33513357

0 commit comments

Comments
 (0)