| 1 |
from fastapi.testclient import TestClient |
| 2 |
from inline_snapshot import snapshot |
| 3 |
|
| 4 |
from docs_src.generate_clients.tutorial003_py310 import app |
| 5 |
|
| 6 |
client = TestClient(app) |
| 7 |
|
| 8 |
|
| 9 |
def test_post_items(): |
| 10 |
response = client.post("/items/", json={"name": "Foo", "price": 5}) |
| 11 |
assert response.status_code == 200, response.text |
| 12 |
assert response.json() == {"message": "Item received"} |
| 13 |
|
| 14 |
|
| 15 |
def test_post_users(): |
| 16 |
response = client.post( |
| 17 |
"/users/", json={"username": "Foo", "email": "[email protected]"} |
| 18 |
) |
| 19 |
assert response.status_code == 200, response.text |
| 20 |
assert response.json() == {"message": "User received"} |
| 21 |
|
| 22 |
|
| 23 |
def test_get_items(): |
| 24 |
response = client.get("/items/") |
| 25 |
assert response.status_code == 200, response.text |
| 26 |
assert response.json() == [ |
| 27 |
{"name": "Plumbus", "price": 3}, |
| 28 |
{"name": "Portal Gun", "price": 9001}, |
| 29 |
] |
| 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 |
"/items/": { |
| 41 |
"get": { |
| 42 |
"tags": ["items"], |
| 43 |
"summary": "Get Items", |
| 44 |
"operationId": "items-get_items", |
| 45 |
"responses": { |
| 46 |
"200": { |
| 47 |
"description": "Successful Response", |
| 48 |
"content": { |
| 49 |
"application/json": { |
| 50 |
"schema": { |
| 51 |
"title": "Response Items-Get Items", |
| 52 |
"type": "array", |
| 53 |
"items": { |
| 54 |
"$ref": "#/components/schemas/Item" |
| 55 |
}, |
| 56 |
} |
| 57 |
} |
| 58 |
}, |
| 59 |
} |
| 60 |
}, |
| 61 |
}, |
| 62 |
"post": { |
| 63 |
"tags": ["items"], |
| 64 |
"summary": "Create Item", |
| 65 |
"operationId": "items-create_item", |
| 66 |
"requestBody": { |
| 67 |
"content": { |
| 68 |
"application/json": { |
| 69 |
"schema": {"$ref": "#/components/schemas/Item"} |
| 70 |
} |
| 71 |
}, |
| 72 |
"required": True, |
| 73 |
}, |
| 74 |
"responses": { |
| 75 |
"200": { |
| 76 |
"description": "Successful Response", |
| 77 |
"content": { |
| 78 |
"application/json": { |
| 79 |
"schema": { |
| 80 |
"$ref": "#/components/schemas/ResponseMessage" |
| 81 |
} |
| 82 |
} |
| 83 |
}, |
| 84 |
}, |
| 85 |
"422": { |
| 86 |
"description": "Validation Error", |
| 87 |
"content": { |
| 88 |
"application/json": { |
| 89 |
"schema": { |
| 90 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 91 |
} |
| 92 |
} |
| 93 |
}, |
| 94 |
}, |
| 95 |
}, |
| 96 |
}, |
| 97 |
}, |
| 98 |
"/users/": { |
| 99 |
"post": { |
| 100 |
"tags": ["users"], |
| 101 |
"summary": "Create User", |
| 102 |
"operationId": "users-create_user", |
| 103 |
"requestBody": { |
| 104 |
"content": { |
| 105 |
"application/json": { |
| 106 |
"schema": {"$ref": "#/components/schemas/User"} |
| 107 |
} |
| 108 |
}, |
| 109 |
"required": True, |
| 110 |
}, |
| 111 |
"responses": { |
| 112 |
"200": { |
| 113 |
"description": "Successful Response", |
| 114 |
"content": { |
| 115 |
"application/json": { |
| 116 |
"schema": { |
| 117 |
"$ref": "#/components/schemas/ResponseMessage" |
| 118 |
} |
| 119 |
} |
| 120 |
}, |
| 121 |
}, |
| 122 |
"422": { |
| 123 |
"description": "Validation Error", |
| 124 |
"content": { |
| 125 |
"application/json": { |
| 126 |
"schema": { |
| 127 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 128 |
} |
| 129 |
} |
| 130 |
}, |
| 131 |
}, |
| 132 |
}, |
| 133 |
} |
| 134 |
}, |
| 135 |
}, |
| 136 |
"components": { |
| 137 |
"schemas": { |
| 138 |
"HTTPValidationError": { |
| 139 |
"title": "HTTPValidationError", |
| 140 |
"type": "object", |
| 141 |
"properties": { |
| 142 |
"detail": { |
| 143 |
"title": "Detail", |
| 144 |
"type": "array", |
| 145 |
"items": { |
| 146 |
"$ref": "#/components/schemas/ValidationError" |
| 147 |
}, |
| 148 |
} |
| 149 |
}, |
| 150 |
}, |
| 151 |
"Item": { |
| 152 |
"title": "Item", |
| 153 |
"required": ["name", "price"], |
| 154 |
"type": "object", |
| 155 |
"properties": { |
| 156 |
"name": {"title": "Name", "type": "string"}, |
| 157 |
"price": {"title": "Price", "type": "number"}, |
| 158 |
}, |
| 159 |
}, |
| 160 |
"ResponseMessage": { |
| 161 |
"title": "ResponseMessage", |
| 162 |
"required": ["message"], |
| 163 |
"type": "object", |
| 164 |
"properties": { |
| 165 |
"message": {"title": "Message", "type": "string"} |
| 166 |
}, |
| 167 |
}, |
| 168 |
"User": { |
| 169 |
"title": "User", |
| 170 |
"required": ["username", "email"], |
| 171 |
"type": "object", |
| 172 |
"properties": { |
| 173 |
"username": {"title": "Username", "type": "string"}, |
| 174 |
"email": {"title": "Email", "type": "string"}, |
| 175 |
}, |
| 176 |
}, |
| 177 |
"ValidationError": { |
| 178 |
"title": "ValidationError", |
| 179 |
"required": ["loc", "msg", "type"], |
| 180 |
"type": "object", |
| 181 |
"properties": { |
| 182 |
"loc": { |
| 183 |
"title": "Location", |
| 184 |
"type": "array", |
| 185 |
"items": { |
| 186 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 187 |
}, |
| 188 |
}, |
| 189 |
"msg": {"title": "Message", "type": "string"}, |
| 190 |
"type": {"title": "Error Type", "type": "string"}, |
| 191 |
"input": {"title": "Input"}, |
| 192 |
"ctx": {"title": "Context", "type": "object"}, |
| 193 |
}, |
| 194 |
}, |
| 195 |
} |
| 196 |
}, |
| 197 |
} |
| 198 |
) |