diff --git a/pytest/modules/Audio/test_youtube.py b/pytest/modules/Audio/test_youtube.py index e2d9bcd..d52e5d3 100644 --- a/pytest/modules/Audio/test_youtube.py +++ b/pytest/modules/Audio/test_youtube.py @@ -1,5 +1,7 @@ +"""Tests for youtube.py""" + import unittest -from unittest.mock import patch, MagicMock +from unittest.mock import patch from src.modules.Audio.youtube import get_youtube_title from src.modules.Audio.youtube import download_and_convert_thumbnail @@ -14,7 +16,7 @@ class TestGetYoutubeTitle(unittest.TestCase): "title": " Test Artist - Test Track ", "channel": " Test Channel " } - url = " https://www.youtube.com/watch?v=dQw4w9WgXcQ " + url = " https://fakeUrl " # Act result = get_youtube_title(url) @@ -24,20 +26,16 @@ class TestGetYoutubeTitle(unittest.TestCase): mock_youtube_dl.assert_called_once() @patch("src.modules.Audio.youtube.yt_dlp.YoutubeDL") - @patch("src.modules.Audio.youtube.Image.open") - @patch("src.modules.Audio.youtube.os.path.join") - @patch("src.modules.Audio.youtube.crop_image_to_square") - def test_download_and_convert_thumbnail(self, mock_crop_image_to_square, mock_os_path_join, mock_image_open, mock_youtube_dl): + @patch("src.modules.Audio.youtube.save_image") + def test_download_and_convert_thumbnail(self, mock_save_image, mock_youtube_dl): # Arrange mock_youtube_dl.return_value.__enter__.return_value.extract_info.return_value = {"thumbnail": "test_thumbnail_url"} mock_youtube_dl.return_value.__enter__.return_value.urlopen.return_value.read.return_value = b"test_image_data" - mock_image = MagicMock() - mock_image.convert.return_value = mock_image - mock_image_open.return_value = mock_image - mock_os_path_join.return_value = "/path/to/output/test.jpg" + + mock_save_image.return_value = None ydl_opts = {} - url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" + url = "https://fakeUrl" clear_filename = "test" output_path = "/path/to/output" @@ -48,10 +46,6 @@ class TestGetYoutubeTitle(unittest.TestCase): mock_youtube_dl.assert_called_once_with(ydl_opts) mock_youtube_dl.return_value.__enter__.return_value.extract_info.assert_called_once_with(url, download=False) mock_youtube_dl.return_value.__enter__.return_value.urlopen.assert_called_once_with("test_thumbnail_url") - mock_image.convert.assert_called_once_with('RGB') - mock_os_path_join.assert_called_once_with(output_path, clear_filename + " [CO].jpg") - mock_image.save.assert_called_once_with("/path/to/output/test.jpg", "JPEG") - mock_crop_image_to_square.assert_called_once_with("/path/to/output/test.jpg") if __name__ == "__main__": unittest.main() \ No newline at end of file diff --git a/pytest/modules/Image/test_image_helper.py b/pytest/modules/Image/test_image_helper.py new file mode 100644 index 0000000..1edc264 --- /dev/null +++ b/pytest/modules/Image/test_image_helper.py @@ -0,0 +1,32 @@ +"""Tests for image_helper.py""" + +import unittest +from unittest.mock import patch, MagicMock +from src.modules.Image.image_helper import save_image + +class TestImageHelper(unittest.TestCase): + + @patch("src.modules.Image.image_helper.Image.open") + @patch("src.modules.Image.image_helper.os.path.join") + @patch("src.modules.Image.image_helper.crop_image_to_square") + def test_save_image(self, mock_crop_image_to_square, mock_os_path_join, mock_image_open): + # Arrange + mock_image = MagicMock() + mock_image.convert.return_value = mock_image + mock_image_open.return_value = mock_image + mock_os_path_join.return_value = "/path/to/output/test.jpg" + + clear_filename = "test" + output_path = "/path/to/output" + + # Act + save_image(b'fake_image_bytes', clear_filename, output_path) + + # Assert + mock_image.convert.assert_called_once_with('RGB') + mock_os_path_join.assert_called_once_with(output_path, clear_filename + " [CO].jpg") + mock_image.save.assert_called_once_with("/path/to/output/test.jpg", "JPEG") + mock_crop_image_to_square.assert_called_once_with("/path/to/output/test.jpg") + +if __name__ == "__main__": + unittest.main() \ No newline at end of file diff --git a/src/modules/Image/image_helper.py b/src/modules/Image/image_helper.py index 0c6d7fb..a87fc1d 100644 --- a/src/modules/Image/image_helper.py +++ b/src/modules/Image/image_helper.py @@ -11,10 +11,10 @@ def save_image(image_data: bytes, clear_filename: str, output_path: str) -> None image = image.convert('RGB') # Convert to RGB to avoid transparency or RGBA issues image_path = os.path.join(output_path, clear_filename + " [CO].jpg") image.save(image_path, "JPEG") - __crop_image_to_square(image_path) + crop_image_to_square(image_path) -def __crop_image_to_square(image_path): +def crop_image_to_square(image_path): image = Image.open(image_path) width, height = image.size size = min(width, height)