コード例 #1
0
def test_openapi_types_dict_of_union():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(Dict[str, Union[model.Project, model.Environment]])
    assert openapi_type.type == "object"
    assert len(openapi_type.additionalProperties.anyOf) == 2
    assert openapi_type.additionalProperties.anyOf[0].ref == type_converter.ref_prefix + "Project"
    assert openapi_type.additionalProperties.anyOf[1].ref == type_converter.ref_prefix + "Environment"
コード例 #2
0
def test_get_operation_no_docstring(api_methods_fixture):
    """
    Test whether an OpenAPI operation is constructed correctly for a
    GET method which doesn't have a docstring.
    """
    get = UrlMethod(
        properties=MethodProperties.methods["dummy_get_with_parameters_no_docstring"][0],
        slice=None,
        method_name="dummy_get_with_parameters_no_docstring",
        handler=None,
    )

    operation_handler = OperationHandler(OpenApiTypeConverter(), ArgOptionHandler(OpenApiTypeConverter()))
    operation = operation_handler.handle_method(get, "/operation/{id}")

    # Asserts on request body
    assert operation.requestBody is None

    # Asserts on parameters
    assert operation.summary is None
    assert operation.description is None
    assert sorted(["header-val", "non_header", "param", "id"]) == sorted([parameter.name for parameter in operation.parameters])
    param_map = {param.name: param.description for param in operation.parameters}
    assert len(param_map) == 4
    assert param_map["header-val"] is None
    assert param_map["non_header"] is None
    assert param_map["param"] is None
    assert param_map["id"] is None

    # Asserts on response
    assert len(operation.responses) == 1
    assert ["header-val"] == list(operation.responses["200"].headers.keys())
    assert operation.responses["200"].description == ""
コード例 #3
0
def test_return_value(api_methods_fixture):
    operation_handler = OperationHandler(OpenApiTypeConverter(), ArgOptionHandler(OpenApiTypeConverter()))

    json_response_content = operation_handler._build_return_value_wrapper(MethodProperties.methods["post_method"][0])
    assert json_response_content == {
        "application/json": MediaType(schema=Schema(type="object", properties={"data": Schema(type="string")}))
    }
コード例 #4
0
def test_post_operation(api_methods_fixture):
    """
    Test whether an OpenAPI operation is constructed correctly for a
    POST method which is fully annotated and documented.
    """
    short_description = "This is a brief description."
    long_description = "This is a more in depth description of the method."
    header_description = "A header value."
    non_header_description = "Non header value via arg_options."
    param_description = "A parameter."
    id_description = "The id of the resource."
    return_value_description = "A return value."
    raises_os_error_description = "Something went wrong"
    raises_not_found_description = "Resource was not found."
    raises_dummy_exception_description = "A dummy exception"
    method_name = "dummy_post_with_parameters"

    post = UrlMethod(
        properties=MethodProperties.methods["dummy_post_with_parameters"][0],
        slice=None,
        method_name=method_name,
        handler=None,
    )

    operation_handler = OperationHandler(
        OpenApiTypeConverter(), ArgOptionHandler(OpenApiTypeConverter()))
    operation = operation_handler.handle_method(post, "/operation/{id}")

    # Asserts on request body
    expected_params = ["param", "non_header"]
    actual_params = list(operation.requestBody.content["application/json"].
                         schema_.properties.keys())
    assert sorted(expected_params) == sorted(actual_params)
    assert (
        f"* **non_header:** {non_header_description}\n* **param:** {param_description}\n"
        == operation.requestBody.description)

    # Asserts on parameters
    assert operation.summary == short_description
    assert operation.description == long_description
    assert operation.operationId == method_name
    assert sorted(["header-val", "id"]) == sorted(
        [parameter.name for parameter in operation.parameters])
    param_map = {
        param.name: param.description
        for param in operation.parameters
    }
    assert len(param_map) == 2
    assert param_map["header-val"] == header_description
    assert param_map["id"] == id_description

    # Asserts on response
    assert len(operation.responses) == 4
    assert operation.responses["200"].description == return_value_description
    assert operation.responses[
        "404"].description == raises_not_found_description
    assert operation.responses[
        "405"].description == raises_dummy_exception_description
    assert operation.responses[
        "500"].description == raises_os_error_description
コード例 #5
0
def test_openapi_types_list_of_list_of_optional_model():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(List[List[Optional[model.Project]]])
    assert openapi_type.type == "array"
    assert openapi_type.items.type == "array"
    assert openapi_type.items.items.ref == type_converter.ref_prefix + "Project"
    assert openapi_type.items.items.nullable
