def test_response_from_data_property_error(mocker): from openapi_python_client.parser import responses property_from_data = mocker.patch.object(responses, "property_from_data", return_value=(PropertyError(), Schemas())) data = oai.Response.construct( description="", content={ "application/json": oai.MediaType.construct(media_type_schema="something") }) config = MagicMock() response, schemas = responses.response_from_data(status_code=400, data=data, schemas=Schemas(), parent_name="parent", config=config) assert response == PropertyError() property_from_data.assert_called_once_with(name="response_400", required=True, data="something", schemas=Schemas(), parent_name="parent", config=config)
def test_response_from_data_property(mocker): from openapi_python_client.parser import responses prop = StringProperty(name="prop", required=True, nullable=False, default=None) property_from_data = mocker.patch.object(responses, "property_from_data", return_value=(prop, Schemas())) data = oai.Response.construct( description="", content={ "application/json": oai.MediaType.construct(media_type_schema="something") }) response, schemas = responses.response_from_data(status_code=400, data=data, schemas=Schemas(), parent_name="parent") assert response == responses.Response( status_code=400, prop=prop, source="response.json()", ) property_from_data.assert_called_once_with(name="response_400", required=True, data="something", schemas=Schemas(), parent_name="parent")
def test_response_from_data_unsupported_content_type(self): from openapi_python_client.parser.errors import ParseError from openapi_python_client.parser.responses import response_from_data content = {"not/real": {}} data = oai.Response.construct(content=content) assert response_from_data(status_code=200, data=data) == ParseError( data=data, detail=f"Unsupported content_type {content}")
def test_response_from_data_empty(self, mocker): status_code = mocker.MagicMock(autospec=int) data = oai.Response.construct() Response = mocker.patch(f"{MODULE_NAME}.Response") from openapi_python_client.parser.responses import response_from_data response = response_from_data(status_code=status_code, data=data) Response.assert_called_once_with(status_code=status_code) assert response == Response()
def test_response_from_data_unsupported_content_type(): from openapi_python_client.parser.responses import response_from_data data = oai.Response.construct(description="", content={"blah": None}) response, schemas = response_from_data(status_code=200, data=data, schemas=Schemas(), parent_name="parent") assert response == ParseError( data=data, detail="Unsupported content_type {'blah': None}")
def test_response_from_data_octet_stream(self, mocker): status_code = mocker.MagicMock(autospec=int) data = oai.Response.construct( content={ "application/octet-stream": oai.MediaType.construct(media_type_schema=mocker.MagicMock()) }) BytesResponse = mocker.patch(f"{MODULE_NAME}.BytesResponse") from openapi_python_client.parser.responses import response_from_data response = response_from_data(status_code=status_code, data=data) assert response == BytesResponse()
def test_response_from_dict_unsupported_type(self): from openapi_python_client.parser.errors import ParseError from openapi_python_client.parser.responses import response_from_data data = oai.Response.construct( content={ "text/html": oai.MediaType.construct(media_type_schema=oai.Schema.construct( type="BLAH")) }) assert response_from_data(status_code=200, data=data) == ParseError( data=data, detail="Unrecognized type BLAH")
def test_response_from_data_no_response_type(self, mocker): status_code = mocker.MagicMock(autospec=int) data = oai.Response.construct( content={ "application/json": oai.MediaType.construct(media_type_schema=oai.Schema.construct( type=None)) }) Response = mocker.patch(f"{MODULE_NAME}.Response") from openapi_python_client.parser.responses import response_from_data response = response_from_data(status_code=status_code, data=data) Response.assert_called_once_with(status_code=status_code) assert response == Response()
def test_response_from_data_basic(self, mocker): status_code = mocker.MagicMock(autospec=int) data = oai.Response.construct( content={ "text/html": oai.MediaType.construct(media_type_schema=oai.Schema.construct( type="string")) }) BasicResponse = mocker.patch(f"{MODULE_NAME}.BasicResponse") from openapi_python_client.parser.responses import response_from_data response = response_from_data(status_code=status_code, data=data) BasicResponse.assert_called_once_with(status_code=status_code, openapi_type="string") assert response == BasicResponse.return_value
def test_response_from_basic_array(self, mocker): status_code = mocker.MagicMock(autospec=int) data = oai.Response.construct( content={ "application/json": oai.MediaType.construct(media_type_schema=oai.Schema.construct( type="array", items=oai.Schema.construct(type="string"))) }) ListBasicResponse = mocker.patch(f"{MODULE_NAME}.ListBasicResponse") from openapi_python_client.parser.responses import response_from_data response = response_from_data(status_code=status_code, data=data) ListBasicResponse.assert_called_once_with(status_code=status_code, openapi_type="string") assert response == ListBasicResponse.return_value
def test_response_from_data_no_content(): from openapi_python_client.parser.responses import Response, response_from_data response, schemas = response_from_data( status_code=200, data=oai.Response.construct(description=""), schemas=Schemas(), parent_name="parent") assert response == Response( status_code=200, prop=NoneProperty(name="response_200", default=None, nullable=False, required=True), source="None", )
def test_response_from_data_array(self, mocker): ref = mocker.MagicMock() status_code = mocker.MagicMock(autospec=int) data = oai.Response.construct( content={ "application/json": oai.MediaType.construct(media_type_schema=oai.Schema.construct( type="array", items=oai.Reference.construct(ref=ref))) }) from_ref = mocker.patch(f"{MODULE_NAME}.Reference.from_ref") ListRefResponse = mocker.patch(f"{MODULE_NAME}.ListRefResponse") from openapi_python_client.parser.responses import response_from_data response = response_from_data(status_code=status_code, data=data) from_ref.assert_called_once_with(ref) ListRefResponse.assert_called_once_with(status_code=status_code, reference=from_ref()) assert response == ListRefResponse()
def test_response_from_data_no_content_schema(): from openapi_python_client.parser.responses import Response, response_from_data data = oai.Response.construct( description="", content={"application/json": oai.MediaType.construct()}) response, schemas = response_from_data(status_code=200, data=data, schemas=Schemas(), parent_name="parent", config=MagicMock()) assert response == Response( status_code=200, prop=NoneProperty(name="response_200", default=None, nullable=False, required=True), source="None", )
def test_response_from_data_reference(any_property_factory): from openapi_python_client.parser.responses import Response, response_from_data response, schemas = response_from_data( status_code=200, data=oai.Reference.construct(), schemas=Schemas(), parent_name="parent", config=MagicMock(), ) assert response == Response( status_code=200, prop=any_property_factory( name="response_200", default=None, nullable=False, required=True, ), source="None", )