Skip to content

Commit

Permalink
set default value for Query attribute for QueryExtensionPostRequest
Browse files Browse the repository at this point in the history
… model (#701)
  • Loading branch information
vincentsarago authored May 31, 2024
1 parent 1c3546a commit a8eec23
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## [Unreleased] - TBD

## [3.0.0a2] - 2024-05-31

### Fixed

* Fix missing default (`None`) for optional `query` attribute in `QueryExtensionPostRequest` model ([#701](https://github.com/stac-utils/stac-fastapi/pull/701))

## [3.0.0a1] - 2024-05-22

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ class QueryExtensionGetRequest(APIRequest):
class QueryExtensionPostRequest(BaseModel):
"""Query Extension POST request model."""

query: Optional[Dict[str, Dict[str, Any]]]
query: Optional[Dict[str, Dict[str, Any]]] = None
95 changes: 95 additions & 0 deletions stac_fastapi/extensions/tests/test_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import json
from typing import Iterator
from urllib.parse import quote_plus, unquote_plus

import pytest
from starlette.testclient import TestClient

from stac_fastapi.api.app import StacApi
from stac_fastapi.api.models import create_get_request_model, create_post_request_model
from stac_fastapi.extensions.core import QueryExtension
from stac_fastapi.types.config import ApiSettings
from stac_fastapi.types.core import BaseCoreClient


class DummyCoreClient(BaseCoreClient):
def all_collections(self, *args, **kwargs):
raise NotImplementedError

def get_collection(self, *args, **kwargs):
raise NotImplementedError

def get_item(self, *args, **kwargs):
raise NotImplementedError

def get_search(self, *args, **kwargs):
return kwargs.pop("query", None)

def post_search(self, *args, **kwargs):
return args[0].query

def item_collection(self, *args, **kwargs):
raise NotImplementedError


@pytest.fixture
def client() -> Iterator[TestClient]:
settings = ApiSettings()
extensions = [QueryExtension()]

api = StacApi(
settings=settings,
client=DummyCoreClient(),
extensions=extensions,
search_get_request_model=create_get_request_model(extensions),
search_post_request_model=create_post_request_model(extensions),
)
with TestClient(api.app) as client:
yield client


def test_search_query_get(client: TestClient):
"""Test search GET endpoints with query ext."""
response = client.get(
"/search",
params={"collections": ["test"]},
)
assert response.is_success
assert not response.text

response = client.get(
"/search",
params={
"collections": ["test"],
"query": quote_plus(
json.dumps({"eo:cloud_cover": {"gte": 95}}),
),
},
)
assert response.is_success, response.json()
query = json.loads(unquote_plus(response.json()))
assert query["eo:cloud_cover"] == {"gte": 95}


def test_search_query_post(client: TestClient):
"""Test search POST endpoints with query ext."""
response = client.post(
"/search",
json={
"collections": ["test"],
},
)

assert response.is_success
assert not response.text

response = client.post(
"/search",
json={
"collections": ["test"],
"query": {"eo:cloud_cover": {"gte": 95}},
},
)

assert response.is_success, response.json()
assert response.json()["eo:cloud_cover"] == {"gte": 95}

0 comments on commit a8eec23

Please sign in to comment.