Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement endpoint for deleting attachments by entity ID #105

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from

Conversation

VKTB
Copy link
Collaborator

@VKTB VKTB commented Jan 29, 2025

Description

This PR implements a DELETE endpoint at /attachments that accepts an entity_id query parameter and deletes all attachments with that entity_id.

Testing instructions

  • Review code
  • Check Actions build
  • Review changes to test coverage
  • Successfully deletes all attachments associated with the supplied entity_id
  • Returns 204 if entity_id is invalid or not associated with any attachments

Agile board tracking

closes #76

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.37%. Comparing base (ba67c36) to head (20329b5).
Report is 8 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #105      +/-   ##
===========================================
- Coverage    99.57%   99.37%   -0.20%     
===========================================
  Files           19       19              
  Lines          468      483      +15     
===========================================
+ Hits           466      480      +14     
- Misses           2        3       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@VKTB VKTB marked this pull request as ready for review January 29, 2025 12:48

:param entity_id: The entity ID of the attachments to delete.
:param session: PyMongo ClientSession to use for database operations.
:return:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:return:



class DeleteByEntityIdDSL(ListDSL):
"""Base class for delete tests."""
Copy link
Member

@rowan04 rowan04 Jan 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Base class for delete tests."""
"""Base class for delete_by_entity_id tests."""

Comment on lines +388 to +404
def test_delete_attachments_by_entity_id(self):
"""Test deleting attachments."""
attachments = self.post_test_attachments()
entity_id = attachments[0]["entity_id"]

self.delete_attachments_by_entity_id(entity_id)
self.check_delete_attachments_by_entity_id_success()

self.get_attachments(filters={"entity_id": entity_id})
self.check_get_attachments_success([])

def test_delete_attachments_by_entity_id_with_non_existent_id(self):
"""Test deleting attachments with a non-existent entity ID."""
self.delete_attachments_by_entity_id(str(ObjectId()))
self.check_delete_attachments_by_entity_id_success()

def test_delete_attachments_by_entity_id_with_invalid_id(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is attachments needed in each function name? could they just be e.g. test_delete_by_entity_id

for the code that relies on the `deleted_count` value to work.

:param collection_mock: Mocked MongoDB database collection instance.
:param deleted_count: The value to be assigned to the `deleted_count` attribute of the `DeleteResult` object
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:param deleted_count: The value to be assigned to the `deleted_count` attribute of the `DeleteResult` object
:param deleted_count: The value to be assigned to the `deleted_count` attribute of the `DeleteResult` object.

batch = object_keys[i : i + batch_size]
s3_client.delete_objects(
Bucket=object_storage_config.bucket_name.get_secret_value(),
Delete={"Objects": [{"Key": key for key in batch}]},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Delete={"Objects": [{"Key": key for key in batch}]},
Delete={"Objects": [{"Key": key } for key in batch]},

The current implementation works, but I'm not sure why it does?

The docs for the function requires the parameter to be structured as so:
image

If I modify the code to compare the 2 outputs of both options this is the result.
image
image

Again both implementation work, but I don't understand why yours works?

Comment on lines +100 to +105
try:
attachment_service.delete_by_entity_id(entity_id)
except InvalidObjectIdError:
# As this endpoint takes in a query parameter to delete multiple attachments, and to hide the database
# behaviour, we treat any invalid entity_id the same as a valid one that has no attachments associated to it.
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst it is fine to catch the exception in the router layer, I think it would be better if it was in the repo layer to match what we are doing in the rest of the repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add an endpoint for deleting attachments by entity ID
4 participants