Debian Codegraph @

tests/test_allow_inf_nan_in_enforcing.py

raw
1 from typing import Annotated
2
3 import pytest
4 from fastapi import Body, FastAPI, Query
5 from fastapi.testclient import TestClient
6
7 app = FastAPI()
8
9
10 @app.post("/")
11 async def get(
12 x: Annotated[float, Query(allow_inf_nan=True)] = 0,
13 y: Annotated[float, Query(allow_inf_nan=False)] = 0,
14 z: Annotated[float, Query()] = 0,
15 b: Annotated[float, Body(allow_inf_nan=False)] = 0,
16 ) -> str:
17 return "OK"
18
19
20 client = TestClient(app)
21
22
23 @pytest.mark.parametrize(
24 "value,code",
25 [
26 ("-1", 200),
27 ("inf", 200),
28 ("-inf", 200),
29 ("nan", 200),
30 ("0", 200),
31 ("342", 200),
32 ],
33 )
34 def test_allow_inf_nan_param_true(value: str, code: int):
35 response = client.post(f"/?x={value}")
36 assert response.status_code == code, response.text
37
38
39 @pytest.mark.parametrize(
40 "value,code",
41 [
42 ("-1", 200),
43 ("inf", 422),
44 ("-inf", 422),
45 ("nan", 422),
46 ("0", 200),
47 ("342", 200),
48 ],
49 )
50 def test_allow_inf_nan_param_false(value: str, code: int):
51 response = client.post(f"/?y={value}")
52 assert response.status_code == code, response.text
53
54
55 @pytest.mark.parametrize(
56 "value,code",
57 [
58 ("-1", 200),
59 ("inf", 200),
60 ("-inf", 200),
61 ("nan", 200),
62 ("0", 200),
63 ("342", 200),
64 ],
65 )
66 def test_allow_inf_nan_param_default(value: str, code: int):
67 response = client.post(f"/?z={value}")
68 assert response.status_code == code, response.text
69
70
71 @pytest.mark.parametrize(
72 "value,code",
73 [
74 ("-1", 200),
75 ("inf", 422),
76 ("-inf", 422),
77 ("nan", 422),
78 ("0", 200),
79 ("342", 200),
80 ],
81 )
82 def test_allow_inf_nan_body(value: str, code: int):
83 response = client.post("/", json=value)
84 assert response.status_code == code, response.text