3 # Allow direct execution
7 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
9 from test
.helper
import FakeYDL
13 def __init__(self
, *args
, **kwargs
):
14 super(YDL
, self
).__init
__(*args
, **kwargs
)
15 self
.downloaded_info_dicts
= []
18 def process_info(self
, info_dict
):
19 self
.downloaded_info_dicts
.append(info_dict
)
21 def to_screen(self
, msg
):
25 class TestFormatSelection(unittest
.TestCase
):
26 def test_prefer_free_formats(self
):
27 # Same resolution => download webm
29 ydl
.params
['prefer_free_formats'] = True
31 {u'ext': u'webm', u'height': 460}
,
32 {u'ext': u'mp4', u'height': 460}
,
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')
39 # Different resolution => download best quality (mp4)
41 ydl
.params
['prefer_free_formats'] = True
43 {u'ext': u'webm', u'height': 720}
,
44 {u'ext': u'mp4', u'height': 1080}
,
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')
51 # No prefer_free_formats => keep original formats order
53 ydl
.params
['prefer_free_formats'] = False
55 {u'ext': u'webm', u'height': 720}
,
56 {u'ext': u'flv', u'height': 720}
,
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')
63 def test_format_limit(self
):
65 {u'format_id': u'meh'}
,
66 {u'format_id': u'good'}
,
67 {u'format_id': u'great'}
,
68 {u'format_id': u'excellent'}
,
71 u
'formats': formats
, u
'extractor': u
'test', 'id': 'testvid'}
74 ydl
.process_ie_result(info_dict
)
75 downloaded
= ydl
.downloaded_info_dicts
[0]
76 self
.assertEqual(downloaded
[u
'format_id'], u
'excellent')
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')
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])
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')
98 if __name__
== '__main__':