forked from fastapi/fastapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ambiguous_params.py
More file actions
66 lines (52 loc) · 1.78 KB
/
test_ambiguous_params.py
File metadata and controls
66 lines (52 loc) · 1.78 KB
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
import pytest
from fastapi import Depends, FastAPI, Path
from fastapi.param_functions import Query
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_no_multiple_annotations():
async def dep():
pass # pragma: nocover
with pytest.raises(
AssertionError,
match="Cannot specify multiple `Annotated` FastAPI arguments for 'foo'",
):
@app.get("/")
async def get(foo: Annotated[int, Query(min_length=1), Query()]):
pass # pragma: nocover
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