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 limit in sqlalchemy backend between 1 and 10000 #251

Merged
merged 4 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from typing import Any, Callable, Dict, List, Optional, Set, Union

import sqlalchemy as sa
from pydantic import Field, ValidationError, root_validator
from pydantic import Field, ValidationError, conint, root_validator
from pydantic.error_wrappers import ErrorWrapper
from stac_pydantic.api import Search
from stac_pydantic.api.extensions.fields import FieldsExtension as FieldsBase
Expand Down Expand Up @@ -145,6 +145,7 @@ class SQLAlchemySTACSearch(Search):
# Override query extension with supported operators
query: Optional[Dict[Queryables, Dict[Operator, Any]]]
token: Optional[str] = None
limit: Optional[conint(ge=0, le=10000)] = 10

@root_validator(pre=True)
def validate_query_fields(cls, values: Dict) -> Dict:
Expand Down
33 changes: 33 additions & 0 deletions stac_fastapi/sqlalchemy/tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,39 @@ def test_app_query_extension(load_test_data, app_client, postgres_transactions):
assert len(resp_json["features"]) == 0


def test_app_query_extension_limit_lt0(
load_test_data, app_client, postgres_transactions
):
item = load_test_data("test_item.json")
postgres_transactions.create_item(item, request=MockStarletteRequest)

params = {"limit": -1}
resp = app_client.post("/search", json=params)
assert resp.status_code == 400


def test_app_query_extension_limit_gt10000(
load_test_data, app_client, postgres_transactions
):
item = load_test_data("test_item.json")
postgres_transactions.create_item(item, request=MockStarletteRequest)

params = {"limit": 10001}
resp = app_client.post("/search", json=params)
assert resp.status_code == 400


def test_app_query_extension_limit_10000(
load_test_data, app_client, postgres_transactions
):
item = load_test_data("test_item.json")
postgres_transactions.create_item(item, request=MockStarletteRequest)

params = {"limit": 10000}
resp = app_client.post("/search", json=params)
assert resp.status_code == 200


def test_app_sort_extension(load_test_data, app_client, postgres_transactions):
first_item = load_test_data("test_item.json")
item_date = datetime.strptime(
Expand Down