1 changed files with 189 additions and 0 deletions
@ -0,0 +1,189 @@ |
|||||
|
import importlib |
||||
|
|
||||
|
import pytest |
||||
|
from dirty_equals import IsList |
||||
|
from fastapi.testclient import TestClient |
||||
|
|
||||
|
from ...utils import needs_py39, needs_py310, needs_pydanticv2 |
||||
|
|
||||
|
skip_for_pydantic_v1 = needs_pydanticv2 |
||||
|
|
||||
|
|
||||
|
@pytest.fixture( |
||||
|
name="client", |
||||
|
params=[ |
||||
|
pytest.param("tutorial001"), |
||||
|
pytest.param("tutorial001_py39", marks=needs_py39), |
||||
|
pytest.param("tutorial001_py310", marks=needs_py310), |
||||
|
], |
||||
|
) |
||||
|
def get_client(request: pytest.FixtureRequest) -> TestClient: |
||||
|
mod = importlib.import_module( |
||||
|
f"docs_src.path_operation_configuration.{request.param}" |
||||
|
) |
||||
|
return TestClient(mod.app) |
||||
|
|
||||
|
|
||||
|
def test_post_items(client: TestClient): |
||||
|
response = client.post( |
||||
|
"/items/", |
||||
|
json={ |
||||
|
"name": "Foo", |
||||
|
"description": "Item description", |
||||
|
"price": 42.0, |
||||
|
"tax": 3.2, |
||||
|
"tags": ["bar", "baz"], |
||||
|
}, |
||||
|
) |
||||
|
assert response.status_code == 201, response.text |
||||
|
assert response.json() == { |
||||
|
"name": "Foo", |
||||
|
"description": "Item description", |
||||
|
"price": 42.0, |
||||
|
"tax": 3.2, |
||||
|
"tags": IsList("bar", "baz", check_order=False), |
||||
|
} |
||||
|
|
||||
|
|
||||
|
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": { |
||||
|
"/items/": { |
||||
|
"post": { |
||||
|
"summary": "Create Item", |
||||
|
"operationId": "create_item_items__post", |
||||
|
"requestBody": { |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": {"$ref": "#/components/schemas/Item"} |
||||
|
} |
||||
|
}, |
||||
|
"required": True, |
||||
|
}, |
||||
|
"responses": { |
||||
|
"201": { |
||||
|
"description": "Successful Response", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": {"$ref": "#/components/schemas/Item"} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
"422": { |
||||
|
"description": "Validation Error", |
||||
|
"content": { |
||||
|
"application/json": { |
||||
|
"schema": { |
||||
|
"$ref": "#/components/schemas/HTTPValidationError" |
||||
|
} |
||||
|
} |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
"components": { |
||||
|
"schemas": { |
||||
|
"HTTPValidationError": { |
||||
|
"properties": { |
||||
|
"detail": { |
||||
|
"items": { |
||||
|
"$ref": "#/components/schemas/ValidationError", |
||||
|
}, |
||||
|
"title": "Detail", |
||||
|
"type": "array", |
||||
|
}, |
||||
|
}, |
||||
|
"title": "HTTPValidationError", |
||||
|
"type": "object", |
||||
|
}, |
||||
|
"Item": { |
||||
|
"properties": { |
||||
|
"description": { |
||||
|
"anyOf": [ |
||||
|
{ |
||||
|
"type": "string", |
||||
|
}, |
||||
|
{ |
||||
|
"type": "null", |
||||
|
}, |
||||
|
], |
||||
|
"title": "Description", |
||||
|
}, |
||||
|
"name": { |
||||
|
"title": "Name", |
||||
|
"type": "string", |
||||
|
}, |
||||
|
"price": { |
||||
|
"title": "Price", |
||||
|
"type": "number", |
||||
|
}, |
||||
|
"tags": { |
||||
|
"default": [], |
||||
|
"items": { |
||||
|
"type": "string", |
||||
|
}, |
||||
|
"title": "Tags", |
||||
|
"type": "array", |
||||
|
"uniqueItems": True, |
||||
|
}, |
||||
|
"tax": { |
||||
|
"anyOf": [ |
||||
|
{ |
||||
|
"type": "number", |
||||
|
}, |
||||
|
{ |
||||
|
"type": "null", |
||||
|
}, |
||||
|
], |
||||
|
"title": "Tax", |
||||
|
}, |
||||
|
}, |
||||
|
"required": [ |
||||
|
"name", |
||||
|
"price", |
||||
|
], |
||||
|
"title": "Item", |
||||
|
"type": "object", |
||||
|
}, |
||||
|
"ValidationError": { |
||||
|
"properties": { |
||||
|
"loc": { |
||||
|
"items": { |
||||
|
"anyOf": [ |
||||
|
{ |
||||
|
"type": "string", |
||||
|
}, |
||||
|
{ |
||||
|
"type": "integer", |
||||
|
}, |
||||
|
], |
||||
|
}, |
||||
|
"title": "Location", |
||||
|
"type": "array", |
||||
|
}, |
||||
|
"msg": { |
||||
|
"title": "Message", |
||||
|
"type": "string", |
||||
|
}, |
||||
|
"type": { |
||||
|
"title": "Error Type", |
||||
|
"type": "string", |
||||
|
}, |
||||
|
}, |
||||
|
"required": [ |
||||
|
"loc", |
||||
|
"msg", |
||||
|
"type", |
||||
|
], |
||||
|
"title": "ValidationError", |
||||
|
"type": "object", |
||||
|
}, |
||||
|
}, |
||||
|
}, |
||||
|
} |
||||
Loading…
Reference in new issue