| 1 |
import importlib |
| 2 |
import os |
| 3 |
import shutil |
| 4 |
|
| 5 |
import pytest |
| 6 |
from fastapi.testclient import TestClient |
| 7 |
from inline_snapshot import snapshot |
| 8 |
|
| 9 |
from tests.utils import needs_py310, workdir_lock |
| 10 |
|
| 11 |
|
| 12 |
@pytest.fixture( |
| 13 |
name="client", |
| 14 |
params=[ |
| 15 |
pytest.param("tutorial002_py310", marks=needs_py310), |
| 16 |
], |
| 17 |
) |
| 18 |
def get_client(request: pytest.FixtureRequest): |
| 19 |
mod = importlib.import_module(f"docs_src.additional_responses.{request.param}") |
| 20 |
|
| 21 |
client = TestClient(mod.app) |
| 22 |
client.headers.clear() |
| 23 |
return client |
| 24 |
|
| 25 |
|
| 26 |
def test_path_operation(client: TestClient): |
| 27 |
response = client.get("/items/foo") |
| 28 |
assert response.status_code == 200, response.text |
| 29 |
assert response.json() == {"id": "foo", "value": "there goes my hero"} |
| 30 |
|
| 31 |
|
| 32 |
@workdir_lock |
| 33 |
def test_path_operation_img(client: TestClient): |
| 34 |
shutil.copy("./docs/en/docs/img/favicon.png", "./image.png") |
| 35 |
response = client.get("/items/foo?img=1") |
| 36 |
assert response.status_code == 200, response.text |
| 37 |
assert response.headers["Content-Type"] == "image/png" |
| 38 |
assert len(response.content) |
| 39 |
os.remove("./image.png") |
| 40 |
|
| 41 |
|
| 42 |
def test_openapi_schema(client: TestClient): |
| 43 |
response = client.get("/openapi.json") |
| 44 |
assert response.status_code == 200, response.text |
| 45 |
assert response.json() == snapshot( |
| 46 |
{ |
| 47 |
"openapi": "3.1.0", |
| 48 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 49 |
"paths": { |
| 50 |
"/items/{item_id}": { |
| 51 |
"get": { |
| 52 |
"responses": { |
| 53 |
"200": { |
| 54 |
"description": "Return the JSON item or an image.", |
| 55 |
"content": { |
| 56 |
"image/png": {}, |
| 57 |
"application/json": { |
| 58 |
"schema": {"$ref": "#/components/schemas/Item"} |
| 59 |
}, |
| 60 |
}, |
| 61 |
}, |
| 62 |
"422": { |
| 63 |
"description": "Validation Error", |
| 64 |
"content": { |
| 65 |
"application/json": { |
| 66 |
"schema": { |
| 67 |
"$ref": "#/components/schemas/HTTPValidationError" |
| 68 |
} |
| 69 |
} |
| 70 |
}, |
| 71 |
}, |
| 72 |
}, |
| 73 |
"summary": "Read Item", |
| 74 |
"operationId": "read_item_items__item_id__get", |
| 75 |
"parameters": [ |
| 76 |
{ |
| 77 |
"required": True, |
| 78 |
"schema": {"title": "Item Id", "type": "string"}, |
| 79 |
"name": "item_id", |
| 80 |
"in": "path", |
| 81 |
}, |
| 82 |
{ |
| 83 |
"required": False, |
| 84 |
"schema": { |
| 85 |
"anyOf": [{"type": "boolean"}, {"type": "null"}], |
| 86 |
"title": "Img", |
| 87 |
}, |
| 88 |
"name": "img", |
| 89 |
"in": "query", |
| 90 |
}, |
| 91 |
], |
| 92 |
} |
| 93 |
} |
| 94 |
}, |
| 95 |
"components": { |
| 96 |
"schemas": { |
| 97 |
"Item": { |
| 98 |
"title": "Item", |
| 99 |
"required": ["id", "value"], |
| 100 |
"type": "object", |
| 101 |
"properties": { |
| 102 |
"id": {"title": "Id", "type": "string"}, |
| 103 |
"value": {"title": "Value", "type": "string"}, |
| 104 |
}, |
| 105 |
}, |
| 106 |
"ValidationError": { |
| 107 |
"title": "ValidationError", |
| 108 |
"required": ["loc", "msg", "type"], |
| 109 |
"type": "object", |
| 110 |
"properties": { |
| 111 |
"loc": { |
| 112 |
"title": "Location", |
| 113 |
"type": "array", |
| 114 |
"items": { |
| 115 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 116 |
}, |
| 117 |
}, |
| 118 |
"msg": {"title": "Message", "type": "string"}, |
| 119 |
"type": {"title": "Error Type", "type": "string"}, |
| 120 |
"input": {"title": "Input"}, |
| 121 |
"ctx": {"title": "Context", "type": "object"}, |
| 122 |
}, |
| 123 |
}, |
| 124 |
"HTTPValidationError": { |
| 125 |
"title": "HTTPValidationError", |
| 126 |
"type": "object", |
| 127 |
"properties": { |
| 128 |
"detail": { |
| 129 |
"title": "Detail", |
| 130 |
"type": "array", |
| 131 |
"items": { |
| 132 |
"$ref": "#/components/schemas/ValidationError" |
| 133 |
}, |
| 134 |
} |
| 135 |
}, |
| 136 |
}, |
| 137 |
} |
| 138 |
}, |
| 139 |
} |
| 140 |
) |