| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from dirty_equals import IsList |
| 5 |
from fastapi.testclient import TestClient |
| 6 |
from inline_snapshot import snapshot |
| 7 |
|
| 8 |
from ...utils import needs_py310 |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture( |
| 12 |
name="client", |
| 13 |
params=[ |
| 14 |
pytest.param("tutorial002_py310", marks=needs_py310), |
| 15 |
], |
| 16 |
) |
| 17 |
def get_client(request: pytest.FixtureRequest) -> TestClient: |
| 18 |
mod = importlib.import_module( |
| 19 |
f"docs_src.path_operation_configuration.{request.param}" |
| 20 |
) |
| 21 |
return TestClient(mod.app) |
| 22 |
|
| 23 |
|
| 24 |
def test_post_items(client: TestClient): |
| 25 |
response = client.post( |
| 26 |
"/items/", |
| 27 |
json={ |
| 28 |
"name": "Foo", |
| 29 |
"description": "Item description", |
| 30 |
"price": 42.0, |
| 31 |
"tax": 3.2, |
| 32 |
"tags": ["bar", "baz"], |
| 33 |
}, |
| 34 |
) |
| 35 |
assert response.status_code == 200, response.text |
| 36 |
assert response.json() == { |
| 37 |
"name": "Foo", |
| 38 |
"description": "Item description", |
| 39 |
"price": 42.0, |
| 40 |
"tax": 3.2, |
| 41 |
"tags": IsList("bar", "baz", check_order=False), |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
def test_get_items(client: TestClient): |
| 46 |
response = client.get("/items/") |
| 47 |
assert response.status_code == 200, response.text |
| 48 |
assert response.json() == [{"name": "Foo", "price": 42}] |
| 49 |
|
| 50 |
|
| 51 |
def test_get_users(client: TestClient): |
| 52 |
response = client.get("/users/") |
| 53 |
assert response.status_code == 200, response.text |
| 54 |
assert response.json() == [{"username": "johndoe"}] |
| 55 |
|
| 56 |
|
| 57 |
def test_openapi_schema(client: TestClient): |
| 58 |
response = client.get("/openapi.json") |
| 59 |
assert response.status_code == 200, response.text |
| 60 |
assert response.json() == snapshot( |
| 61 |
{ |
| 62 |
"openapi": "3.1.0", |
| 63 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 64 |
"paths": { |
| 65 |
"/items/": { |
| 66 |
"get": { |
| 67 |
"tags": ["items"], |
| 68 |
"summary": "Read Items", |
| 69 |
"operationId": "read_items_items__get", |
| 70 |
"responses": { |
| 71 |
"200": { |
| 72 |
"description": "Successful Response", |
| 73 |
"content": {"application/json": {"schema": {}}}, |
| 74 |
} |
| 75 |
}, |
| 76 |
}, |
| 77 |
"post": { |
| 78 |
"tags": ["items"], |
| 79 |
"summary": "Create Item", |
| 80 |
"operationId": "create_item_items__post", |
| 81 |
"requestBody": { |
| 82 |
"content": { |
| 83 |
"application/json": { |
| 84 |
"schema": {"$ref": "#/components/schemas/Item"} |
| 85 |
} |
| 86 |
}, |
| 87 |
"required": True, |
| 88 |
}, |
| 89 |
"responses": { |
| 90 |
"200": { |
| 91 |
"description": "Successful Response", |
| 92 |
"content": { |
| 93 |
"application/json": { |
| 94 |
"schema": {"$ref": "#/components/schemas/Item"} |
| 95 |
} |
| 96 |
}, |
| 97 |
}, |
| 98 |
"422": { |
| 99 |
"description": "Validation Error", |
| 100 |
"content": { |
| 101 |
"application/json": { |
| 102 |
"schema": { |
| 103 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 104 |
} |
| 105 |
} |
| 106 |
}, |
| 107 |
}, |
| 108 |
}, |
| 109 |
}, |
| 110 |
}, |
| 111 |
"/users/": { |
| 112 |
"get": { |
| 113 |
"tags": ["users"], |
| 114 |
"summary": "Read Users", |
| 115 |
"operationId": "read_users_users__get", |
| 116 |
"responses": { |
| 117 |
"200": { |
| 118 |
"description": "Successful Response", |
| 119 |
"content": {"application/json": {"schema": {}}}, |
| 120 |
} |
| 121 |
}, |
| 122 |
} |
| 123 |
}, |
| 124 |
}, |
| 125 |
"components": { |
| 126 |
"schemas": { |
| 127 |
"HTTPValidationError": { |
| 128 |
"properties": { |
| 129 |
"detail": { |
| 130 |
"items": { |
| 131 |
"$ref": "#/components/schemas/ValidationError", |
| 132 |
}, |
| 133 |
"title": "Detail", |
| 134 |
"type": "array", |
| 135 |
}, |
| 136 |
}, |
| 137 |
"title": "HTTPValidationError", |
| 138 |
"type": "object", |
| 139 |
}, |
| 140 |
"Item": { |
| 141 |
"properties": { |
| 142 |
"description": { |
| 143 |
"anyOf": [ |
| 144 |
{ |
| 145 |
"type": "string", |
| 146 |
}, |
| 147 |
{ |
| 148 |
"type": "null", |
| 149 |
}, |
| 150 |
], |
| 151 |
"title": "Description", |
| 152 |
}, |
| 153 |
"name": { |
| 154 |
"title": "Name", |
| 155 |
"type": "string", |
| 156 |
}, |
| 157 |
"price": { |
| 158 |
"title": "Price", |
| 159 |
"type": "number", |
| 160 |
}, |
| 161 |
"tags": { |
| 162 |
"default": [], |
| 163 |
"items": { |
| 164 |
"type": "string", |
| 165 |
}, |
| 166 |
"title": "Tags", |
| 167 |
"type": "array", |
| 168 |
"uniqueItems": True, |
| 169 |
}, |
| 170 |
"tax": { |
| 171 |
"anyOf": [ |
| 172 |
{ |
| 173 |
"type": "number", |
| 174 |
}, |
| 175 |
{ |
| 176 |
"type": "null", |
| 177 |
}, |
| 178 |
], |
| 179 |
"title": "Tax", |
| 180 |
}, |
| 181 |
}, |
| 182 |
"required": [ |
| 183 |
"name", |
| 184 |
"price", |
| 185 |
], |
| 186 |
"title": "Item", |
| 187 |
"type": "object", |
| 188 |
}, |
| 189 |
"ValidationError": { |
| 190 |
"properties": { |
| 191 |
"ctx": {"title": "Context", "type": "object"}, |
| 192 |
"input": {"title": "Input"}, |
| 193 |
"loc": { |
| 194 |
"items": { |
| 195 |
"anyOf": [ |
| 196 |
{ |
| 197 |
"type": "string", |
| 198 |
}, |
| 199 |
{ |
| 200 |
"type": "integer", |
| 201 |
}, |
| 202 |
], |
| 203 |
}, |
| 204 |
"title": "Location", |
| 205 |
"type": "array", |
| 206 |
}, |
| 207 |
"msg": { |
| 208 |
"title": "Message", |
| 209 |
"type": "string", |
| 210 |
}, |
| 211 |
"type": { |
| 212 |
"title": "Error Type", |
| 213 |
"type": "string", |
| 214 |
}, |
| 215 |
}, |
| 216 |
"required": [ |
| 217 |
"loc", |
| 218 |
"msg", |
| 219 |
"type", |
| 220 |
], |
| 221 |
"title": "ValidationError", |
| 222 |
"type": "object", |
| 223 |
}, |
| 224 |
}, |
| 225 |
}, |
| 226 |
} |
| 227 |
) |