]>
Raphaël G. Git Repositories - youtubedl/blob - test/test_utils.py
84553b94386236352e9ad856782c4b3428f3587b
4 # Allow direct execution
8 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
11 # Various small unit tests
12 import xml
.etree
.ElementTree
14 #from youtube_dl.utils import htmlentity_transform
15 from youtube_dl
.utils
import (
37 if sys
.version_info
< (3, 0):
38 _compat_str
= lambda b
: b
.decode('unicode-escape')
40 _compat_str
= lambda s
: s
43 class TestUtil(unittest
.TestCase
):
44 def test_timeconvert(self
):
45 self
.assertTrue(timeconvert('') is None)
46 self
.assertTrue(timeconvert('bougrg') is None)
48 def test_sanitize_filename(self
):
49 self
.assertEqual(sanitize_filename('abc'), 'abc')
50 self
.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
52 self
.assertEqual(sanitize_filename('123'), '123')
54 self
.assertEqual('abc_de', sanitize_filename('abc/de'))
55 self
.assertFalse('/' in sanitize_filename('abc/de///'))
57 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
58 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
59 self
.assertEqual('yes no', sanitize_filename('yes? no'))
60 self
.assertEqual('this - that', sanitize_filename('this: that'))
62 self
.assertEqual(sanitize_filename('AT&T'), 'AT&T')
63 aumlaut
= _compat_str('\xe4')
64 self
.assertEqual(sanitize_filename(aumlaut
), aumlaut
)
65 tests
= _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
66 self
.assertEqual(sanitize_filename(tests
), tests
)
71 self
.assertTrue(fbc
not in sanitize_filename(fc
))
73 def test_sanitize_filename_restricted(self
):
74 self
.assertEqual(sanitize_filename('abc', restricted
=True), 'abc')
75 self
.assertEqual(sanitize_filename('abc_d-e', restricted
=True), 'abc_d-e')
77 self
.assertEqual(sanitize_filename('123', restricted
=True), '123')
79 self
.assertEqual('abc_de', sanitize_filename('abc/de', restricted
=True))
80 self
.assertFalse('/' in sanitize_filename('abc/de///', restricted
=True))
82 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted
=True))
83 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted
=True))
84 self
.assertEqual('yes_no', sanitize_filename('yes? no', restricted
=True))
85 self
.assertEqual('this_-_that', sanitize_filename('this: that', restricted
=True))
87 tests
= _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
88 self
.assertEqual(sanitize_filename(tests
, restricted
=True), 'a_b_c')
89 self
.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted
=True) != '') # No empty filename
91 forbidden
= '"\0\\/&!: \'\t\n()[]{}$;`^,#'
94 self
.assertTrue(fbc
not in sanitize_filename(fc
, restricted
=True))
96 # Handle a common case more neatly
97 self
.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted
=True), 'Song')
98 self
.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted
=True), 'Speech')
99 # .. but make sure the file name is never empty
100 self
.assertTrue(sanitize_filename('-', restricted
=True) != '')
101 self
.assertTrue(sanitize_filename(':', restricted
=True) != '')
103 def test_sanitize_ids(self
):
104 self
.assertEqual(sanitize_filename('_n_cd26wFpw', is_id
=True), '_n_cd26wFpw')
105 self
.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id
=True), '_BD_eEpuzXw')
106 self
.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id
=True), 'N0Y__7-UOdI')
108 def test_ordered_set(self
):
109 self
.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
110 self
.assertEqual(orderedSet([]), [])
111 self
.assertEqual(orderedSet([1]), [1])
112 #keep the list ordered
113 self
.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
115 def test_unescape_html(self
):
116 self
.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
118 def test_daterange(self
):
119 _20century
= DateRange("19000101","20000101")
120 self
.assertFalse("17890714" in _20century
)
121 _ac
= DateRange("00010101")
122 self
.assertTrue("19690721" in _ac
)
123 _firstmilenium
= DateRange(end
="10000101")
124 self
.assertTrue("07110427" in _firstmilenium
)
126 def test_unified_dates(self
):
127 self
.assertEqual(unified_strdate('December 21, 2010'), '20101221')
128 self
.assertEqual(unified_strdate('8/7/2009'), '20090708')
129 self
.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
130 self
.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
131 self
.assertEqual(unified_strdate('1968-12-10'), '19681210')
133 def test_find_xpath_attr(self
):
140 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
142 self
.assertEqual(find_xpath_attr(doc
, './/fourohfour', 'n', 'v'), None)
143 self
.assertEqual(find_xpath_attr(doc
, './/node', 'x', 'a'), doc
[1])
144 self
.assertEqual(find_xpath_attr(doc
, './/node', 'y', 'c'), doc
[2])
146 def test_meta_parser(self
):
149 <meta name="description" content="foo & bar">
150 <meta content='Plato' name='author'/>
153 get_meta
= lambda name
: get_meta_content(name
, testhtml
)
154 self
.assertEqual(get_meta('description'), u
'foo & bar')
155 self
.assertEqual(get_meta('author'), 'Plato')
157 def test_xpath_with_ns(self
):
158 testxml
= u
'''<root xmlns:media="http://example.com/">
160 <media:author>The Author</media:author>
161 <url>http://server.com/download.mp3</url>
164 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
165 find
= lambda p
: doc
.find(xpath_with_ns(p
, {'media': 'http://example.com/'}))
166 self
.assertTrue(find('media:song') is not None)
167 self
.assertEqual(find('media:song/media:author').text
, u
'The Author')
168 self
.assertEqual(find('media:song/url').text
, u
'http://server.com/download.mp3')
170 def test_smuggle_url(self
):
171 data
= {u
"ö": u
"ö", u
"abc": [3]}
172 url
= 'https://foo.bar/baz?x=y#a'
173 smug_url
= smuggle_url(url
, data
)
174 unsmug_url
, unsmug_data
= unsmuggle_url(smug_url
)
175 self
.assertEqual(url
, unsmug_url
)
176 self
.assertEqual(data
, unsmug_data
)
178 res_url
, res_data
= unsmuggle_url(url
)
179 self
.assertEqual(res_url
, url
)
180 self
.assertEqual(res_data
, None)
182 def test_shell_quote(self
):
183 args
= ['ffmpeg', '-i', encodeFilename(u
'ñ€ß\'.mp4')]
184 self
.assertEqual(shell_quote(args
), u
"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
186 def test_str_to_int(self
):
187 self
.assertEqual(str_to_int('123,456'), 123456)
188 self
.assertEqual(str_to_int('123.456'), 123456)
190 def test_url_basename(self
):
191 self
.assertEqual(url_basename(u
'http://foo.de/'), u
'')
192 self
.assertEqual(url_basename(u
'http://foo.de/bar/baz'), u
'baz')
193 self
.assertEqual(url_basename(u
'http://foo.de/bar/baz?x=y'), u
'baz')
194 self
.assertEqual(url_basename(u
'http://foo.de/bar/baz#x=y'), u
'baz')
195 self
.assertEqual(url_basename(u
'http://foo.de/bar/baz/'), u
'baz')
197 url_basename(u
'http://media.w3.org/2010/05/sintel/trailer.mp4'),
200 def test_parse_duration(self
):
201 self
.assertEqual(parse_duration(None), None)
202 self
.assertEqual(parse_duration('1'), 1)
203 self
.assertEqual(parse_duration('1337:12'), 80232)
204 self
.assertEqual(parse_duration('9:12:43'), 33163)
205 self
.assertEqual(parse_duration('12:00'), 720)
206 self
.assertEqual(parse_duration('00:01:01'), 61)
207 self
.assertEqual(parse_duration('x:y'), None)
208 self
.assertEqual(parse_duration('3h11m53s'), 11513)
209 self
.assertEqual(parse_duration('62m45s'), 3765)
210 self
.assertEqual(parse_duration('6m59s'), 419)
211 self
.assertEqual(parse_duration('49s'), 49)
212 self
.assertEqual(parse_duration('0h0m0s'), 0)
213 self
.assertEqual(parse_duration('0m0s'), 0)
214 self
.assertEqual(parse_duration('0s'), 0)
216 def test_fix_xml_ampersands(self
):
218 fix_xml_ampersands('"&x=y&z=a'), '"&x=y&z=a')
220 fix_xml_ampersands('"&x=y&wrong;&z=a'),
221 '"&x=y&wrong;&z=a')
223 fix_xml_ampersands('&'><"'),
224 '&'><"')
226 fix_xml_ampersands('Ӓ᪼'), 'Ӓ᪼')
227 self
.assertEqual(fix_xml_ampersands('&#&#'), '&#&#')
229 def test_paged_list(self
):
230 def testPL(size
, pagesize
, sliceargs
, expected
):
231 def get_page(pagenum
):
232 firstid
= pagenum
* pagesize
233 upto
= min(size
, pagenum
* pagesize
+ pagesize
)
234 for i
in range(firstid
, upto
):
237 pl
= PagedList(get_page
, pagesize
)
238 got
= pl
.getslice(*sliceargs
)
239 self
.assertEqual(got
, expected
)
241 testPL(5, 2, (), [0, 1, 2, 3, 4])
242 testPL(5, 2, (1,), [1, 2, 3, 4])
243 testPL(5, 2, (2,), [2, 3, 4])
244 testPL(5, 2, (4,), [4])
245 testPL(5, 2, (0, 3), [0, 1, 2])
246 testPL(5, 2, (1, 4), [1, 2, 3])
247 testPL(5, 2, (2, 99), [2, 3, 4])
248 testPL(5, 2, (20, 99), [])
250 def test_struct_unpack(self
):
251 self
.assertEqual(struct_unpack(u
'!B', b
'\x00'), (0,))
253 if __name__
== '__main__':