Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set default value for Query attribute for QueryExtensionPostRequest model #701

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
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}