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"
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 == ""
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")})) }
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
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
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 == ""
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") ]))
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
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"]
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"]
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"]
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" ]))
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")
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 == ""
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")
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)
def test_openapi_types_uuid(): type_converter = OpenApiTypeConverter() openapi_type = type_converter.get_openapi_type(UUID) assert openapi_type == Schema(type="string", format="uuid")
def test_openapi_types_bytes(): type_converter = OpenApiTypeConverter() openapi_type = type_converter.get_openapi_type(bytes) assert openapi_type == Schema(type="string", format="binary")
def test_openapi_types_float(): type_converter = OpenApiTypeConverter() openapi_type = type_converter.get_openapi_type(float) assert openapi_type == Schema(type="number")
def test_openapi_types_string(): type_converter = OpenApiTypeConverter() openapi_type = type_converter.get_openapi_type(str) assert openapi_type == Schema(type="string")
def test_openapi_types_int(): type_converter = OpenApiTypeConverter() openapi_type = type_converter.get_openapi_type(int) assert openapi_type == Schema(type="integer")
def test_openapi_types_bool(): type_converter = OpenApiTypeConverter() openapi_type = type_converter.get_openapi_type(bool) assert openapi_type == Schema(type="boolean")
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
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"
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"))
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")])
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")
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)
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
def test_openapi_types_tuple(): type_converter = OpenApiTypeConverter() openapi_type = type_converter.get_openapi_type(tuple) assert openapi_type == Schema(type="array", items=Schema())