Skip to content

Commit

Permalink
reviewed testing implementation: TODO add file name as editable #35
Browse files Browse the repository at this point in the history
  • Loading branch information
asuresh-code committed Dec 9, 2024
1 parent 59eab0f commit 9a5c324
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 68 deletions.
6 changes: 3 additions & 3 deletions test/e2e/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,10 +348,10 @@ def check_patch_image_failed_with_detail(self, status_code: int, detail: str) ->


class TestUpdate(UpdateDSL):
"""Tests for updating a image."""
"""Tests for updating an image."""

def test_partial_update_all_fields(self):
"""Test updating every field of a image."""
"""Test updating every field of an image."""
image_id = self.post_image(IMAGE_POST_METADATA_DATA_ALL_VALUES, "image.jpg")
self.patch_image(image_id, IMAGE_PATCH_METADATA_DATA_ALL_VALUES_B)
self.check_patch_image_success(IMAGE_GET_METADATA_ALL_VALUES_B)
Expand All @@ -362,6 +362,6 @@ def test_partial_update_with_non_existent_id(self):
self.check_patch_image_failed_with_detail(404, "Image not found")

def test_partial_update_invalid_id(self):
"""Test updating a image with an invalid ID."""
"""Test updating an image with an invalid ID."""
self.patch_image("invalid-id", {})
self.check_patch_image_failed_with_detail(404, "Image not found")
15 changes: 7 additions & 8 deletions test/unit/repositories/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,27 +282,26 @@ class UpdateDSL(ImageRepoDSL):
_updated_image: ImageOut
_update_exception: pytest.ExceptionInfo

def set_update_data(self, image_in_data: dict):
def set_update_data(self, new_image_in_data: dict):
"""
Assigns the update data to use during a call to `call_update`.
:param image_in_data: New image data as would be required for an `ImageIn` database model to supply to the
:param new_image_in_data: New image data as would be required for an `ImageIn` database model to supply to the
`ImageRepo` `update` method.
"""
self._image_in = ImageIn(**image_in_data)
self._image_in = ImageIn(**new_image_in_data)

def mock_update(
self,
image_in_data: dict,
new_image_in_data: dict,
) -> None:
"""
Mocks database methods appropriately to test the `update` repo method.
:param image_id: ID of the image that will be updated.
:param image_in_data: Dictionary containing the new image data as would be required for an `ImageIn` database
model (i.e. no created and modified times required).
:param new_image_in_data: Dictionary containing the new image data as would be required for an `ImageIn`
database model (i.e. no created and modified times required).
"""
self.set_update_data(image_in_data)
self.set_update_data(new_image_in_data)

self._expected_image_out = ImageOut(**self._image_in.model_dump())
RepositoryTestHelpers.mock_find_one(self.images_collection, self._expected_image_out.model_dump(by_alias=True))
Expand Down
69 changes: 12 additions & 57 deletions test/unit/services/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,16 @@ class UpdateDSL(ImageServiceDSL):
_expected_image_out: ImageOut
_updated_image_id: str
_updated_image: MagicMock
_update_exception: pytest.ExceptionInfo

def mock_update(self, image_id: str, image_patch_data: dict, stored_image_post_data: Optional[dict]) -> None:
def mock_update(self, image_patch_data: dict, stored_image_post_data: Optional[dict]) -> None:
"""
Mocks the repository methods appropriately to test the `update` service method.
:param image_id: ID of the image to be updated.
:param image_patch_data: Dictionary containing the patch data as would be required for a
`ImagePatchMetadataSchema` (i.e. no ID, or created and modified times required).
`ImagePatchMetadataSchema` (i.e. no created and modified times required).
:param stored_image_post_data: Dictionary containing the image data for the existing stored
image as would be required for `ImagePostMetadataSchema` (i.e. no ID, or created and modified
image as would be required for `ImagePostMetadataSchema` (i.e. no created and modified
times required).
"""
# Stored image
Expand All @@ -290,31 +289,23 @@ def mock_update(self, image_id: str, image_patch_data: dict, stored_image_post_d
if stored_image_post_data
else None
)
if self._stored_image is not None:
self.mock_image_repository.get.return_value = self._stored_image
else:
self.mock_image_repository.get.side_effect = MissingRecordError(
detail=f"No image found with ID: {image_id}", entity_name="image"
)
self.mock_image_repository.get.return_value = self._stored_image

# Patch schema
self._image_patch = ImagePatchMetadataSchema(**image_patch_data)

# Construct the expected input for the repository
merged_image_data = {**(stored_image_post_data or {}), **image_patch_data}
self._expected_image_in = ImageIn(**merged_image_data)

# Updated image
image_out = (
ImageOut(
**ImageIn(**{**stored_image_post_data, **image_patch_data}).model_dump(),
)
if stored_image_post_data
else None
image_out = ImageOut(
**self._expected_image_in.model_dump(),
)
self.mock_image_repository.update.return_value = image_out

self._expected_image_out = ImageMetadataSchema(**image_out.model_dump()) if stored_image_post_data else None
self.mock_image_repository.update.return_value = image_out

# Construct the expected input for the repository
merged_image_data = {**(stored_image_post_data or {}), **image_patch_data}
self._expected_image_in = ImageIn(**merged_image_data) if stored_image_post_data else None
self._expected_image_out = ImageMetadataSchema(**image_out.model_dump())

def call_update(self, image_id: str) -> None:
"""
Expand All @@ -325,18 +316,6 @@ def call_update(self, image_id: str) -> None:
self._updated_image_id = image_id
self._updated_image = self.image_service.update(image_id, self._image_patch)

def call_update_expecting_error(self, image_id: str, error_type: type[BaseException]) -> None:
"""
Class the `ImageService` `update` method with the appropriate data from a prior call to `mock_update`
while expecting an error to be raised.
:param image_id: ID of the image to be updated.
:param error_type: Expected exception to be raised.
"""
with pytest.raises(error_type) as exc:
self.image_service.update(image_id, self._image_patch)
self._update_exception = exc

def check_update_success(self) -> None:
"""Checks that a prior call to `call_update` worked as updated."""
# Ensure obtained old image
Expand All @@ -349,16 +328,6 @@ def check_update_success(self) -> None:

assert self._updated_image == self._expected_image_out

def check_update_failed_with_exception(self, message: str):
"""
Checks that a prior call to `call_update_expecting_error` worked as expected, raising an exception with the
correct message.
:param message: Expected message of the raised exception.
"""
self.mock_image_repository.update.assert_not_called()
assert str(self._update_exception.value) == message


class TestUpdate(UpdateDSL):
"""Tests for updating a image."""
Expand All @@ -368,22 +337,8 @@ def test_update(self):
image_id = str(ObjectId())

self.mock_update(
image_id,
image_patch_data=IMAGE_PATCH_METADATA_DATA_ALL_VALUES_A,
stored_image_post_data=IMAGE_IN_DATA_ALL_VALUES,
)
print(self._expected_image_in)
self.call_update(image_id)
self.check_update_success()

def test_update_with_non_existent_id(self):
"""Test updating a image with a non-existent ID."""
image_id = str(ObjectId())

self.mock_update(
image_id,
image_patch_data=IMAGE_PATCH_METADATA_DATA_ALL_VALUES_A,
stored_image_post_data=None,
)
self.call_update_expecting_error(image_id, MissingRecordError)
self.check_update_failed_with_exception(f"No image found with ID: {image_id}")

0 comments on commit 9a5c324

Please sign in to comment.