diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/helper.py | 47 | ||||
| -rw-r--r-- | test/test_YoutubeDL.py | 74 | ||||
| -rw-r--r-- | test/test_all_urls.py | 20 | ||||
| -rw-r--r-- | test/test_download.py | 47 | ||||
| -rw-r--r-- | test/test_playlists.py | 55 | ||||
| -rw-r--r-- | test/test_utils.py | 14 | 
6 files changed, 181 insertions, 76 deletions
| diff --git a/test/helper.py b/test/helper.py index 17de951c5..8739f816c 100644 --- a/test/helper.py +++ b/test/helper.py @@ -9,7 +9,10 @@ import sys  import youtube_dl.extractor  from youtube_dl import YoutubeDL -from youtube_dl.utils import preferredencoding +from youtube_dl.utils import ( +    compat_str, +    preferredencoding, +)  def get_params(override=None): @@ -83,3 +86,45 @@ def gettestcases():  md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest() + + +def expect_info_dict(self, expected_dict, got_dict): +    for info_field, expected in expected_dict.items(): +        if isinstance(expected, compat_str) and expected.startswith('re:'): +            got = got_dict.get(info_field) +            match_str = expected[len('re:'):] +            match_rex = re.compile(match_str) + +            self.assertTrue( +                isinstance(got, compat_str) and match_rex.match(got), +                u'field %s (value: %r) should match %r' % (info_field, got, match_str)) +        elif isinstance(expected, type): +            got = got_dict.get(info_field) +            self.assertTrue(isinstance(got, expected), +                u'Expected type %r, but got value %r of type %r' % (expected, got, type(got))) +        else: +            if isinstance(expected, compat_str) and expected.startswith('md5:'): +                got = 'md5:' + md5(got_dict.get(info_field)) +            else: +                got = got_dict.get(info_field) +            self.assertEqual(expected, got, +                u'invalid value for field %s, expected %r, got %r' % (info_field, expected, got)) + +    # Check for the presence of mandatory fields +    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 +    for key in ['webpage_url', 'extractor', 'extractor_key']: +        self.assertTrue(got_dict.get(key), u'Missing field: %s' % key) + +    # Are checkable fields missing from the test case definition? +    test_info_dict = dict((key, value if not isinstance(value, compat_str) or len(value) < 250 else 'md5:' + md5(value)) +        for key, value in got_dict.items() +        if value and key in ('title', 'description', 'uploader', 'upload_date', 'timestamp', 'uploader_id', 'location')) +    missing_keys = set(test_info_dict.keys()) - set(expected_dict.keys()) +    if missing_keys: +        sys.stderr.write(u'\n"info_dict": ' + json.dumps(test_info_dict, ensure_ascii=False, indent=4) + u'\n') +        self.assertFalse( +            missing_keys, +            'Missing keys in test definition: %s' % ( +                ', '.join(sorted(missing_keys)))) diff --git a/test/test_YoutubeDL.py b/test/test_YoutubeDL.py index 1f3ccaea0..2902dbec7 100644 --- a/test/test_YoutubeDL.py +++ b/test/test_YoutubeDL.py @@ -26,16 +26,27 @@ class YDL(FakeYDL):          self.msgs.append(msg) +def _make_result(formats, **kwargs): +    res = { +        'formats': formats, +        'id': 'testid', +        'title': 'testttitle', +        'extractor': 'testex', +    } +    res.update(**kwargs) +    return res + +  class TestFormatSelection(unittest.TestCase):      def test_prefer_free_formats(self):          # Same resolution => download webm          ydl = YDL()          ydl.params['prefer_free_formats'] = True          formats = [ -            {'ext': 'webm', 'height': 460}, -            {'ext': 'mp4',  'height': 460}, +            {'ext': 'webm', 'height': 460, 'url': 'x'}, +            {'ext': 'mp4', 'height': 460, 'url': 'y'},          ] -        info_dict = {'formats': formats, 'extractor': 'test'} +        info_dict = _make_result(formats)          yie = YoutubeIE(ydl)          yie._sort_formats(info_dict['formats'])          ydl.process_ie_result(info_dict) @@ -46,8 +57,8 @@ class TestFormatSelection(unittest.TestCase):          ydl = YDL()          ydl.params['prefer_free_formats'] = True          formats = [ -            {'ext': 'webm', 'height': 720}, -            {'ext': 'mp4', 'height': 1080}, +            {'ext': 'webm', 'height': 720, 'url': 'a'}, +            {'ext': 'mp4', 'height': 1080, 'url': 'b'},          ]          info_dict['formats'] = formats          yie = YoutubeIE(ydl) @@ -60,9 +71,9 @@ class TestFormatSelection(unittest.TestCase):          ydl = YDL()          ydl.params['prefer_free_formats'] = False          formats = [ -            {'ext': 'webm', 'height': 720}, -            {'ext': 'mp4', 'height': 720}, -            {'ext': 'flv', 'height': 720}, +            {'ext': 'webm', 'height': 720, 'url': '_'}, +            {'ext': 'mp4', 'height': 720, 'url': '_'}, +            {'ext': 'flv', 'height': 720, 'url': '_'},          ]          info_dict['formats'] = formats          yie = YoutubeIE(ydl) @@ -74,8 +85,8 @@ class TestFormatSelection(unittest.TestCase):          ydl = YDL()          ydl.params['prefer_free_formats'] = False          formats = [ -            {'ext': 'flv', 'height': 720}, -            {'ext': 'webm', 'height': 720}, +            {'ext': 'flv', 'height': 720, 'url': '_'}, +            {'ext': 'webm', 'height': 720, 'url': '_'},          ]          info_dict['formats'] = formats          yie = YoutubeIE(ydl) @@ -91,8 +102,7 @@ class TestFormatSelection(unittest.TestCase):              {'format_id': 'great', 'url': 'http://example.com/great', 'preference': 3},              {'format_id': 'excellent', 'url': 'http://example.com/exc', 'preference': 4},          ] -        info_dict = { -            'formats': formats, 'extractor': 'test', 'id': 'testvid'} +        info_dict = _make_result(formats)          ydl = YDL()          ydl.process_ie_result(info_dict) @@ -120,12 +130,12 @@ class TestFormatSelection(unittest.TestCase):      def test_format_selection(self):          formats = [ -            {'format_id': '35', 'ext': 'mp4', 'preference': 1}, -            {'format_id': '45', 'ext': 'webm', 'preference': 2}, -            {'format_id': '47', 'ext': 'webm', 'preference': 3}, -            {'format_id': '2', 'ext': 'flv', 'preference': 4}, +            {'format_id': '35', 'ext': 'mp4', 'preference': 1, 'url': '_'}, +            {'format_id': '45', 'ext': 'webm', 'preference': 2, 'url': '_'}, +            {'format_id': '47', 'ext': 'webm', 'preference': 3, 'url': '_'}, +            {'format_id': '2', 'ext': 'flv', 'preference': 4, 'url': '_'},          ] -        info_dict = {'formats': formats, 'extractor': 'test'} +        info_dict = _make_result(formats)          ydl = YDL({'format': '20/47'})          ydl.process_ie_result(info_dict.copy()) @@ -154,12 +164,12 @@ class TestFormatSelection(unittest.TestCase):      def test_format_selection_audio(self):          formats = [ -            {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none'}, -            {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none'}, -            {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none'}, -            {'format_id': 'vid', 'ext': 'mp4', 'preference': 4}, +            {'format_id': 'audio-low', 'ext': 'webm', 'preference': 1, 'vcodec': 'none', 'url': '_'}, +            {'format_id': 'audio-mid', 'ext': 'webm', 'preference': 2, 'vcodec': 'none', 'url': '_'}, +            {'format_id': 'audio-high', 'ext': 'flv', 'preference': 3, 'vcodec': 'none', 'url': '_'}, +            {'format_id': 'vid', 'ext': 'mp4', 'preference': 4, 'url': '_'},          ] -        info_dict = {'formats': formats, 'extractor': 'test'} +        info_dict = _make_result(formats)          ydl = YDL({'format': 'bestaudio'})          ydl.process_ie_result(info_dict.copy()) @@ -172,10 +182,10 @@ class TestFormatSelection(unittest.TestCase):          self.assertEqual(downloaded['format_id'], 'audio-low')          formats = [ -            {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1}, -            {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2}, +            {'format_id': 'vid-low', 'ext': 'mp4', 'preference': 1, 'url': '_'}, +            {'format_id': 'vid-high', 'ext': 'mp4', 'preference': 2, 'url': '_'},          ] -        info_dict = {'formats': formats, 'extractor': 'test'} +        info_dict = _make_result(formats)          ydl = YDL({'format': 'bestaudio/worstaudio/best'})          ydl.process_ie_result(info_dict.copy()) @@ -184,11 +194,11 @@ class TestFormatSelection(unittest.TestCase):      def test_format_selection_video(self):          formats = [ -            {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none'}, -            {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none'}, -            {'format_id': 'vid', 'ext': 'mp4', 'preference': 3}, +            {'format_id': 'dash-video-low', 'ext': 'mp4', 'preference': 1, 'acodec': 'none', 'url': '_'}, +            {'format_id': 'dash-video-high', 'ext': 'mp4', 'preference': 2, 'acodec': 'none', 'url': '_'}, +            {'format_id': 'vid', 'ext': 'mp4', 'preference': 3, 'url': '_'},          ] -        info_dict = {'formats': formats, 'extractor': 'test'} +        info_dict = _make_result(formats)          ydl = YDL({'format': 'bestvideo'})          ydl.process_ie_result(info_dict.copy()) @@ -217,10 +227,12 @@ class TestFormatSelection(unittest.TestCase):          for f1id, f2id in zip(order, order[1:]):              f1 = YoutubeIE._formats[f1id].copy()              f1['format_id'] = f1id +            f1['url'] = 'url:' + f1id              f2 = YoutubeIE._formats[f2id].copy()              f2['format_id'] = f2id +            f2['url'] = 'url:' + f2id -            info_dict = {'formats': [f1, f2], 'extractor': 'youtube'} +            info_dict = _make_result([f1, f2], extractor='youtube')              ydl = YDL()              yie = YoutubeIE(ydl)              yie._sort_formats(info_dict['formats']) @@ -228,7 +240,7 @@ class TestFormatSelection(unittest.TestCase):              downloaded = ydl.downloaded_info_dicts[0]              self.assertEqual(downloaded['format_id'], f1id) -            info_dict = {'formats': [f2, f1], 'extractor': 'youtube'} +            info_dict = _make_result([f2, f1], extractor='youtube')              ydl = YDL()              yie = YoutubeIE(ydl)              yie._sort_formats(info_dict['formats']) diff --git a/test/test_all_urls.py b/test/test_all_urls.py index 39ac8b8a1..577f6ac32 100644 --- a/test/test_all_urls.py +++ b/test/test_all_urls.py @@ -143,5 +143,25 @@ class TestAllURLsMatching(unittest.TestCase):          self.assertMatch('http://video.pbs.org/viralplayer/2365173446/', ['PBS'])          self.assertMatch('http://video.pbs.org/widget/partnerplayer/980042464/', ['PBS']) +    def test_ComedyCentralShows(self): +        self.assertMatch( +            'http://thedailyshow.cc.com/extended-interviews/xm3fnq/andrew-napolitano-extended-interview', +            ['ComedyCentralShows']) +        self.assertMatch( +            'http://thecolbertreport.cc.com/videos/29w6fx/-realhumanpraise-for-fox-news', +            ['ComedyCentralShows']) +        self.assertMatch( +            'http://thecolbertreport.cc.com/videos/gh6urb/neil-degrasse-tyson-pt--1?xrs=eml_col_031114', +            ['ComedyCentralShows']) +        self.assertMatch( +            'http://thedailyshow.cc.com/guests/michael-lewis/3efna8/exclusive---michael-lewis-extended-interview-pt--3', +            ['ComedyCentralShows']) + +    def test_yahoo_https(self): +        # https://github.com/rg3/youtube-dl/issues/2701 +        self.assertMatch( +            'https://screen.yahoo.com/smartwatches-latest-wearable-gadgets-163745379-cbs.html', +            ['Yahoo']) +  if __name__ == '__main__':      unittest.main() diff --git a/test/test_download.py b/test/test_download.py index 815f5bb09..f171c10ba 100644 --- a/test/test_download.py +++ b/test/test_download.py @@ -9,16 +9,16 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))  from test.helper import (      get_params,      gettestcases, -    try_rm, +    expect_info_dict,      md5, -    report_warning +    try_rm, +    report_warning,  )  import hashlib  import io  import json -import re  import socket  import youtube_dl.YoutubeDL @@ -135,45 +135,8 @@ def generator(test_case):                      self.assertEqual(md5_for_file, tc['md5'])                  with io.open(info_json_fn, encoding='utf-8') as infof:                      info_dict = json.load(infof) -                for (info_field, expected) in tc.get('info_dict', {}).items(): -                    if isinstance(expected, compat_str) and expected.startswith('re:'): -                        got = info_dict.get(info_field) -                        match_str = expected[len('re:'):] -                        match_rex = re.compile(match_str) - -                        self.assertTrue( -                            isinstance(got, compat_str) and match_rex.match(got), -                            u'field %s (value: %r) should match %r' % (info_field, got, match_str)) -                    elif isinstance(expected, type): -                        got = info_dict.get(info_field) -                        self.assertTrue(isinstance(got, expected), -                            u'Expected type %r, but got value %r of type %r' % (expected, got, type(got))) -                    else: -                        if isinstance(expected, compat_str) and expected.startswith('md5:'): -                            got = 'md5:' + md5(info_dict.get(info_field)) -                        else: -                            got = info_dict.get(info_field) -                        self.assertEqual(expected, got, -                            u'invalid value for field %s, expected %r, got %r' % (info_field, expected, got)) - -                # Check for the presence of mandatory fields -                for key in ('id', 'url', 'title', 'ext'): -                    self.assertTrue(key in info_dict.keys() and info_dict[key]) -                # Check for mandatory fields that are automatically set by YoutubeDL -                for key in ['webpage_url', 'extractor', 'extractor_key']: -                    self.assertTrue(info_dict.get(key), u'Missing field: %s' % key) - -                # Are checkable fields missing from the test case definition? -                test_info_dict = dict((key, value if not isinstance(value, compat_str) or len(value) < 250 else 'md5:' + md5(value)) -                    for key, value in info_dict.items() -                    if value and key in ('title', 'description', 'uploader', 'upload_date', 'timestamp', 'uploader_id', 'location')) -                missing_keys = set(test_info_dict.keys()) - set(tc.get('info_dict', {}).keys()) -                if missing_keys: -                    sys.stderr.write(u'\n"info_dict": ' + json.dumps(test_info_dict, ensure_ascii=False, indent=4) + u'\n') -                    self.assertFalse( -                        missing_keys, -                        'Missing keys in test definition: %s' % ( -                            ','.join(sorted(missing_keys)))) + +                expect_info_dict(self, tc.get('info_dict', {}), info_dict)          finally:              try_rm_tcs_files() diff --git a/test/test_playlists.py b/test/test_playlists.py index 4c9c34057..5fb679aa1 100644 --- a/test/test_playlists.py +++ b/test/test_playlists.py @@ -9,8 +9,10 @@ import sys  import unittest  sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -from test.helper import FakeYDL - +from test.helper import ( +    expect_info_dict, +    FakeYDL, +)  from youtube_dl.extractor import (      AcademicEarthCourseIE, @@ -38,6 +40,9 @@ from youtube_dl.extractor import (      GenericIE,      TEDIE,      ToypicsUserIE, +    XTubeUserIE, +    InstagramUserIE, +    CSpanIE,  ) @@ -278,5 +283,51 @@ class TestPlaylists(unittest.TestCase):          self.assertEqual(result['id'], 'Mikey')          self.assertTrue(len(result['entries']) >= 17) +    def test_xtube_user(self): +        dl = FakeYDL() +        ie = XTubeUserIE(dl) +        result = ie.extract('http://www.xtube.com/community/profile.php?user=greenshowers') +        self.assertIsPlaylist(result) +        self.assertEqual(result['id'], 'greenshowers') +        self.assertTrue(len(result['entries']) >= 155) + +    def test_InstagramUser(self): +        dl = FakeYDL() +        ie = InstagramUserIE(dl) +        result = ie.extract('http://instagram.com/porsche') +        self.assertIsPlaylist(result) +        self.assertEqual(result['id'], 'porsche') +        self.assertTrue(len(result['entries']) >= 2) +        test_video = next( +            e for e in result['entries'] +            if e['id'] == '614605558512799803_462752227') +        dl.add_default_extra_info(test_video, ie, '(irrelevant URL)') +        dl.process_video_result(test_video, download=False) +        EXPECTED = { +            'id': '614605558512799803_462752227', +            'ext': 'mp4', +            'title': '#Porsche Intelligent Performance.', +            'thumbnail': 're:^https?://.*\.jpg', +            'uploader': 'Porsche', +            'uploader_id': 'porsche', +            'timestamp': 1387486713, +            'upload_date': '20131219', +        } +        expect_info_dict(self, EXPECTED, test_video) + +    def test_CSpan_playlist(self): +        dl = FakeYDL() +        ie = CSpanIE(dl) +        result = ie.extract( +            'http://www.c-span.org/video/?318608-1/gm-ignition-switch-recall') +        self.assertIsPlaylist(result) +        self.assertEqual(result['id'], '342759') +        self.assertEqual( +            result['title'], 'General Motors Ignition Switch Recall') +        self.assertEqual(len(result['entries']), 9) +        whole_duration = sum(e['duration'] for e in result['entries']) +        self.assertEqual(whole_duration, 14855) + +  if __name__ == '__main__':      unittest.main() diff --git a/test/test_utils.py b/test/test_utils.py index 7ee74e36c..2348c0415 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -10,6 +10,7 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))  # Various small unit tests  import io +import json  import xml.etree.ElementTree  #from youtube_dl.utils import htmlentity_transform @@ -35,6 +36,8 @@ from youtube_dl.utils import (      url_basename,      urlencode_postdata,      xpath_with_ns, +    parse_iso8601, +    strip_jsonp,  )  if sys.version_info < (3, 0): @@ -266,5 +269,16 @@ class TestUtil(unittest.TestCase):          data = urlencode_postdata({'username': 'foo@bar.com', 'password': '1234'})          self.assertTrue(isinstance(data, bytes)) +    def test_parse_iso8601(self): +        self.assertEqual(parse_iso8601('2014-03-23T23:04:26+0100'), 1395612266) +        self.assertEqual(parse_iso8601('2014-03-23T22:04:26+0000'), 1395612266) +        self.assertEqual(parse_iso8601('2014-03-23T22:04:26Z'), 1395612266) + +    def test_strip_jsonp(self): +        stripped = strip_jsonp('cb ([ {"id":"532cb",\n\n\n"x":\n3}\n]\n);') +        d = json.loads(stripped) +        self.assertEqual(d, [{"id": "532cb", "x": 3}]) + +  if __name__ == '__main__':      unittest.main() | 
