Debian Codegraph @

tests/test_response_set_response_code_empty.py

raw
1 from typing import Any
2
3 from fastapi import FastAPI, Response
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7 app = FastAPI()
8
9
10 @app.delete(
11 "/{id}",
12 status_code=204,
13 response_model=None,
14 )
15 async def delete_deployment(
16 id: int,
17 response: Response,
18 ) -> Any:
19 response.status_code = 400
20 return {"msg": "Status overwritten", "id": id}
21
22
23 client = TestClient(app)
24
25
26 def test_dependency_set_status_code():
27 response = client.delete("/1")
28 assert response.status_code == 400 and response.content
29 assert response.json() == {"msg": "Status overwritten", "id": 1}
30
31
32 def test_openapi_schema():
33 response = client.get("/openapi.json")
34 assert response.status_code == 200, response.text
35 assert response.json() == snapshot(
36 {
37 "openapi": "3.1.0",
38 "info": {"title": "FastAPI", "version": "0.1.0"},
39 "paths": {
40 "/{id}": {
41 "delete": {
42 "summary": "Delete Deployment",
43 "operationId": "delete_deployment__id__delete",
44 "parameters": [
45 {
46 "required": True,
47 "schema": {"title": "Id", "type": "integer"},
48 "name": "id",
49 "in": "path",
50 }
51 ],
52 "responses": {
53 "204": {"description": "Successful Response"},
54 "422": {
55 "description": "Validation Error",
56 "content": {
57 "application/json": {
58 "schema": {
59 "$ref": "#/components/schemas/HTTPValidationError"
60 }
61 }
62 },
63 },
64 },
65 }
66 }
67 },
68 "components": {
69 "schemas": {
70 "HTTPValidationError": {
71 "title": "HTTPValidationError",
72 "type": "object",
73 "properties": {
74 "detail": {
75 "title": "Detail",
76 "type": "array",
77 "items": {
78 "$ref": "#/components/schemas/ValidationError"
79 },
80 }
81 },
82 },
83 "ValidationError": {
84 "title": "ValidationError",
85 "required": ["loc", "msg", "type"],
86 "type": "object",
87 "properties": {
88 "loc": {
89 "title": "Location",
90 "type": "array",
91 "items": {
92 "anyOf": [{"type": "string"}, {"type": "integer"}]
93 },
94 },
95 "msg": {"title": "Message", "type": "string"},
96 "type": {"title": "Error Type", "type": "string"},
97 "input": {"title": "Input"},
98 "ctx": {"title": "Context", "type": "object"},
99 },
100 },
101 }
102 },
103 }
104 )