]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_YoutubeDL.py
Prepare for upload.
[youtubedl] / test / test_YoutubeDL.py
1 #!/usr/bin/env python
2
3 # Allow direct execution
4 import os
5 import sys
6 import unittest
7 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
9 from test.helper import FakeYDL
10
11
12 class YDL(FakeYDL):
13 def __init__(self, *args, **kwargs):
14 super(YDL, self).__init__(*args, **kwargs)
15 self.downloaded_info_dicts = []
16 self.msgs = []
17
18 def process_info(self, info_dict):
19 self.downloaded_info_dicts.append(info_dict)
20
21 def to_screen(self, msg):
22 self.msgs.append(msg)
23
24
25 class 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
30 formats = [
31 {u'ext': u'webm', u'height': 460},
32 {u'ext': u'mp4', u'height': 460},
33 ]
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
42 formats = [
43 {u'ext': u'webm', u'height': 720},
44 {u'ext': u'mp4', u'height': 1080},
45 ]
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
54 formats = [
55 {u'ext': u'webm', u'height': 720},
56 {u'ext': u'flv', u'height': 720},
57 ]
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
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 def test_format_selection(self):
98 formats = [
99 {u'format_id': u'35', u'ext': u'mp4'},
100 {u'format_id': u'45', u'ext': u'webm'},
101 {u'format_id': u'47', u'ext': u'webm'},
102 {u'format_id': u'2', u'ext': u'flv'},
103 ]
104 info_dict = {u'formats': formats, u'extractor': u'test'}
105
106 ydl = YDL({'format': u'20/47'})
107 ydl.process_ie_result(info_dict)
108 downloaded = ydl.downloaded_info_dicts[0]
109 self.assertEqual(downloaded['format_id'], u'47')
110
111 ydl = YDL({'format': u'20/71/worst'})
112 ydl.process_ie_result(info_dict)
113 downloaded = ydl.downloaded_info_dicts[0]
114 self.assertEqual(downloaded['format_id'], u'35')
115
116 ydl = YDL()
117 ydl.process_ie_result(info_dict)
118 downloaded = ydl.downloaded_info_dicts[0]
119 self.assertEqual(downloaded['format_id'], u'2')
120
121 ydl = YDL({'format': u'webm/mp4'})
122 ydl.process_ie_result(info_dict)
123 downloaded = ydl.downloaded_info_dicts[0]
124 self.assertEqual(downloaded['format_id'], u'47')
125
126 ydl = YDL({'format': u'3gp/40/mp4'})
127 ydl.process_ie_result(info_dict)
128 downloaded = ydl.downloaded_info_dicts[0]
129 self.assertEqual(downloaded['format_id'], u'35')
130
131
132 if __name__ == '__main__':
133 unittest.main()