Coverage for tests/test_union_forms.py: 100%
34 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-09-09 09:16 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-09-09 09:16 +0000
1from typing import Union 1abcdef
3from fastapi import FastAPI, Form 1abcdef
4from fastapi.testclient import TestClient 1abcdef
5from pydantic import BaseModel 1abcdef
6from typing_extensions import Annotated 1abcdef
8app = FastAPI() 1abcdef
11class UserForm(BaseModel): 1abcdef
12 name: str 1abcdef
13 email: str 1abcdef
16class CompanyForm(BaseModel): 1abcdef
17 company_name: str 1abcdef
18 industry: str 1abcdef
21@app.post("/form-union/") 1abcdef
22def post_union_form(data: Annotated[Union[UserForm, CompanyForm], Form()]): 1abcdef
23 return {"received": data} 1ghijklmnopqr
26client = TestClient(app) 1abcdef
29def test_post_user_form(): 1abcdef
30 response = client.post( 1hjlnpr
31 "/form-union/", data={"name": "John Doe", "email": "john@example.com"}
32 )
33 assert response.status_code == 200, response.text 1hjlnpr
34 assert response.json() == { 1hjlnpr
35 "received": {"name": "John Doe", "email": "john@example.com"}
36 }
39def test_post_company_form(): 1abcdef
40 response = client.post( 1gikmoq
41 "/form-union/", data={"company_name": "Tech Corp", "industry": "Technology"}
42 )
43 assert response.status_code == 200, response.text 1gikmoq
44 assert response.json() == { 1gikmoq
45 "received": {"company_name": "Tech Corp", "industry": "Technology"}
46 }
49def test_invalid_form_data(): 1abcdef
50 response = client.post( 1yzABCD
51 "/form-union/",
52 data={"name": "John", "company_name": "Tech Corp"},
53 )
54 assert response.status_code == 422, response.text 1yzABCD
57def test_empty_form(): 1abcdef
58 response = client.post("/form-union/") 1EFGHIJ
59 assert response.status_code == 422, response.text 1EFGHIJ
62def test_openapi_schema(): 1abcdef
63 response = client.get("/openapi.json") 1stuvwx
64 assert response.status_code == 200, response.text 1stuvwx
66 assert response.json() == { 1stuvwx
67 "openapi": "3.1.0",
68 "info": {"title": "FastAPI", "version": "0.1.0"},
69 "paths": {
70 "/form-union/": {
71 "post": {
72 "summary": "Post Union Form",
73 "operationId": "post_union_form_form_union__post",
74 "requestBody": {
75 "content": {
76 "application/x-www-form-urlencoded": {
77 "schema": {
78 "anyOf": [
79 {"$ref": "#/components/schemas/UserForm"},
80 {"$ref": "#/components/schemas/CompanyForm"},
81 ],
82 "title": "Data",
83 }
84 }
85 },
86 "required": True,
87 },
88 "responses": {
89 "200": {
90 "description": "Successful Response",
91 "content": {"application/json": {"schema": {}}},
92 },
93 "422": {
94 "description": "Validation Error",
95 "content": {
96 "application/json": {
97 "schema": {
98 "$ref": "#/components/schemas/HTTPValidationError"
99 }
100 }
101 },
102 },
103 },
104 }
105 }
106 },
107 "components": {
108 "schemas": {
109 "CompanyForm": {
110 "properties": {
111 "company_name": {"type": "string", "title": "Company Name"},
112 "industry": {"type": "string", "title": "Industry"},
113 },
114 "type": "object",
115 "required": ["company_name", "industry"],
116 "title": "CompanyForm",
117 },
118 "HTTPValidationError": {
119 "properties": {
120 "detail": {
121 "items": {"$ref": "#/components/schemas/ValidationError"},
122 "type": "array",
123 "title": "Detail",
124 }
125 },
126 "type": "object",
127 "title": "HTTPValidationError",
128 },
129 "UserForm": {
130 "properties": {
131 "name": {"type": "string", "title": "Name"},
132 "email": {"type": "string", "title": "Email"},
133 },
134 "type": "object",
135 "required": ["name", "email"],
136 "title": "UserForm",
137 },
138 "ValidationError": {
139 "properties": {
140 "loc": {
141 "items": {
142 "anyOf": [{"type": "string"}, {"type": "integer"}]
143 },
144 "type": "array",
145 "title": "Location",
146 },
147 "msg": {"type": "string", "title": "Message"},
148 "type": {"type": "string", "title": "Error Type"},
149 },
150 "type": "object",
151 "required": ["loc", "msg", "type"],
152 "title": "ValidationError",
153 },
154 }
155 },
156 }