From 548a87e9458f9d0d779750ca7f165b5fb12d4d37 Mon Sep 17 00:00:00 2001 From: Ritiek Malhotra Date: Tue, 1 Jan 2019 17:20:43 +0530 Subject: [PATCH] Re-add test for m3u files (removed accidently in #448) --- test/loader.py | 11 +++++++++++ test/test_download_with_metadata.py | 10 ++-------- test/test_youtube_tools.py | 30 +++++++++++++++++++++++------ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/test/loader.py b/test/loader.py index 33e288d..79c9771 100644 --- a/test/loader.py +++ b/test/loader.py @@ -1,6 +1,7 @@ from spotdl import const from spotdl import handle from spotdl import spotdl +import urllib import pytest @@ -13,3 +14,13 @@ def load_defaults(): spotdl.log = const.logzero.setup_logger( formatter=const._formatter, level=const.args.log_level ) + + +# GIST_URL is the monkeypatched version of: https://www.youtube.com/results?search_query=janji+-+heroes +# so that we get same results even if YouTube changes the list/order of videos on their page. +GIST_URL = "https://gist.githubusercontent.com/ritiek/e731338e9810e31c2f00f13c249a45f5/raw/c11a27f3b5d11a8d082976f1cdd237bd605ec2c2/search_results.html" + +def monkeypatch_youtube_search_page(*args, **kwargs): + fake_urlopen = urllib.request.urlopen(GIST_URL) + return fake_urlopen + diff --git a/test/test_download_with_metadata.py b/test/test_download_with_metadata.py index cd2340f..9c376c0 100644 --- a/test/test_download_with_metadata.py +++ b/test/test_download_with_metadata.py @@ -1,4 +1,3 @@ -import urllib import subprocess import os @@ -54,16 +53,11 @@ class TestFileFormat: assert title == EXPECTED_SPOTIFY_TITLE.replace(" ", "_") -def monkeypatch_youtube_search_page(*args, **kwargs): - fake_urlopen = urllib.request.urlopen(GIST_URL) - return fake_urlopen - - def test_youtube_url(metadata_fixture, monkeypatch): monkeypatch.setattr( youtube_tools.GenerateYouTubeURL, "_fetch_response", - monkeypatch_youtube_search_page, + loader.monkeypatch_youtube_search_page, ) url = youtube_tools.generate_youtube_url(SPOTIFY_TRACK_URL, metadata_fixture) assert url == EXPECTED_YOUTUBE_URL @@ -73,7 +67,7 @@ def test_youtube_title(metadata_fixture, monkeypatch): monkeypatch.setattr( youtube_tools.GenerateYouTubeURL, "_fetch_response", - monkeypatch_youtube_search_page, + loader.monkeypatch_youtube_search_page, ) content = youtube_tools.go_pafy(SPOTIFY_TRACK_URL, metadata_fixture) pytest.content_fixture = content diff --git a/test/test_youtube_tools.py b/test/test_youtube_tools.py index 4ddd258..364760e 100644 --- a/test/test_youtube_tools.py +++ b/test/test_youtube_tools.py @@ -17,7 +17,6 @@ YT_API_KEY = "AIzaSyAnItl3udec-Q1d5bkjKJGL-RgrKO_vU90" TRACK_SEARCH = "Tony's Videos VERY SHORT VIDEO 28.10.2016" EXPECTED_TITLE = TRACK_SEARCH EXPECTED_YT_URL = "http://youtube.com/watch?v=qOOcy2-tmbk" -EXPECTED_YT_URLS = (EXPECTED_YT_URL, "http://youtube.com/watch?v=5USR1Omo7f0") RESULT_COUNT_SEARCH = "she is still sleeping SAO" @@ -71,8 +70,7 @@ class TestYouTubeURL: def test_only_music_category(self, metadata_fixture): const.args.music_videos_only = True url = youtube_tools.generate_youtube_url(TRACK_SEARCH, metadata_fixture) - # YouTube keeps changing its results - assert url in EXPECTED_YT_URLS + assert url == EXPECTED_YT_URL def test_all_categories(self, metadata_fixture): const.args.music_videos_only = False @@ -131,19 +129,19 @@ class TestMetadataOrigin: assert metadata["spotify_metadata"] == metadata_type @pytest.mark.parametrize("track, metadata_type", MATCH_METADATA_NO_FALLBACK_TEST_TABLE) - def test_match_metadata_with_no_fallback(self, track, metadata_type, monkeypatch, content_fixture): + def test_match_metadata_with_no_fallback(self, track, metadata_type, content_fixture, monkeypatch): monkeypatch.setattr(youtube_tools, "go_pafy", lambda track, meta_tags: content_fixture) const.args.no_fallback_metadata = True self.match_metadata(track, metadata_type) @pytest.mark.parametrize("track, metadata_type", MATCH_METADATA_FALLBACK_TEST_TABLE) - def test_match_metadata_with_fallback(self, track, metadata_type, monkeypatch, content_fixture): + def test_match_metadata_with_fallback(self, track, metadata_type, content_fixture, monkeypatch): monkeypatch.setattr(youtube_tools, "go_pafy", lambda track, meta_tags: content_fixture) const.args.no_fallback_metadata = False self.match_metadata(track, metadata_type) @pytest.mark.parametrize("track, metadata_type", MATCH_METADATA_NO_METADATA_TEST_TABLE) - def test_match_metadata_with_no_metadata(self, track, metadata_type, monkeypatch, content_fixture): + def test_match_metadata_with_no_metadata(self, track, metadata_type, content_fixture, monkeypatch): monkeypatch.setattr(youtube_tools, "go_pafy", lambda track, meta_tags: content_fixture) const.args.no_metadata = True self.match_metadata(track, metadata_type) @@ -186,6 +184,26 @@ def test_check_exists(metadata_fixture, filename_fixture, tmpdir): assert check == expect_check +def test_generate_m3u(tmpdir, monkeypatch): + monkeypatch.setattr(youtube_tools.GenerateYouTubeURL, "_fetch_response", loader.monkeypatch_youtube_search_page) + expect_m3u = ( + "#EXTM3U\n\n" + "#EXTINF:208,Janji - Heroes Tonight (feat. Johnning) [NCS Release]\n" + "http://www.youtube.com/watch?v=3nQNiWdeH2Q\n" + "#EXTINF:226,Alan Walker - Spectre [NCS Release]\n" + "http://www.youtube.com/watch?v=AOeY-nDp7hI\n" + ) + m3u_track_file = os.path.join(str(tmpdir), "m3u_test.txt") + with open(m3u_track_file, "w") as track_file: + track_file.write("\nhttps://open.spotify.com/track/3SipFlNddvL0XNZRLXvdZD") + track_file.write("\nhttp://www.youtube.com/watch?v=AOeY-nDp7hI") + youtube_tools.generate_m3u(m3u_track_file) + m3u_file = "{}.m3u".format(m3u_track_file.split(".")[0]) + with open(m3u_file, "r") as m3u_in: + m3u = m3u_in.readlines() + assert "".join(m3u) == expect_m3u + + class TestDownload: def test_webm(self, content_fixture, filename_fixture, monkeypatch): # content_fixture does not have any .webm audiostream