Skip to content

Commit

Permalink
test: fetching and merging external enhanced product types metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
dalpasso committed Feb 2, 2024
1 parent cf5ca36 commit 89a6f0b
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 1 deletion.
100 changes: 99 additions & 1 deletion tests/units/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from shapely.geometry import LineString, MultiPolygon, Polygon

from eodag import __version__ as eodag_version
from eodag.utils import GENERIC_PRODUCT_TYPE
from eodag.utils import GENERIC_PRODUCT_TYPE, deepcopy
from tests import TEST_RESOURCES_PATH
from tests.context import (
DEFAULT_MAX_ITEMS_PER_PAGE,
Expand Down Expand Up @@ -882,6 +882,104 @@ def assertListProductTypesRightStructure(self, structure):
or structure["_id"] in self.SUPPORTED_PRODUCT_TYPES
)

@mock.patch(
"eodag.api.core.get_ext_product_types_conf",
autospec=True,
return_value=json.loads(
"""{
"new_field":"New Value",
"title":"A different title for Sentinel 2 MSI Level 1C",
"summaries": {
"instruments": [
"MSI"
],
"platform": [
"SENTINEL-2A",
"SENTINEL-2B"
],
"constellation": [
"Sentinel-2"
],
"processing:level": [
"L1C"
]
}
}"""
),
)
def test_load_external_product_type_metadata(self, mock_get_ext_product_types_conf):
"""Load the supported EODAG metadata from an external STAC collection"""
product_type_conf = deepcopy(self.dag.product_types_config["S2_MSI_L1C"])
ext_stac_collection_path = "/path/to/external/stac/collections/S2_MSI_L1C.json"
product_type_conf["stacCollection"] = ext_stac_collection_path
enhanced_product_type = self.dag.load_external_product_type_metadata(
"S2_MSI_L1C",
product_type_conf,
)
mock_get_ext_product_types_conf.assert_called_with(ext_stac_collection_path)
# Fields not supported by EODAG
self.assertNotIn("new_field", enhanced_product_type)
# Merge lists
self.assertEqual(
enhanced_product_type["platformSerialIdentifier"],
"S2A,S2B,SENTINEL-2A,SENTINEL-2B",
)
# Don't override existings keys
self.assertEqual(enhanced_product_type["title"], "SENTINEL2 Level-1C")
# The parameter passed `load_external_product_type_metadata` is not modified
del product_type_conf["stacCollection"]
self.assertDictEqual(
product_type_conf,
self.dag.product_types_config["S2_MSI_L1C"],
)

@mock.patch(
"eodag.api.core.get_ext_product_types_conf",
autospec=True,
return_value=json.loads(
"""{
"new_field":"New Value",
"title":"A different title for Sentinel 2 MSI Level 1C",
"summaries": {
"instruments": [
"MSI"
],
"platform": [
"SENTINEL-2A",
"SENTINEL-2B"
],
"constellation": [
"Sentinel-2"
],
"processing:level": [
"L1C"
]
}
}"""
),
)
def test_fetch_external_product_types_metadata(
self, mock_get_ext_product_types_conf
):
"""Updates product types config with metadata from external STAC collections"""
product_type_conf = self.dag.product_types_config["S2_MSI_L1C"]
ext_stac_collection_path = "/path/to/external/stac/collections/S2_MSI_L1C.json"
product_type_conf["stacCollection"] = ext_stac_collection_path
self.dag.fetch_external_product_types_metadata()
mock_get_ext_product_types_conf.assert_called_with(ext_stac_collection_path)
enhanced_product_type = self.dag.product_types_config["S2_MSI_L1C"]
# Fields not supported by EODAG
self.assertNotIn("new_field", enhanced_product_type)
# Merge lists
self.assertEqual(
enhanced_product_type["platformSerialIdentifier"],
"S2A,S2B,SENTINEL-2A,SENTINEL-2B",
)
# Don't override existings keys
self.assertEqual(enhanced_product_type["title"], "SENTINEL2 Level-1C")
# Restore the product type
del self.dag.product_types_config["S2_MSI_L1C"]["stacCollection"]

@mock.patch("eodag.api.core.open_dir", autospec=True)
@mock.patch("eodag.api.core.exists_in", autospec=True, return_value=True)
def test_core_object_open_index_if_exists(self, exists_in_mock, open_dir_mock):
Expand Down
63 changes: 63 additions & 0 deletions tests/units/test_stac_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import pytest

from eodag.rest.stac import StacCollection
from eodag.utils.exceptions import ValidationError
from tests import TEST_RESOURCES_PATH, mock
from tests.context import RequestError, SearchResult
Expand Down Expand Up @@ -576,3 +577,65 @@ def test_search_stac_items_post(self, mock__request):
"body": {"collections": ["S2_MSI_L1C"], "page": 3},
},
)

@mock.patch("eodag.rest.stac.get_ext_product_types_conf", autospec=True)
def test_fetch_external_stac_collections(
self, mock_stac_get_ext_product_types_conf
):
"""Load external STAC collections"""
external_json = """{
"new_field":"New Value",
"title":"A different title for Sentinel 2 MSI Level 1C",
"summaries": {
"instruments": [
"MSI"
],
"platform": [
"SENTINEL-2A",
"SENTINEL-2B"
],
"constellation": [
"Sentinel-2"
],
"processing:level": [
"L1C"
]
}
}"""
mock_stac_get_ext_product_types_conf.return_value = json.loads(external_json)
product_type_conf = self.rest_utils.eodag_api.product_types_config["S2_MSI_L1C"]
ext_stac_collection_path = "/path/to/external/stac/collections/S2_MSI_L1C.json"
product_type_conf["stacCollection"] = ext_stac_collection_path
# using a context manager cause `self.rest_utils.get_stac_collection_by_id needs`
# to call the not-patched versione of `eodag.api.core.get_ext_product_types_conf`
with mock.patch(
"eodag.api.core.get_ext_product_types_conf", autospec=True
) as mock_core_get_ext_product_types_conf:
mock_core_get_ext_product_types_conf.return_value = json.loads(
external_json
)
self.rest_utils.eodag_api.fetch_external_product_types_metadata()
mock_core_get_ext_product_types_conf.assert_called_once_with(
ext_stac_collection_path
)
StacCollection.fetch_external_stac_collections(self.rest_utils.eodag_api)
r = self.rest_utils.get_stac_collection_by_id(
url="", root="", collection_id="S2_MSI_L1C"
)
mock_stac_get_ext_product_types_conf.assert_called_with(
ext_stac_collection_path
)
mock_stac_get_ext_product_types_conf.call_args_list
# Fields not supported by EODAG
self.assertIn("new_field", r)
# Merge lists
self.assertEqual(
r["summaries"]["platform"][0], "S2A,S2B,SENTINEL-2A,SENTINEL-2B"
)
# Don't override existings keys
self.assertEqual(r["title"], "SENTINEL2 Level-1C")
# Restore previous state
del self.rest_utils.eodag_api.product_types_config["S2_MSI_L1C"][
"stacCollection"
]
StacCollection.ext_stac_collections.clear()

0 comments on commit 89a6f0b

Please sign in to comment.