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

rename filter to filter_expr to avoid python method conflict #784

Merged
merged 1 commit into from
Jan 17, 2025
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## Changed

* use `string` type instead of python `datetime.datetime` for datetime parameter in `BaseSearchGetRequest`, `ItemCollectionUri` and `BaseCollectionSearchGetRequest` GET models
* rename `filter` to `filter_expr` for `FilterExtensionGetRequest` and `FilterExtensionPostRequest` attributes to avoid conflict with python filter method

## [3.0.5] - 2025-01-10

Expand Down
19 changes: 5 additions & 14 deletions stac_fastapi/api/tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def post_search(
self, search_request: BaseSearchPostRequest, **kwargs
) -> stac.ItemCollection:
search_request.collections = ["test"]
search_request.filter = {}
search_request.filter_expr = {}
search_request.filter_crs = "EPSG:4326"
search_request.filter_lang = "cql2-text"

Expand All @@ -142,23 +142,14 @@ def get_search(
intersects: Optional[str] = None,
datetime: Optional[str] = None,
limit: Optional[int] = 10,
filter: Optional[str] = None,
filter_expr: Optional[str] = None,
filter_crs: Optional[str] = None,
filter_lang: Optional[str] = None,
**kwargs,
) -> stac.ItemCollection:
# Check if all filter parameters are passed correctly

assert filter == "TEST"

# FIXME: https://github.com/stac-utils/stac-fastapi/issues/638
# hyphen alias for filter_crs and filter_lang are currently not working
# Query parameters `filter-crs` and `filter-lang`
# should be recognized by the API
# They are present in the `request.query_params` but not in the `kwargs`

# assert filter_crs == "EPSG:4326"
# assert filter_lang == "cql2-text"
assert filter_expr == "TEST"
assert filter_crs == "EPSG:4326"
assert filter_lang == "cql2-text"

return stac.ItemCollection(
type="FeatureCollection", features=[stac.Item(**item_dict)]
Expand Down
26 changes: 16 additions & 10 deletions stac_fastapi/api/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ def test_create_get_request_model():
),
datetime="2020-01-01T00:00:00.00001Z",
limit=10,
filter="test==test",
filter_expr="test==test",
filter_crs="epsg:4326",
filter_lang="cql2-text",
)

assert model.collections == ["test1", "test2"]
assert model.filter_expr == "test==test"
assert model.filter_crs == "epsg:4326"
d = model.start_date
assert d.microsecond == 10
Expand Down Expand Up @@ -64,13 +65,15 @@ def route(model=Depends(request_model)):
"/test",
params={
"collections": "test1,test2",
"filter": "test=test",
"filter-crs": "epsg:4326",
"filter-lang": "cql2-text",
},
)
assert resp.status_code == 200
response_dict = resp.json()
assert response_dict["collections"] == ["test1", "test2"]
assert response_dict["filter_expr"] == "test=test"
assert response_dict["filter_crs"] == "epsg:4326"
assert response_dict["filter_lang"] == "cql2-text"

Expand All @@ -89,19 +92,22 @@ def test_create_post_request_model(filter_val, passes):
with pytest.raises(ValidationError):
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:00.00001Z",
limit=10,
filter=filter_val,
**{"filter-crs": "epsg:4326", "filter-lang": "cql2-json"},
model = request_model.model_validate(
{
"collections": ["test1", "test2"],
"ids": ["test1", "test2"],
"bbox": [0, 0, 1, 1],
"datetime": "2020-01-01T00:00:00.00001Z",
"limit": 10,
"filter": filter_val,
"filter-crs": "epsg:4326",
"filter-lang": "cql2-json",
}
)

assert model.collections == ["test1", "test2"]
assert model.filter_expr == filter_val
assert model.filter_crs == "epsg:4326"
assert model.filter == filter_val
assert model.datetime == "2020-01-01T00:00:00.00001Z"

with pytest.raises(ValidationError):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
class FilterExtensionGetRequest(APIRequest):
"""Filter extension GET request model."""

