]> jfr.im git - yt-dlp.git/blame - test/test_YoutubeDL.py
Merge remote-tracking branch 'rbrito/set-age'
[yt-dlp.git] / test / test_YoutubeDL.py
CommitLineData
e028d0d1
JMF
1#!/usr/bin/env python
2
5d254f77
PH
3# Allow direct execution
4import os
e028d0d1
JMF
5import sys
6import unittest
5d254f77 7sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
e028d0d1 8
5d254f77 9from test.helper import FakeYDL
e028d0d1 10
e028d0d1
JMF
11
12class YDL(FakeYDL):
f4d96df0
PH
13 def __init__(self, *args, **kwargs):
14 super(YDL, self).__init__(*args, **kwargs)
e028d0d1 15 self.downloaded_info_dicts = []
f4d96df0 16 self.msgs = []
5d254f77 17
e028d0d1
JMF
18 def process_info(self, info_dict):
19 self.downloaded_info_dicts.append(info_dict)
20
f4d96df0
PH
21 def to_screen(self, msg):
22 self.msgs.append(msg)
23
5d254f77 24
e028d0d1
JMF
25class TestFormatSelection(unittest.TestCase):
26 def test_prefer_free_formats(self):
27 # Same resolution => download webm
28 ydl = YDL()
29 ydl.params['prefer_free_formats'] = True
5d254f77
PH
30 formats = [
31 {u'ext': u'webm', u'height': 460},
32 {u'ext': u'mp4', u'height': 460},
33 ]
e028d0d1
JMF
34 info_dict = {u'formats': formats, u'extractor': u'test'}
35 ydl.process_ie_result(info_dict)
36 downloaded = ydl.downloaded_info_dicts[0]
37 self.assertEqual(downloaded[u'ext'], u'webm')
38
39 # Different resolution => download best quality (mp4)
40 ydl = YDL()
41 ydl.params['prefer_free_formats'] = True
5d254f77
PH
42 formats = [
43 {u'ext': u'webm', u'height': 720},
44 {u'ext': u'mp4', u'height': 1080},
45 ]
e028d0d1
JMF
46 info_dict[u'formats'] = formats
47 ydl.process_ie_result(info_dict)
48 downloaded = ydl.downloaded_info_dicts[0]
49 self.assertEqual(downloaded[u'ext'], u'mp4')
50
51 # No prefer_free_formats => keep original formats order
52 ydl = YDL()
53 ydl.params['prefer_free_formats'] = False
5d254f77
PH
54 formats = [
55 {u'ext': u'webm', u'height': 720},
56 {u'ext': u'flv', u'height': 720},
57 ]
e028d0d1
JMF
58 info_dict[u'formats'] = formats
59 ydl.process_ie_result(info_dict)
60 downloaded = ydl.downloaded_info_dicts[0]
61 self.assertEqual(downloaded[u'ext'], u'flv')
62
f4d96df0
PH
63 def test_format_limit(self):
64 formats = [
65 {u'format_id': u'meh'},
66 {u'format_id': u'good'},
67 {u'format_id': u'great'},
68 {u'format_id': u'excellent'},
69 ]
70 info_dict = {
71 u'formats': formats, u'extractor': u'test', 'id': 'testvid'}
72
73 ydl = YDL()
74 ydl.process_ie_result(info_dict)
75 downloaded = ydl.downloaded_info_dicts[0]
76 self.assertEqual(downloaded[u'format_id'], u'excellent')
77
78 ydl = YDL({'format_limit': 'good'})
79 assert ydl.params['format_limit'] == 'good'
80 ydl.process_ie_result(info_dict)
81 downloaded = ydl.downloaded_info_dicts[0]
82 self.assertEqual(downloaded[u'format_id'], u'good')
83
84 ydl = YDL({'format_limit': 'great', 'format': 'all'})
85 ydl.process_ie_result(info_dict)
86 self.assertEqual(ydl.downloaded_info_dicts[0][u'format_id'], u'meh')
87 self.assertEqual(ydl.downloaded_info_dicts[1][u'format_id'], u'good')
88 self.assertEqual(ydl.downloaded_info_dicts[2][u'format_id'], u'great')
89 self.assertTrue('3' in ydl.msgs[0])
90
91 ydl = YDL()
92 ydl.params['format_limit'] = 'excellent'
93 ydl.process_ie_result(info_dict)
94 downloaded = ydl.downloaded_info_dicts[0]
95 self.assertEqual(downloaded[u'format_id'], u'excellent')
96
97
e028d0d1
JMF
98if __name__ == '__main__':
99 unittest.main()