Skip to content

Commit a8eec23

Browse files
set default value for Query attribute for QueryExtensionPostRequest model (#701)
1 parent 1c3546a commit a8eec23

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [Unreleased] - TBD
44

5+
## [3.0.0a2] - 2024-05-31
6+
7+
### Fixed
8+
9+
* Fix missing default (`None`) for optional `query` attribute in `QueryExtensionPostRequest` model ([#701](https://github.com/stac-utils/stac-fastapi/pull/701))
10+
511
## [3.0.0a1] - 2024-05-22
612

713
### Changed

stac_fastapi/extensions/stac_fastapi/extensions/core/query/request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ class QueryExtensionGetRequest(APIRequest):
1818
class QueryExtensionPostRequest(BaseModel):
1919
"""Query Extension POST request model."""
2020

21-
query: Optional[Dict[str, Dict[str, Any]]]
21+
query: Optional[Dict[str, Dict[str, Any]]] = None
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import json
2+
from typing import Iterator
3+
from urllib.parse import quote_plus, unquote_plus
4+
5+
import pytest
6+
from starlette.testclient import TestClient
7+
8+
from stac_fastapi.api.app import StacApi
9+
from stac_fastapi.api.models import create_get_request_model, create_post_request_model
10+
from stac_fastapi.extensions.core import QueryExtension
11+
from stac_fastapi.types.config import ApiSettings
12+
from stac_fastapi.types.core import BaseCoreClient
13+
14+
15+
class DummyCoreClient(BaseCoreClient):
16+
def all_collections(self, *args, **kwargs):
17+
raise NotImplementedError
18+
19+
def get_collection(self, *args, **kwargs):
20+
raise NotImplementedError
21+
22+
def get_item(self, *args, **kwargs):
23+
raise NotImplementedError
24+
25+
def get_search(self, *args, **kwargs):
26+
return kwargs.pop("query", None)
27+
28+
def post_search(self, *args, **kwargs):
29+
return args[0].query
30+
31+
def item_collection(self, *args, **kwargs):
32+
raise NotImplementedError
33+
34+
35+
@pytest.fixture
36+
def client() -> Iterator[TestClient]:
37+
settings = ApiSettings()
38+
extensions = [QueryExtension()]
39+
40+
api = StacApi(
41+
settings=settings,
42+
client=DummyCoreClient(),
43+
extensions=extensions,
44+
search_get_request_model=create_get_request_model(extensions),
45+
search_post_request_model=create_post_request_model(extensions),
46+
)
47+
with TestClient(api.app) as client:
48+
yield client
49+
50+
51+
def test_search_query_get(client: TestClient):
52+
"""Test search GET endpoints with query ext."""
53+
response = client.get(
54+
"/search",
55+
params={"collections": ["test"]},
56+
)
57+
assert response.is_success
58+
assert not response.text
59+
60+
response = client.get(
61+
"/search",
62+
params={
63+
"collections": ["test"],
64+
"query": quote_plus(
65+
json.dumps({"eo:cloud_cover": {"gte": 95}}),
66+
),
67+
},
68+
)
69+
assert response.is_success, response.json()
70+
query = json.loads(unquote_plus(response.json()))
71+
assert query["eo:cloud_cover"] == {"gte": 95}
72+
73+
74+
def test_search_query_post(client: TestClient):
75+
"""Test search POST endpoints with query ext."""
76+
response = client.post(
77+
"/search",
78+
json={
79+
"collections": ["test"],
80+
},
81+
)
82+
83+
assert response.is_success
84+
assert not response.text
85+
86+
response = client.post(
87+
"/search",
88+
json={
89+
"collections": ["test"],
90+
"query": {"eo:cloud_cover": {"gte": 95}},
91+
},
92+
)
93+
94+
assert response.is_success, response.json()
95+
assert response.json()["eo:cloud_cover"] == {"gte": 95}

0 commit comments

Comments
 (0)