forked from fastapi/fastapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ambiguous_params.py
75 lines (59 loc) · 2.1 KB
/
test_ambiguous_params.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import pytest
from fastapi import Depends, FastAPI, Path
from fastapi.param_functions import Query
from fastapi.testclient import TestClient
from fastapi.utils import PYDANTIC_V2
from typing_extensions import Annotated
app = FastAPI()
def test_no_annotated_defaults():
with pytest.raises(
AssertionError, match="Path parameters cannot have a default value"
):
@app.get("/items/{item_id}/")
async def get_item(item_id: Annotated[int, Path(default=1)]):
pass # pragma: nocover
with pytest.raises(
AssertionError,
match=(
"`Query` default value cannot be set in `Annotated` for 'item_id'. Set the"
" default value with `=` instead."
),
):
@app.get("/")
async def get(item_id: Annotated[int, Query(default=1)]):
pass # pragma: nocover
def test_multiple_annotations():
async def dep():
pass # pragma: nocover
@app.get("/multi-query")
async def get(foo: Annotated[int, Query(gt=2), Query(lt=10)]):
return foo
with pytest.raises(
AssertionError,
match=(
"Cannot specify `Depends` in `Annotated` and default value"
" together for 'foo'"
),
):
@app.get("/")
async def get2(foo: Annotated[int, Depends(dep)] = Depends(dep)):
pass # pragma: nocover
with pytest.raises(
AssertionError,
match=(
"Cannot specify a FastAPI annotation in `Annotated` and `Depends` as a"
" default value together for 'foo'"
),
):
@app.get("/")
async def get3(foo: Annotated[int, Query(min_length=1)] = Depends(dep)):
pass # pragma: nocover
client = TestClient(app)
response = client.get("/multi-query", params={"foo": "5"})
assert response.status_code == 200
assert response.json() == 5
response = client.get("/multi-query", params={"foo": "123"})
assert response.status_code == 422
if PYDANTIC_V2:
response = client.get("/multi-query", params={"foo": "1"})
assert response.status_code == 422