Skip to content

Commit

Permalink
add more datetime tests - issue768 (#771)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago authored Jan 6, 2025
1 parent 47eac1f commit c3b15c7
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
67 changes: 67 additions & 0 deletions stac_fastapi/api/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
17 changes: 10 additions & 7 deletions stac_fastapi/api/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")
Expand Down Expand Up @@ -62,32 +64,33 @@ 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,
)

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(
Expand Down

0 comments on commit c3b15c7

Please sign in to comment.