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(cop_marine): search_by_id for products without date in id #1471

Merged
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
12 changes: 11 additions & 1 deletion eodag/plugins/search/cop_marine.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,20 @@ def _get_product_by_id(
dataset_item: Dict[str, Any],
collection_dict: Dict[str, Any],
):
# try to find date(s) in product id
item_dates = re.findall(r"(\d{4})(0[1-9]|1[0-2])([0-3]\d)", product_id)
if not item_dates:
item_dates = re.findall(r"_(\d{4})(0[1-9]|1[0-2])", product_id)
use_dataset_dates = not bool(item_dates)
for obj in collection_objects["Contents"]:
if product_id in obj["Key"]:
return self._create_product(
product_type, obj["Key"], s3_url, dataset_item, collection_dict
product_type,
obj["Key"],
s3_url,
dataset_item,
collection_dict,
use_dataset_dates,
)
return None

Expand Down
72 changes: 72 additions & 0 deletions tests/units/test_search_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2982,6 +2982,78 @@ def test_plugins_search_cop_marine_query_with_id(self, mock_requests_get):
result[0].properties["id"],
)

@mock.patch("eodag.plugins.search.cop_marine.requests.get")
def test_plugins_search_cop_marine_search_by_id_no_dates_in_id(
self, mock_requests_get
):
mock_requests_get.return_value.json.side_effect = [
self.product_data,
self.dataset1_data,
self.dataset2_data,
]

search_plugin = self.get_search_plugin("PRODUCT_A", self.provider)
search_plugin.config.products = {
"PRODUCT_A": {
"productType": "PRODUCT_A",
"code_mapping": {"param": "platformSerialIdentifier", "index": 1},
}
}

with mock.patch("eodag.plugins.search.cop_marine._get_s3_client") as s3_stub:
s3_stub.return_value = self.s3
stubber = Stubber(self.s3)
stubber.add_response(
"list_objects",
self.list_objects_response1,
{"Bucket": "bucket1", "Prefix": "native/PRODUCT_A/dataset-number-one"},
)
stubber.add_response(
"list_objects",
{},
{
"Bucket": "bucket1",
"Marker": "native/PRODUCT_A/dataset-number-one/item_20200302_20200303_hdkIFEKFNEDN_20210101.nc",
"Prefix": "native/PRODUCT_A/dataset-number-one",
},
)
stubber.add_response(
"list_objects",
self.list_objects_response3,
{"Bucket": "bucket1", "Prefix": "native/PRODUCT_A/dataset-number-two"},
)
stubber.add_response(
"list_objects",
{},
{
"Bucket": "bucket1",
"Prefix": "native/PRODUCT_A/dataset-number-two",
"Marker": "native/PRODUCT_A/dataset-number-two/item_846282_niznjvnqkrf.nc",
},
)
stubber.activate()
result, num_total = search_plugin.query(
productType="PRODUCT_A", id="item_846282_niznjvnqkrf"
)
mock_requests_get.assert_has_calls(
calls=[
call(
"https://stac.marine.copernicus.eu/metadata/PRODUCT_A/product.stac.json"
),
call().json(),
call(
"https://stac.marine.copernicus.eu/metadata/PRODUCT_A/dataset-number-one/dataset.stac.json"
),
call().json(),
call(
"https://stac.marine.copernicus.eu/metadata/PRODUCT_A/dataset-number-two/dataset.stac.json"
),
call().json(),
]
)
self.assertEqual(1, num_total)
self.assertEqual("item_846282_niznjvnqkrf", result[0].properties["id"])

@mock.patch("eodag.plugins.search.cop_marine.requests.get")
def test_plugins_search_cop_marine_with_errors(self, mock_requests_get):
exc = requests.RequestException()
Expand Down
Loading