Skip to content

Commit a9d2192

Browse files
authored
fix: make datetime filter work again on /collections (#46)
On `/collections` endpoint, the `datetime` filter made the request fail. It fixes by the given input type from `str` to the type required by the processing.
1 parent 65d9e25 commit a9d2192

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

stac_fastapi/eodag/core.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import asyncio
2323
import logging
24-
from datetime import datetime
2524
from typing import TYPE_CHECKING, Any, cast
2625
from urllib.parse import unquote_plus
2726

@@ -37,6 +36,7 @@
3736
from pygeofilter.parsers.cql2_text import parse as parse_cql2_text
3837
from stac_fastapi.types.errors import NotFoundError
3938
from stac_fastapi.types.requests import get_base_url
39+
from stac_fastapi.types.rfc3339 import str_to_interval
4040
from stac_fastapi.types.search import BaseSearchPostRequest
4141
from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection
4242
from stac_pydantic.links import Relations
@@ -75,7 +75,6 @@
7575

7676
from fastapi import Request
7777
from pydantic import BaseModel
78-
from stac_fastapi.types.rfc3339 import DateTimeType
7978

8079
from eodag.api.product._product import EOProduct
8180

@@ -222,7 +221,7 @@ async def all_collections(
222221
self,
223222
request: Request,
224223
bbox: Optional[list[NumType]] = None,
225-
datetime: Optional[DateTimeType] = None,
224+
datetime: Optional[str] = None,
226225
limit: Optional[int] = 10,
227226
offset: Optional[int] = 0,
228227
q: Optional[str] = None,
@@ -259,7 +258,7 @@ async def all_collections(
259258

260259
# datetime & free-text-search filters
261260
if any((q, datetime)):
262-
start, end = dt_range_to_eodag(datetime)
261+
start, end = dt_range_to_eodag(str_to_interval(datetime))
263262

264263
try:
265264
guessed_product_types = request.app.state.dag.guess_product_type(
@@ -355,7 +354,7 @@ async def item_collection(
355354
collection_id: str,
356355
request: Request,
357356
bbox: Optional[list[NumType]] = None,
358-
datetime: Optional[Union[str, datetime]] = None,
357+
datetime: Optional[str] = None,
359358
limit: Optional[int] = None,
360359
page: Optional[str] = None,
361360
sortby: Optional[list[str]] = None,
@@ -422,7 +421,7 @@ def get_search(
422421
collections: Optional[list[str]] = None,
423422
ids: Optional[list[str]] = None,
424423
bbox: Optional[list[NumType]] = None,
425-
datetime: Optional[DateTimeType] = None,
424+
datetime: Optional[str] = None,
426425
limit: Optional[int] = None,
427426
query: Optional[str] = None,
428427
page: Optional[str] = None,

tests/test_collections.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,30 @@ async def test_search_collections_bbox(app_client, mock_list_product_types, mock
127127
assert ["S2_MSI_L1C", "S1_SAR_GRD"] == [col["id"] for col in r.json().get("collections", [])]
128128

129129

130+
async def test_search_collections_datetime(app_client, mock_list_product_types, mock_guess_product_type):
131+
"""A collections datetime search must succeed"""
132+
mock_list_product_types.return_value = [
133+
{
134+
"_id": "S2_MSI_L1C",
135+
"ID": "S2_MSI_L1C",
136+
"title": "SENTINEL2 Level-1C",
137+
"missionStartDate": "2015-06-23T00:00:00Z",
138+
},
139+
{"_id": "S2_MSI_L2A", "ID": "S2_MSI_L2A"},
140+
]
141+
mock_guess_product_type.return_value = ["S2_MSI_L1C"]
142+
143+
start = "2014-01-01T00:00:00Z"
144+
end = "2016-01-01T00:00:00Z"
145+
146+
r = await app_client.get(f"/collections?datetime={start}/{end}")
147+
148+
assert mock_list_product_types.called
149+
mock_guess_product_type.assert_called_once_with(free_text=None, missionStartDate=start, missionEndDate=end)
150+
assert r.status_code == 200
151+
assert ["S2_MSI_L1C"] == [col["id"] for col in r.json().get("collections", [])]
152+
153+
130154
async def test_collections_pagination_default_and_custom_limits(app_client, mock_list_product_types):
131155
"""
132156
Test pagination behavior for collections with default and custom limits.

0 commit comments

Comments
 (0)