Update tests (#384)

* Update tests

* Move comment regarding changing YT URLs to the appropriate assert
This commit is contained in:
Linus Groh
2018-10-02 12:13:21 +02:00
committed by Ritiek Malhotra
parent 6cb12722d0
commit 2725402ab3
7 changed files with 118 additions and 112 deletions

View File

@@ -1,6 +1,7 @@
from spotdl import const from spotdl import const
from spotdl import handle from spotdl import handle
from spotdl import spotdl from spotdl import spotdl
import pytest import pytest
@@ -10,5 +11,6 @@ def load_defaults():
const.args.log_level = 10 const.args.log_level = 10
spotdl.args = const.args spotdl.args = const.args
spotdl.log = const.logzero.setup_logger(formatter=const._formatter, spotdl.log = const.logzero.setup_logger(
level=const.args.log_level) formatter=const._formatter, level=const.args.log_level
)

View File

@@ -1,18 +1,20 @@
from spotdl import const
from spotdl import spotdl
import loader
import os import os
from spotdl import const
from spotdl import spotdl
import loader
loader.load_defaults() loader.load_defaults()
TRACK_URL = 'http://open.spotify.com/track/0JlS7BXXD07hRmevDnbPDU'
def test_dry_download_list(tmpdir): def test_dry_download_list(tmpdir):
song = 'http://open.spotify.com/track/0JlS7BXXD07hRmevDnbPDU'
const.args.folder = str(tmpdir) const.args.folder = str(tmpdir)
const.args.dry_run = True const.args.dry_run = True
file_path = os.path.join(const.args.folder, 'test_list.txt') file_path = os.path.join(const.args.folder, 'test_list.txt')
with open(file_path, 'w') as tin: with open(file_path, 'w') as f:
tin.write(song) f.write(TRACK_URL)
downloaded_song, *_ = spotdl.download_list(file_path) downloaded_song, *_ = spotdl.download_list(file_path)
assert downloaded_song == song assert downloaded_song == TRACK_URL

View File

@@ -1,12 +1,12 @@
import yaml import os
import sys
import argparse
from spotdl import handle from spotdl import handle
from spotdl import const from spotdl import const
import pytest import pytest
import os import yaml
import sys
import argparse
def test_log_str_to_int(): def test_log_str_to_int():

View File

@@ -1,11 +1,38 @@
from spotdl import internals
import sys import sys
import os import os
import subprocess import subprocess
from spotdl import internals
import pytest import pytest
DUPLICATE_TRACKS_TEST_TABLE = [
(('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ'),
('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',)),
(('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
'',
'https://open.spotify.com/track/3SipFlNddvL0XNZRLXvdZD'),
('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
'https://open.spotify.com/track/3SipFlNddvL0XNZRLXvdZD')),
(('ncs fade',
'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
'',
'ncs fade'),
('ncs fade',
'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ')),
(('ncs spectre ',
' https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
''),
('ncs spectre',
'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ'))
]
def test_default_music_directory(): def test_default_music_directory():
if sys.platform.startswith('linux'): if sys.platform.startswith('linux'):
output = subprocess.check_output(['xdg-user-dir', 'MUSIC']) output = subprocess.check_output(['xdg-user-dir', 'MUSIC'])
@@ -82,37 +109,11 @@ class TestGetSeconds:
internals.get_sec('02,28,46') internals.get_sec('02,28,46')
duplicate_tracks_test_table = [ @pytest.mark.parametrize("duplicates, expected", DUPLICATE_TRACKS_TEST_TABLE)
(('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ'),
('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',)),
(('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
'',
'https://open.spotify.com/track/3SipFlNddvL0XNZRLXvdZD'),
('https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
'https://open.spotify.com/track/3SipFlNddvL0XNZRLXvdZD')),
(('ncs fade',
'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
'',
'ncs fade'),
('ncs fade',
'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ')),
(('ncs spectre ',
' https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ',
''),
('ncs spectre',
'https://open.spotify.com/track/2DGa7iaidT5s0qnINlwMjJ'))
]
@pytest.mark.parametrize("duplicates, expected", duplicate_tracks_test_table)
def test_get_unique_tracks(tmpdir, duplicates, expected): def test_get_unique_tracks(tmpdir, duplicates, expected):
file_path = os.path.join(str(tmpdir), 'test_duplicates.txt') file_path = os.path.join(str(tmpdir), 'test_duplicates.txt')
with open(file_path, 'w') as tin: with open(file_path, 'w') as f:
tin.write('\n'.join(duplicates)) f.write('\n'.join(duplicates))
unique_tracks = internals.get_unique_tracks(file_path) unique_tracks = internals.get_unique_tracks(file_path)
assert tuple(unique_tracks) == expected assert tuple(unique_tracks) == expected

View File

@@ -1,28 +1,30 @@
from spotdl import spotify_tools
from spotdl import const
from spotdl import spotdl
import builtins import builtins
import os import os
from spotdl import spotify_tools
from spotdl import const
from spotdl import spotdl
PLAYLIST_URL = 'https://open.spotify.com/user/alex/playlist/0iWOVoumWlkXIrrBTSJmN8'
ALBUM_URL = 'https://open.spotify.com/album/499J8bIsEnU7DSrosFDJJg'
def test_user_playlists(tmpdir, monkeypatch): def test_user_playlists(tmpdir, monkeypatch):
expect_tracks = 14 expect_tracks = 14
text_file = os.path.join(str(tmpdir), 'test_us.txt') text_file = os.path.join(str(tmpdir), 'test_us.txt')
monkeypatch.setattr('builtins.input', lambda x: 1) monkeypatch.setattr('builtins.input', lambda x: 1)
spotify_tools.write_user_playlist('alex', text_file) spotify_tools.write_user_playlist('alex', text_file)
with open(text_file, 'r') as tin: with open(text_file, 'r') as f:
tracks = len(tin.readlines()) tracks = len(f.readlines())
assert tracks == expect_tracks assert tracks == expect_tracks
def test_playlist(tmpdir): def test_playlist(tmpdir):
expect_tracks = 14 expect_tracks = 14
text_file = os.path.join(str(tmpdir), 'test_pl.txt') text_file = os.path.join(str(tmpdir), 'test_pl.txt')
spotify_tools.write_playlist('https://open.spotify.com/user/alex/playlist/0iWOVoumWlkXIrrBTSJmN8', text_file) spotify_tools.write_playlist(PLAYLIST_URL, text_file)
with open(text_file, 'r') as tin: with open(text_file, 'r') as f:
tracks = len(tin.readlines()) tracks = len(f.readlines())
assert tracks == expect_tracks assert tracks == expect_tracks
@@ -30,9 +32,9 @@ def test_album(tmpdir):
expect_tracks = 15 expect_tracks = 15
global text_file global text_file
text_file = os.path.join(str(tmpdir), 'test_al.txt') text_file = os.path.join(str(tmpdir), 'test_al.txt')
spotify_tools.write_album('https://open.spotify.com/album/499J8bIsEnU7DSrosFDJJg', text_file) spotify_tools.write_album(ALBUM_URL, text_file)
with open(text_file, 'r') as tin: with open(text_file, 'r') as f:
tracks = len(tin.readlines()) tracks = len(f.readlines())
assert tracks == expect_tracks assert tracks == expect_tracks
@@ -47,4 +49,4 @@ def test_trim():
with open(text_file, 'r') as track_file: with open(text_file, 'r') as track_file:
number = len(track_file.readlines()) number = len(track_file.readlines())
assert (expect_number == number and expect_track == track) assert expect_number == number and expect_track == track

View File

@@ -1,3 +1,4 @@
import os
from spotdl import const from spotdl import const
from spotdl import internals from spotdl import internals
@@ -5,48 +6,46 @@ from spotdl import spotify_tools
from spotdl import youtube_tools from spotdl import youtube_tools
from spotdl import convert from spotdl import convert
from spotdl import metadata from spotdl import metadata
from spotdl import spotdl from spotdl import spotdl
import loader import loader
import os
loader.load_defaults() loader.load_defaults()
raw_song = 'http://open.spotify.com/track/0JlS7BXXD07hRmevDnbPDU'
TRACK_URL = 'http://open.spotify.com/track/0JlS7BXXD07hRmevDnbPDU'
EXPECTED_TITLE = 'David André Østby - Intro'
EXPECTED_YT_TITLE = 'Intro - David André Østby'
EXPECTED_YT_URL = 'http://youtube.com/watch?v=rg1wfcty0BA'
def test_metadata(): def test_metadata():
expect_number = 23 expect_number = 23
global meta_tags global meta_tags
meta_tags = spotify_tools.generate_metadata(raw_song) meta_tags = spotify_tools.generate_metadata(TRACK_URL)
assert len(meta_tags) == expect_number assert len(meta_tags) == expect_number
class TestFileFormat: class TestFileFormat:
def test_with_spaces(self): def test_with_spaces(self):
expect_title = 'David André Østby - Intro'
title = internals.format_string(const.args.file_format, meta_tags) title = internals.format_string(const.args.file_format, meta_tags)
assert title == expect_title assert title == EXPECTED_TITLE
def test_without_spaces(self): def test_without_spaces(self):
expect_title = 'David_André_Østby_-_Intro'
const.args.no_spaces = True const.args.no_spaces = True
title = internals.format_string(const.args.file_format, meta_tags) title = internals.format_string(const.args.file_format, meta_tags)
assert title == expect_title assert title == EXPECTED_TITLE.replace(' ', '_')
def test_youtube_url(): def test_youtube_url():
expect_url = 'http://youtube.com/watch?v=rg1wfcty0BA' url = youtube_tools.generate_youtube_url(TRACK_URL, meta_tags)
url = youtube_tools.generate_youtube_url(raw_song, meta_tags) assert url == EXPECTED_YT_URL
assert url == expect_url
def test_youtube_title(): def test_youtube_title():
expect_title = 'Intro - David André Østby'
global content global content
content = youtube_tools.go_pafy(raw_song, meta_tags) content = youtube_tools.go_pafy(TRACK_URL, meta_tags)
title = youtube_tools.get_youtube_title(content) title = youtube_tools.get_youtube_title(content)
assert title == expect_title assert title == EXPECTED_YT_TITLE
def test_check_track_exists_before_download(tmpdir): def test_check_track_exists_before_download(tmpdir):
@@ -56,7 +55,7 @@ def test_check_track_exists_before_download(tmpdir):
songname = internals.format_string(const.args.file_format, meta_tags) songname = internals.format_string(const.args.file_format, meta_tags)
global file_name global file_name
file_name = internals.sanitize_title(songname) file_name = internals.sanitize_title(songname)
check = spotdl.check_exists(file_name, raw_song, meta_tags) check = spotdl.check_exists(file_name, TRACK_URL, meta_tags)
assert check == expect_check assert check == expect_check
@@ -72,7 +71,7 @@ class TestDownload:
assert download == expect_download assert download == expect_download
class TestFFmpeg(): class TestFFmpeg:
def test_convert_from_webm_to_mp3(self): def test_convert_from_webm_to_mp3(self):
expect_return_code = 0 expect_return_code = 0
return_code = convert.song(file_name + '.webm', return_code = convert.song(file_name + '.webm',
@@ -149,6 +148,6 @@ class TestEmbedMetadata:
def test_check_track_exists_after_download(): def test_check_track_exists_after_download():
expect_check = True expect_check = True
check = spotdl.check_exists(file_name, raw_song, meta_tags) check = spotdl.check_exists(file_name, TRACK_URL, meta_tags)
os.remove(track_path + '.mp3') os.remove(track_path + '.mp3')
assert check == expect_check assert check == expect_check

View File

@@ -1,38 +1,47 @@
import os
import builtins
from spotdl import const from spotdl import const
from spotdl import internals from spotdl import internals
from spotdl import spotify_tools from spotdl import spotify_tools
from spotdl import youtube_tools from spotdl import youtube_tools
from spotdl import spotdl from spotdl import spotdl
import loader import loader
import os
import builtins
loader.load_defaults() loader.load_defaults()
raw_song = "Tony's Videos VERY SHORT VIDEO 28.10.2016"
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"
EXPECTED_YT_API_KEY = 'AIzaSyC6cEeKlxtOPybk9sEe5ksFN5sB-7wzYp0'
EXPECTED_YT_API_KEY_CUSTOM = 'some_api_key'
class TestYouTubeAPIKeys: class TestYouTubeAPIKeys:
def test_custom(self): def test_custom(self):
expect_key = 'some_api_key' const.args.youtube_api_key = EXPECTED_YT_API_KEY_CUSTOM
const.args.youtube_api_key = expect_key
youtube_tools.set_api_key() youtube_tools.set_api_key()
key = youtube_tools.pafy.g.api_key key = youtube_tools.pafy.g.api_key
assert key == expect_key assert key == EXPECTED_YT_API_KEY_CUSTOM
def test_default(self): def test_default(self):
expect_key = 'AIzaSyC6cEeKlxtOPybk9sEe5ksFN5sB-7wzYp0'
const.args.youtube_api_key = None const.args.youtube_api_key = None
youtube_tools.set_api_key() youtube_tools.set_api_key()
key = youtube_tools.pafy.g.api_key key = youtube_tools.pafy.g.api_key
assert key == expect_key assert key == EXPECTED_YT_API_KEY
def test_metadata(): def test_metadata():
expect_metadata = None expect_metadata = None
global metadata global metadata
metadata = spotify_tools.generate_metadata(raw_song) metadata = spotify_tools.generate_metadata(TRACK_SEARCH)
assert metadata == expect_metadata assert metadata == expect_metadata
@@ -40,15 +49,13 @@ class TestArgsManualResultCount:
# Regresson test for issue #264 # Regresson test for issue #264
def test_scrape(self): def test_scrape(self):
const.args.manual = True const.args.manual = True
url = youtube_tools.GenerateYouTubeURL("she is still sleeping SAO", url = youtube_tools.GenerateYouTubeURL(RESULT_COUNT_SEARCH, meta_tags=None)
meta_tags=None)
video_ids = url.scrape(bestmatch=False) video_ids = url.scrape(bestmatch=False)
# Web scraping gives us all videos on the 1st page # Web scraping gives us all videos on the 1st page
assert len(video_ids) == 20 assert len(video_ids) == 20
def test_api(self): def test_api(self):
url = youtube_tools.GenerateYouTubeURL("she is still sleeping SAO", url = youtube_tools.GenerateYouTubeURL(RESULT_COUNT_SEARCH, meta_tags=None)
meta_tags=None)
video_ids = url.api(bestmatch=False) video_ids = url.api(bestmatch=False)
const.args.manual = False const.args.manual = False
# API gives us 50 videos (or as requested) # API gives us 50 videos (or as requested)
@@ -57,30 +64,26 @@ class TestArgsManualResultCount:
class TestYouTubeURL: class TestYouTubeURL:
def test_only_music_category(self): def test_only_music_category(self):
# YouTube keeps changing its results
expect_urls = ('http://youtube.com/watch?v=qOOcy2-tmbk',
'http://youtube.com/watch?v=5USR1Omo7f0')
const.args.music_videos_only = True const.args.music_videos_only = True
url = youtube_tools.generate_youtube_url(raw_song, metadata) url = youtube_tools.generate_youtube_url(TRACK_SEARCH, metadata)
assert url in expect_urls # YouTube keeps changing its results
assert url in EXPECTED_YT_URLS
def test_all_categories(self): def test_all_categories(self):
expect_url = 'http://youtube.com/watch?v=qOOcy2-tmbk'
const.args.music_videos_only = False const.args.music_videos_only = False
url = youtube_tools.generate_youtube_url(raw_song, metadata) url = youtube_tools.generate_youtube_url(TRACK_SEARCH, metadata)
assert url == expect_url assert url == EXPECTED_YT_URL
def test_args_manual(self, monkeypatch): def test_args_manual(self, monkeypatch):
expect_url = 'http://youtube.com/watch?v=qOOcy2-tmbk'
const.args.manual = True const.args.manual = True
monkeypatch.setattr('builtins.input', lambda x: '1') monkeypatch.setattr('builtins.input', lambda x: '1')
url = youtube_tools.generate_youtube_url(raw_song, metadata) url = youtube_tools.generate_youtube_url(TRACK_SEARCH, metadata)
assert url == expect_url assert url == EXPECTED_YT_URL
def test_args_manual_none(self, monkeypatch): def test_args_manual_none(self, monkeypatch):
expect_url = None expect_url = None
monkeypatch.setattr('builtins.input', lambda x: '0') monkeypatch.setattr('builtins.input', lambda x: '0')
url = youtube_tools.generate_youtube_url(raw_song, metadata) url = youtube_tools.generate_youtube_url(TRACK_SEARCH, metadata)
const.args.manual = False const.args.manual = False
assert url == expect_url assert url == expect_url
@@ -89,21 +92,18 @@ class TestYouTubeTitle:
def test_single_download_with_youtube_api(self): def test_single_download_with_youtube_api(self):
global content global content
global title global title
expect_title = "Tony's Videos VERY SHORT VIDEO 28.10.2016" const.args.youtube_api_key = YT_API_KEY
key = 'AIzaSyAnItl3udec-Q1d5bkjKJGL-RgrKO_vU90'
const.args.youtube_api_key = key
youtube_tools.set_api_key() youtube_tools.set_api_key()
content = youtube_tools.go_pafy(raw_song, metadata) content = youtube_tools.go_pafy(TRACK_SEARCH, metadata)
title = youtube_tools.get_youtube_title(content) title = youtube_tools.get_youtube_title(content)
assert title == expect_title assert title == EXPECTED_TITLE
def test_download_from_list_without_youtube_api(self): def test_download_from_list_without_youtube_api(self):
expect_title = "1. Tony's Videos VERY SHORT VIDEO 28.10.2016"
const.args.youtube_api_key = None const.args.youtube_api_key = None
youtube_tools.set_api_key() youtube_tools.set_api_key()
content = youtube_tools.go_pafy(raw_song, metadata) content = youtube_tools.go_pafy(TRACK_SEARCH, metadata)
title = youtube_tools.get_youtube_title(content, 1) title = youtube_tools.get_youtube_title(content, 1)
assert title == expect_title assert title == "1. {0}".format(EXPECTED_TITLE)
def test_check_exists(tmpdir): def test_check_exists(tmpdir):
@@ -112,7 +112,7 @@ def test_check_exists(tmpdir):
# prerequisites for determining filename # prerequisites for determining filename
global file_name global file_name
file_name = internals.sanitize_title(title) file_name = internals.sanitize_title(title)
check = spotdl.check_exists(file_name, raw_song, metadata) check = spotdl.check_exists(file_name, TRACK_SEARCH, metadata)
assert check == expect_check assert check == expect_check