From 4fb9d8d1ba5500a3c4c67b92d2220f76cc9749bb Mon Sep 17 00:00:00 2001 From: Rafael Vargas Date: Sat, 9 Jul 2022 23:22:55 -0300 Subject: [PATCH] Adding new tests using unit test module --- Music/Downloader.py | 3 +++ Music/Song.py | 10 +++---- Tests/TestsDownload.py | 59 ++++++++++++++++++++++++++++++++++++++++++ Tests/TestsHelper.py | 10 +++++++ Tests/__init__.py | 0 5 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 Tests/TestsDownload.py create mode 100644 Tests/TestsHelper.py create mode 100644 Tests/__init__.py diff --git a/Music/Downloader.py b/Music/Downloader.py index 0c4a8af..f36fa1d 100644 --- a/Music/Downloader.py +++ b/Music/Downloader.py @@ -57,6 +57,9 @@ class Downloader(): @run_async def extract_info(self, url: str) -> List[dict]: + if url == '': + return [] + if Utils.is_url(url): # If Url options = Downloader.__YDL_OPTIONS_EXTRACT with YoutubeDL(options) as ydl: diff --git a/Music/Song.py b/Music/Song.py index b28f65c..6c407a0 100644 --- a/Music/Song.py +++ b/Music/Song.py @@ -11,10 +11,10 @@ class Song: self.destroy() return None - self.__usefull_keys = ['duration', - 'title', 'webpage_url', - 'channel', 'id', 'uploader', - 'thumbnail', 'original_url'] + self.__useful_keys = ['duration', + 'title', 'webpage_url', + 'channel', 'id', 'uploader', + 'thumbnail', 'original_url'] self.__required_keys = ['url'] for key in self.__required_keys: @@ -24,7 +24,7 @@ class Song: print(f'DEVELOPER NOTE -> {key} not found in info of music: {self.identifier}') self.destroy() - for key in self.__usefull_keys: + for key in self.__useful_keys: if key in info.keys(): self.__info[key] = info[key] diff --git a/Tests/TestsDownload.py b/Tests/TestsDownload.py new file mode 100644 index 0000000..1f93c42 --- /dev/null +++ b/Tests/TestsDownload.py @@ -0,0 +1,59 @@ +import asyncio +from typing import List +import unittest +from Music.Downloader import Downloader +from Music.Playlist import Playlist +from Music.Song import Song +from Tests.TestsHelper import TestsConstants + + +def myAsyncTest(coro): + def wrapper(*args, **kwargs): + loop = asyncio.new_event_loop() + asyncio.set_event_loop(None) + try: + return loop.run_until_complete(coro(*args, **kwargs)) + finally: + loop.close() + return wrapper + + +class TestDownloader(unittest.IsolatedAsyncioTestCase): + def __init__(self, methodName: str = ...) -> None: + self.downloader = Downloader() + self.constants = TestsConstants() + super().__init__(methodName) + + @myAsyncTest + async def test_emptyString(self): + musicsList = await self.downloader.extract_info('') + + self.assertEqual(musicsList, [], self.constants.EMPTY_STRING_ERROR_MSG) + + @myAsyncTest + async def test_YoutubeMusicUrl(self) -> None: + musicInfo = await self.downloader.extract_info(self.constants.YOUTUBE_MUSIC_URL) + + self.assertTrue(self.__infoExtractedSuccessfully(musicInfo)) + + def test_musicTitle(self): + playlist = Playlist() + song = Song(self.constants.MUSIC_TITLE_STRING, playlist, '') + playlist.add_song(song) + + self.downloader.finish_one_song(song) + self.assertFalse(song.problematic) + + def __downloadSucceeded(self, downloadReturn: List[dict]) -> bool: + # print(downloadReturn) + return True + + def __infoExtractedSuccessfully(self, info: List[dict]) -> bool: + if len(info) > 0: + return True + else: + return False + + +if __name__ == 'main': + unittest.main() diff --git a/Tests/TestsHelper.py b/Tests/TestsHelper.py new file mode 100644 index 0000000..c237842 --- /dev/null +++ b/Tests/TestsHelper.py @@ -0,0 +1,10 @@ +from Config.Configs import Singleton + + +class TestsConstants(Singleton): + def __init__(self) -> None: + if not super().created: + self.EMPTY_STRING_ERROR_MSG = 'Downloader with Empty String should be empty list.' + self.SPOTIFY_TRACK_URL = 'https://open.spotify.com/track/7wpnz7hje4FbnjZuWQtJHP' + self.MUSIC_TITLE_STRING = 'Experience || AMV || Anime Mix' + self.YOUTUBE_MUSIC_URL = 'https://www.youtube.com/watch?v=MvJoiv842mk' diff --git a/Tests/__init__.py b/Tests/__init__.py new file mode 100644 index 0000000..e69de29