Coverage for tests / test_additional_responses_router.py: 100%
42 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 fastapi import APIRouter, FastAPI 1adbc
2from fastapi.testclient import TestClient 1adbc
3from inline_snapshot import snapshot 1adbc
4from pydantic import BaseModel 1adbc
7class ResponseModel(BaseModel): 1adbc
8 message: str 1abc
11app = FastAPI() 1adbc
12router = APIRouter() 1adbc
15@router.get("/a", responses={501: {"description": "Error 1"}}) 1adbc
16async def a(): 1adbc
17 return "a" 1efgh
20@router.get( 1adbc
21 "/b",
22 responses={
23 502: {"description": "Error 2"},
24 "4XX": {"description": "Error with range, upper"},
25 },
26)
27async def b(): 1adbc
28 return "b" 1ijkl
31@router.get( 1adbc
32 "/c",
33 responses={
34 "400": {"description": "Error with str"},
35 "5xx": {"description": "Error with range, lower"},
36 "default": {"description": "A default response"},
37 },
38)
39async def c(): 1adbc
40 return "c" 1mnop
43@router.get( 1adbc
44 "/d",
45 responses={
46 "400": {"description": "Error with str"},
47 "5XX": {"model": ResponseModel},
48 "default": {"model": ResponseModel},
49 },
50)
51async def d(): 1adbc
52 return "d" 1qrst
55app.include_router(router) 1adbc
58client = TestClient(app) 1adbc
61def test_a(): 1adbc
62 response = client.get("/a") 1efgh
63 assert response.status_code == 200, response.text 1efgh
64 assert response.json() == "a" 1efgh
67def test_b(): 1adbc
68 response = client.get("/b") 1ijkl
69 assert response.status_code == 200, response.text 1ijkl
70 assert response.json() == "b" 1ijkl
73def test_c(): 1adbc
74 response = client.get("/c") 1mnop
75 assert response.status_code == 200, response.text 1mnop
76 assert response.json() == "c" 1mnop
79def test_d(): 1adbc
80 response = client.get("/d") 1qrst
81 assert response.status_code == 200, response.text 1qrst
82 assert response.json() == "d" 1qrst
85def test_openapi_schema(): 1adbc
86 response = client.get("/openapi.json") 1uvwx
87 assert response.status_code == 200, response.text 1uvwx
88 assert response.json() == snapshot( 1uvwx
89 {
90 "openapi": "3.1.0",
91 "info": {"title": "FastAPI", "version": "0.1.0"},
92 "paths": {
93 "/a": {
94 "get": {
95 "responses": {
96 "501": {"description": "Error 1"},
97 "200": {
98 "description": "Successful Response",
99 "content": {"application/json": {"schema": {}}},
100 },
101 },
102 "summary": "A",
103 "operationId": "a_a_get",
104 }
105 },
106 "/b": {
107 "get": {
108 "responses": {
109 "502": {"description": "Error 2"},
110 "4XX": {"description": "Error with range, upper"},
111 "200": {
112 "description": "Successful Response",
113 "content": {"application/json": {"schema": {}}},
114 },
115 },
116 "summary": "B",
117 "operationId": "b_b_get",
118 }
119 },
120 "/c": {
121 "get": {
122 "responses": {
123 "400": {"description": "Error with str"},
124 "5XX": {"description": "Error with range, lower"},
125 "200": {
126 "description": "Successful Response",
127 "content": {"application/json": {"schema": {}}},
128 },
129 "default": {"description": "A default response"},
130 },
131 "summary": "C",
132 "operationId": "c_c_get",
133 }
134 },
135 "/d": {
136 "get": {
137 "responses": {
138 "400": {"description": "Error with str"},
139 "5XX": {
140 "description": "Server Error",
141 "content": {
142 "application/json": {
143 "schema": {
144 "$ref": "#/components/schemas/ResponseModel"
145 }
146 }
147 },
148 },
149 "200": {
150 "description": "Successful Response",
151 "content": {"application/json": {"schema": {}}},
152 },
153 "default": {
154 "description": "Default Response",
155 "content": {
156 "application/json": {
157 "schema": {
158 "$ref": "#/components/schemas/ResponseModel"
159 }
160 }
161 },
162 },
163 },
164 "summary": "D",
165 "operationId": "d_d_get",
166 }
167 },
168 },
169 "components": {
170 "schemas": {
171 "ResponseModel": {
172 "title": "ResponseModel",
173 "required": ["message"],
174 "type": "object",
175 "properties": {
176 "message": {"title": "Message", "type": "string"}
177 },
178 }
179 }
180 },
181 }
182 )