diff --git a/stac_fastapi/api/tests/test_app.py b/stac_fastapi/api/tests/test_app.py index 0887a62ee..54c8f5dfe 100644 --- a/stac_fastapi/api/tests/test_app.py +++ b/stac_fastapi/api/tests/test_app.py @@ -387,3 +387,70 @@ class ItemRequest(APIRequest): "/collections/test_collection/items/test_item", params={"user": "Chewbacca"} ) assert resp.status_code == 200 + + +def test_client_datetime_input_params(): + """Test all endpoints. Verify input models.""" + + class FakeClient(BaseCoreClient): + def post_search(self, search_request: BaseSearchPostRequest, **kwargs): + return search_request.datetime + + def get_search( + self, + collections: Optional[List[str]] = None, + ids: Optional[List[str]] = None, + bbox: Optional[List[NumType]] = None, + intersects: Optional[str] = None, + datetime: Optional[Union[str, datetime]] = None, + limit: Optional[int] = 10, + **kwargs, + ): + return datetime + + def get_item(self, item_id: str, collection_id: str, **kwargs) -> stac.Item: + raise NotImplementedError + + def all_collections(self, **kwargs) -> stac.Collections: + raise NotImplementedError + + def get_collection(self, collection_id: str, **kwargs) -> stac.Collection: + raise NotImplementedError + + def item_collection( + self, + collection_id: str, + bbox: Optional[List[Union[float, int]]] = None, + datetime: Optional[Union[str, datetime]] = None, + limit: int = 10, + token: str = None, + **kwargs, + ) -> stac.ItemCollection: + raise NotImplementedError + + test_app = app.StacApi( + settings=ApiSettings(enable_response_models=False), + client=FakeClient(), + ) + + with TestClient(test_app.app) as client: + get_search = client.get( + "/search", + params={ + "collections": ["test"], + "datetime": "2020-01-01T00:00:00.00001Z", + }, + ) + post_search = client.post( + "/search", + json={ + "collections": ["test"], + "datetime": "2020-01-01T00:00:00.00001Z", + }, + ) + + assert get_search.status_code == 200, get_search.text + assert get_search.json() == "2020-01-01T00:00:00.000010+00:00" + + assert post_search.status_code == 200, post_search.text + assert post_search.json() == "2020-01-01T00:00:00.00001Z" diff --git a/stac_fastapi/api/tests/test_models.py b/stac_fastapi/api/tests/test_models.py index 1df385d93..5c4fdc8c6 100644 --- a/stac_fastapi/api/tests/test_models.py +++ b/stac_fastapi/api/tests/test_models.py @@ -26,7 +26,7 @@ def test_create_get_request_model(): "coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]], } ), - datetime="2020-01-01T00:00:00Z", + datetime="2020-01-01T00:00:00.00001Z", limit=10, filter="test==test", filter_crs="epsg:4326", @@ -35,6 +35,8 @@ def test_create_get_request_model(): assert model.collections == ["test1", "test2"] assert model.filter_crs == "epsg:4326" + d = model.datetime + assert d.microsecond == 10 with pytest.raises(HTTPException): request_model(datetime="yo") @@ -62,10 +64,10 @@ def route(model=Depends(request_model)): @pytest.mark.parametrize( - "filter,passes", + "filter_val,passes", [(None, True), ({"test": "test"}, True), ([], False)], ) -def test_create_post_request_model(filter, passes): +def test_create_post_request_model(filter_val, passes): request_model = create_post_request_model( extensions=[FilterExtension(), FieldsExtension()], base_model=BaseSearchPostRequest, @@ -73,21 +75,22 @@ def test_create_post_request_model(filter, passes): if not passes: with pytest.raises(ValidationError): - model = request_model(filter=filter) + model = request_model(filter=filter_val) else: model = request_model( collections=["test1", "test2"], ids=["test1", "test2"], bbox=[0, 0, 1, 1], - datetime="2020-01-01T00:00:00Z", + datetime="2020-01-01T00:00:00.00001Z", limit=10, - filter=filter, + filter=filter_val, **{"filter-crs": "epsg:4326", "filter-lang": "cql2-json"}, ) assert model.collections == ["test1", "test2"] assert model.filter_crs == "epsg:4326" - assert model.filter == filter + assert model.filter == filter_val + assert model.datetime == "2020-01-01T00:00:00.00001Z" @pytest.mark.parametrize(