Coverage for tests / test_forms_single_model.py: 100%
45 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 1adbc
3from fastapi import FastAPI, Form 1adbc
4from fastapi.testclient import TestClient 1adbc
5from pydantic import BaseModel, Field 1adbc
7app = FastAPI() 1adbc
10class FormModel(BaseModel): 1adbc
11 username: str 1abc
12 lastname: str 1abc
13 age: int | None = None 1adbc
14 tags: list[str] = ["foo", "bar"] 1adbc
15 alias_with: str = Field(alias="with", default="nothing") 1adbc
18class FormModelExtraAllow(BaseModel): 1adbc
19 param: str 1abc
21 model_config = {"extra": "allow"} 1adbc
24@app.post("/form/") 1adbc
25def post_form(user: Annotated[FormModel, Form()]): 1adbc
26 return user 1efghijk
29@app.post("/form-extra-allow/") 1adbc
30def post_form_extra_allow(params: Annotated[FormModelExtraAllow, Form()]): 1adbc
31 return params 1lmnopqr
34client = TestClient(app) 1adbc
37def test_send_all_data(): 1adbc
38 response = client.post( 1fgik
39 "/form/",
40 data={
41 "username": "Rick",
42 "lastname": "Sanchez",
43 "age": "70",
44 "tags": ["plumbus", "citadel"],
45 "with": "something",
46 },
47 )
48 assert response.status_code == 200, response.text 1fgik
49 assert response.json() == { 1fgik
50 "username": "Rick",
51 "lastname": "Sanchez",
52 "age": 70,
53 "tags": ["plumbus", "citadel"],
54 "with": "something",
55 }
58def test_defaults(): 1adbc
59 response = client.post("/form/", data={"username": "Rick", "lastname": "Sanchez"}) 1eshj
60 assert response.status_code == 200, response.text 1eshj
61 assert response.json() == { 1eshj
62 "username": "Rick",
63 "lastname": "Sanchez",
64 "age": None,
65 "tags": ["foo", "bar"],
66 "with": "nothing",
67 }
70def test_invalid_data(): 1adbc
71 response = client.post( 1tuvw
72 "/form/",
73 data={
74 "username": "Rick",
75 "lastname": "Sanchez",
76 "age": "seventy",
77 "tags": ["plumbus", "citadel"],
78 },
79 )
80 assert response.status_code == 422, response.text 1tuvw
81 assert response.json() == { 1tuvw
82 "detail": [
83 {
84 "type": "int_parsing",
85 "loc": ["body", "age"],
86 "msg": "Input should be a valid integer, unable to parse string as an integer",
87 "input": "seventy",
88 }
89 ]
90 }
93def test_no_data(): 1adbc
94 response = client.post("/form/") 1xyzA
95 assert response.status_code == 422, response.text 1xyzA
96 assert response.json() == { 1xyzA
97 "detail": [
98 {
99 "type": "missing",
100 "loc": ["body", "username"],
101 "msg": "Field required",
102 "input": {"tags": ["foo", "bar"], "with": "nothing"},
103 },
104 {
105 "type": "missing",
106 "loc": ["body", "lastname"],
107 "msg": "Field required",
108 "input": {"tags": ["foo", "bar"], "with": "nothing"},
109 },
110 ]
111 }
114def test_extra_param_single(): 1adbc
115 response = client.post( 1mnpr
116 "/form-extra-allow/",
117 data={
118 "param": "123",
119 "extra_param": "456",
120 },
121 )
122 assert response.status_code == 200, response.text 1mnpr
123 assert response.json() == { 1mnpr
124 "param": "123",
125 "extra_param": "456",
126 }
129def test_extra_param_list(): 1adbc
130 response = client.post( 1lBoq
131 "/form-extra-allow/",
132 data={
133 "param": "123",
134 "extra_params": ["456", "789"],
135 },
136 )
137 assert response.status_code == 200, response.text 1lBoq
138 assert response.json() == { 1lBoq
139 "param": "123",
140 "extra_params": ["456", "789"],
141 }