Simplify tests

* Make them directly executable again
* Move common stuff (md5, parameters) to helper
* Never import *
* General clean up
This commit is contained in:
Philipp Hagemeister 2013-10-15 02:00:53 +02:00
parent a623df4c7b
commit 44a5f1718a
12 changed files with 154 additions and 123 deletions

View File

@ -1,22 +1,27 @@
import errno import errno
import io import io
import hashlib
import json import json
import os.path import os.path
import re import re
import types import types
import youtube_dl.extractor import youtube_dl.extractor
from youtube_dl import YoutubeDL, YoutubeDLHandler from youtube_dl import YoutubeDL
from youtube_dl.utils import (
compat_cookiejar,
compat_urllib_request,
)
youtube_dl._setup_opener(timeout=10)
PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json") def global_setup():
with io.open(PARAMETERS_FILE, encoding='utf-8') as pf: youtube_dl._setup_opener(timeout=10)
def get_params(override=None):
PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
"parameters.json")
with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
parameters = json.load(pf) parameters = json.load(pf)
if override:
parameters.update(override)
return parameters
def try_rm(filename): def try_rm(filename):
@ -32,7 +37,7 @@ class FakeYDL(YoutubeDL):
def __init__(self): def __init__(self):
# Different instances of the downloader can't share the same dictionary # Different instances of the downloader can't share the same dictionary
# some test set the "sublang" parameter, which would break the md5 checks. # some test set the "sublang" parameter, which would break the md5 checks.
params = dict(parameters) params = get_params()
super(FakeYDL, self).__init__(params) super(FakeYDL, self).__init__(params)
self.result = [] self.result = []
@ -62,3 +67,6 @@ def get_testcases():
for t in getattr(ie, '_TESTS', []): for t in getattr(ie, '_TESTS', []):
t['name'] = type(ie).__name__[:-len('IE')] t['name'] = type(ie).__name__[:-len('IE')]
yield t yield t
md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()

View File

@ -1,14 +1,16 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys
import unittest
# Allow direct execution # Allow direct execution
import os import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from test.helper import global_setup, try_rm
global_setup()
from youtube_dl import YoutubeDL from youtube_dl import YoutubeDL
from .helper import try_rm
def _download_restricted(url, filename, age): def _download_restricted(url, filename, age):

View File

@ -1,14 +1,20 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys
import unittest
# Allow direct execution # Allow direct execution
import os import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from test.helper import get_testcases
from youtube_dl.extractor import (
gen_extractors,
JustinTVIE,
YoutubeIE,
)
from youtube_dl.extractor import YoutubeIE, YoutubePlaylistIE, YoutubeChannelIE, JustinTVIE, gen_extractors
from .helper import get_testcases
class TestAllURLsMatching(unittest.TestCase): class TestAllURLsMatching(unittest.TestCase):
def setUp(self): def setUp(self):

View File

@ -1,18 +1,16 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys
import unittest
import hashlib
# Allow direct execution # Allow direct execution
import os import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from test.helper import FakeYDL, global_setup, md5
global_setup()
from youtube_dl.extractor import DailymotionIE from youtube_dl.extractor import DailymotionIE
from youtube_dl.utils import *
from .helper import FakeYDL
md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
class TestDailymotionSubtitles(unittest.TestCase): class TestDailymotionSubtitles(unittest.TestCase):
def setUp(self): def setUp(self):

View File