コード例 #6
0
def test_post_operation_no_docstring(api_methods_fixture):
    """
    Test whether an OpenAPI operation is constructed correctly for a
    POST method which doesn't have a docstring.
    """
    post = UrlMethod(
        properties=MethodProperties.methods["dummy_post_with_parameters_no_docstring"][0],
        slice=None,
        method_name="dummy_post_with_parameters_no_docstring",
        handler=None,
    )

    operation_handler = OperationHandler(OpenApiTypeConverter(), ArgOptionHandler(OpenApiTypeConverter()))
    operation = operation_handler.handle_method(post, "/operation/{id}")

    # Asserts on request body
    expected_params = ["param", "non_header"]
    actual_params = list(operation.requestBody.content["application/json"].schema_.properties.keys())
    assert sorted(expected_params) == sorted(actual_params)
    assert operation.requestBody.description == "* **non_header:**\n* **param:**\n"

    # Asserts on parameters
    assert operation.summary is None
    assert operation.description is None
    assert sorted(["header-val", "id"]) == sorted([parameter.name for parameter in operation.parameters])
    param_map = {param.name: param.description for param in operation.parameters}
    assert len(param_map) == 2
    assert param_map["header-val"] is None
    assert param_map["id"] is None

    # Asserts on response
    assert len(operation.responses) == 1
    assert ["header-val"] == list(operation.responses["200"].headers.keys())
    assert operation.responses["200"].description == ""
コード例 #7
0
def test_openapi_types_list():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(List[Union[int, UUID]])
    assert openapi_type == Schema(type="array",
                                  items=Schema(anyOf=[
                                      Schema(type="integer"),
                                      Schema(type="string", format="uuid")
                                  ]))
コード例 #8
0
def test_openapi_types_list_of_list_of_optional_model():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(
        List[List[Optional[model.Project]]])
    assert openapi_type.type == "array"
    assert openapi_type.items.type == "array"
    assert openapi_type.items.items.required == ["id", "name", "environments"]
    assert openapi_type.items.items.nullable
コード例 #9
0
def test_openapi_types_env_setting():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(EnvironmentSetting)
    assert openapi_type.ref == type_converter.ref_prefix + "EnvironmentSetting"

    env_settings_type = type_converter.resolve_reference(openapi_type.ref)
    assert env_settings_type.title == "EnvironmentSetting"
    assert env_settings_type.type == "object"
    assert env_settings_type.required == ["name", "type", "default", "doc", "recompile", "update_model", "agent_restart"]
コード例 #10
0
def test_openapi_types_base_model():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type_of_parameter(
        inspect.Parameter("param", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=model.Environment)
    )
    assert openapi_type.ref == type_converter.ref_prefix + "Environment"

    environment_type = type_converter.resolve_reference(openapi_type.ref)
    assert environment_type.required == ["id", "name", "project_id", "repo_url", "repo_branch", "settings", "halted"]
コード例 #11
0
def test_openapi_types_enum():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(List[ResourceAction])
    assert openapi_type.type == "array"
    assert openapi_type.items.ref == type_converter.ref_prefix + "ResourceAction"

    resource_action_type = type_converter.components.schemas["ResourceAction"]
    assert resource_action_type.type == "string"
    assert resource_action_type.enum == ["store", "push", "pull", "deploy", "dryrun", "getfact", "other"]
コード例 #12
0
def test_openapi_types_enum():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(List[ResourceAction])
    assert openapi_type == Schema(type="array",
                                  items=Schema(type="string",
                                               enum=[
                                                   "store", "push", "pull",
                                                   "deploy", "dryrun",
                                                   "getfact", "other"
                                               ]))
コード例 #13
0
def test_openapi_types_anyurl():
    type_converter = OpenApiTypeConverter()

    openapi_type = type_converter.get_openapi_type(AnyUrl)
    assert openapi_type == Schema(type="string", format="uri")

    openapi_type = type_converter.get_openapi_type(AnyHttpUrl)
    assert openapi_type == Schema(type="string", format="uri")

    openapi_type = type_converter.get_openapi_type(PostgresDsn)
    assert openapi_type == Schema(type="string", format="uri")
