Fix tests

This commit is contained in:
Ritiek Malhotra
2020-05-15 15:00:51 +05:30
parent e71989a963
commit 39ebd5f57e
6 changed files with 97 additions and 78 deletions

View File

@@ -1,24 +1,37 @@
import spotdl.command_line.arguments import spotdl.command_line.arguments
from spotdl.command_line.exceptions import ArgumentError
import logging
import sys import sys
import pytest import pytest
def test_log_str_to_int(): def test_logging_levels():
expect_levels = [20, 30, 40, 10] expect_logging_levels = {
levels = [spotdl.command_line.arguments.log_leveller(level) "INFO": logging.INFO,
for level in spotdl.command_line.arguments._LOG_LEVELS_STR] "WARNING": logging.WARNING,
assert levels == expect_levels "DEBUG": logging.DEBUG,
"ERROR": logging.ERROR,
}
assert spotdl.command_line.arguments._LOG_LEVELS == expect_logging_levels
class TestBadArguments: class TestBadArguments:
def test_error_m3u_without_list(self): def test_error_m3u_without_list(self):
with pytest.raises(SystemExit): previous_argv = sys.argv
spotdl.command_line.arguments.get_arguments(argv=("-t cool song", "--write-m3u")) sys.argv[1:] = ["-s", "cool song", "--write-m3u"]
argument_handler = spotdl.command_line.arguments.get_arguments()
with pytest.raises(ArgumentError):
argument_handler.run_errands()
sys.argv[1:] = previous_argv[1:]
def test_write_to_error(self): def test_write_to_error(self):
with pytest.raises(SystemExit): previous_argv = sys.argv
spotdl.command_line.arguments.get_arguments(argv=("-t", "sekai all i had", "--write-to", "output.txt")) sys.argv[1:] = ["-s", "sekai all i had", "--write-to", "output.txt"]
argument_handler = spotdl.command_line.arguments.get_arguments()
with pytest.raises(ArgumentError):
argument_handler.run_errands()
sys.argv[1:] = previous_argv[1:]
class TestArguments: class TestArguments:
@@ -69,6 +82,9 @@ class TestArguments:
assert arguments == expect_arguments assert arguments == expect_arguments
def test_grouped_arguments(self): def test_grouped_arguments(self):
previous_argv = sys.argv
sys.argv[1:] = []
with pytest.raises(SystemExit): with pytest.raises(SystemExit):
spotdl.command_line.arguments.get_arguments() argument_handler = spotdl.command_line.arguments.get_arguments()
sys.argv[1:] = previous_argv[1:]

View File