@ -1,26 +1,31 @@
#!/usr/bin/env python #!/usr/bin/env python
# Allow direct execution
import os
import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from test.helper import get_params, get_testcases, global_setup, try_rm, md5
global_setup()
import hashlib import hashlib
import io import io
import os
import json import json
import unittest
import sys
import socket import socket
import binascii
# Allow direct execution
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import youtube_dl.YoutubeDL import youtube_dl.YoutubeDL
from youtube_dl.utils import * from youtube_dl.utils import (
compat_str,
PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json") compat_urllib_error,
DownloadError,
ExtractorError,
UnavailableVideoError,
)
RETRIES = 3 RETRIES = 3
md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
class YoutubeDL(youtube_dl.YoutubeDL): class YoutubeDL(youtube_dl.YoutubeDL):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.to_stderr = self.to_screen self.to_stderr = self.to_screen
@ -37,18 +42,12 @@ def _file_md5(fn):
with open(fn, 'rb') as f: with open(fn, 'rb') as f:
return hashlib.md5(f.read()).hexdigest() return hashlib.md5(f.read()).hexdigest()
import test.helper as helper # Set up remaining global configuration
from .helper import get_testcases, try_rm
defs = get_testcases() defs = get_testcases()
with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
parameters = json.load(pf)
class TestDownload(unittest.TestCase): class TestDownload(unittest.TestCase):
maxDiff = None maxDiff = None
def setUp(self): def setUp(self):
self.parameters = parameters
self.defs = defs self.defs = defs
### Dynamically generate tests ### Dynamically generate tests
@ -68,8 +67,7 @@ def generator(test_case):
print_skipping(test_case['skip']) print_skipping(test_case['skip'])
return return
params = self.parameters.copy() params = get_params(test_case.get('params', {}))
params.update(test_case.get('params', {}))
ydl = YoutubeDL(params) ydl = YoutubeDL(params)
ydl.add_default_info_extractors() ydl.add_default_info_extractors()

View File