コード例 #14
0
def test_get_operation_partial_documentation(api_methods_fixture):
    """
    Test whether an OpenAPI operation is constructed correctly for a
    GET method which has missing entries in its docstring.
    """
    short_description = "This is a brief description."
    tid_description = "The inmanta environment id."
    param_description = "A parameter."
    id_description = "The id of the resource."

    get = UrlMethod(
        properties=MethodProperties.
        methods["dummy_get_with_parameters_partial_documentation"][0],
        slice=None,
        method_name="dummy_get_with_parameters_partial_documentation",
        handler=None,
    )

    operation_handler = OperationHandler(
        OpenApiTypeConverter(), ArgOptionHandler(OpenApiTypeConverter()))
    operation = operation_handler.handle_method(
        get, "/operation/{id_doc}/{id_no_doc}")

    # Asserts on request body
    assert operation.requestBody is None

    # Asserts on parameters
    assert operation.summary == short_description
    assert operation.description is None

    expected_parameters = [
        "header-doc", "header-no-doc", "param_doc", "param_no_doc", "id_doc",
        "id_no_doc"
    ]
    actual_parameters = [parameter.name for parameter in operation.parameters]
    assert sorted(expected_parameters) == sorted(actual_parameters)
    param_map = {
        param.name: param.description
        for param in operation.parameters
    }
    assert len(param_map) == 6
    assert param_map["header-doc"] == tid_description
    assert param_map["header-no-doc"] is None
    assert param_map["param_doc"] == param_description
    assert param_map["param_no_doc"] is None
    assert param_map["id_doc"] == id_description
    assert param_map["id_no_doc"] is None

    # Asserts on response
    assert len(operation.responses) == 1
    assert sorted(["header-doc", "header-no-doc"]) == sorted(
        list(operation.responses["200"].headers.keys()))
    assert operation.responses["200"].description == ""
コード例 #15
0
def test_get_openapi_types():
    type_converter = OpenApiTypeConverter()

    openapi_type = type_converter.get_openapi_type_of_parameter(
        inspect.Parameter("param", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=UUID)
    )
    assert openapi_type == Schema(type="string", format="uuid")

    openapi_type = type_converter.get_openapi_type_of_parameter(
        inspect.Parameter("param", kind=inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int)
    )
    assert openapi_type == Schema(type="integer")
コード例 #16
0
def test_get_function_parameters(api_methods_fixture):
    url_method = UrlMethod(
        properties=MethodProperties.methods["dummy_get_with_parameters"][0],
        slice=None,
        method_name="dummy_get_with_parameters",
        handler=None,
    )
    function_parameter_handler = FunctionParameterHandler(
        OpenApiTypeConverter(), ArgOptionHandler(OpenApiTypeConverter()), "/basepath", url_method.properties
    )
    function_parameters = function_parameter_handler.all_params_dct
    assert len(function_parameters) == 4
    assert function_parameters["param"] == inspect.Parameter("param", inspect.Parameter.POSITIONAL_OR_KEYWORD, annotation=int)
コード例 #17
0
def test_openapi_types_uuid():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(UUID)
    assert openapi_type == Schema(type="string", format="uuid")
コード例 #18
0
def test_openapi_types_bytes():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(bytes)
    assert openapi_type == Schema(type="string", format="binary")
コード例 #19
0
def test_openapi_types_float():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(float)
    assert openapi_type == Schema(type="number")
コード例 #20
0
def test_openapi_types_string():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(str)
    assert openapi_type == Schema(type="string")
コード例 #21
0
def test_openapi_types_int():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(int)
    assert openapi_type == Schema(type="integer")
コード例 #22
0
def test_openapi_types_bool():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(bool)
    assert openapi_type == Schema(type="boolean")
コード例 #23
0
def test_openapi_types_union_optional():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(Union[Optional[int], Optional[str]])
    assert len(openapi_type.anyOf) == 2
    assert openapi_type.nullable
コード例 #24
0
def test_openapi_types_list_of_model():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(List[model.Project])
    assert openapi_type.type == "array"
    assert openapi_type.items.ref == type_converter.ref_prefix + "Project"
コード例 #25
0
def test_openapi_types_dict():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(Dict[str, UUID])
    assert openapi_type == Schema(type="object", additionalProperties=Schema(type="string", format="uuid"))
コード例 #26
0
def test_openapi_types_union():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(Union[str, bytes])
    assert openapi_type == Schema(anyOf=[Schema(type="string"), Schema(type="string", format="binary")])
コード例 #27
0
def test_openapi_types_datetime():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(datetime)
    assert openapi_type == Schema(type="string", format="date-time")
コード例 #28
0
def test_openapi_types_optional():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(Optional[str])
    assert openapi_type == Schema(type="string", nullable=True)
コード例 #29
0
def test_openapi_types_optional_union():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(Optional[Union[int, str]])
    assert len(openapi_type.anyOf) == 2
    assert openapi_type.nullable
コード例 #30
0
def test_openapi_types_tuple():
    type_converter = OpenApiTypeConverter()
    openapi_type = type_converter.get_openapi_type(tuple)
    assert openapi_type == Schema(type="array", items=Schema())