| 1 |
import importlib |
| 2 |
from types import ModuleType |
| 3 |
|
| 4 |
import pytest |
| 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="mod", |
| 13 |
params=[ |
| 14 |
pytest.param("tutorial001_py310", marks=needs_py310), |
| 15 |
], |
| 16 |
) |
| 17 |
def get_module(request: pytest.FixtureRequest): |
| 18 |
module = importlib.import_module(f"docs_src.encoder.{request.param}") |
| 19 |
return module |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture(name="client") |
| 23 |
def get_client(mod: ModuleType): |
| 24 |
client = TestClient(mod.app) |
| 25 |
return client |
| 26 |
|
| 27 |
|
| 28 |
def test_put(client: TestClient, mod: ModuleType): |
| 29 |
fake_db = mod.fake_db |
| 30 |
|
| 31 |
response = client.put( |
| 32 |
"/items/123", |
| 33 |
json={ |
| 34 |
"title": "Foo", |
| 35 |
"timestamp": "2023-01-01T12:00:00", |
| 36 |
"description": "An optional description", |
| 37 |
}, |
| 38 |
) |
| 39 |
assert response.status_code == 200 |
| 40 |
assert "123" in fake_db |
| 41 |
assert fake_db["123"] == { |
| 42 |
"title": "Foo", |
| 43 |
"timestamp": "2023-01-01T12:00:00", |
| 44 |
"description": "An optional description", |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
def test_put_invalid_data(client: TestClient, mod: ModuleType): |
| 49 |
fake_db = mod.fake_db |
| 50 |
|
| 51 |
response = client.put( |
| 52 |
"/items/345", |
| 53 |
json={ |
| 54 |
"title": "Foo", |
| 55 |
"timestamp": "not a date", |
| 56 |
}, |
| 57 |
) |
| 58 |
assert response.status_code == 422 |
| 59 |
assert response.json() == { |
| 60 |
"detail": [ |
| 61 |
{ |
| 62 |
"loc": ["body", "timestamp"], |
| 63 |
"msg": "Input should be a valid datetime or date, invalid character in year", |
| 64 |
"type": "datetime_from_date_parsing", |
| 65 |
"input": "not a date", |
| 66 |
"ctx": {"error": "invalid character in year"}, |
| 67 |
} |
| 68 |
] |
| 69 |
} |
| 70 |
assert "345" not in fake_db |
| 71 |
|
| 72 |
|
| 73 |
def test_openapi_schema(client: TestClient): |
| 74 |
response = client.get("/openapi.json") |
| 75 |
assert response.status_code == 200 |
| 76 |
assert response.json() == snapshot( |
| 77 |
{ |
| 78 |
"openapi": "3.1.0", |
| 79 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 80 |
"paths": { |
| 81 |
"/items/{id}": { |
| 82 |
"put": { |
| 83 |
"operationId": "update_item_items__id__put", |
| 84 |
"parameters": [ |
| 85 |
{ |
| 86 |
"in": "path", |
| 87 |
"name": "id", |
| 88 |
"required": True, |
| 89 |
"schema": { |
| 90 |
"title": "Id", |
| 91 |
"type": "string", |
| 92 |
}, |
| 93 |
}, |
| 94 |
], |
| 95 |
"requestBody": { |
| 96 |
"content": { |
| 97 |
"application/json": { |
| 98 |
"schema": { |
| 99 |
"$ref": "#/components/schemas/Item", |
| 100 |
}, |
| 101 |
}, |
| 102 |
}, |
| 103 |
"required": True, |
| 104 |
}, |
| 105 |
"responses": { |
| 106 |
"200": { |
| 107 |
"content": { |
| 108 |
"application/json": { |
| 109 |
"schema": {}, |
| 110 |
}, |
| 111 |
}, |
| 112 |
"description": "Successful Response", |
| 113 |
}, |
| 114 |
"422": { |
| 115 |
"content": { |
| 116 |
"application/json": { |
| 117 |
"schema": { |
| 118 |
"$ref": "#/components/schemas/HTTPValidationError", |
| 119 |
}, |
| 120 |
}, |
| 121 |
}, |
| 122 |
"description": "Validation Error", |
| 123 |
}, |
| 124 |
}, |
| 125 |
"summary": "Update Item", |
| 126 |
}, |
| 127 |
}, |
| 128 |
}, |
| 129 |
"components": { |
| 130 |
"schemas": { |
| 131 |
"HTTPValidationError": { |
| 132 |
"properties": { |
| 133 |
"detail": { |
| 134 |
"items": { |
| 135 |
"$ref": "#/components/schemas/ValidationError", |
| 136 |
}, |
| 137 |
"title": "Detail", |
| 138 |
"type": "array", |
| 139 |
}, |
| 140 |
}, |
| 141 |
"title": "HTTPValidationError", |
| 142 |
"type": "object", |
| 143 |
}, |
| 144 |
"Item": { |
| 145 |
"properties": { |
| 146 |
"description": { |
| 147 |
"anyOf": [ |
| 148 |
{ |
| 149 |
"type": "string", |
| 150 |
}, |
| 151 |
{ |
| 152 |
"type": "null", |
| 153 |
}, |
| 154 |
], |
| 155 |
"title": "Description", |
| 156 |
}, |
| 157 |
"timestamp": { |
| 158 |
"format": "date-time", |
| 159 |
"title": "Timestamp", |
| 160 |
"type": "string", |
| 161 |
}, |
| 162 |
"title": { |
| 163 |
"title": "Title", |
| 164 |
"type": "string", |
| 165 |
}, |
| 166 |
}, |
| 167 |
"required": [ |
| 168 |
"title", |
| 169 |
"timestamp", |
| 170 |
], |
| 171 |
"title": "Item", |
| 172 |
"type": "object", |
| 173 |
}, |
| 174 |
"ValidationError": { |
| 175 |
"properties": { |
| 176 |
"ctx": {"title": "Context", "type": "object"}, |
| 177 |
"input": {"title": "Input"}, |
| 178 |
"loc": { |
| 179 |
"items": { |
| 180 |
"anyOf": [ |
| 181 |
{ |
| 182 |
"type": "string", |
| 183 |
}, |
| 184 |
{ |
| 185 |
"type": "integer", |
| 186 |
}, |
| 187 |
], |
| 188 |
}, |
| 189 |
"title": "Location", |
| 190 |
"type": "array", |
| 191 |
}, |
| 192 |
"msg": { |
| 193 |
"title": "Message", |
| 194 |
"type": "string", |
| 195 |
}, |
| 196 |
"type": { |
| 197 |
"title": "Error Type", |
| 198 |
"type": "string", |
| 199 |
}, |
| 200 |
}, |
| 201 |
"required": [ |
| 202 |
"loc", |
| 203 |
"msg", |
| 204 |
"type", |
| 205 |
], |
| 206 |
"title": "ValidationError", |
| 207 |
"type": "object", |
| 208 |
}, |
| 209 |
}, |
| 210 |
}, |
| 211 |
} |
| 212 |
) |