def test_get_by_id_raises_exception_if_internal_error(self, mock_get_by_id): # Arrange mock_absent_id = ObjectId() mock_get_by_id.side_effect = exceptions.ModelError("Error.") # Act + Assert with self.assertRaises(exceptions.ModelError): template_xsl_rendering_api.get_by_id(mock_absent_id)
def test_get_by_id_raises_exception_if_object_does_not_exist(self, mock_get_by_id): # Arrange mock_absent_id = ObjectId() mock_get_by_id.side_effect = exceptions.DoesNotExist("Error.") # Act + Assert with self.assertRaises(exceptions.DoesNotExist): template_xsl_rendering_api.get_by_id(mock_absent_id)
def patch(self, request, pk, xslt_id): """Remove a detail xslt from `TemplateXSLRendering` object from db Args: request: HTTP request pk: ObjectId xslt_id : XsltObjectId Returns: TemplateXSLRendering """ try: # Get objects template_xsl_rendering_object = template_xsl_rendering_api.get_by_id( pk) xsl_transformation_object = xsl_transformation_api.get_by_id( xslt_id) # Remove xsl transformation template_xsl_rendering_api.delete_detail_xslt( template_xsl_rendering_object, xsl_transformation_object) # Build serializer template_xsl_rendering_serializer = TemplateXslRenderingSerializer( instance=template_xsl_rendering_object) return Response(template_xsl_rendering_serializer.data, status=status.HTTP_200_OK) except Http404: content = {"message": "Data not found."} return Response(content, status=status.HTTP_404_NOT_FOUND) except Exception as api_exception: content = {"message": str(api_exception)} return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get(self, request, pk): """Get `TemplateXSLRendering` object from db Args: request: HTTP request pk: ObjectId Returns: TemplateXSLRendering """ try: # Get object template_xsl_rendering_object = template_xsl_rendering_api.get_by_id( pk) # Serialize object template_xsl_rendering_serializer = TemplateXslRenderingSerializer( template_xsl_rendering_object) # Return response return Response(template_xsl_rendering_serializer.data) except exceptions.DoesNotExist: content = {"message": "XSL rendering object not found."} return Response(content, status=status.HTTP_404_NOT_FOUND) except Exception as api_exception: content = {"message": str(api_exception)} return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def test_get_by_id_returns_object(self, mock_get_by_id): # Arrange mock_template_xsl_rendering = _create_mock_template_xsl_rendering() mock_template_xsl_rendering.id = ObjectId() mock_get_by_id.return_value = mock_template_xsl_rendering # Act result = template_xsl_rendering_api.get_by_id(mock_get_by_id.id) # Assert self.assertIsInstance(result, TemplateXslRendering)
def patch(self, request, pk): """Add detail xslt to `TemplateXSLRendering` object from db Args: request: HTTP request pk: ObjectId Parameters: { "ids": [ "xslt_id_1", "xslt_id_2" ] } Returns: TemplateXSLRendering """ try: # Get objects template_xsl_rendering_object = template_xsl_rendering_api.get_by_id( pk) xsl_transformation_objects = xsl_transformation_api.get_by_id_list( request.data["ids"]) # Add an xsl transformation template_xsl_rendering_api.set_list_detail_xslt( template_xsl_rendering_object, xsl_transformation_objects) # Build serializer template_xsl_rendering_serializer = TemplateXslRenderingSerializer( instance=template_xsl_rendering_object) return Response(template_xsl_rendering_serializer.data, status=status.HTTP_200_OK) except ValidationError as validation_exception: content = {"message": validation_exception.detail} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Http404: content = {"message": "Data not found."} return Response(content, status=status.HTTP_404_NOT_FOUND) except Exception as api_exception: content = {"message": str(api_exception)} return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def delete(self, request, pk): """Delete a TemplateXSLRendering Args: request: HTTP request pk: ObjectId Returns: - code: 204 content: Deletion successful - code: 403 content: Authentication error - code: 404 content: Object was not found - code: 500 content: Internal server error """ try: # Get object template_xsl_rendering_object = template_xsl_rendering_api.get_by_id( pk) # delete object template_xsl_rendering_api.delete(template_xsl_rendering_object) # Return response return Response(status=status.HTTP_204_NO_CONTENT) except exceptions.DoesNotExist: content = {"message": "Template XSL rendering not found."} return Response(content, status=status.HTTP_404_NOT_FOUND) except AccessControlError as ace: content = {"message": str(ace)} return Response(content, status=status.HTTP_403_FORBIDDEN) except Exception as api_exception: content = {"message": str(api_exception)} return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def patch(self, request, pk): """Edit `TemplateXSLRendering` object from db Args: request: HTTP request pk: ObjectId Returns: TemplateXSLRendering """ try: # Get object template_xsl_rendering_object = template_xsl_rendering_api.get_by_id( pk) # Build serializer template_xsl_rendering_serializer = TemplateXslRenderingSerializer( instance=template_xsl_rendering_object, data=request.data) # Validate data template_xsl_rendering_serializer.is_valid(True) # Save data template_xsl_rendering_serializer.save() return Response(template_xsl_rendering_serializer.data, status=status.HTTP_200_OK) except ValidationError as validation_exception: content = {"message": validation_exception.detail} return Response(content, status=status.HTTP_400_BAD_REQUEST) except Http404: content = {"message": "Data not found."} return Response(content, status=status.HTTP_404_NOT_FOUND) except Exception as api_exception: content = {"message": str(api_exception)} return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)