@ -1,13 +1,16 @@
#!/usr/bin/env python #!/usr/bin/env python
# encoding: utf-8 # encoding: utf-8
import sys
import unittest
import json
# Allow direct execution # Allow direct execution
import os import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from test.helper import FakeYDL, global_setup
global_setup()
from youtube_dl.extractor import ( from youtube_dl.extractor import (
DailymotionPlaylistIE, DailymotionPlaylistIE,
@ -18,9 +21,7 @@ from youtube_dl.extractor import (
LivestreamIE, LivestreamIE,
NHLVideocenterIE, NHLVideocenterIE,
) )
from youtube_dl.utils import *
from .helper import FakeYDL
class TestPlaylists(unittest.TestCase): class TestPlaylists(unittest.TestCase):
def assertIsPlaylist(self, info): def assertIsPlaylist(self, info):

View File

@ -1,14 +1,14 @@
#!/usr/bin/env python #!/usr/bin/env python
# Various small unit tests
import sys
import unittest
import xml.etree.ElementTree
# Allow direct execution # Allow direct execution
import os import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Various small unit tests
import xml.etree.ElementTree
#from youtube_dl.utils import htmlentity_transform #from youtube_dl.utils import htmlentity_transform
from youtube_dl.utils import ( from youtube_dl.utils import (

View File

@ -1,39 +1,38 @@
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
import xml.etree.ElementTree # Allow direct execution
import os import os
import sys import sys
import unittest import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Allow direct execution from test.helper import get_params, global_setup, try_rm
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) global_setup()
import io
import xml.etree.ElementTree
import youtube_dl.YoutubeDL import youtube_dl.YoutubeDL
import youtube_dl.extractor import youtube_dl.extractor
from youtube_dl.utils import * from youtube_dl.utils import True
from .helper import try_rm
PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
# General configuration (from __init__, not very elegant...)
jar = compat_cookiejar.CookieJar()
cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
proxy_handler = compat_urllib_request.ProxyHandler()
opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
compat_urllib_request.install_opener(opener)
class YoutubeDL(youtube_dl.YoutubeDL): class YoutubeDL(youtube_dl.YoutubeDL):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(YoutubeDL, self).__init__(*args, **kwargs) super(YoutubeDL, self).__init__(*args, **kwargs)
self.to_stderr = self.to_screen self.to_stderr = self.to_screen
with io.open(PARAMETERS_FILE, encoding='utf-8') as pf: params = get_params({
params = json.load(pf) 'writeannotations': True,
params['writeannotations'] = True 'skip_download': True,
params['skip_download'] = True 'writeinfojson': False,
params['writeinfojson'] = False 'format': 'flv',
params['format'] = 'flv' })
TEST_ID = 'gr51aVj-mLg' TEST_ID = 'gr51aVj-mLg'
ANNOTATIONS_FILE = TEST_ID + '.flv.annotations.xml' ANNOTATIONS_FILE = TEST_ID + '.flv.annotations.xml'

View File

@ -1,37 +1,34 @@
#!/usr/bin/env python #!/usr/bin/env python
# coding: utf-8 # coding: utf-8
import json # Allow direct execution
import os import os
import sys import sys
import unittest import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Allow direct execution from test.helper import get_params, global_setup
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) global_setup()
import io
import json
import youtube_dl.YoutubeDL import youtube_dl.YoutubeDL
import youtube_dl.extractor import youtube_dl.extractor
from youtube_dl.utils import *
PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
# General configuration (from __init__, not very elegant...)
jar = compat_cookiejar.CookieJar()
cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
proxy_handler = compat_urllib_request.ProxyHandler()
opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
compat_urllib_request.install_opener(opener)
class YoutubeDL(youtube_dl.YoutubeDL): class YoutubeDL(youtube_dl.YoutubeDL):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(YoutubeDL, self).__init__(*args, **kwargs) super(YoutubeDL, self).__init__(*args, **kwargs)
self.to_stderr = self.to_screen self.to_stderr = self.to_screen
with io.open(PARAMETERS_FILE, encoding='utf-8') as pf: params = get_params({
params = json.load(pf) 'writeinfojson': True,
params['writeinfojson'] = True 'skip_download': True,
params['skip_download'] = True 'writedescription': True,
params['writedescription'] = True })
TEST_ID = 'BaW_jenozKc' TEST_ID = 'BaW_jenozKc'
INFO_JSON_FILE = TEST_ID + '.mp4.info.json' INFO_JSON_FILE = TEST_ID + '.mp4.info.json'
@ -42,6 +39,7 @@ This is a test video for youtube-dl.
For more information, contact phihag@phihag.de .''' For more information, contact phihag@phihag.de .'''
class TestInfoJSON(unittest.TestCase): class TestInfoJSON(unittest.TestCase):
def setUp(self): def setUp(self):
# Clear old files # Clear old files

View File

@ -1,20 +1,26 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys
import unittest
import json
# Allow direct execution # Allow direct execution
import os import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from youtube_dl.extractor import YoutubeUserIE, YoutubePlaylistIE, YoutubeIE, YoutubeChannelIE, YoutubeShowIE from test.helper import FakeYDL, global_setup
from youtube_dl.utils import * global_setup()
from youtube_dl.extractor import (
YoutubeUserIE,
YoutubePlaylistIE,
YoutubeIE,
YoutubeChannelIE,
YoutubeShowIE,
)
from .helper import FakeYDL
class TestYoutubeLists(unittest.TestCase): class TestYoutubeLists(unittest.TestCase):
def assertIsPlaylist(self,info): def assertIsPlaylist(self, info):
"""Make sure the info has '_type' set to 'playlist'""" """Make sure the info has '_type' set to 'playlist'"""
self.assertEqual(info['_type'], 'playlist') self.assertEqual(info['_type'], 'playlist')

View File

@ -1,14 +1,18 @@
#!/usr/bin/env python #!/usr/bin/env python
# Allow direct execution
import os
import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from test.helper import global_setup
global_setup()
import io import io
import re import re
import string import string
import sys
import unittest
# Allow direct execution
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from youtube_dl.extractor import YoutubeIE from youtube_dl.extractor import YoutubeIE
from youtube_dl.utils import compat_str, compat_urlretrieve from youtube_dl.utils import compat_str, compat_urlretrieve

View File

@ -1,69 +1,79 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys
import unittest
import hashlib
# Allow direct execution # Allow direct execution
import os import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import sys
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from test.helper import FakeYDL, global_setup, md5
global_setup()
from youtube_dl.extractor import YoutubeIE from youtube_dl.extractor import YoutubeIE
from youtube_dl.utils import *
from .helper import FakeYDL
md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
class TestYoutubeSubtitles(unittest.TestCase): class TestYoutubeSubtitles(unittest.TestCase):
def setUp(self): def setUp(self):
self.DL = FakeYDL() self.DL = FakeYDL()
self.url = 'QRS8MkLhQmM' self.url = 'QRS8MkLhQmM'
def getInfoDict(self): def getInfoDict(self):
IE = YoutubeIE(self.DL) IE = YoutubeIE(self.DL)
info_dict = IE.extract(self.url) info_dict = IE.extract(self.url)
return info_dict return info_dict
def getSubtitles(self): def getSubtitles(self):
info_dict = self.getInfoDict() info_dict = self.getInfoDict()
return info_dict[0]['subtitles'] return info_dict[0]['subtitles']
def test_youtube_no_writesubtitles(self): def test_youtube_no_writesubtitles(self):
self.DL.params['writesubtitles'] = False self.DL.params['writesubtitles'] = False
subtitles = self.getSubtitles() subtitles = self.getSubtitles()
self.assertEqual(subtitles, None) self.assertEqual(subtitles, None)
def test_youtube_subtitles(self): def test_youtube_subtitles(self):
self.DL.params['writesubtitles'] = True self.DL.params['writesubtitles'] = True
subtitles = self.getSubtitles() subtitles = self.getSubtitles()
self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260') self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
def test_youtube_subtitles_lang(self): def test_youtube_subtitles_lang(self):
self.DL.params['writesubtitles'] = True self.DL.params['writesubtitles'] = True
self.DL.params['subtitleslangs'] = ['it'] self.DL.params['subtitleslangs'] = ['it']
subtitles = self.getSubtitles() subtitles = self.getSubtitles()
self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d') self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
def test_youtube_allsubtitles(self): def test_youtube_allsubtitles(self):
self.DL.params['writesubtitles'] = True self.DL.params['writesubtitles'] = True
self.DL.params['allsubtitles'] = True self.DL.params['allsubtitles'] = True
subtitles = self.getSubtitles() subtitles = self.getSubtitles()
self.assertEqual(len(subtitles.keys()), 13) self.assertEqual(len(subtitles.keys()), 13)
def test_youtube_subtitles_sbv_format(self): def test_youtube_subtitles_sbv_format(self):
self.DL.params['writesubtitles'] = True self.DL.params['writesubtitles'] = True
self.DL.params['subtitlesformat'] = 'sbv' self.DL.params['subtitlesformat'] = 'sbv'
subtitles = self.getSubtitles() subtitles = self.getSubtitles()
self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b') self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
def test_youtube_subtitles_vtt_format(self): def test_youtube_subtitles_vtt_format(self):
self.DL.params['writesubtitles'] = True self.DL.params['writesubtitles'] = True
self.DL.params['subtitlesformat'] = 'vtt' self.DL.params['subtitlesformat'] = 'vtt'
subtitles = self.getSubtitles() subtitles = self.getSubtitles()
self.assertEqual(md5(subtitles['en']), '356cdc577fde0c6783b9b822e7206ff7') self.assertEqual(md5(subtitles['en']), '356cdc577fde0c6783b9b822e7206ff7')
def test_youtube_list_subtitles(self): def test_youtube_list_subtitles(self):
self.DL.expect_warning(u'Video doesn\'t have automatic captions') self.DL.expect_warning(u'Video doesn\'t have automatic captions')
self.DL.params['listsubtitles'] = True self.DL.params['listsubtitles'] = True
info_dict = self.getInfoDict() info_dict = self.getInfoDict()
self.assertEqual(info_dict, None) self.assertEqual(info_dict, None)
def test_youtube_automatic_captions(self): def test_youtube_automatic_captions(self):
self.url = '8YoUxe5ncPo' self.url = '8YoUxe5ncPo'
self.DL.params['writeautomaticsub'] = True self.DL.params['writeautomaticsub'] = True
self.DL.params['subtitleslangs'] = ['it'] self.DL.params['subtitleslangs'] = ['it']
subtitles = self.getSubtitles() subtitles = self.getSubtitles()
self.assertTrue(subtitles['it'] is not None) self.assertTrue(subtitles['it'] is not None)
def test_youtube_nosubtitles(self): def test_youtube_nosubtitles(self):
self.DL.expect_warning(u'video doesn\'t have subtitles') self.DL.expect_warning(u'video doesn\'t have subtitles')
self.url = 'sAjKT8FhjI8' self.url = 'sAjKT8FhjI8'
@ -71,6 +81,7 @@ class TestYoutubeSubtitles(unittest.TestCase):
self.DL.params['allsubtitles'] = True self.DL.params['allsubtitles'] = True
subtitles = self.getSubtitles() subtitles = self.getSubtitles()
self.assertEqual(len(subtitles), 0) self.assertEqual(len(subtitles), 0)
def test_youtube_multiple_langs(self): def test_youtube_multiple_langs(self):
self.url = 'QRS8MkLhQmM' self.url = 'QRS8MkLhQmM'
self.DL.params['writesubtitles'] = True self.DL.params['writesubtitles'] = True