mirror of
				https://github.com/KevinMidboe/spotify-downloader.git
				synced 2025-10-29 18:00:15 +00:00 
			
		
		
		
	Fix tests
This commit is contained in:
		@@ -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:]
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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": {
 | 
			
		||||
 
 | 
			
		||||
@@ -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"
 | 
			
		||||
 
 | 
			
		||||
@@ -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(
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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),
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user