Coverage for tests / test_ambiguous_params.py: 100%
32 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from typing import Annotated 1efgh
3import pytest 1efgh
4from fastapi import Depends, FastAPI, Path 1efgh
5from fastapi.param_functions import Query 1efgh
6from fastapi.testclient import TestClient 1efgh
8app = FastAPI() 1efgh
11def test_no_annotated_defaults(): 1efgh
12 with pytest.raises( 1ijkl
13 AssertionError, match="Path parameters cannot have a default value"
14 ):
16 @app.get("/items/{item_id}/") 1ijkl
17 async def get_item(item_id: Annotated[int, Path(default=1)]): 1ijkl
18 pass # pragma: nocover
20 with pytest.raises( 1ijkl
21 AssertionError,
22 match=(
23 "`Query` default value cannot be set in `Annotated` for 'item_id'. Set the"
24 " default value with `=` instead."
25 ),
26 ):
28 @app.get("/") 1ijkl
29 async def get(item_id: Annotated[int, Query(default=1)]): 1ijkl
30 pass # pragma: nocover
33def test_multiple_annotations(): 1efgh
34 async def dep(): 1abcd
35 pass # pragma: nocover
37 @app.get("/multi-query") 1abcd
38 async def get(foo: Annotated[int, Query(gt=2), Query(lt=10)]): 1abcd
39 return foo 1abcd
41 with pytest.raises( 1abcd
42 AssertionError,
43 match=(
44 "Cannot specify `Depends` in `Annotated` and default value"
45 " together for 'foo'"
46 ),
47 ):
49 @app.get("/") 1abcd
50 async def get2(foo: Annotated[int, Depends(dep)] = Depends(dep)): 1abcd
51 pass # pragma: nocover
53 with pytest.raises( 1abcd
54 AssertionError,
55 match=(
56 "Cannot specify a FastAPI annotation in `Annotated` and `Depends` as a"
57 " default value together for 'foo'"
58 ),
59 ):
61 @app.get("/") 1abcd
62 async def get3(foo: Annotated[int, Query(min_length=1)] = Depends(dep)): 1abcd
63 pass # pragma: nocover
65 client = TestClient(app) 1abcd
66 response = client.get("/multi-query", params={"foo": "5"}) 1abcd
67 assert response.status_code == 200 1abcd
68 assert response.json() == 5 1abcd
70 response = client.get("/multi-query", params={"foo": "123"}) 1abcd
71 assert response.status_code == 422 1abcd
73 response = client.get("/multi-query", params={"foo": "1"}) 1abcd
74 assert response.status_code == 422 1abcd