| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi import FastAPI |
| 5 |
from fastapi.testclient import TestClient |
| 6 |
from inline_snapshot import snapshot |
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture( |
| 10 |
name="app", |
| 11 |
params=[ |
| 12 |
"tutorial003_py310", |
| 13 |
"tutorial003_an_py310", |
| 14 |
], |
| 15 |
) |
| 16 |
def get_app(request: pytest.FixtureRequest): |
| 17 |
mod = importlib.import_module(f"docs_src.request_files.{request.param}") |
| 18 |
|
| 19 |
return mod.app |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture(name="client") |
| 23 |
def get_client(app: FastAPI): |
| 24 |
client = TestClient(app) |
| 25 |
return client |
| 26 |
|
| 27 |
|
| 28 |
def test_post_files(tmp_path, app: FastAPI): |
| 29 |
path = tmp_path / "test.txt" |
| 30 |
path.write_bytes(b"<file content>") |
| 31 |
path2 = tmp_path / "test2.txt" |
| 32 |
path2.write_bytes(b"<file content2>") |
| 33 |
|
| 34 |
client = TestClient(app) |
| 35 |
with path.open("rb") as file, path2.open("rb") as file2: |
| 36 |
response = client.post( |
| 37 |
"/files/", |
| 38 |
files=( |
| 39 |
("files", ("test.txt", file)), |
| 40 |
("files", ("test2.txt", file2)), |
| 41 |
), |
| 42 |
) |
| 43 |
assert response.status_code == 200, response.text |
| 44 |
assert response.json() == {"file_sizes": [14, 15]} |
| 45 |
|
| 46 |
|
| 47 |
def test_post_upload_file(tmp_path, app: FastAPI): |
| 48 |
path = tmp_path / "test.txt" |
| 49 |
path.write_bytes(b"<file content>") |
| 50 |
path2 = tmp_path / "test2.txt" |
| 51 |
path2.write_bytes(b"<file content2>") |
| 52 |
|
| 53 |
client = TestClient(app) |
| 54 |
with path.open("rb") as file, path2.open("rb") as file2: |
| 55 |
response = client.post( |
| 56 |
"/uploadfiles/", |
| 57 |
files=( |
| 58 |
("files", ("test.txt", file)), |
| 59 |
("files", ("test2.txt", file2)), |
| 60 |
), |
| 61 |
) |
| 62 |
assert response.status_code == 200, response.text |
| 63 |
assert response.json() == {"filenames": ["test.txt", "test2.txt"]} |
| 64 |
|
| 65 |
|
| 66 |
def test_get_root(app: FastAPI): |
| 67 |
client = TestClient(app) |
| 68 |
response = client.get("/") |
| 69 |
assert response.status_code == 200, response.text |
| 70 |
assert b"<form" in response.content |
| 71 |
|
| 72 |
|
| 73 |
def test_openapi_schema(client: TestClient): |
| 74 |
response = client.get("/openapi.json") |
| 75 |
assert response.status_code == 200, response.text |
| 76 |
assert response.json() == snapshot( |
| 77 |
{ |
| 78 |
"openapi": "3.1.0", |
| 79 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 80 |
"paths": { |
| 81 |
"/files/": { |
| 82 |
"post": { |
| 83 |
"summary": "Create Files", |
| 84 |
"operationId": "create_files_files__post", |
| 85 |
"requestBody": { |
| 86 |
"content": { |
| 87 |
"multipart/form-data": { |
| 88 |
"schema": { |
| 89 |
"$ref": "#/components/schemas/Body_create_files_files__post" |
| 90 |
} |
| 91 |
} |
| 92 |
}, |
| 93 |
"required": True, |
| 94 |
}, |
| 95 |
"responses": { |
| 96 |
"200": { |
| 97 |
"description": "Successful Response", |
| 98 |
"content": {"application/json": {"schema": {}}}, |
| 99 |
}, |
| 100 |
"422": { |
| 101 |
"description": "Validation Error", |
| 102 |
"content": { |
| 103 |
"application/json": { |
| 104 |
"schema": { |
| 105 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 106 |
} |
| 107 |
} |
| 108 |
}, |
| 109 |
}, |
| 110 |
}, |
| 111 |
} |
| 112 |
}, |
| 113 |
"/uploadfiles/": { |
| 114 |
"post": { |
| 115 |
"summary": "Create Upload Files", |
| 116 |
"operationId": "create_upload_files_uploadfiles__post", |
| 117 |
"requestBody": { |
| 118 |
"content": { |
| 119 |
"multipart/form-data": { |
| 120 |
"schema": { |
| 121 |
"$ref": "#/components/schemas/Body_create_upload_files_uploadfiles__post" |
| 122 |
} |
| 123 |
} |
| 124 |
}, |
| 125 |
"required": True, |
| 126 |
}, |
| 127 |
"responses": { |
| 128 |
"200": { |
| 129 |
"description": "Successful Response", |
| 130 |
"content": {"application/json": {"schema": {}}}, |
| 131 |
}, |
| 132 |
"422": { |
| 133 |
"description": "Validation Error", |
| 134 |
"content": { |
| 135 |
"application/json": { |
| 136 |
"schema": { |
| 137 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 138 |
} |
| 139 |
} |
| 140 |
}, |
| 141 |
}, |
| 142 |
}, |
| 143 |
} |
| 144 |
}, |
| 145 |
"/": { |
| 146 |
"get": { |
| 147 |
"summary": "Main", |
| 148 |
"operationId": "main__get", |
| 149 |
"responses": { |
| 150 |
"200": { |
| 151 |
"description": "Successful Response", |
| 152 |
"content": {"application/json": {"schema": {}}}, |
| 153 |
} |
| 154 |
}, |
| 155 |
} |
| 156 |
}, |
| 157 |
}, |
| 158 |
"components": { |
| 159 |
"schemas": { |
| 160 |
"Body_create_files_files__post": { |
| 161 |
"title": "Body_create_files_files__post", |
| 162 |
"required": ["files"], |
| 163 |
"type": "object", |
| 164 |
"properties": { |
| 165 |
"files": { |
| 166 |
"title": "Files", |
| 167 |
"type": "array", |
| 168 |
"items": { |
| 169 |
"type": "string", |
| 170 |
"contentMediaType": "application/octet-stream", |
| 171 |
}, |
| 172 |
"description": "Multiple files as bytes", |
| 173 |
} |
| 174 |
}, |
| 175 |
}, |
| 176 |
"Body_create_upload_files_uploadfiles__post": { |
| 177 |
"title": "Body_create_upload_files_uploadfiles__post", |
| 178 |
"required": ["files"], |
| 179 |
"type": "object", |
| 180 |
"properties": { |
| 181 |
"files": { |
| 182 |
"title": "Files", |
| 183 |
"type": "array", |
| 184 |
"items": { |
| 185 |
"type": "string", |
| 186 |
"contentMediaType": "application/octet-stream", |
| 187 |
}, |
| 188 |
"description": "Multiple files as UploadFile", |
| 189 |
} |
| 190 |
}, |
| 191 |
}, |
| 192 |
"HTTPValidationError": { |
| 193 |
"title": "HTTPValidationError", |
| 194 |
"type": "object", |
| 195 |
"properties": { |
| 196 |
"detail": { |
| 197 |
"title": "Detail", |
| 198 |
"type": "array", |
| 199 |
"items": { |
| 200 |
"$ref": "#/components/schemas/ValidationError" |
| 201 |
}, |
| 202 |
} |
| 203 |
}, |
| 204 |
}, |
| 205 |
"ValidationError": { |
| 206 |
"title": "ValidationError", |
| 207 |
"required": ["loc", "msg", "type"], |
| 208 |
"type": "object", |
| 209 |
"properties": { |
| 210 |
"loc": { |
| 211 |
"title": "Location", |
| 212 |
"type": "array", |
| 213 |
"items": { |
| 214 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 215 |
}, |
| 216 |
}, |
| 217 |
"msg": {"title": "Message", "type": "string"}, |
| 218 |
"type": {"title": "Error Type", "type": "string"}, |
| 219 |
"input": {"title": "Input"}, |
| 220 |
"ctx": {"title": "Context", "type": "object"}, |
| 221 |
}, |
| 222 |
}, |
| 223 |
} |
| 224 |
}, |
| 225 |
} |
| 226 |
) |