Debian Codegraph @

tests/test_tutorial/test_request_files/test_tutorial001_03.py

raw
1 import importlib
2
3 import pytest
4 from fastapi.testclient import TestClient
5 from inline_snapshot import snapshot
6
7
8 @pytest.fixture(
9 name="client",
10 params=[
11 "tutorial001_03_py310",
12 "tutorial001_03_an_py310",
13 ],
14 )
15 def get_client(request: pytest.FixtureRequest):
16 mod = importlib.import_module(f"docs_src.request_files.{request.param}")
17
18 client = TestClient(mod.app)
19 return client
20
21
22 def test_post_file(tmp_path, client: TestClient):
23 path = tmp_path / "test.txt"
24 path.write_bytes(b"<file content>")
25
26 with path.open("rb") as file:
27 response = client.post("/files/", files={"file": file})
28 assert response.status_code == 200, response.text
29 assert response.json() == {"file_size": 14}
30
31
32 def test_post_upload_file(tmp_path, client: TestClient):
33 path = tmp_path / "test.txt"
34 path.write_bytes(b"<file content>")
35
36 with path.open("rb") as file:
37 response = client.post("/uploadfile/", files={"file": file})
38 assert response.status_code == 200, response.text
39 assert response.json() == {"filename": "test.txt"}
40
41
42 def test_openapi_schema(client: TestClient):
43 response = client.get("/openapi.json")
44 assert response.status_code == 200, response.text
45 assert response.json() == snapshot(
46 {
47 "openapi": "3.1.0",
48 "info": {"title": "FastAPI", "version": "0.1.0"},
49 "paths": {
50 "/files/": {
51 "post": {
52 "summary": "Create File",
53 "operationId": "create_file_files__post",
54 "requestBody": {
55 "content": {
56 "multipart/form-data": {
57 "schema": {
58 "$ref": "#/components/schemas/Body_create_file_files__post"
59 }
60 }
61 },
62 "required": True,
63 },
64 "responses": {
65 "200": {
66 "description": "Successful Response",
67 "content": {"application/json": {"schema": {}}},
68 },
69 "422": {
70 "description": "Validation Error",
71 "content": {
72 "application/json": {
73 "schema": {
74 "$ref": "#/components/schemas/HTTPValidationError"
75 }
76 }
77 },
78 },
79 },
80 }
81 },
82 "/uploadfile/": {
83 "post": {
84 "summary": "Create Upload File",
85 "operationId": "create_upload_file_uploadfile__post",
86 "requestBody": {
87 "content": {
88 "multipart/form-data": {
89 "schema": {
90 "$ref": "#/components/schemas/Body_create_upload_file_uploadfile__post"
91 }
92 }
93 },
94 "required": True,
95 },
96 "responses": {
97 "200": {
98 "description": "Successful Response",
99 "content": {"application/json": {"schema": {}}},
100 },
101 "422": {
102 "description": "Validation Error",
103 "content": {
104 "application/json": {
105 "schema": {
106 "$ref": "#/components/schemas/HTTPValidationError"
107 }
108 }
109 },
110 },
111 },
112 }
113 },
114 },
115 "components": {
116 "schemas": {
117 "Body_create_file_files__post": {
118 "title": "Body_create_file_files__post",
119 "required": ["file"],
120 "type": "object",
121 "properties": {
122 "file": {
123 "title": "File",
124 "type": "string",
125 "description": "A file read as bytes",
126 "contentMediaType": "application/octet-stream",
127 }
128 },
129 },
130 "Body_create_upload_file_uploadfile__post": {
131 "title": "Body_create_upload_file_uploadfile__post",
132 "required": ["file"],
133 "type": "object",
134 "properties": {
135 "file": {
136 "title": "File",
137 "contentMediaType": "application/octet-stream",
138 "type": "string",
139 "description": "A file read as UploadFile",
140 }
141 },
142 },
143 "HTTPValidationError": {
144 "title": "HTTPValidationError",
145 "type": "object",
146 "properties": {
147 "detail": {
148 "title": "Detail",
149 "type": "array",
150 "items": {
151 "$ref": "#/components/schemas/ValidationError"
152 },
153 }
154 },
155 },
156 "ValidationError": {
157 "title": "ValidationError",
158 "required": ["loc", "msg", "type"],
159 "type": "object",
160 "properties": {
161 "loc": {
162 "title": "Location",
163 "type": "array",
164 "items": {
165 "anyOf": [{"type": "string"}, {"type": "integer"}]
166 },
167 },
168 "msg": {"title": "Message", "type": "string"},
169 "type": {"title": "Error Type", "type": "string"},
170 "input": {"title": "Input"},
171 "ctx": {"title": "Context", "type": "object"},
172 },
173 },
174 }
175 },
176 }
177 )