1
from typing import Optional
3
from dirty_equals import IsDict
4
from fastapi import APIRouter, FastAPI
5
from fastapi.testclient import TestClient
10
user_router = APIRouter()
11
item_router = APIRouter()
16
return [{"user_id": "u1"}, {"user_id": "u2"}]
19
@user_router.get("/{user_id}")
20
def get_user(user_id: str):
21
return {"user_id": user_id}
25
def get_items(user_id: Optional[str] = None):
27
return [{"item_id": "i1", "user_id": "u1"}, {"item_id": "i2", "user_id": "u2"}]
29
return [{"item_id": "i2", "user_id": user_id}]
32
@item_router.get("/{item_id}")
33
def get_item(item_id: str, user_id: Optional[str] = None):
35
return {"item_id": item_id}
37
return {"item_id": item_id, "user_id": user_id}
40
app.include_router(user_router, prefix="/users")
41
app.include_router(item_router, prefix="/items")
43
app.include_router(item_router, prefix="/users/{user_id}/items")
46
client = TestClient(app)
50
"""Check that /users returns expected data"""
51
response = client.get("/users")
52
assert response.status_code == 200, response.text
53
assert response.json() == [{"user_id": "u1"}, {"user_id": "u2"}]
57
"""Check that /users/{user_id} returns expected data"""
58
response = client.get("/users/abc123")
59
assert response.status_code == 200, response.text
60
assert response.json() == {"user_id": "abc123"}
63
def test_get_items_1():
64
"""Check that /items returns expected data"""
65
response = client.get("/items")
66
assert response.status_code == 200, response.text
67
assert response.json() == [
68
{"item_id": "i1", "user_id": "u1"},
69
{"item_id": "i2", "user_id": "u2"},
73
def test_get_items_2():
74
"""Check that /items returns expected data with user_id specified"""
75
response = client.get("/items?user_id=abc123")
76
assert response.status_code == 200, response.text
77
assert response.json() == [{"item_id": "i2", "user_id": "abc123"}]
81
"""Check that /items/{item_id} returns expected data"""
82
response = client.get("/items/item01")
83
assert response.status_code == 200, response.text
84
assert response.json() == {"item_id": "item01"}
88
"""Check that /items/{item_id} returns expected data with user_id specified"""
89
response = client.get("/items/item01?user_id=abc123")
90
assert response.status_code == 200, response.text
91
assert response.json() == {"item_id": "item01", "user_id": "abc123"}
94
def test_get_users_items():
95
"""Check that /users/{user_id}/items returns expected data"""
96
response = client.get("/users/abc123/items")
97
assert response.status_code == 200, response.text
98
assert response.json() == [{"item_id": "i2", "user_id": "abc123"}]
101
def test_get_users_item():
102
"""Check that /users/{user_id}/items returns expected data"""
103
response = client.get("/users/abc123/items/item01")
104
assert response.status_code == 200, response.text
105
assert response.json() == {"item_id": "item01", "user_id": "abc123"}
108
def test_openapi_schema():
109
response = client.get("/openapi.json")
110
assert response.status_code == 200, response.text
111
assert response.json() == {
113
"info": {"title": "FastAPI", "version": "0.1.0"},
117
"summary": "Get Users",
118
"operationId": "get_users_users__get",
121
"description": "Successful Response",
122
"content": {"application/json": {"schema": {}}},
127
"/users/{user_id}": {
129
"summary": "Get User",
130
"operationId": "get_user_users__user_id__get",
134
"schema": {"title": "User Id", "type": "string"},
141
"description": "Successful Response",
142
"content": {"application/json": {"schema": {}}},
145
"description": "Validation Error",
147
"application/json": {
149
"$ref": "#/components/schemas/HTTPValidationError"
159
"summary": "Get Items",
160
"operationId": "get_items_items__get",
168
"anyOf": [{"type": "string"}, {"type": "null"}],
174
{"title": "User Id", "type": "string"}
180
"description": "Successful Response",
181
"content": {"application/json": {"schema": {}}},
184
"description": "Validation Error",
186
"application/json": {
188
"$ref": "#/components/schemas/HTTPValidationError"
196
"/items/{item_id}": {
198
"summary": "Get Item",
199
"operationId": "get_item_items__item_id__get",
203
"schema": {"title": "Item Id", "type": "string"},
213
"anyOf": [{"type": "string"}, {"type": "null"}],
219
{"title": "User Id", "type": "string"}
225
"description": "Successful Response",
226
"content": {"application/json": {"schema": {}}},
229
"description": "Validation Error",
231
"application/json": {
233
"$ref": "#/components/schemas/HTTPValidationError"
241
"/users/{user_id}/items/": {
243
"summary": "Get Items",
244
"operationId": "get_items_users__user_id__items__get",
252
"anyOf": [{"type": "string"}, {"type": "null"}],
258
{"title": "User Id", "type": "string"}
264
"description": "Successful Response",
265
"content": {"application/json": {"schema": {}}},
268
"description": "Validation Error",
270
"application/json": {
272
"$ref": "#/components/schemas/HTTPValidationError"
280
"/users/{user_id}/items/{item_id}": {
282
"summary": "Get Item",
283
"operationId": "get_item_users__user_id__items__item_id__get",
287
"schema": {"title": "Item Id", "type": "string"},
297
"anyOf": [{"type": "string"}, {"type": "null"}],
303
{"title": "User Id", "type": "string"}
309
"description": "Successful Response",
310
"content": {"application/json": {"schema": {}}},
313
"description": "Validation Error",
315
"application/json": {
317
"$ref": "#/components/schemas/HTTPValidationError"
328
"HTTPValidationError": {
329
"title": "HTTPValidationError",
335
"items": {"$ref": "#/components/schemas/ValidationError"},
340
"title": "ValidationError",
341
"required": ["loc", "msg", "type"],
348
"anyOf": [{"type": "string"}, {"type": "integer"}]
351
"msg": {"title": "Message", "type": "string"},
352
"type": {"title": "Error Type", "type": "string"},