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)
def test_get_spec_path_preferred_format( json_path, yaml_path, preferred_format, expected_result ): docs = OpenAPIHandler( info=Info("Example", "0.0.1"), json_spec_path=json_path, yaml_spec_path=yaml_path, preferred_format=preferred_format, ) assert docs.get_spec_path() == expected_result
def test_register_schema_for_enum(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) docs.register_schema_for_type(FooLevel) assert docs.components.schemas is not None assert len(docs.components.schemas) == 1 schema = docs.components.schemas["FooLevel"] assert isinstance(schema, Schema) assert schema is not None assert schema.type == ValueType.INTEGER assert schema.enum == [x.value for x in FooLevel]
def test_register_schema_handles_repeated_calls(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) docs.register_schema_for_type(Foo) docs.register_schema_for_type(Foo) docs.register_schema_for_type(Foo) assert docs.components.schemas is not None assert len(docs.components.schemas) == 1 foo_schema = docs.components.schemas["Foo"] assert "Foo1" not in docs.components.schemas assert foo_schema is not None assert isinstance(foo_schema, Schema) assert "a" in foo_schema.properties
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()
def test_handles_forward_refs(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) @dataclass class Friend: foo: "Foo" docs.register_schema_for_type(Foo) docs.register_schema_for_type(Friend) assert docs.components.schemas is not None assert len(docs.components.schemas) == 2 friend_schema = docs.components.schemas["Friend"] assert friend_schema is not None assert isinstance(friend_schema, Schema) assert friend_schema.properties["foo"] == Reference(ref="#/components/schemas/Foo")
def test_register_schema_can_handle_classes_with_same_name(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) @dataclass class FooX: x: str FooX.__name__ = "Foo" docs.register_schema_for_type(Foo) docs.register_schema_for_type(FooX) foo_schema = docs.components.schemas["Foo"] foox_schema = docs.components.schemas["Foo1"] assert foo_schema is not None assert foox_schema is not None assert isinstance(foo_schema, Schema) assert isinstance(foox_schema, Schema) assert "x" in foox_schema.properties assert "a" in foo_schema.properties
from openapidocs.v3 import ( Discriminator, Info, MediaType, Operation, 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"))
def test_raises_for_union_type(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) with pytest.raises(UnsupportedUnionTypeException): docs.get_schema_by_type(Union[Foo, Ufo])
def test_get_spec_path_raises_for_unsupported_preferred_format(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) docs.preferred_format = "NOPE" # type: ignore with pytest.raises(OpenAPIEndpointException): docs.get_spec_path()
def test_get_schema_by_type_returns_reference_for_forward_ref(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) assert docs._get_schema_by_type(ForwardRef("Foo")) == Reference( "#/components/schemas/Foo" )
def test_get_content_from_response_info_returns_none_for_missing_content(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) assert docs._get_content_from_response_info(None) is None
def test_get_parameters_returns_non_for_object_without_binders(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) assert docs.get_parameters(Foo) is None assert docs.get_request_body(Foo) is None
def test_try_get_schema_for_enum_returns_none_for_not_enum(): docs = OpenAPIHandler(info=Info("Example", "0.0.1")) assert docs._try_get_schema_for_enum(Foo) is None
from blacksheep.server.responses import created, no_content, ok 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}")