filter: Annotated[
filter_expr: Annotated[
Optional[str],
Query(
alias="filter",
description="""A CQL filter expression for filtering items.\n
Supports `CQL-JSON` as defined in https://portal.ogc.org/files/96288\n
Remember to URL encode the CQL-JSON if using GET""",
Expand Down Expand Up @@ -46,8 +47,9 @@ class FilterExtensionGetRequest(APIRequest):
class FilterExtensionPostRequest(BaseModel):
"""Filter extension POST request model."""

filter: Optional[Dict[str, Any]] = Field(
filter_expr: Optional[Dict[str, Any]] = Field(
default=None,
alias="filter",
description="A CQL filter expression for filtering items.",
json_schema_extra={
"example": {
Expand Down
14 changes: 8 additions & 6 deletions stac_fastapi/extensions/tests/test_collection_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_collection_search_extension_models():
assert "datetime" in response_dict
assert "limit" in response_dict
assert "q" in response_dict
assert "filter" in response_dict
assert "filter_expr" in response_dict
gadomski marked this conversation as resolved.
Show resolved Hide resolved
assert "query" in response_dict
assert "sortby" in response_dict
assert "fields" in response_dict
Expand All @@ -211,7 +211,9 @@ def test_collection_search_extension_models():
assert "2020-06-13T13:00:00Z/2020-06-13T14:00:00Z" == response_dict["datetime"]
assert 100 == response_dict["limit"]
assert ["EO", "Earth Observation"] == response_dict["q"]
assert "id='item_id' AND collection='collection_id'" == response_dict["filter"]
assert (
"id='item_id' AND collection='collection_id'" == response_dict["filter_expr"]
)
assert "filter_crs" in response_dict
assert "cql2-text" in response_dict["filter_lang"]
assert "query" in response_dict
Expand Down Expand Up @@ -347,7 +349,7 @@ def test_collection_search_extension_post_models():
assert "datetime" in response_dict
assert "limit" in response_dict
assert "q" in response_dict
assert "filter" in response_dict
assert "filter_expr" in response_dict
assert "query" in response_dict
assert "sortby" in response_dict
assert "fields" in response_dict
Expand Down Expand Up @@ -388,7 +390,7 @@ def test_collection_search_extension_post_models():
assert "2020-06-13T13:00:00Z/2020-06-13T14:00:00Z" == response_dict["datetime"]
assert 10_000 == response_dict["limit"]
assert ["EO", "Earth Observation"] == response_dict["q"]
assert response_dict["filter"]
assert response_dict["filter_expr"]
assert "filter_crs" in response_dict
assert "cql2-json" in response_dict["filter_lang"]
assert response_dict["query"]
Expand Down Expand Up @@ -433,7 +435,7 @@ def test_from_extensions_methods(extensions):
assert hasattr(collection_search, "fields")
assert hasattr(collection_search, "q")
assert hasattr(collection_search, "sortby")
assert hasattr(collection_search, "filter")
assert hasattr(collection_search, "filter_expr")
assert ext.conformance_classes == [
ConformanceClasses.COLLECTIONSEARCH,
ConformanceClasses.BASIS,
Expand All @@ -457,7 +459,7 @@ def test_from_extensions_methods(extensions):
assert hasattr(collection_search, "fields")
assert hasattr(collection_search, "q")
assert hasattr(collection_search, "sortby")
assert hasattr(collection_search, "filter")
assert hasattr(collection_search, "filter_expr")
assert ext.conformance_classes == [
ConformanceClasses.COLLECTIONSEARCH,
ConformanceClasses.BASIS,
Expand Down
6 changes: 4 additions & 2 deletions stac_fastapi/extensions/tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def test_search_filter_post_filter_lang_default(client: TestClient):
)
assert response.is_success, response.json()
response_dict = response.json()
assert response_dict["filter_expr"]
assert response_dict["filter_lang"] == "cql2-json"


Expand All @@ -73,6 +74,7 @@ def test_search_filter_post_filter_lang_non_default(client: TestClient):
)
assert response.is_success, response.json()
response_dict = response.json()
assert response_dict["filter_expr"]
assert response_dict["filter_lang"] == filter_lang_value


Expand All @@ -87,7 +89,7 @@ def test_search_filter_get(client: TestClient):
assert response.is_success, response.json()
response_dict = response.json()
assert not response_dict["collections"]
assert response_dict["filter"] == "id='item_id' AND collection='collection_id'"
assert response_dict["filter_expr"] == "id='item_id' AND collection='collection_id'"
assert not response_dict["filter_crs"]
assert response_dict["filter_lang"] == "cql2-text"

Expand All @@ -102,7 +104,7 @@ def test_search_filter_get(client: TestClient):
response_dict = response.json()
assert not response_dict["collections"]
assert (
response_dict["filter"]
response_dict["filter_expr"]
== "{'op': '=', 'args': [{'property': 'id'}, 'test-item']}"
)
assert not response_dict["filter_crs"]
Expand Down
Loading