| 1 |
import importlib |
| 2 |
|
| 3 |
import pytest |
| 4 |
from fastapi.testclient import TestClient |
| 5 |
from inline_snapshot import snapshot |
| 6 |
|
| 7 |
from ...utils import needs_py310 |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture( |
| 11 |
name="client", |
| 12 |
params=[ |
| 13 |
pytest.param("tutorial003_py310", marks=needs_py310), |
| 14 |
], |
| 15 |
) |
| 16 |
def get_client(request: pytest.FixtureRequest): |
| 17 |
mod = importlib.import_module(f"docs_src.response_model.{request.param}") |
| 18 |
|
| 19 |
client = TestClient(mod.app) |
| 20 |
return client |
| 21 |
|
| 22 |
|
| 23 |
def test_post_user(client: TestClient): |
| 24 |
response = client.post( |
| 25 |
"/user/", |
| 26 |
json={ |
| 27 |
"username": "foo", |
| 28 |
"password": "fighter", |
| 29 |
"email": "[email protected]", |
| 30 |
"full_name": "Grave Dohl", |
| 31 |
}, |
| 32 |
) |
| 33 |
assert response.status_code == 200, response.text |
| 34 |
assert response.json() == { |
| 35 |
"username": "foo", |
| 36 |
"email": "[email protected]", |
| 37 |
"full_name": "Grave Dohl", |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
def test_openapi_schema(client: TestClient): |
| 42 |
response = client.get("/openapi.json") |
| 43 |
assert response.status_code == 200, response.text |
| 44 |
assert response.json() == snapshot( |
| 45 |
{ |
| 46 |
"openapi": "3.1.0", |
| 47 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 48 |
"paths": { |
| 49 |
"/user/": { |
| 50 |
"post": { |
| 51 |
"responses": { |
| 52 |
"200": { |
| 53 |
"description": "Successful Response", |
| 54 |
"content": { |
| 55 |
"application/json": { |
| 56 |
"schema": { |
| 57 |
"$ref": "#/components/schemas/UserOut" |
| 58 |
} |
| 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": "Create User", |
| 74 |
"operationId": "create_user_user__post", |
| 75 |
"requestBody": { |
| 76 |
"content": { |
| 77 |
"application/json": { |
| 78 |
"schema": {"$ref": "#/components/schemas/UserIn"} |
| 79 |
} |
| 80 |
}, |
| 81 |
"required": True, |
| 82 |
}, |
| 83 |
} |
| 84 |
} |
| 85 |
}, |
| 86 |
"components": { |
| 87 |
"schemas": { |
| 88 |
"UserOut": { |
| 89 |
"title": "UserOut", |
| 90 |
"required": ["username", "email"], |
| 91 |
"type": "object", |
| 92 |
"properties": { |
| 93 |
"username": {"title": "Username", "type": "string"}, |
| 94 |
"email": { |
| 95 |
"title": "Email", |
| 96 |
"type": "string", |
| 97 |
"format": "email", |
| 98 |
}, |
| 99 |
"full_name": { |
| 100 |
"title": "Full Name", |
| 101 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 102 |
}, |
| 103 |
}, |
| 104 |
}, |
| 105 |
"UserIn": { |
| 106 |
"title": "UserIn", |
| 107 |
"required": ["username", "password", "email"], |
| 108 |
"type": "object", |
| 109 |
"properties": { |
| 110 |
"username": {"title": "Username", "type": "string"}, |
| 111 |
"password": {"title": "Password", "type": "string"}, |
| 112 |
"email": { |
| 113 |
"title": "Email", |
| 114 |
"type": "string", |
| 115 |
"format": "email", |
| 116 |
}, |
| 117 |
"full_name": { |
| 118 |
"title": "Full Name", |
| 119 |
"anyOf": [{"type": "string"}, {"type": "null"}], |
| 120 |
}, |
| 121 |
}, |
| 122 |
}, |
| 123 |
"ValidationError": { |
| 124 |
"title": "ValidationError", |
| 125 |
"required": ["loc", "msg", "type"], |
| 126 |
"type": "object", |
| 127 |
"properties": { |
| 128 |
"ctx": {"title": "Context", "type": "object"}, |
| 129 |
"input": {"title": "Input"}, |
| 130 |
"loc": { |
| 131 |
"title": "Location", |
| 132 |
"type": "array", |
| 133 |
"items": { |
| 134 |
"anyOf": [{"type": "string"}, {"type": "integer"}] |
| 135 |
}, |
| 136 |
}, |
| 137 |
"msg": {"title": "Message", "type": "string"}, |
| 138 |
"type": {"title": "Error Type", "type": "string"}, |
| 139 |
}, |
| 140 |
}, |
| 141 |
"HTTPValidationError": { |
| 142 |
"title": "HTTPValidationError", |
| 143 |
"type": "object", |
| 144 |
"properties": { |
| 145 |
"detail": { |
| 146 |
"title": "Detail", |
| 147 |
"type": "array", |
| 148 |
"items": { |
| 149 |
"$ref": "#/components/schemas/ValidationError" |
| 150 |
}, |
| 151 |
} |
| 152 |
}, |
| 153 |
}, |
| 154 |
} |
| 155 |
}, |
| 156 |
} |
| 157 |
) |