1 changed files with 163 additions and 0 deletions
@ -0,0 +1,163 @@ |
|||
import importlib |
|||
|
|||
import pytest |
|||
from dirty_equals import IsList |
|||
from fastapi.testclient import TestClient |
|||
|
|||
from ...utils import needs_py310, needs_pydanticv2 |
|||
|
|||
skip_for_pydantic_v1 = needs_pydanticv2 |
|||
|
|||
# Remove when deprecating Pydantic v1 |
|||
pytestmark = pytest.mark.filterwarnings( |
|||
"ignore:The `dict` method is deprecated; use `model_dump` instead.:DeprecationWarning", |
|||
) |
|||
|
|||
|
|||
@pytest.fixture( |
|||
name="client", |
|||
params=[ |
|||
"tutorial001", |
|||
pytest.param("tutorial001_py310", marks=needs_py310), |
|||
"tutorial002", |
|||
pytest.param("tutorial002_py310", marks=needs_py310), |
|||
], |
|||
) |
|||
def get_client(request: pytest.FixtureRequest): |
|||
mod = importlib.import_module(f"docs_src.extra_models.{request.param}") |
|||
|
|||
client = TestClient(mod.app) |
|||
return client |
|||
|
|||
|
|||
def test_post(client: TestClient): |
|||
response = client.post( |
|||
"/user/", |
|||
json={ |
|||
"username": "johndoe", |
|||
"password": "secret", |
|||
"email": "[email protected]", |
|||
"full_name": "John Doe", |
|||
}, |
|||
) |
|||
assert response.status_code == 200, response.text |
|||
assert response.json() == { |
|||
"username": "johndoe", |
|||
"email": "[email protected]", |
|||
"full_name": "John Doe", |
|||
} |
|||
|
|||
|
|||
def test_openapi_schema(client: TestClient): |
|||
response = client.get("/openapi.json") |
|||
assert response.status_code == 200, response.text |
|||
assert response.json() == { |
|||
"openapi": "3.1.0", |
|||
"info": {"title": "FastAPI", "version": "0.1.0"}, |
|||
"paths": { |
|||
"/user/": { |
|||
"post": { |
|||
"responses": { |
|||
"200": { |
|||
"description": "Successful Response", |
|||
"content": { |
|||
"application/json": { |
|||
"schema": { |
|||
"$ref": "#/components/schemas/UserOut", |
|||
} |
|||
} |
|||
}, |
|||
}, |
|||
"422": { |
|||
"description": "Validation Error", |
|||
"content": { |
|||
"application/json": { |
|||
"schema": { |
|||
"$ref": "#/components/schemas/HTTPValidationError" |
|||
} |
|||
} |
|||
}, |
|||
}, |
|||
}, |
|||
"summary": "Create User", |
|||
"operationId": "create_user_user__post", |
|||
"requestBody": { |
|||
"content": { |
|||
"application/json": { |
|||
"schema": {"$ref": "#/components/schemas/UserIn"} |
|||
} |
|||
}, |
|||
"required": True, |
|||
}, |
|||
} |
|||
} |
|||
}, |
|||
"components": { |
|||
"schemas": { |
|||
"UserIn": { |
|||
"title": "UserIn", |
|||
"required": IsList( |
|||
"username", "password", "email", check_order=False |
|||
), |
|||
"type": "object", |
|||
"properties": { |
|||
"username": {"title": "Username", "type": "string"}, |
|||
"password": {"title": "Password", "type": "string"}, |
|||
"email": { |
|||
"title": "Email", |
|||
"type": "string", |
|||
"format": "email", |
|||
}, |
|||
"full_name": { |
|||
"title": "Full Name", |
|||
"anyOf": [{"type": "string"}, {"type": "null"}], |
|||
}, |
|||
}, |
|||
}, |
|||
"UserOut": { |
|||
"title": "UserOut", |
|||
"required": ["username", "email"], |
|||
"type": "object", |
|||
"properties": { |
|||
"username": {"title": "Username", "type": "string"}, |
|||
"email": { |
|||
"title": "Email", |
|||
"type": "string", |
|||
"format": "email", |
|||
}, |
|||
"full_name": { |
|||
"title": "Full Name", |
|||
"anyOf": [{"type": "string"}, {"type": "null"}], |
|||
}, |
|||
}, |
|||
}, |
|||
"ValidationError": { |
|||
"title": "ValidationError", |
|||
"required": ["loc", "msg", "type"], |
|||
"type": "object", |
|||
"properties": { |
|||
"loc": { |
|||
"title": "Location", |
|||
"type": "array", |
|||
"items": { |
|||
"anyOf": [{"type": "string"}, {"type": "integer"}] |
|||
}, |
|||
}, |
|||
"msg": {"title": "Message", "type": "string"}, |
|||
"type": {"title": "Error Type", "type": "string"}, |
|||
}, |
|||
}, |
|||
"HTTPValidationError": { |
|||
"title": "HTTPValidationError", |
|||
"type": "object", |
|||
"properties": { |
|||
"detail": { |
|||
"title": "Detail", |
|||
"type": "array", |
|||
"items": {"$ref": "#/components/schemas/ValidationError"}, |
|||
} |
|||
}, |
|||
}, |
|||
} |
|||
}, |
|||
} |
|||
Loading…
Reference in new issue