From 39ebd5f57e33ca3d425b75ebeaeb663d6bf8604e Mon Sep 17 00:00:00 2001 From: Ritiek Malhotra Date: Fri, 15 May 2020 15:00:51 +0530 Subject: [PATCH] Fix tests --- spotdl/command_line/tests/test_arguments.py | 36 ++++++-- spotdl/lyrics/providers/tests/test_genius.py | 2 +- .../metadata/providers/tests/test_youtube.py | 91 ++++++++++++++----- spotdl/metadata/providers/youtube.py | 3 + spotdl/tests/test_config.py | 2 +- spotdl/tests/test_util.py | 41 --------- 6 files changed, 97 insertions(+), 78 deletions(-) diff --git a/spotdl/command_line/tests/test_arguments.py b/spotdl/command_line/tests/test_arguments.py index b780453..80e9851 100644 --- a/spotdl/command_line/tests/test_arguments.py +++ b/spotdl/command_line/tests/test_arguments.py @@ -1,24 +1,37 @@ import spotdl.command_line.arguments +from spotdl.command_line.exceptions import ArgumentError +import logging import sys import pytest -def test_log_str_to_int(): - expect_levels = [20, 30, 40, 10] - levels = [spotdl.command_line.arguments.log_leveller(level) - for level in spotdl.command_line.arguments._LOG_LEVELS_STR] - assert levels == expect_levels +def test_logging_levels(): + expect_logging_levels = { + "INFO": logging.INFO, + "WARNING": logging.WARNING, + "DEBUG": logging.DEBUG, + "ERROR": logging.ERROR, + } + assert spotdl.command_line.arguments._LOG_LEVELS == expect_logging_levels class TestBadArguments: def test_error_m3u_without_list(self): - with pytest.raises(SystemExit): - spotdl.command_line.arguments.get_arguments(argv=("-t cool song", "--write-m3u")) + previous_argv = sys.argv + 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): - with pytest.raises(SystemExit): - spotdl.command_line.arguments.get_arguments(argv=("-t", "sekai all i had", "--write-to", "output.txt")) + previous_argv = sys.argv + 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: @@ -69,6 +82,9 @@ class TestArguments: assert arguments == expect_arguments def test_grouped_arguments(self): + previous_argv = sys.argv + sys.argv[1:] = [] with pytest.raises(SystemExit): - spotdl.command_line.arguments.get_arguments() + argument_handler = spotdl.command_line.arguments.get_arguments() + sys.argv[1:] = previous_argv[1:] diff --git a/spotdl/lyrics/providers/tests/test_genius.py b/spotdl/lyrics/providers/tests/test_genius.py index c9ac518..0008689 100644 --- a/spotdl/lyrics/providers/tests/test_genius.py +++ b/spotdl/lyrics/providers/tests/test_genius.py @@ -51,7 +51,7 @@ class TestGenius: expect_lyrics = "" 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: read_method = lambda: json.dumps({ "response": {"sections": [{"hits": [{"result": { diff --git a/spotdl/metadata/providers/tests/test_youtube.py b/spotdl/metadata/providers/tests/test_youtube.py index a07a935..a9b257d 100644 --- a/spotdl/metadata/providers/tests/test_youtube.py +++ b/spotdl/metadata/providers/tests/test_youtube.py @@ -1,5 +1,6 @@ from spotdl.metadata.providers.youtube import YouTubeSearch 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 ProviderYouTube from spotdl.metadata.exceptions import YouTubeMetadataNotFoundError @@ -36,18 +37,38 @@ def expect_search_results(): These are the expected search results for the "track" query. """ - return [ - "https://www.youtube.com/watch?v=cH4E_t3m3xM", - "https://www.youtube.com/watch?v=xrbY9gDVms0", - "https://www.youtube.com/watch?v=jX0n2rSmDbE", - "https://www.youtube.com/watch?v=nVzA1uWTydQ", - "https://www.youtube.com/watch?v=rQ6jcpwzQZU", - "https://www.youtube.com/watch?v=VY1eFxgRR-k", - "https://www.youtube.com/watch?v=j0AxZ4V5WQw", - "https://www.youtube.com/watch?v=zbWsb36U0uo", - "https://www.youtube.com/watch?v=3B1aY9Ob8r0", - "https://www.youtube.com/watch?v=hd2SGk90r9k", - ] + return YouTubeVideos([ + {'duration': '3:33', + 'title': 'Selena Gomez, Marshmello - Wolves', + 'url': 'https://www.youtube.com/watch?v=cH4E_t3m3xM'}, + {'duration': '3:18', + 'title': 'Selena Gomez, Marshmello - Wolves (Lyrics)', + 'url': 'https://www.youtube.com/watch?v=xrbY9gDVms0'}, + {'duration': '3:21', + 'title': 'Wolves - Selena Gomez, Marshmello (Lyrics)', + 'url': 'https://www.youtube.com/watch?v=jX0n2rSmDbE'}, + {'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") @@ -56,18 +77,38 @@ def expect_mock_search_results(): These are the expected mock search results for the "track" query. """ - return [ - "https://www.youtube.com/watch?v=cH4E_t3m3xM", - "https://www.youtube.com/watch?v=xrbY9gDVms0", - "https://www.youtube.com/watch?v=jX0n2rSmDbE", - "https://www.youtube.com/watch?v=rQ6jcpwzQZU", - "https://www.youtube.com/watch?v=nVzA1uWTydQ", - "https://www.youtube.com/watch?v=-grLLLTza6k", - "https://www.youtube.com/watch?v=zbWsb36U0uo", - "https://www.youtube.com/watch?v=rykH1BkGwTo", - "https://www.youtube.com/watch?v=j0AxZ4V5WQw", - "https://www.youtube.com/watch?v=RyxsaKfu-ZY", - ] + return YouTubeVideos([ + {'duration': '3:33', + 'title': 'Selena Gomez, Marshmello - Wolves', + 'url': 'https://www.youtube.com/watch?v=cH4E_t3m3xM'}, + {'duration': '3:18', + 'title': 'Selena Gomez, Marshmello - Wolves (Lyrics)', + 'url': 'https://www.youtube.com/watch?v=xrbY9gDVms0'}, + {'duration': '3:21', + 'title': 'Wolves - Selena Gomez, Marshmello (Lyrics)', + 'url': 'https://www.youtube.com/watch?v=jX0n2rSmDbE'}, + {'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: @@ -135,7 +176,7 @@ class TestYouTubeSearch: @pytest.mark.network def test_no_videos_search(self, no_result_track, youtube_searcher): 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): MockHTTPResponse.response_file = "youtube_no_search_results.html" diff --git a/spotdl/metadata/providers/youtube.py b/spotdl/metadata/providers/youtube.py index b5a0959..a7fc453 100644 --- a/spotdl/metadata/providers/youtube.py +++ b/spotdl/metadata/providers/youtube.py @@ -32,6 +32,9 @@ class YouTubeVideos(Sequence): def __getitem__(self, index): return self.videos[index] + def __eq__(self, instance): + return self.videos == instance.videos + def bestmatch(self): video = self.videos[0] logger.debug("Matched with: {title} ({url}) [{duration}]".format( diff --git a/spotdl/tests/test_config.py b/spotdl/tests/test_config.py index 1a0245d..f354a08 100644 --- a/spotdl/tests/test_config.py +++ b/spotdl/tests/test_config.py @@ -53,7 +53,7 @@ class TestDefaultConfigFile: class TestConfig: def test_default_config(self, config_path): 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 @pytest.mark.xfail diff --git a/spotdl/tests/test_util.py b/spotdl/tests/test_util.py index 277ebec..b1bd3a0 100644 --- a/spotdl/tests/test_util.py +++ b/spotdl/tests/test_util.py @@ -7,53 +7,12 @@ import spotdl.util 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") def directory_fixture(tmpdir_factory): dir_path = os.path.join(str(tmpdir_factory.mktemp("tmpdir")), "filter_this_directory") 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", [ ("0:23", 23), ("0:45", 45),