Skip to content

Commit

Permalink
Test:Changed parameter from 'url' to 'file_path' in function calls an…
Browse files Browse the repository at this point in the history
…d corresponding tests. Moved 'os.makedirs' and directory existence check from 'downsample_and_save_image' function to main function.
  • Loading branch information
zoeduan committed Oct 23, 2024
1 parent d07849d commit 305a02f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
6 changes: 5 additions & 1 deletion src/cautiousrobot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,18 @@ def download_images(data, img_dir, log_filepath, error_log_filepath, filename =
if os.path.exists(downsample_dir_path + "/" + image_name):
# Don't overwrite resized images either
continue

if not os.path.exists(downsample_dir_path):
os.makedirs(downsample_dir_path, exist_ok=False)

downsample_and_save_image(
image_dir_path=image_dir_path,
image_name=image_name,
downsample_dir_path=downsample_dir_path,
downsample_size=downsample,
log_errors=log_errors,
image_index=i,
url=url,
file_path=url,
error_log_filepath=error_log_filepath
)

Expand Down
12 changes: 6 additions & 6 deletions src/cautiousrobot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def process_csv(csv_path, expected_cols):

return df

def downsample_and_save_image(image_dir_path, image_name, downsample_dir_path, downsample_size, log_errors, image_index, url, error_log_filepath):
def downsample_and_save_image(image_dir_path, image_name, downsample_dir_path, downsample_size, log_errors, image_index, file_path, error_log_filepath):
"""
Downsample an image and save it to the specified directory.
Expand All @@ -58,14 +58,13 @@ def downsample_and_save_image(image_dir_path, image_name, downsample_dir_path, d
- downsample_size (int): The new size (both width and height) for the downsampled image.
- log_errors (dict): A dictionary to store errors encountered during the downsampling process.
- image_index (int): The index of the current image being processed, used for logging.
- url (str): The URL associated with the image, used for logging errors.
- file_path (str): The file path or URL associated with the image, used for logging errors.
- error_log_filepath (str): The file path where error logs are stored.
Returns:
None
"""
if not os.path.exists(downsample_dir_path):
os.makedirs(downsample_dir_path, exist_ok=False)


try:
img = Image.open(f"{image_dir_path}/{image_name}")
Expand All @@ -76,7 +75,8 @@ def downsample_and_save_image(image_dir_path, image_name, downsample_dir_path, d
log_errors,
index=image_index,
image="downsized_" + image_name,
url=url,
file_path=file_path,
response_code=str(e)
)
update_log(log=log_errors, index=image_index, filepath=error_log_filepath)
update_log(log=log_errors, index=image_index, filepath=error_log_filepath)

31 changes: 12 additions & 19 deletions tests/test_downsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setUp(self):
self.downsample_size = 100
self.log_errors = {} # Dictionary to store error logs
self.error_log_filepath = "error_log.json"
self.url = "http://example.com/image.jpg"
self.file_path = "file://example.com/image.jpg"

def tearDown(self):
if os.path.exists(self.image_dir_path):
Expand All @@ -24,15 +24,13 @@ def tearDown(self):
if os.path.exists(self.error_log_filepath):
os.remove(self.error_log_filepath)

def mock_log_response_side_effect(self, log_errors, index, image, url, response_code):
def mock_log_response_side_effect(self, log_errors, index, image, file_path, response_code):
"""Helper function to mimic the behavior of log_response."""
log_errors[index] = {'image': image, 'url': url, 'response_code': response_code}
log_errors[index] = {'image': image, 'file_path': file_path, 'response_code': response_code}
return log_errors

@patch("os.makedirs")
@patch("os.path.exists", return_value=False)
@patch("PIL.Image.open")
def test_downsample_and_save_image_success(self, mock_open, mock_exists, mock_makedirs):
def test_downsample_and_save_image_success(self, mock_open):
""" Test the successful downsampling and saving of an image. """

mock_image = MagicMock(spec=Image.Image)
Expand All @@ -49,21 +47,19 @@ def test_downsample_and_save_image_success(self, mock_open, mock_exists, mock_ma
self.downsample_size,
self.log_errors,
0, # image_index
self.url,
self.file_path,
self.error_log_filepath
)

mock_makedirs.assert_called_once_with(self.downsample_dir_path, exist_ok=False)
mock_open.assert_called_once_with(f"{self.image_dir_path}/test_image.jpg")
mock_image.resize.assert_called_once_with((self.downsample_size, self.downsample_size))
mock_resized_image.save.assert_called_once_with(f"{self.downsample_dir_path}/test_image.jpg")

@patch("os.makedirs")
@patch("os.path.exists", return_value=True)
@patch("PIL.Image.open", side_effect=FileNotFoundError("File not found"))
@patch("cautiousrobot.utils.log_response")
@patch("cautiousrobot.utils.update_log")
def test_downsample_and_save_image_file_not_found(self, mock_update_log, mock_log_response, mock_open, mock_exists, mock_makedirs):
def test_downsample_and_save_image_file_not_found(self, mock_update_log, mock_log_response, mock_open, mock_exists):
""" Test the behavior when the image file is not found. """

mock_log_response.side_effect = self.mock_log_response_side_effect
Expand All @@ -75,17 +71,16 @@ def test_downsample_and_save_image_file_not_found(self, mock_update_log, mock_lo
self.downsample_size,
self.log_errors,
0, # image_index
self.url,
self.file_path,
self.error_log_filepath
)

mock_makedirs.assert_not_called()
mock_open.assert_called_once_with(f"{self.image_dir_path}/missing_image.jpg")
mock_log_response.assert_called_once_with(
self.log_errors,
index=0,
image="downsized_missing_image.jpg",
url=self.url,
file_path=self.file_path,
response_code="File not found"
)
mock_update_log.assert_called_once_with(
Expand All @@ -98,12 +93,11 @@ def test_downsample_and_save_image_file_not_found(self, mock_update_log, mock_lo
self.assertIn(0, self.log_errors)
self.assertEqual(self.log_errors[0]['response_code'], "File not found")

@patch("os.makedirs")
@patch("os.path.exists", return_value=False)
@patch("PIL.Image.open", side_effect=Exception("Unexpected error"))
@patch("cautiousrobot.utils.log_response")
@patch("cautiousrobot.utils.update_log")
def test_downsample_and_save_image_unexpected_error(self, mock_update_log, mock_log_response, mock_open, mock_exists, mock_makedirs):
def test_downsample_and_save_image_unexpected_error(self, mock_update_log, mock_log_response, mock_open, mock_exists):
""" Test the behavior when an unexpected error occurs. """

mock_log_response.side_effect = self.mock_log_response_side_effect
Expand All @@ -115,17 +109,16 @@ def test_downsample_and_save_image_unexpected_error(self, mock_update_log, mock_
self.downsample_size,
self.log_errors,
1,
self.url,
self.file_path,
self.error_log_filepath
)

mock_makedirs.assert_called_once_with(self.downsample_dir_path, exist_ok=False)
mock_open.assert_called_once_with(f"{self.image_dir_path}/test_image.jpg")
mock_log_response.assert_called_once_with(
self.log_errors,
index=1,
image="downsized_test_image.jpg",
url=self.url,
file_path=self.file_path,
response_code="Unexpected error"
)
mock_update_log.assert_called_once_with(
Expand All @@ -138,4 +131,4 @@ def test_downsample_and_save_image_unexpected_error(self, mock_update_log, mock_
self.assertEqual(self.log_errors[1]['response_code'], "Unexpected error")

if __name__ == "__main__":
unittest.main()
unittest.main()

0 comments on commit 305a02f

Please sign in to comment.