| 1 |
from fastapi import APIRouter, FastAPI |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from inline_snapshot import snapshot |
| 4 |
|
| 5 |
app = FastAPI() |
| 6 |
|
| 7 |
|
| 8 |
user_router = APIRouter() |
| 9 |
item_router = APIRouter() |
| 10 |
|
| 11 |
|
| 12 |
@user_router.get("/") |
| 13 |
def get_users(): |
| 14 |
return [{"user_id": "u1"}, {"user_id": "u2"}] |
| 15 |
|
| 16 |
|
| 17 |
@user_router.get("/{user_id}") |
| 18 |
def get_user(user_id: str): |
| 19 |
return {"user_id": user_id} |
| 20 |
|
| 21 |
|
| 22 |
@item_router.get("/") |
| 23 |
def get_items(user_id: str | None = None): |
| 24 |
if user_id is None: |
| 25 |
return [{"item_id": "i1", "user_id": "u1"}, {"item_id": "i2", "user_id": "u2"}] |
| 26 |
else: |
| 27 |
return [{"item_id": "i2", "user_id": user_id}] |
| 28 |
|
| 29 |
|
| 30 |
@item_router.get("/{item_id}") |
| 31 |
def get_item(item_id: str, user_id: str | None = None): |
| 32 |
if user_id is None: |
| 33 |
return {"item_id": item_id} |
| 34 |
else: |
| 35 |
return {"item_id": item_id, "user_id": user_id} |
| 36 |
|
| 37 |
|
| 38 |
app.include_router(user_router, prefix="/users") |
| 39 |
app.include_router(item_router, prefix="/items") |
| 40 |
|
| 41 |
app.include_router(item_router, prefix="/users/{user_id}/items") |
| 42 |
|
| 43 |
|
| 44 |
client = TestClient(app) |
| 45 |
|
| 46 |
|
| 47 |
def test_get_users(): |
| 48 |
"""Check that /users returns expected data""" |
| 49 |
response = client.get("/users") |
| 50 |
assert response.status_code == 200, response.text |
| 51 |
assert response.json() == [{"user_id": "u1"}, {"user_id": "u2"}] |
| 52 |
|
| 53 |
|
| 54 |
def test_get_user(): |
| 55 |
"""Check that /users/{user_id} returns expected data""" |
| 56 |
response = client.get("/users/abc123") |
| 57 |
assert response.status_code == 200, response.text |
| 58 |
assert response.json() == {"user_id": "abc123"} |
| 59 |
|
| 60 |
|
| 61 |
def test_get_items_1(): |
| 62 |
"""Check that /items returns expected data""" |
| 63 |
response = client.get("/items") |
| 64 |
assert response.status_code == 200, response.text |
| 65 |
assert response.json() == [ |
| 66 |
{"item_id": "i1", "user_id": "u1"}, |
| 67 |
{"item_id": "i2", "user_id": "u2"}, |
| 68 |
] |
| 69 |
|
| 70 |
|
| 71 |
def test_get_items_2(): |
| 72 |
"""Check that /items returns expected data with user_id specified""" |
| 73 |
response = client.get("/items?user_id=abc123") |
| 74 |
assert response.status_code == 200, response.text |
| 75 |
assert response.json() == [{"item_id": "i2", "user_id": "abc123"}] |
| 76 |
|
| 77 |
|
| 78 |
def test_get_item_1(): |
| 79 |
"""Check that /items/{item_id} returns expected data""" |
| 80 |
response = client.get("/items/item01") |
| 81 |
assert response.status_code == 200, response.text |
| 82 |
assert response.json() == {"item_id": "item01"} |
| 83 |
|
| 84 |
|
| 85 |
def test_get_item_2(): |
| 86 |
"""Check that /items/{item_id} returns expected data with user_id specified""" |
| 87 |
response = client.get("/items/item01?user_id=abc123") |
| 88 |
assert response.status_code == 200, response.text |
| 89 |
assert response.json() == {"item_id": "item01", "user_id": "abc123"} |
| 90 |
|
| 91 |
|
| 92 |
def test_get_users_items(): |
| 93 |
"""Check that /users/{user_id}/items returns expected data""" |
| 94 |
response = client.get("/users/abc123/items") |
| 95 |
assert response.status_code == 200, response.text |
| 96 |
assert response.json() == [{"item_id": "i2", "user_id": "abc123"}] |
| 97 |
|
| 98 |
|
| 99 |
def test_get_users_item(): |
| 100 |
"""Check that /users/{user_id}/items returns expected data""" |
| 101 |
response = client.get("/users/abc123/items/item01") |
| 102 |
assert response.status_code == 200, response.text |
| 103 |
assert response.json() == {"item_id": "item01", "user_id": "abc123"} |
| 104 |
|
| 105 |
|
| 106 |
def test_openapi_schema(): |
| 107 |
response = client.get("/openapi.json") |
| 108 |
assert response.status_code == 200, response.text |
| 109 |
assert response.json() == snapshot( |
| 110 |
{ |
| 111 |
"openapi": "3.1.0", |
| 112 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 113 |
"paths": { |
| 114 |
"/users/": { |
| 115 |
"get": { |
| 116 |
"summary": "Get Users", |
| 117 |
"operationId": "get_users_users__get", |
| 118 |
"responses": { |
| 119 |
"200": { |
| 120 |
"description": "Successful Response", |
| 121 |
"content": {"application/json": {"schema": {}}}, |
| 122 |
} |
| 123 |
}, |
| 124 |
} |
| 125 |
}, |
| 126 |
"/users/{user_id}": { |
| 127 |
"get": { |
| 128 |
"summary": "Get User", |
| 129 |
"operationId": "get_user_users__user_id__get", |
| 130 |
"parameters": [ |
| 131 |
{ |
| 132 |
"required": True, |
| 133 |
"schema": {"title": "User Id", "type": "string"}, |
| 134 |
"name": "user_id", |
| 135 |
"in": "path", |
| 136 |
} |
| 137 |
], |
| 138 |
"responses": { |
| 139 |
"200": { |
| 140 |
"description": "Successful Response", |
| 141 |
"content": {"application/json": {"schema": {}}}, |
| 142 |
}, |
| 143 |
"422": { |
| 144 |
"description": "Validation Error", |
| 145 |
"content": { |
| 146 |
"application/json": { |
| 147 |
"schema": { |
| 148 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 149 |
} |
| 150 |
} |
| 151 |
}, |
| 152 |
}, |
| 153 |
}, |
| 154 |
} |
| 155 |
}, |
| 156 |
"/items/": { |
| 157 |
"get": { |
| 158 |
"summary": "Get Items", |
| 159 |
"operationId": "get_items_items__get", |
| 160 |
"parameters": [ |
| 161 |
{ |
| 162 |
"required": False, |
| 163 |
"name": "user_id", |
| 164 |
"in": "query", |
| 165 |
"schema": { |
| 166 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 167 |
"title": "User Id", |
| 168 |
}, |
| 169 |
} |
| 170 |
], |
| 171 |
"responses": { |
| 172 |
"200": { |
| 173 |
"description": "Successful Response", |
| 174 |
"content": {"application/json": {"schema": {}}}, |
| 175 |
}, |
| 176 |
"422": { |
| 177 |
"description": "Validation Error", |
| 178 |
"content": { |
| 179 |
"application/json": { |
| 180 |
"schema": { |
| 181 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 182 |
} |
| 183 |
} |
| 184 |
}, |
| 185 |
}, |
| 186 |
}, |
| 187 |
} |
| 188 |
}, |
| 189 |
"/items/{item_id}": { |
| 190 |
"get": { |
| 191 |
"summary": "Get Item", |
| 192 |
"operationId": "get_item_items__item_id__get", |
| 193 |
"parameters": [ |
| 194 |
{ |
| 195 |
"required": True, |
| 196 |
"schema": {"title": "Item Id", "type": "string"}, |
| 197 |
"name": "item_id", |
| 198 |
"in": "path", |
| 199 |
}, |
| 200 |
{ |
| 201 |
"required": False, |
| 202 |
"name": "user_id", |
| 203 |
"in": "query", |
| 204 |
"schema": { |
| 205 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 206 |
"title": "User Id", |
| 207 |
}, |
| 208 |
}, |
| 209 |
], |
| 210 |
"responses": { |
| 211 |
"200": { |
| 212 |
"description": "Successful Response", |
| 213 |
"content": {"application/json": {"schema": {}}}, |
| 214 |
}, |
| 215 |
"422": { |
| 216 |
"description": "Validation Error", |
| 217 |
"content": { |
| 218 |
"application/json": { |
| 219 |
"schema": { |
| 220 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 221 |
} |
| 222 |
} |
| 223 |
}, |
| 224 |
}, |
| 225 |
}, |
| 226 |
} |
| 227 |
}, |
| 228 |
"/users/{user_id}/items/": { |
| 229 |
"get": { |
| 230 |
"summary": "Get Items", |
| 231 |
"operationId": "get_items_users__user_id__items__get", |
| 232 |
"parameters": [ |
| 233 |
{ |
| 234 |
"required": True, |
| 235 |
"name": "user_id", |
| 236 |
"in": "path", |
| 237 |
"schema": { |
| 238 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 239 |
"title": "User Id", |
| 240 |
}, |
| 241 |
} |
| 242 |
], |
| 243 |
"responses": { |
| 244 |
"200": { |
| 245 |
"description": "Successful Response", |
| 246 |
"content": {"application/json": {"schema": {}}}, |
| 247 |
}, |
| 248 |
"422": { |
| 249 |
"description": "Validation Error", |
| 250 |
"content": { |
| 251 |
"application/json": { |
| 252 |
"schema": { |
| 253 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 254 |
} |
| 255 |
} |
| 256 |
}, |
| 257 |
}, |
| 258 |
}, |
| 259 |
} |
| 260 |
}, |
| 261 |
"/users/{user_id}/items/{item_id}": { |
| 262 |
"get": { |
| 263 |
"summary": "Get Item", |
| 264 |
"operationId": "get_item_users__user_id__items__item_id__get", |
| 265 |
"parameters": [ |
| 266 |
{ |
| 267 |
"required": True, |
| 268 |
"schema": {"title": "Item Id", "type": "string"}, |
| 269 |
"name": "item_id", |
| 270 |
"in": "path", |
| 271 |
}, |
| 272 |
{ |
| 273 |
"required": True, |
| 274 |
"name": "user_id", |
| 275 |
"in": "path", |
| 276 |
"schema": { |
| 277 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 278 |
"title": "User Id", |
| 279 |
}, |
| 280 |
}, |
| 281 |
], |
| 282 |
"responses": { |
| 283 |
"200": { |
| 284 |
"description": "Successful Response", |
| 285 |
"content": {"application/json": {"schema": {}}}, |
| 286 |
}, |
| 287 |
"422": { |
| 288 |
"description": "Validation Error", |
| 289 |
"content": { |
| 290 |
"application/json": { |
| 291 |
"schema": { |
| 292 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 293 |
} |
| 294 |
} |
| 295 |
}, |
| 296 |
}, |
| 297 |
}, |
| 298 |
} |
| 299 |
}, |
| 300 |
}, |
| 301 |
"components": { |
| 302 |
"schemas": { |
| 303 |
"HTTPValidationError": { |
| 304 |
"title": "HTTPValidationError", |
| 305 |
"type": "object", |
| 306 |
"properties": { |
| 307 |
"detail": { |
| 308 |
"title": "Detail", |
| 309 |
"type": "array", |
| 310 |
"items": { |
| 311 |
"$ref": "#/components/schemas/ValidationError" |
| 312 |
}, |
| 313 |
} |
| 314 |
}, |
| 315 |
}, |
| 316 |
"ValidationError": { |
| 317 |
"title": "ValidationError", |
| 318 |
"required": ["loc", "msg", "type"], |
| 319 |
"type": "object", |
| 320 |
"properties": { |
| 321 |
"loc": { |
| 322 |
"title": "Location", |
| 323 |
"type": "array", |
| 324 |
"items": { |
| 325 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 326 |
}, |
| 327 |
}, |
| 328 |
"msg": {"title": "Message", "type": "string"}, |
| 329 |
"type": {"title": "Error Type", "type": "string"}, |
| 330 |
"input": {"title": "Input"}, |
| 331 |
"ctx": {"title": "Context", "type": "object"}, |
| 332 |
}, |
| 333 |
}, |
| 334 |
} |
| 335 |
}, |
| 336 |
} |
| 337 |
) |