Пример #1
0
async def test_raises_for_started_app():
    app = Application()

    await app.start()

    docs = OpenAPIHandler(info=Info("Example", "0.0.1"))

    with pytest.raises(TypeError):
        docs.bind_app(app)
Пример #2
0
async def test_raises_for_duplicated_content_example():
    app = Application()

    docs = OpenAPIHandler(info=Info("Example", "0.0.1"))

    @app.router.get("/")
    @docs(
        responses={
            200: ResponseInfo("Example", content=[ContentInfo(Foo), ContentInfo(Foo)])
        }
    )
    async def example():
        ...

    with pytest.raises(DuplicatedContentTypeDocsException):
        docs.bind_app(app)
        await app.start()
Пример #3
0
    Response as ResponseDoc,
    Schema,
)
from pydantic import BaseModel

from itests.utils import CrashTest

app_two = Application()

# OpenAPI v3 configuration:
docs = OpenAPIHandler(info=Info(title="Cats API", version="0.0.1"))
docs.ui_providers.append(ReDocUIProvider())

# include only endpoints whose path starts with "/api/"
docs.include = lambda path, _: path.startswith("/api/")
docs.bind_app(app_two)


class HandledException(Exception):
    def __init__(self):
        super().__init__("Example exception")


async def handle_test_exception(app, request, http_exception):
    return Response(200,
                    content=TextContent(f"Fake exception, to test handlers"))


app_two.exceptions_handlers[HandledException] = handle_test_exception

Пример #4
0
from models import UserPydanticIn, UserPydanticOut, Users

from tortoise.contrib.blacksheep import register_tortoise

app = Application()
register_tortoise(
    app,
    db_url="sqlite://:memory:",
    modules={"models": ["models"]},
    generate_schemas=True,
    add_exception_handlers=True,
)


docs = OpenAPIHandler(info=Info(title="Tortoise ORM BlackSheep example", version="0.0.1"))
docs.bind_app(app)


@app.router.get("/")
async def users_list() -> Union[UserPydanticOut]:
    return ok(await UserPydanticOut.from_queryset(Users.all()))


@app.router.post("/")
async def users_create(user: UserPydanticIn) -> UserPydanticOut:
    user = await Users.create(**user.dict(exclude_unset=True))
    return created(await UserPydanticOut.from_tortoise_orm(user))


@app.router.patch("/{id}")
async def users_patch(id: UUID, user: UserPydanticIn) -> UserPydanticOut: