Skip to content

Commit

Permalink
Write unit tests for new repo method #104
Browse files Browse the repository at this point in the history
  • Loading branch information
VKTB committed Feb 11, 2025
1 parent 88f10b0 commit df1c2b0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
10 changes: 5 additions & 5 deletions test/unit/repositories/test_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,6 @@ def test_delete_invalid_id(self):
self.check_delete_failed_with_exception(f"Invalid ObjectId value '{attachment_id}'")


# pylint: enable=duplicate-code


class DeleteByEntityIdDSL(AttachmentRepoDSL):
"""Base class for `delete_by_entity_id` tests."""

Expand Down Expand Up @@ -418,7 +415,7 @@ def check_delete_by_entity_id_success(self, assert_delete: bool = True) -> None:


class TestDeleteByEntityId(DeleteByEntityIdDSL):
"""Tests for deleting attachments by entity ID."""
"""Tests for deleting attachments by `entity_id`."""

def test_delete_by_entity_id(self):
"""Test deleting attachments."""
Expand All @@ -427,13 +424,16 @@ def test_delete_by_entity_id(self):
self.check_delete_by_entity_id_success()

def test_delete_by_entity_id_invalid_id(self):
"""Test deleting attachments with an invalid entity ID."""
"""Test deleting attachments with an invalid `entity_id`."""
entity_id = "invalid-id"

self.call_delete_by_entity_id(entity_id)
self.check_delete_by_entity_id_success(False)


# pylint: enable=duplicate-code


class UpdateDSL(AttachmentRepoDSL):
"""Base class for `update` tests."""

Expand Down
62 changes: 58 additions & 4 deletions test/unit/repositories/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class GetDSL(ImageRepoDSL):
_obtained_image_out: ImageOut
_get_exception: pytest.ExceptionInfo

def mock_get(self, image_id: str, image_in_data: dict) -> None:
def mock_get(self, image_id: str, image_in_data: Optional[dict] = None) -> None:
"""
Mocks database methods appropriately to test the `get` repo method.
Expand Down Expand Up @@ -180,15 +180,15 @@ def test_get_with_non_existent_id(self):

image_id = str(ObjectId())

self.mock_get(image_id, None)
self.mock_get(image_id)
self.call_get_expecting_error(image_id, MissingRecordError)
self.check_get_failed_with_exception(f"No image found with ID: {image_id}", True)

def test_get_with_invalid_id(self):
"""Test getting an image with an invalid image ID."""
image_id = "invalid-id"

self.mock_get(image_id, None)
self.mock_get(image_id)
self.call_get_expecting_error(image_id, InvalidObjectIdError)
self.check_get_failed_with_exception(f"Invalid ObjectId value '{image_id}'")

Expand Down Expand Up @@ -299,7 +299,7 @@ def test_list_with_entity_id_with_no_results(self):
self.check_list_success()

def test_list_with_invalid_id_returns_empty_list(self):
"""Test listing all attachments with an invalid `entity_id` argument returns an empty list."""
"""Test listing all images with an invalid `entity_id` argument returns an empty list."""

entity_id = "invalid-id"

Expand Down Expand Up @@ -535,4 +535,58 @@ def test_delete_invalid_id(self):
self.check_delete_failed_with_exception(f"Invalid ObjectId value '{image_id}'")


class DeleteByEntityIdDSL(ImageRepoDSL):
"""Base class for `delete_by_entity_id` tests."""

_delete_entity_id: str
_delete_by_entity_id_exception: pytest.ExceptionInfo

def mock_delete_by_entity_id(self, deleted_count: int) -> None:
"""
Mocks database methods appropriately to test the `delete_by_entity_id` repo method.
:param deleted_count: Number of documents deleted successfully.
"""
RepositoryTestHelpers.mock_delete_many(self.images_collection, deleted_count)

def call_delete_by_entity_id(self, entity_id: str) -> None:
"""
Calls the `ImageRepo` `delete_by_entity_id` method.
:param entity_id: The entity ID of the images to be deleted.
"""
self._delete_entity_id = entity_id
self.image_repository.delete_by_entity_id(entity_id, session=self.mock_session)

def check_delete_by_entity_id_success(self, assert_delete: bool = True) -> None:
"""
Checks that a prior call to `call_delete_by_entity_id` worked as expected.
:param assert_delete: Whether the `delete_many` method is expected to be called or not.
"""
if assert_delete:
self.images_collection.delete_many.assert_called_once_with(
filter={"entity_id": ObjectId(self._delete_entity_id)}, session=self.mock_session
)
else:
self.images_collection.delete_many.assert_not_called()


class TestDeleteByEntityId(DeleteByEntityIdDSL):
"""Tests for deleting images by `entity_id`."""

def test_delete_by_entity_id(self):
"""Test deleting images."""
self.mock_delete_by_entity_id(3)
self.call_delete_by_entity_id(str(ObjectId()))
self.check_delete_by_entity_id_success()

def test_delete_by_entity_id_invalid_id(self):
"""Test deleting images with an invalid `entity_id`."""
entity_id = "invalid-id"

self.call_delete_by_entity_id(entity_id)
self.check_delete_by_entity_id_success(False)


# pylint: enable=duplicate-code

0 comments on commit df1c2b0

Please sign in to comment.