def test_from_dict(self, mocker, model_property_factory, enum_property_factory): from openapi_python_client.parser.properties import Schemas build_schemas = mocker.patch(f"{MODULE_NAME}.build_schemas") EndpointCollection = mocker.patch(f"{MODULE_NAME}.EndpointCollection") schemas = mocker.MagicMock() schemas.classes_by_name = { "Model": model_property_factory(), "Enum": enum_property_factory(), } endpoints_collections_by_tag = mocker.MagicMock() EndpointCollection.from_data.return_value = ( endpoints_collections_by_tag, schemas) OpenAPI = mocker.patch(f"{MODULE_NAME}.oai.OpenAPI") openapi = OpenAPI.parse_obj.return_value openapi.openapi = mocker.MagicMock(major=3) config = mocker.MagicMock() in_dict = mocker.MagicMock() from openapi_python_client.parser.openapi import GeneratorData generator_data = GeneratorData.from_dict(in_dict, config=config) OpenAPI.parse_obj.assert_called_once_with(in_dict) build_schemas.assert_called_once_with( components=openapi.components.schemas, config=config, schemas=Schemas()) EndpointCollection.from_data.assert_called_once_with( data=openapi.paths, schemas=build_schemas.return_value, config=config) assert generator_data.title == openapi.info.title assert generator_data.description == openapi.info.description assert generator_data.version == openapi.info.version assert generator_data.endpoint_collections_by_tag == endpoints_collections_by_tag assert generator_data.errors == schemas.errors assert list( generator_data.models) == [schemas.classes_by_name["Model"]] assert list(generator_data.enums) == [schemas.classes_by_name["Enum"]] # Test no components openapi.components = None build_schemas.reset_mock() GeneratorData.from_dict(in_dict, config=config) build_schemas.assert_not_called()
def test_swagger_document_invalid_schema(self, mocker): Schemas = mocker.patch(f"{MODULE_NAME}.Schemas") config = mocker.MagicMock() in_dict = {"swagger": "2.0"} from openapi_python_client.parser.openapi import GeneratorData generator_data = GeneratorData.from_dict(in_dict, config=config) assert generator_data == GeneratorError( header="Failed to parse OpenAPI document", detail=( "You may be trying to use a Swagger document; this is not supported by this project.\n\n" "3 validation errors for OpenAPI\n" "info\n" " field required (type=value_error.missing)\n" "paths\n" " field required (type=value_error.missing)\n" "openapi\n" " field required (type=value_error.missing)" ), ) Schemas.build.assert_not_called() Schemas.assert_not_called()
def test_from_dict(self, mocker): build_schemas = mocker.patch(f"{MODULE_NAME}.build_schemas") EndpointCollection = mocker.patch(f"{MODULE_NAME}.EndpointCollection") schemas = mocker.MagicMock() endpoints_collections_by_tag = mocker.MagicMock() EndpointCollection.from_data.return_value = ( endpoints_collections_by_tag, schemas) OpenAPI = mocker.patch(f"{MODULE_NAME}.oai.OpenAPI") openapi = OpenAPI.parse_obj.return_value openapi.openapi = mocker.MagicMock(major=3) in_dict = mocker.MagicMock() from openapi_python_client.parser.openapi import GeneratorData generator_data = GeneratorData.from_dict(in_dict) OpenAPI.parse_obj.assert_called_once_with(in_dict) build_schemas.assert_called_once_with( components=openapi.components.schemas) EndpointCollection.from_data.assert_called_once_with( data=openapi.paths, schemas=build_schemas.return_value) assert generator_data == GeneratorData( title=openapi.info.title, description=openapi.info.description, version=openapi.info.version, endpoint_collections_by_tag=endpoints_collections_by_tag, errors=schemas.errors, models=schemas.models, enums=schemas.enums, ) # Test no components openapi.components = None build_schemas.reset_mock() GeneratorData.from_dict(in_dict) build_schemas.assert_not_called()
def test_from_dict_invalid_version(self, mocker): Schemas = mocker.patch(f"{MODULE_NAME}.Schemas") OpenAPI = mocker.patch(f"{MODULE_NAME}.oai.OpenAPI") openapi = OpenAPI.parse_obj.return_value openapi.openapi = oai.SemVer("2.1.3") in_dict = mocker.MagicMock() config = mocker.MagicMock() from openapi_python_client.parser.openapi import GeneratorData generator_data = GeneratorData.from_dict(in_dict, config=config) assert generator_data == GeneratorError( header="openapi-python-client only supports OpenAPI 3.x", detail="The version of the provided document was 2.1.3", ) Schemas.build.assert_not_called() Schemas.assert_not_called()
def test_from_dict_invalid_schema(self, mocker): Schemas = mocker.patch(f"{MODULE_NAME}.Schemas") in_dict = {} from openapi_python_client.parser.openapi import GeneratorData generator_data = GeneratorData.from_dict(in_dict) assert generator_data == GeneratorError( header="Failed to parse OpenAPI document", detail=("2 validation errors for OpenAPI\n" "info\n" " field required (type=value_error.missing)\n" "paths\n" " field required (type=value_error.missing)"), ) Schemas.build.assert_not_called() Schemas.assert_not_called()