]> Raphaël G. Git Repositories - youtubedl/blob - test/test_utils.py
84553b94386236352e9ad856782c4b3428f3587b
[youtubedl] / test / test_utils.py
1 #!/usr/bin/env python
2 # coding: utf-8
3
4 # Allow direct execution
5 import os
6 import sys
7 import unittest
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10
11 # Various small unit tests
12 import xml.etree.ElementTree
13
14 #from youtube_dl.utils import htmlentity_transform
15 from youtube_dl.utils import (
16 DateRange,
17 encodeFilename,
18 find_xpath_attr,
19 fix_xml_ampersands,
20 get_meta_content,
21 orderedSet,
22 PagedList,
23 parse_duration,
24 sanitize_filename,
25 shell_quote,
26 smuggle_url,
27 str_to_int,
28 struct_unpack,
29 timeconvert,
30 unescapeHTML,
31 unified_strdate,
32 unsmuggle_url,
33 url_basename,
34 xpath_with_ns,
35 )
36
37 if sys.version_info < (3, 0):
38 _compat_str = lambda b: b.decode('unicode-escape')
39 else:
40 _compat_str = lambda s: s
41
42
43 class TestUtil(unittest.TestCase):
44 def test_timeconvert(self):
45 self.assertTrue(timeconvert('') is None)
46 self.assertTrue(timeconvert('bougrg') is None)
47
48 def test_sanitize_filename(self):
49 self.assertEqual(sanitize_filename('abc'), 'abc')
50 self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
51
52 self.assertEqual(sanitize_filename('123'), '123')
53
54 self.assertEqual('abc_de', sanitize_filename('abc/de'))
55 self.assertFalse('/' in sanitize_filename('abc/de///'))
56
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'))
61
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)
67
68 forbidden = '"\0\\/'
69 for fc in forbidden:
70 for fbc in forbidden:
71 self.assertTrue(fbc not in sanitize_filename(fc))
72
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')
76
77 self.assertEqual(sanitize_filename('123', restricted=True), '123')
78
79 self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
80 self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
81
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))
86
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
90
91 forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
92 for fc in forbidden:
93 for fbc in forbidden:
94 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
95
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) != '')
102
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')
107
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])
114
115 def test_unescape_html(self):
116 self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
117
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)
125
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')
132
133 def test_find_xpath_attr(self):
134 testxml = u'''<root>
135 <node/>
136 <node x="a"/>
137 <node x="a" y="c" />
138 <node x="b" y="d" />
139 </root>'''
140 doc = xml.etree.ElementTree.fromstring(testxml)
141
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])
145
146 def test_meta_parser(self):
147 testhtml = u'''
148 <head>
149 <meta name="description" content="foo &amp; bar">
150 <meta content='Plato' name='author'/>
151 </head>
152 '''
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')
156
157 def test_xpath_with_ns(self):
158 testxml = u'''<root xmlns:media="http://example.com/">
159 <media:song>
160 <media:author>The Author</media:author>
161 <url>http://server.com/download.mp3</url>
162 </media:song>
163 </root>'''
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')
169
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)
177
178 res_url, res_data = unsmuggle_url(url)
179 self.assertEqual(res_url, url)
180 self.assertEqual(res_data, None)
181
182 def test_shell_quote(self):
183 args = ['ffmpeg', '-i', encodeFilename(u'ñ€ß\'.mp4')]
184 self.assertEqual(shell_quote(args), u"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
185
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)
189
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')
196 self.assertEqual(
197 url_basename(u'http://media.w3.org/2010/05/sintel/trailer.mp4'),
198 u'trailer.mp4')
199
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)
215
216 def test_fix_xml_ampersands(self):
217 self.assertEqual(
218 fix_xml_ampersands('"&x=y&z=a'), '"&amp;x=y&amp;z=a')
219 self.assertEqual(
220 fix_xml_ampersands('"&amp;x=y&wrong;&z=a'),
221 '"&amp;x=y&amp;wrong;&amp;z=a')
222 self.assertEqual(
223 fix_xml_ampersands('&amp;&apos;&gt;&lt;&quot;'),
224 '&amp;&apos;&gt;&lt;&quot;')
225 self.assertEqual(
226 fix_xml_ampersands('&#1234;&#x1abC;'), '&#1234;&#x1abC;')
227 self.assertEqual(fix_xml_ampersands('&#&#'), '&amp;#&amp;#')
228
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):
235 yield i
236
237 pl = PagedList(get_page, pagesize)
238 got = pl.getslice(*sliceargs)
239 self.assertEqual(got, expected)
240
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), [])
249
250 def test_struct_unpack(self):
251 self.assertEqual(struct_unpack(u'!B', b'\x00'), (0,))
252
253 if __name__ == '__main__':
254 unittest.main()