@@ -51,7 +51,7 @@ class TestGenius:
expect_lyrics = "" expect_lyrics = ""
def __init__(self, request, timeout=None): def __init__(self, request, timeout=None):
search_results_url = "https://genius.com/api/search/multi?per_page=1&q=selena+gomez+wolves" search_results_url = "https://genius.com/api/search/multi?per_page=1&q=selena%2Bgomez%2Bwolves"
if request._full_url == search_results_url: if request._full_url == search_results_url:
read_method = lambda: json.dumps({ read_method = lambda: json.dumps({
"response": {"sections": [{"hits": [{"result": { "response": {"sections": [{"hits": [{"result": {

View File

@@ -1,5 +1,6 @@
from spotdl.metadata.providers.youtube import YouTubeSearch from spotdl.metadata.providers.youtube import YouTubeSearch
from spotdl.metadata.providers.youtube import YouTubeStreams from spotdl.metadata.providers.youtube import YouTubeStreams
from spotdl.metadata.providers.youtube import YouTubeVideos
from spotdl.metadata.providers import youtube from spotdl.metadata.providers import youtube
from spotdl.metadata.providers import ProviderYouTube from spotdl.metadata.providers import ProviderYouTube
from spotdl.metadata.exceptions import YouTubeMetadataNotFoundError from spotdl.metadata.exceptions import YouTubeMetadataNotFoundError
@@ -36,18 +37,38 @@ def expect_search_results():
These are the expected search results for the "track" These are the expected search results for the "track"
query. query.
""" """
return [ return YouTubeVideos([
"https://www.youtube.com/watch?v=cH4E_t3m3xM", {'duration': '3:33',
"https://www.youtube.com/watch?v=xrbY9gDVms0", 'title': 'Selena Gomez, Marshmello - Wolves',
"https://www.youtube.com/watch?v=jX0n2rSmDbE", 'url': 'https://www.youtube.com/watch?v=cH4E_t3m3xM'},
"https://www.youtube.com/watch?v=nVzA1uWTydQ", {'duration': '3:18',
"https://www.youtube.com/watch?v=rQ6jcpwzQZU", 'title': 'Selena Gomez, Marshmello - Wolves (Lyrics)',
"https://www.youtube.com/watch?v=VY1eFxgRR-k", 'url': 'https://www.youtube.com/watch?v=xrbY9gDVms0'},
"https://www.youtube.com/watch?v=j0AxZ4V5WQw", {'duration': '3:21',
"https://www.youtube.com/watch?v=zbWsb36U0uo", 'title': 'Wolves - Selena Gomez, Marshmello (Lyrics)',
"https://www.youtube.com/watch?v=3B1aY9Ob8r0", 'url': 'https://www.youtube.com/watch?v=jX0n2rSmDbE'},
"https://www.youtube.com/watch?v=hd2SGk90r9k", {'duration': '6:26',
] 'title': 'Selena Gomez and Marshmello - Wolves (Official) Extended',
'url': 'https://www.youtube.com/watch?v=rQ6jcpwzQZU'},
{'duration': '3:43',
'title': 'Selena Gomez, Marshmello - Wolves (Vertical Video)',
'url': 'https://www.youtube.com/watch?v=nVzA1uWTydQ'},
{'duration': '3:18',
'title': 'Selena Gomez, Marshmello - Wolves (Visualizer)',
'url': 'https://www.youtube.com/watch?v=-grLLLTza6k'},
{'duration': '1:32',
'title': 'Wolves - Selena Gomez, Marshmello / Jun Liu Choreography',
'url': 'https://www.youtube.com/watch?v=zbWsb36U0uo'},
{'duration': '3:17',
'title': 'Selena Gomez, Marshmello - Wolves (Lyrics)',
'url': 'https://www.youtube.com/watch?v=rykH1BkGwTo'},
{'duration': '3:16',
'title': 'Selena Gomez, Marshmello - Wolves (8D AUDIO)',
'url': 'https://www.youtube.com/watch?v=j0AxZ4V5WQw'},
{'duration': '3:47',
'title': 'Selena Gomez, Marshmello - Wolves (Vanrip Remix)',
'url': 'https://www.youtube.com/watch?v=RyxsaKfu-ZY'}
])
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
@@ -56,18 +77,38 @@ def expect_mock_search_results():
These are the expected mock search results for the These are the expected mock search results for the
"track" query. "track" query.
""" """
return [ return YouTubeVideos([
"https://www.youtube.com/watch?v=cH4E_t3m3xM", {'duration': '3:33',
"https://www.youtube.com/watch?v=xrbY9gDVms0", 'title': 'Selena Gomez, Marshmello - Wolves',
"https://www.youtube.com/watch?v=jX0n2rSmDbE", 'url': 'https://www.youtube.com/watch?v=cH4E_t3m3xM'},
"https://www.youtube.com/watch?v=rQ6jcpwzQZU", {'duration': '3:18',
"https://www.youtube.com/watch?v=nVzA1uWTydQ", 'title': 'Selena Gomez, Marshmello - Wolves (Lyrics)',
"https://www.youtube.com/watch?v=-grLLLTza6k", 'url': 'https://www.youtube.com/watch?v=xrbY9gDVms0'},
"https://www.youtube.com/watch?v=zbWsb36U0uo", {'duration': '3:21',
"https://www.youtube.com/watch?v=rykH1BkGwTo", 'title': 'Wolves - Selena Gomez, Marshmello (Lyrics)',
"https://www.youtube.com/watch?v=j0AxZ4V5WQw", 'url': 'https://www.youtube.com/watch?v=jX0n2rSmDbE'},
"https://www.youtube.com/watch?v=RyxsaKfu-ZY", {'duration': '6:26',
] 'title': 'Selena Gomez and Marshmello - Wolves (Official) Extended',
'url': 'https://www.youtube.com/watch?v=rQ6jcpwzQZU'},
{'duration': '3:43',
'title': 'Selena Gomez, Marshmello - Wolves (Vertical Video)',
'url': 'https://www.youtube.com/watch?v=nVzA1uWTydQ'},
{'duration': '3:18',
'title': 'Selena Gomez, Marshmello - Wolves (Visualizer)',
'url': 'https://www.youtube.com/watch?v=-grLLLTza6k'},
{'duration': '1:32',
'title': 'Wolves - Selena Gomez, Marshmello / Jun Liu Choreography',
'url': 'https://www.youtube.com/watch?v=zbWsb36U0uo'},
{'duration': '3:17',
'title': 'Selena Gomez, Marshmello - Wolves (Lyrics)',
'url': 'https://www.youtube.com/watch?v=rykH1BkGwTo'},
{'duration': '3:16',
'title': 'Selena Gomez, Marshmello - Wolves (8D AUDIO)',
'url': 'https://www.youtube.com/watch?v=j0AxZ4V5WQw'},
{'duration': '3:47',
'title': 'Selena Gomez, Marshmello - Wolves (Vanrip Remix)',
'url': 'https://www.youtube.com/watch?v=RyxsaKfu-ZY'}
])
class MockHTTPResponse: class MockHTTPResponse:
@@ -135,7 +176,7 @@ class TestYouTubeSearch:
@pytest.mark.network @pytest.mark.network
def test_no_videos_search(self, no_result_track, youtube_searcher): def test_no_videos_search(self, no_result_track, youtube_searcher):
results = youtube_searcher.search(no_result_track) results = youtube_searcher.search(no_result_track)
assert results == [] assert results == YouTubeVideos([])
def test_mock_no_videos_search(self, no_result_track, youtube_searcher, monkeypatch): def test_mock_no_videos_search(self, no_result_track, youtube_searcher, monkeypatch):
MockHTTPResponse.response_file = "youtube_no_search_results.html" MockHTTPResponse.response_file = "youtube_no_search_results.html"

View File

@@ -32,6 +32,9 @@ class YouTubeVideos(Sequence):
def __getitem__(self, index): def __getitem__(self, index):
return self.videos[index] return self.videos[index]
def __eq__(self, instance):
return self.videos == instance.videos
def bestmatch(self): def bestmatch(self):
video = self.videos[0] video = self.videos[0]
logger.debug("Matched with: {title} ({url}) [{duration}]".format( logger.debug("Matched with: {title} ({url}) [{duration}]".format(

View File

@@ -53,7 +53,7 @@ class TestDefaultConfigFile:
class TestConfig: class TestConfig:
def test_default_config(self, config_path): def test_default_config(self, config_path):
expect_config = spotdl.config.DEFAULT_CONFIGURATION["spotify-downloader"] expect_config = spotdl.config.DEFAULT_CONFIGURATION["spotify-downloader"]
config = spotdl.config.get_config(config_path) config = spotdl.config.get_config(config_path)["spotify-downloader"]
assert config == expect_config assert config == expect_config
@pytest.mark.xfail @pytest.mark.xfail

View File

@@ -7,53 +7,12 @@ import spotdl.util
import pytest import pytest
def test_default_music_directory():
if sys.platform.startswith("linux"):
output = subprocess.check_output(["xdg-user-dir", "MUSIC"])
expect_directory = output.decode("utf-8").rstrip()
else:
home = os.path.expanduser("~")
expect_directory = os.path.join(home, "Music")
directory = spotdl.util.get_music_dir()
assert directory == expect_directory
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def directory_fixture(tmpdir_factory): def directory_fixture(tmpdir_factory):
dir_path = os.path.join(str(tmpdir_factory.mktemp("tmpdir")), "filter_this_directory") dir_path = os.path.join(str(tmpdir_factory.mktemp("tmpdir")), "filter_this_directory")
return dir_path return dir_path
class TestPathFilterer:
def test_create_directory(self, directory_fixture):
expect_path = True
spotdl.util.filter_path(directory_fixture)
is_path = os.path.isdir(directory_fixture)
assert is_path == expect_path
def test_remove_temp_files(self, directory_fixture):
expect_file = False
file_path = os.path.join(directory_fixture, "pesky_file.temp")
open(file_path, "a")
spotdl.util.filter_path(directory_fixture)
is_file = os.path.isfile(file_path)
assert is_file == expect_file
@pytest.mark.parametrize("sec_duration, str_duration", [
(35, "35"),
(23, "23"),
(158, "2:38"),
(263, "4:23"),
(4562, "1:16:02"),
(26765, "7:26:05"),
])
def test_video_time_from_seconds(sec_duration, str_duration):
duration = spotdl.util.videotime_from_seconds(sec_duration)
assert duration == str_duration
@pytest.mark.parametrize("str_duration, sec_duration", [ @pytest.mark.parametrize("str_duration, sec_duration", [
("0:23", 23), ("0:23", 23),
("0:45", 45), ("0:45", 45),