diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/helper.py | 2 | ||||
| -rw-r--r-- | test/parameters.json | 3 | ||||
| -rw-r--r-- | test/test_YoutubeDL.py | 104 | ||||
| -rw-r--r-- | test/test_aes.py | 4 | ||||
| -rw-r--r-- | test/test_compat.py | 25 | ||||
| -rw-r--r-- | test/test_download.py | 2 | ||||
| -rw-r--r-- | test/test_subtitles.py | 2 | ||||
| -rw-r--r-- | test/test_utils.py | 95 | 
8 files changed, 169 insertions, 68 deletions
diff --git a/test/helper.py b/test/helper.py index 12afdf184..e1129e58f 100644 --- a/test/helper.py +++ b/test/helper.py @@ -150,7 +150,7 @@ def expect_info_dict(self, got_dict, expected_dict):                               'invalid value for field %s, expected %r, got %r' % (info_field, expected, got))      # Check for the presence of mandatory fields -    if got_dict.get('_type') != 'playlist': +    if got_dict.get('_type') not in ('playlist', 'multi_video'):          for key in ('id', 'url', 'title', 'ext'):              self.assertTrue(got_dict.get(key), 'Missing mandatory field %s' % key)      # Check for mandatory fields that are automatically set by YoutubeDL diff --git a/test/parameters.json b/test/parameters.json index cbff9bd16..7bf59c25f 100644 --- a/test/parameters.json +++ b/test/parameters.json @@ -7,8 +7,7 @@      "forcethumbnail": false,       "forcetitle": false,       "forceurl": false,  -    "format": null,  -    "format_limit": null,  +    "format": "best",      "ignoreerrors": false,       "listformats": null,       "logtostderr": false,  diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index 652519831..a13c09ef4 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -12,6 +12,7 @@ import copy  from test.helper import FakeYDL, assertRegexpMatches  from youtube_dl import YoutubeDL +from youtube_dl.compat import compat_str  from youtube_dl.extractor import YoutubeIE  from youtube_dl.postprocessor.common import PostProcessor  from youtube_dl.utils import match_filter_func @@ -101,39 +102,6 @@ class TestFormatSelection(unittest.TestCase):          downloaded = ydl.downloaded_info_dicts[0]          self.assertEqual(downloaded['ext'], 'flv') -    def test_format_limit(self): -        formats = [ -            {'format_id': 'meh', 'url': 'http://example.com/meh', 'preference': 1}, -            {'format_id': 'good', 'url': 'http://example.com/good', 'preference': 2}, -            {'format_id': 'great', 'url': 'http://example.com/great', 'preference': 3}, -            {'format_id': 'excellent', 'url': 'http://example.com/exc', 'preference': 4}, -        ] -        info_dict = _make_result(formats) - -        ydl = YDL() -        ydl.process_ie_result(info_dict) -        downloaded = ydl.downloaded_info_dicts[0] -        self.assertEqual(downloaded['format_id'], 'excellent') - -        ydl = YDL({'format_limit': 'good'}) -        assert ydl.params['format_limit'] == 'good' -        ydl.process_ie_result(info_dict.copy()) -        downloaded = ydl.downloaded_info_dicts[0] -        self.assertEqual(downloaded['format_id'], 'good') - -        ydl = YDL({'format_limit': 'great', 'format': 'all'}) -        ydl.process_ie_result(info_dict.copy()) -        self.assertEqual(ydl.downloaded_info_dicts[0]['format_id'], 'meh') -        self.assertEqual(ydl.downloaded_info_dicts[1]['format_id'], 'good') -        self.assertEqual(ydl.downloaded_info_dicts[2]['format_id'], 'great') -        self.assertTrue('3' in ydl.msgs[0]) - -        ydl = YDL() -        ydl.params['format_limit'] = 'excellent' -        ydl.process_ie_result(info_dict.copy()) -        downloaded = ydl.downloaded_info_dicts[0] -        self.assertEqual(downloaded['format_id'], 'excellent') -      def test_format_selection(self):          formats = [              {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': TEST_URL}, @@ -270,7 +238,7 @@ class TestFormatSelection(unittest.TestCase):              f2['url'] = 'url:' + f2id              info_dict = _make_result([f1, f2], extractor='youtube') -            ydl = YDL() +            ydl = YDL({'format': 'best/bestvideo'})              yie = YoutubeIE(ydl)              yie._sort_formats(info_dict['formats'])              ydl.process_ie_result(info_dict) @@ -278,7 +246,7 @@ class TestFormatSelection(unittest.TestCase):              self.assertEqual(downloaded['format_id'], f1id)              info_dict = _make_result([f2, f1], extractor='youtube') -            ydl = YDL() +            ydl = YDL({'format': 'best/bestvideo'})              yie = YoutubeIE(ydl)              yie._sort_formats(info_dict['formats'])              ydl.process_ie_result(info_dict) @@ -443,27 +411,36 @@ class TestYoutubeDL(unittest.TestCase):              def run(self, info):                  with open(audiofile, 'wt') as f:                      f.write('EXAMPLE') -                info['filepath'] -                return False, info +                return [info['filepath']], info -        def run_pp(params): +        def run_pp(params, PP):              with open(filename, 'wt') as f:                  f.write('EXAMPLE')              ydl = YoutubeDL(params) -            ydl.add_post_processor(SimplePP()) +            ydl.add_post_processor(PP())              ydl.post_process(filename, {'filepath': filename}) -        run_pp({'keepvideo': True}) +        run_pp({'keepvideo': True}, SimplePP)          self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename)          self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)          os.unlink(filename)          os.unlink(audiofile) -        run_pp({'keepvideo': False}) +        run_pp({'keepvideo': False}, SimplePP)          self.assertFalse(os.path.exists(filename), '%s exists' % filename)          self.assertTrue(os.path.exists(audiofile), '%s doesn\'t exist' % audiofile)          os.unlink(audiofile) +        class ModifierPP(PostProcessor): +            def run(self, info): +                with open(info['filepath'], 'wt') as f: +                    f.write('MODIFIED') +                return [], info + +        run_pp({'keepvideo': False}, ModifierPP) +        self.assertTrue(os.path.exists(filename), '%s doesn\'t exist' % filename) +        os.unlink(filename) +      def test_match_filter(self):          class FilterYDL(YDL):              def __init__(self, *args, **kwargs): @@ -531,6 +508,51 @@ class TestYoutubeDL(unittest.TestCase):          res = get_videos(f)          self.assertEqual(res, ['1']) +    def test_playlist_items_selection(self): +        entries = [{ +            'id': compat_str(i), +            'title': compat_str(i), +            'url': TEST_URL, +        } for i in range(1, 5)] +        playlist = { +            '_type': 'playlist', +            'id': 'test', +            'entries': entries, +            'extractor': 'test:playlist', +            'extractor_key': 'test:playlist', +            'webpage_url': 'http://example.com', +        } + +        def get_ids(params): +            ydl = YDL(params) +            # make a copy because the dictionary can be modified +            ydl.process_ie_result(playlist.copy()) +            return [int(v['id']) for v in ydl.downloaded_info_dicts] + +        result = get_ids({}) +        self.assertEqual(result, [1, 2, 3, 4]) + +        result = get_ids({'playlistend': 10}) +        self.assertEqual(result, [1, 2, 3, 4]) + +        result = get_ids({'playlistend': 2}) +        self.assertEqual(result, [1, 2]) + +        result = get_ids({'playliststart': 10}) +        self.assertEqual(result, []) + +        result = get_ids({'playliststart': 2}) +        self.assertEqual(result, [2, 3, 4]) + +        result = get_ids({'playlist_items': '2-4'}) +        self.assertEqual(result, [2, 3, 4]) + +        result = get_ids({'playlist_items': '2,4'}) +        self.assertEqual(result, [2, 4]) + +        result = get_ids({'playlist_items': '10'}) +        self.assertEqual(result, []) +  if __name__ == '__main__':      unittest.main() diff --git a/test/test_aes.py b/test/test_aes.py index 4dc7de7b5..315a3f5ae 100644 --- a/test/test_aes.py +++ b/test/test_aes.py @@ -39,7 +39,7 @@ class TestAES(unittest.TestCase):          encrypted = base64.b64encode(              intlist_to_bytes(self.iv[:8]) +              b'\x17\x15\x93\xab\x8d\x80V\xcdV\xe0\t\xcdo\xc2\xa5\xd8ksM\r\xe27N\xae' -        ) +        ).decode('utf-8')          decrypted = (aes_decrypt_text(encrypted, password, 16))          self.assertEqual(decrypted, self.secret_msg) @@ -47,7 +47,7 @@ class TestAES(unittest.TestCase):          encrypted = base64.b64encode(              intlist_to_bytes(self.iv[:8]) +              b'\x0b\xe6\xa4\xd9z\x0e\xb8\xb9\xd0\xd4i_\x85\x1d\x99\x98_\xe5\x80\xe7.\xbf\xa5\x83' -        ) +        ).decode('utf-8')          decrypted = (aes_decrypt_text(encrypted, password, 32))          self.assertEqual(decrypted, self.secret_msg) diff --git a/test/test_compat.py b/test/test_compat.py index 1eb454e06..c3ba8ad2e 100644 --- a/test/test_compat.py +++ b/test/test_compat.py @@ -14,6 +14,8 @@ from youtube_dl.utils import get_filesystem_encoding  from youtube_dl.compat import (      compat_getenv,      compat_expanduser, +    compat_urllib_parse_unquote, +    compat_urllib_parse_unquote_plus,  ) @@ -42,5 +44,28 @@ class TestCompat(unittest.TestCase):              dir(youtube_dl.compat))) - set(['unicode_literals'])          self.assertEqual(all_names, sorted(present_names)) +    def test_compat_urllib_parse_unquote(self): +        self.assertEqual(compat_urllib_parse_unquote('abc%20def'), 'abc def') +        self.assertEqual(compat_urllib_parse_unquote('%7e/abc+def'), '~/abc+def') +        self.assertEqual(compat_urllib_parse_unquote(''), '') +        self.assertEqual(compat_urllib_parse_unquote('%'), '%') +        self.assertEqual(compat_urllib_parse_unquote('%%'), '%%') +        self.assertEqual(compat_urllib_parse_unquote('%%%'), '%%%') +        self.assertEqual(compat_urllib_parse_unquote('%2F'), '/') +        self.assertEqual(compat_urllib_parse_unquote('%2f'), '/') +        self.assertEqual(compat_urllib_parse_unquote('%E6%B4%A5%E6%B3%A2'), '津波') +        self.assertEqual( +            compat_urllib_parse_unquote('''<meta property="og:description" content="%E2%96%81%E2%96%82%E2%96%83%E2%96%84%25%E2%96%85%E2%96%86%E2%96%87%E2%96%88" /> +%<a href="https://ar.wikipedia.org/wiki/%D8%AA%D8%B3%D9%88%D9%86%D8%A7%D9%85%D9%8A">%a'''), +            '''<meta property="og:description" content="▁▂▃▄%▅▆▇█" /> +%<a href="https://ar.wikipedia.org/wiki/تسونامي">%a''') +        self.assertEqual( +            compat_urllib_parse_unquote('''%28%5E%E2%97%A3_%E2%97%A2%5E%29%E3%81%A3%EF%B8%BB%E3%83%87%E2%95%90%E4%B8%80    %E2%87%80    %E2%87%80    %E2%87%80    %E2%87%80    %E2%87%80    %E2%86%B6%I%Break%25Things%'''), +            '''(^◣_◢^)っ︻デ═一    ⇀    ⇀    ⇀    ⇀    ⇀    ↶%I%Break%Things%''') + +    def test_compat_urllib_parse_unquote_plus(self): +        self.assertEqual(compat_urllib_parse_unquote_plus('abc%20def'), 'abc def') +        self.assertEqual(compat_urllib_parse_unquote_plus('%7e/abc+def'), '~/abc def') +  if __name__ == '__main__':      unittest.main() diff --git a/test/test_download.py b/test/test_download.py index 6a149ae4f..1110357a7 100644 --- a/test/test_download.py +++ b/test/test_download.py @@ -153,7 +153,7 @@ def generator(test_case):                      break              if is_playlist: -                self.assertEqual(res_dict['_type'], 'playlist') +                self.assertTrue(res_dict['_type'] in ['playlist', 'multi_video'])                  self.assertTrue('entries' in res_dict)                  expect_info_dict(self, res_dict, test_case.get('info_dict', {})) diff --git a/test/test_subtitles.py b/test/test_subtitles.py index 891ee620b..c4e3adb67 100644 --- a/test/test_subtitles.py +++ b/test/test_subtitles.py @@ -266,7 +266,7 @@ class TestNRKSubtitles(BaseTestSubtitles):          self.DL.params['allsubtitles'] = True          subtitles = self.getSubtitles()          self.assertEqual(set(subtitles.keys()), set(['no'])) -        self.assertEqual(md5(subtitles['no']), '1d221e6458c95c5494dcd38e6a1f129a') +        self.assertEqual(md5(subtitles['no']), '544fa917d3197fcbee64634559221cc2')  class TestRaiSubtitles(BaseTestSubtitles): diff --git a/test/test_utils.py b/test/test_utils.py index 2e3a6480c..e13e11b59 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -40,7 +40,8 @@ from youtube_dl.utils import (      read_batch_urls,      sanitize_filename,      sanitize_path, -    sanitize_url_path_consecutive_slashes, +    prepend_extension, +    replace_extension,      shell_quote,      smuggle_url,      str_to_int, @@ -51,6 +52,7 @@ from youtube_dl.utils import (      unified_strdate,      unsmuggle_url,      uppercase_escape, +    lowercase_escape,      url_basename,      urlencode_postdata,      version_tuple, @@ -58,6 +60,8 @@ from youtube_dl.utils import (      xpath_text,      render_table,      match_str, +    parse_dfxp_time_expr, +    dfxp2srt,  ) @@ -171,25 +175,21 @@ class TestUtil(unittest.TestCase):          self.assertEqual(sanitize_path('./abc'), 'abc')          self.assertEqual(sanitize_path('./../abc'), '..\\abc') -    def test_sanitize_url_path_consecutive_slashes(self): -        self.assertEqual( -            sanitize_url_path_consecutive_slashes('http://hostname/foo//bar/filename.html'), -            'http://hostname/foo/bar/filename.html') -        self.assertEqual( -            sanitize_url_path_consecutive_slashes('http://hostname//foo/bar/filename.html'), -            'http://hostname/foo/bar/filename.html') -        self.assertEqual( -            sanitize_url_path_consecutive_slashes('http://hostname//'), -            'http://hostname/') -        self.assertEqual( -            sanitize_url_path_consecutive_slashes('http://hostname/foo/bar/filename.html'), -            'http://hostname/foo/bar/filename.html') -        self.assertEqual( -            sanitize_url_path_consecutive_slashes('http://hostname/'), -            'http://hostname/') -        self.assertEqual( -            sanitize_url_path_consecutive_slashes('http://hostname/abc//'), -            'http://hostname/abc/') +    def test_prepend_extension(self): +        self.assertEqual(prepend_extension('abc.ext', 'temp'), 'abc.temp.ext') +        self.assertEqual(prepend_extension('abc.ext', 'temp', 'ext'), 'abc.temp.ext') +        self.assertEqual(prepend_extension('abc.unexpected_ext', 'temp', 'ext'), 'abc.unexpected_ext.temp') +        self.assertEqual(prepend_extension('abc', 'temp'), 'abc.temp') +        self.assertEqual(prepend_extension('.abc', 'temp'), '.abc.temp') +        self.assertEqual(prepend_extension('.abc.ext', 'temp'), '.abc.temp.ext') + +    def test_replace_extension(self): +        self.assertEqual(replace_extension('abc.ext', 'temp'), 'abc.temp') +        self.assertEqual(replace_extension('abc.ext', 'temp', 'ext'), 'abc.temp') +        self.assertEqual(replace_extension('abc.unexpected_ext', 'temp', 'ext'), 'abc.unexpected_ext.temp') +        self.assertEqual(replace_extension('abc', 'temp'), 'abc.temp') +        self.assertEqual(replace_extension('.abc', 'temp'), '.abc.temp') +        self.assertEqual(replace_extension('.abc.ext', 'temp'), '.abc.temp')      def test_ordered_set(self):          self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7]) @@ -398,6 +398,10 @@ class TestUtil(unittest.TestCase):          self.assertEqual(uppercase_escape('aä'), 'aä')          self.assertEqual(uppercase_escape('\\U0001d550'), '𝕐') +    def test_lowercase_escape(self): +        self.assertEqual(lowercase_escape('aä'), 'aä') +        self.assertEqual(lowercase_escape('\\u0026'), '&') +      def test_limit_length(self):          self.assertEqual(limit_length(None, 12), None)          self.assertEqual(limit_length('foo', 12), 'foo') @@ -581,6 +585,57 @@ ffmpeg version 2.4.4 Copyright (c) 2000-2014 the FFmpeg ...'''), '2.4.4')              'like_count > 100 & dislike_count <? 50 & description',              {'like_count': 190, 'dislike_count': 10})) +    def test_parse_dfxp_time_expr(self): +        self.assertEqual(parse_dfxp_time_expr(None), 0.0) +        self.assertEqual(parse_dfxp_time_expr(''), 0.0) +        self.assertEqual(parse_dfxp_time_expr('0.1'), 0.1) +        self.assertEqual(parse_dfxp_time_expr('0.1s'), 0.1) +        self.assertEqual(parse_dfxp_time_expr('00:00:01'), 1.0) +        self.assertEqual(parse_dfxp_time_expr('00:00:01.100'), 1.1) + +    def test_dfxp2srt(self): +        dfxp_data = '''<?xml version="1.0" encoding="UTF-8"?> +            <tt xmlns="http://www.w3.org/ns/ttml" xml:lang="en" xmlns:tts="http://www.w3.org/ns/ttml#parameter"> +            <body> +                <div xml:lang="en"> +                    <p begin="0" end="1">The following line contains Chinese characters and special symbols</p> +                    <p begin="1" end="2">第二行<br/>♪♪</p> +                    <p begin="2" dur="1"><span>Third<br/>Line</span></p> +                </div> +            </body> +            </tt>''' +        srt_data = '''1 +00:00:00,000 --> 00:00:01,000 +The following line contains Chinese characters and special symbols + +2 +00:00:01,000 --> 00:00:02,000 +第二行 +♪♪ + +3 +00:00:02,000 --> 00:00:03,000 +Third +Line + +''' +        self.assertEqual(dfxp2srt(dfxp_data), srt_data) + +        dfxp_data_no_default_namespace = '''<?xml version="1.0" encoding="UTF-8"?> +            <tt xml:lang="en" xmlns:tts="http://www.w3.org/ns/ttml#parameter"> +            <body> +                <div xml:lang="en"> +                    <p begin="0" end="1">The first line</p> +                </div> +            </body> +            </tt>''' +        srt_data = '''1 +00:00:00,000 --> 00:00:01,000 +The first line + +''' +        self.assertEqual(dfxp2srt(dfxp_data_no_default_namespace), srt_data) +  if __name__ == '__main__':      unittest.main()  | 
