Repository: CS-SI/eodag
Pull request #526 (2022-10-13 12:00:41)

fix: rest search without stac formatting
Adds a `stac_formatted` parameter to `eodag.rest.utils.search_products()` to accept parameters that are not STAC formatted.

Needed by https://github.com/CS-SI/eodag-labextension/issues/71

github-actions[bot]: ## Unit Test Results
    1 files   -     1      1 suites   - 1   3m 21s [:stopwatch:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "duration of all tests") - 4m 29s
296 tests ±    0  293 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "passed tests") ±    0  2 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "skipped / disabled tests")  - 1  1 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "failed tests") +1 
296 runs   - 296  293 [:heavy_check_mark:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "passed tests")  - 293  2 [:zzz:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "skipped / disabled tests")  - 4  1 [:x:](https://github.com/EnricoMi/publish-unit-test-result-action/blob/v1.20/README.md#the-symbols "failed tests") +1 

For more details on these failures, see [this check](https://github.com/CS-SI/eodag/runs/8868561617).

Results for commit 8512d0fa. ± Comparison against base commit df0c9b5a.

[test-results]:data:application/gzip;base64,H4sIADD/R2MC/1XMSw6DIBSF4a0Yxh3gpaJ0M80tj4RUpeExMt17AVuxw/87ydmIsbMO5Nb1l46EZOMRKnmM1q05gRbIUywjCP6re0hSVmKNnvZV6ACDdv5+7qC9dz4LzeLT2j5L/F/u0h5rnw5rn/+kWxYbc5Bp6EFRg1qoEajgaIBq8xAScOSDYOyKHGGi5P0BvnUhqQUBAAA=

github-actions[bot]: <strong></strong>

| File | Coverage |   |
| - | :-: | :-: |
| **All files** | `83%` | :white_check_mark: |
| eodag/rest/utils.py | `83%` | :white_check_mark: |
| tests/units/test\_stac\_utils.py | `97%` | :white_check_mark: |

_Minimum allowed coverage is `70%`_

<p align="right">Generated by :monkey: cobertura-action against 8512d0fae9d72096af20efb9c2a7659334a6a280 </p>

Patch:
diff --git a/eodag/rest/utils.py b/eodag/rest/utils.py
index 126cdb70..5b929bd5 100644
--- a/eodag/rest/utils.py
+++ b/eodag/rest/utils.py
@@ -381,13 +381,15 @@ def get_criterias_from_metadata_mapping(metadata_mapping, arguments):
     return criterias
 
 
-def search_products(product_type, arguments):
+def search_products(product_type, arguments, stac_formatted=True):
     """Returns product search results
 
     :param product_type: The product type criteria
     :type product_type: str
     :param arguments: Request args
     :type arguments: dict
+    :param stac_formatted: Whether input is STAC-formatted or not
+    :type stac_formatted: bool
     :returns: A search result
     :rtype serialized GeoJSON response"""
 
@@ -409,13 +411,16 @@ def search_products(product_type, arguments):
             "geom": geom,
         }
 
-        stac_provider_metadata_mapping = stac_provider_config.get("search", {}).get(
-            "metadata_mapping", {}
-        )
-        extra_criterias = get_criterias_from_metadata_mapping(
-            stac_provider_metadata_mapping, arguments
-        )
-        criterias.update(extra_criterias)
+        if stac_formatted:
+            stac_provider_metadata_mapping = stac_provider_config.get("search", {}).get(
+                "metadata_mapping", {}
+            )
+            extra_criterias = get_criterias_from_metadata_mapping(
+                stac_provider_metadata_mapping, arguments
+            )
+            criterias.update(extra_criterias)
+        else:
+            criterias.update(arguments)
 
         # We remove potential None values to use the default values of the search method
         criterias = dict((k, v) for k, v in criterias.items() if v is not None)


Test patch:
diff --git a/tests/units/test_stac_utils.py b/tests/units/test_stac_utils.py
index a85b01e4..0f86ded9 100644
--- a/tests/units/test_stac_utils.py
+++ b/tests/units/test_stac_utils.py
@@ -342,5 +342,76 @@ class TestStacUtils(unittest.TestCase):
         self.rest_utils.search_products("S2_MSI_L1C", {})
         self.rest_utils.search_products("S2_MSI_L1C", {"unserialized": "true"})
 
+        # STAC formatted
+        self.rest_utils.search_products(
+            "S2_MSI_L1C",
+            {
+                "intersects": {
+                    "type": "Polygon",
+                    "coordinates": [
+                        [
+                            [0.25, 43.2],
+                            [0.25, 43.9],
+                            [2.8, 43.9],
+                            [2.8, 43.2],
+                            [0.25, 43.2],
+                        ]
+                    ],
+                },
+                "query": {"eo:cloud_cover": {"lte": 50}},
+                "dtstart": "2020-02-01T00:00:00.000Z",
+                "dtend": "2021-02-20T00:00:00.000Z",
+                "product_type": "S2_MSI_L1C",
+                "unserialized": "true",
+            },
+        )
+        call_args, call_kwargs = mock_do_search.call_args
+        self.assertDictContainsSubset(
+            {
+                "productType": "S2_MSI_L1C",
+                "cloudCover": 50,
+                "startTimeFromAscendingNode": "2020-02-01T00:00:00",
+                "completionTimeFromAscendingNode": "2021-02-20T00:00:00",
+            },
+            call_kwargs,
+        )
+        self.assertEqual(call_kwargs["geometry"].bounds, (0.25, 43.2, 2.8, 43.9))
+
+        # Not STAC formatted
+        self.rest_utils.search_products(
+            "S2_MSI_L1C",
+            {
+                "geom": {
+                    "type": "Polygon",
+                    "coordinates": [
+                        [
+                            [0.25, 43.2],
+                            [0.25, 43.9],
+                            [2.8, 43.9],
+                            [2.8, 43.2],
+                            [0.25, 43.2],
+                        ]
+                    ],
+                },
+                "cloudCover": 50,
+                "dtstart": "2020-02-01T00:00:00.000Z",
+                "dtend": "2021-02-20T00:00:00.000Z",
+                "product_type": "S2_MSI_L1C",
+                "unserialized": "true",
+            },
+            stac_formatted=False,
+        )
+        call_args, call_kwargs = mock_do_search.call_args
+        self.assertDictContainsSubset(
+            {
+                "productType": "S2_MSI_L1C",
+                "cloudCover": 50,
+                "startTimeFromAscendingNode": "2020-02-01T00:00:00",
+                "completionTimeFromAscendingNode": "2021-02-20T00:00:00",
+            },
+            call_kwargs,
+        )
+        self.assertEqual(call_kwargs["geometry"].bounds, (0.25, 43.2, 2.8, 43.9))
+
     def test_search_stac_items(self):
         pass  # TODO