mirror of https://github.com/tiangolo/fastapi.git
80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
from starlette.testclient import TestClient
|
|
|
|
from events.tutorial001 import app
|
|
|
|
openapi_schema = {
|
|
"openapi": "3.0.2",
|
|
"info": {"title": "Fast API", "version": "0.1.0"},
|
|
"paths": {
|
|
"/items/{item_id}": {
|
|
"get": {
|
|
"responses": {
|
|
"200": {
|
|
"description": "Successful Response",
|
|
"content": {"application/json": {"schema": {}}},
|
|
},
|
|
"422": {
|
|
"description": "Validation Error",
|
|
"content": {
|
|
"application/json": {
|
|
"schema": {
|
|
"$ref": "#/components/schemas/HTTPValidationError"
|
|
}
|
|
}
|
|
},
|
|
},
|
|
},
|
|
"summary": "Read Items Get",
|
|
"operationId": "read_items_items__item_id__get",
|
|
"parameters": [
|
|
{
|
|
"required": True,
|
|
"schema": {"title": "Item_Id", "type": "string"},
|
|
"name": "item_id",
|
|
"in": "path",
|
|
}
|
|
],
|
|
}
|
|
}
|
|
},
|
|
"components": {
|
|
"schemas": {
|
|
"ValidationError": {
|
|
"title": "ValidationError",
|
|
"required": ["loc", "msg", "type"],
|
|
"type": "object",
|
|
"properties": {
|
|
"loc": {
|
|
"title": "Location",
|
|
"type": "array",
|
|
"items": {"type": "string"},
|
|
},
|
|
"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"},
|
|
}
|
|
},
|
|
},
|
|
}
|
|
},
|
|
}
|
|
|
|
|
|
def test_events():
|
|
with TestClient(app) as client:
|
|
response = client.get("/openapi.json")
|
|
assert response.status_code == 200
|
|
assert response.json() == openapi_schema
|
|
response = client.get("/items/foo")
|
|
assert response.status_code == 200
|
|
assert response.json() == {"name": "Fighters"}
|