Adding new tests using unit test module

This commit is contained in:
Rafael Vargas 2022-07-09 23:22:55 -03:00
parent 2dbc6c3984
commit 4fb9d8d1ba
5 changed files with 77 additions and 5 deletions

View File

@ -57,6 +57,9 @@ class Downloader():
@run_async @run_async
def extract_info(self, url: str) -> List[dict]: def extract_info(self, url: str) -> List[dict]:
if url == '':
return []
if Utils.is_url(url): # If Url if Utils.is_url(url): # If Url
options = Downloader.__YDL_OPTIONS_EXTRACT options = Downloader.__YDL_OPTIONS_EXTRACT
with YoutubeDL(options) as ydl: with YoutubeDL(options) as ydl:

View File

@ -11,10 +11,10 @@ class Song:
self.destroy() self.destroy()
return None return None
self.__usefull_keys = ['duration', self.__useful_keys = ['duration',
'title', 'webpage_url', 'title', 'webpage_url',
'channel', 'id', 'uploader', 'channel', 'id', 'uploader',
'thumbnail', 'original_url'] 'thumbnail', 'original_url']
self.__required_keys = ['url'] self.__required_keys = ['url']
for key in self.__required_keys: 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}') print(f'DEVELOPER NOTE -> {key} not found in info of music: {self.identifier}')
self.destroy() self.destroy()
for key in self.__usefull_keys: for key in self.__useful_keys:
if key in info.keys(): if key in info.keys():
self.__info[key] = info[key] self.__info[key] = info[key]

59
Tests/TestsDownload.py Normal file
View File

@ -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()

10
Tests/TestsHelper.py Normal file
View File

@ -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'

0
Tests/__init__.py Normal file
View File