def test_persistent_query_example_get_by_id_raises_model_error_if_not_found(self): # Arrange mock_user = create_mock_user("1") # Act # Assert with self.assertRaises(exceptions.ModelError): persistent_query_example_api.get_by_id("mock_id", mock_user)
def test_persistent_query_example_get_by_id_raises_does_not_exist_error_if_not_found( self, mock_get_by_id ): # Arrange mock_get_by_id.side_effect = exceptions.DoesNotExist(message="mock error") mock_user = create_mock_user("1") # Act # Assert with self.assertRaises(exceptions.DoesNotExist): persistent_query_example_api.get_by_id("mock_id", mock_user)
def patch(self, request, pk): """Update a persistent query example Parameters: { "content": "{}", "templates": ["5ea99316d26ebc48e475c60a"], "name": null } Args: request: HTTP request pk: ObjectId Returns: - code: 200 content: Updated data - code: 400 content: Validation error - code: 403 content: Forbidden - code: 404 content: Object was not found - code: 500 content: Internal server error """ try: # Get object persistent_query_example = persistent_query_example_api.get_by_id( pk, request.user) # Build serializer serializer = self.serializer( instance=persistent_query_example, data=request.data, partial=True, context={"request": request}, ) # Validate and save persistent query example serializer.is_valid(True) serializer.save() return Response(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 AccessControlError as e: content = {"message": str(e)} return Response(content, status=status.HTTP_403_FORBIDDEN) except NotUniqueError as e: content = {"message": str(e)} return Response(content, status=status.HTTP_400_BAD_REQUEST) except exceptions.DoesNotExist: content = {"message": "Persistent query example 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_as_anonymous_user(self): # Arrange persistent_query_example_id = self.fixture.persistent_query_example_1.id # Act # Assert if CAN_ANONYMOUS_ACCESS_PUBLIC_DOCUMENT: self.assertTrue( isinstance( persistent_query_example_api.get_by_id( persistent_query_example_id, AnonymousUser()), PersistentQueryExample, )) else: with self.assertRaises(AccessControlError): persistent_query_example_api.get_by_id( persistent_query_example_id, AnonymousUser())
def test_get_by_id_as_user_not_owner_returns_persistent_query_example( self): # Arrange persistent_query_example_id = self.fixture.persistent_query_example_1.id mock_user = create_mock_user("0") # Act # Assert self.assertTrue( isinstance( persistent_query_example_api.get_by_id( persistent_query_example_id, mock_user), PersistentQueryExample, ))
def test_get_by_id_as_superuser_returns_persistent_query_example(self): # Arrange persistent_query_example_id = self.fixture.persistent_query_example_1.id mock_user = create_mock_user("0", is_staff=True, is_superuser=True) # Act persistent_query_example = persistent_query_example_api.get_by_id( persistent_query_example_id, mock_user) # Assert self.assertTrue( isinstance(persistent_query_example, PersistentQueryExample))
def test_persistent_query_example_get_by_id_return_data_if_found( self, mock_get_by_id ): # Arrange expected_result = PersistentQueryExample(user_id="1") mock_get_by_id.return_value = expected_result mock_user = create_mock_user("1") # Act # Assert self.assertEqual( persistent_query_example_api.get_by_id("mock_id", mock_user), expected_result, )
def get(self, request, pk): """Retrieve persistent query example from database Args: request: HTTP request pk: ObjectId Returns: - code: 200 content: persistent query example - code: 403 content: Forbidden - code: 404 content: Object was not found - code: 500 content: Internal server error """ try: # Get object persistent_query_example = persistent_query_example_api.get_by_id( pk, request.user) # Serialize object serializer = self.serializer(persistent_query_example) # Return response return Response(serializer.data) except exceptions.DoesNotExist: content = {"message": "Object not found."} return Response(content, status=status.HTTP_404_NOT_FOUND) except AccessControlError as e: content = {"message": str(e)} 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 delete(self, request, pk): """Delete a persistent query example Args: request: HTTP request pk: ObjectId Returns: - code: 204 content: Deletion success - code: 403 content: Authentication error - code: 404 content: Object was not found - code: 500 content: Internal server error """ try: # Get object persistent_query_example = persistent_query_example_api.get_by_id( pk, request.user) # delete object persistent_query_example_api.delete(persistent_query_example, request.user) # Return response return Response(status=status.HTTP_204_NO_CONTENT) except AccessControlError as e: content = {"message": str(e)} return Response(content, status=status.HTTP_403_FORBIDDEN) except exceptions.DoesNotExist: content = {"message": "Persistent query example 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_persistent_query(persistent_query_id): return persistent_query_example_api.get_by_id(persistent_query_id)