Exemplo n.º 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)
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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]
Exemplo n.º 4
0
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
Exemplo n.º 5
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()
Exemplo n.º 6
0
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")
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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"))
Exemplo n.º 9
0
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])
Exemplo n.º 10
0
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()
Exemplo n.º 11
0
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"
    )
Exemplo n.º 12
0
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
Exemplo n.º 13
0
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
Exemplo n.º 14
0
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