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

DM-47027: Change get param dependency to correctly parse list params #231

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions changelog.d/20241021_232057_steliosvoutsinas_DM_47027.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- Delete the sections that don't apply -->

### Bug fixes

- Change get_params_dependency to fetch params using multi_items method
2 changes: 1 addition & 1 deletion src/vocutouts/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def get_params_dependency(
"""Parse GET parameters into job parameters for a cutout."""
return [
UWSJobParameter(parameter_id=k, value=v)
for k, v in request.query_params.items()
for k, v in request.query_params.multi_items()
if k in {"id", "pos", "circle", "polygon"}
]

Expand Down
30 changes: 28 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,50 @@

from collections.abc import AsyncIterator, Iterator
from datetime import timedelta
from typing import Annotated

import pytest
import pytest_asyncio
import respx
import structlog
from asgi_lifespan import LifespanManager
from fastapi import FastAPI
from fastapi import APIRouter, Depends, FastAPI
from httpx import ASGITransport, AsyncClient
from safir.arq import MockArqQueue
from safir.testing.gcs import MockStorageClient, patch_google_storage
from safir.testing.slack import MockSlackWebhook, mock_slack_webhook
from safir.testing.uws import MockUWSJobRunner
from safir.uws import UWSJobParameter

from vocutouts import main
from vocutouts.config import config, uws
from vocutouts.dependencies import get_params_dependency


@pytest.fixture
def get_params_router() -> APIRouter:
"""Return a router that echoes the parameters passed to it."""
router = APIRouter()

@router.get("/params")
async def get_params(
params: Annotated[
list[UWSJobParameter], Depends(get_params_dependency)
],
) -> dict[str, list[dict[str, str]]]:
return {
"params": [
{"id": p.parameter_id, "value": p.value} for p in params
]
}

return router


@pytest_asyncio.fixture
async def app(arq_queue: MockArqQueue) -> AsyncIterator[FastAPI]:
async def app(
arq_queue: MockArqQueue, get_params_router: APIRouter
) -> AsyncIterator[FastAPI]:
"""Return a configured test application.

Initialize the database before creating the app to ensure that data is
Expand All @@ -35,6 +60,7 @@ async def app(arq_queue: MockArqQueue) -> AsyncIterator[FastAPI]:
# Otherwise, the web application will use the one created in its
# lifespan context manager.
uws.override_arq_queue(arq_queue)
main.app.include_router(get_params_router)
yield main.app


Expand Down
16 changes: 16 additions & 0 deletions tests/handlers/sync_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,19 @@ async def test_bad_parameters(

# None of these requests should have been reported to Slack.
assert mock_slack.messages == []


@pytest.mark.asyncio
async def test_get_dependency_multiple_params(client: AsyncClient) -> None:
response = await client.get(
"/params?id=image1&id=image2&pos=" "RANGE 10 20 30 40&circle=10 20 5"
)
assert response.status_code == 200
assert response.json() == {
"params": [
{"id": "id", "value": "image1"},
{"id": "id", "value": "image2"},
{"id": "pos", "value": "RANGE 10 20 30 40"},
{"id": "circle", "value": "10 20 5"},
]
}