]>
Raphaël G. Git Repositories - youtubedl/blob - test/test_utils.py
e5778cd83ee9ea74e4786243f1e6279aed3697d3
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 (
33 if sys
.version_info
< (3, 0):
34 _compat_str
= lambda b
: b
.decode('unicode-escape')
36 _compat_str
= lambda s
: s
39 class TestUtil(unittest
.TestCase
):
40 def test_timeconvert(self
):
41 self
.assertTrue(timeconvert('') is None)
42 self
.assertTrue(timeconvert('bougrg') is None)
44 def test_sanitize_filename(self
):
45 self
.assertEqual(sanitize_filename('abc'), 'abc')
46 self
.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
48 self
.assertEqual(sanitize_filename('123'), '123')
50 self
.assertEqual('abc_de', sanitize_filename('abc/de'))
51 self
.assertFalse('/' in sanitize_filename('abc/de///'))
53 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
54 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
55 self
.assertEqual('yes no', sanitize_filename('yes? no'))
56 self
.assertEqual('this - that', sanitize_filename('this: that'))
58 self
.assertEqual(sanitize_filename('AT&T'), 'AT&T')
59 aumlaut
= _compat_str('\xe4')
60 self
.assertEqual(sanitize_filename(aumlaut
), aumlaut
)
61 tests
= _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
62 self
.assertEqual(sanitize_filename(tests
), tests
)
67 self
.assertTrue(fbc
not in sanitize_filename(fc
))
69 def test_sanitize_filename_restricted(self
):
70 self
.assertEqual(sanitize_filename('abc', restricted
=True), 'abc')
71 self
.assertEqual(sanitize_filename('abc_d-e', restricted
=True), 'abc_d-e')
73 self
.assertEqual(sanitize_filename('123', restricted
=True), '123')
75 self
.assertEqual('abc_de', sanitize_filename('abc/de', restricted
=True))
76 self
.assertFalse('/' in sanitize_filename('abc/de///', restricted
=True))
78 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted
=True))
79 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted
=True))
80 self
.assertEqual('yes_no', sanitize_filename('yes? no', restricted
=True))
81 self
.assertEqual('this_-_that', sanitize_filename('this: that', restricted
=True))
83 tests
= _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
84 self
.assertEqual(sanitize_filename(tests
, restricted
=True), 'a_b_c')
85 self
.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted
=True) != '') # No empty filename
87 forbidden
= '"\0\\/&!: \'\t\n()[]{}$;`^,#'
90 self
.assertTrue(fbc
not in sanitize_filename(fc
, restricted
=True))
92 # Handle a common case more neatly
93 self
.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted
=True), 'Song')
94 self
.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted
=True), 'Speech')
95 # .. but make sure the file name is never empty
96 self
.assertTrue(sanitize_filename('-', restricted
=True) != '')
97 self
.assertTrue(sanitize_filename(':', restricted
=True) != '')
99 def test_sanitize_ids(self
):
100 self
.assertEqual(sanitize_filename('_n_cd26wFpw', is_id
=True), '_n_cd26wFpw')
101 self
.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id
=True), '_BD_eEpuzXw')
102 self
.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id
=True), 'N0Y__7-UOdI')
104 def test_ordered_set(self
):
105 self
.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
106 self
.assertEqual(orderedSet([]), [])
107 self
.assertEqual(orderedSet([1]), [1])
108 #keep the list ordered
109 self
.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
111 def test_unescape_html(self
):
112 self
.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
114 def test_daterange(self
):
115 _20century
= DateRange("19000101","20000101")
116 self
.assertFalse("17890714" in _20century
)
117 _ac
= DateRange("00010101")
118 self
.assertTrue("19690721" in _ac
)
119 _firstmilenium
= DateRange(end
="10000101")
120 self
.assertTrue("07110427" in _firstmilenium
)
122 def test_unified_dates(self
):
123 self
.assertEqual(unified_strdate('December 21, 2010'), '20101221')
124 self
.assertEqual(unified_strdate('8/7/2009'), '20090708')
125 self
.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
126 self
.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
128 def test_find_xpath_attr(self
):
135 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
137 self
.assertEqual(find_xpath_attr(doc
, './/fourohfour', 'n', 'v'), None)
138 self
.assertEqual(find_xpath_attr(doc
, './/node', 'x', 'a'), doc
[1])
139 self
.assertEqual(find_xpath_attr(doc
, './/node', 'y', 'c'), doc
[2])
141 def test_meta_parser(self
):
144 <meta name="description" content="foo & bar">
145 <meta content='Plato' name='author'/>
148 get_meta
= lambda name
: get_meta_content(name
, testhtml
)
149 self
.assertEqual(get_meta('description'), u
'foo & bar')
150 self
.assertEqual(get_meta('author'), 'Plato')
152 def test_xpath_with_ns(self
):
153 testxml
= u
'''<root xmlns:media="http://example.com/">
155 <media:author>The Author</media:author>
156 <url>http://server.com/download.mp3</url>
159 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
160 find
= lambda p
: doc
.find(xpath_with_ns(p
, {'media': 'http://example.com/'}))
161 self
.assertTrue(find('media:song') is not None)
162 self
.assertEqual(find('media:song/media:author').text
, u
'The Author')
163 self
.assertEqual(find('media:song/url').text
, u
'http://server.com/download.mp3')
165 def test_smuggle_url(self
):
166 data
= {u
"ö": u
"ö", u
"abc": [3]}
167 url
= 'https://foo.bar/baz?x=y#a'
168 smug_url
= smuggle_url(url
, data
)
169 unsmug_url
, unsmug_data
= unsmuggle_url(smug_url
)
170 self
.assertEqual(url
, unsmug_url
)
171 self
.assertEqual(data
, unsmug_data
)
173 res_url
, res_data
= unsmuggle_url(url
)
174 self
.assertEqual(res_url
, url
)
175 self
.assertEqual(res_data
, None)
177 def test_shell_quote(self
):
178 args
= ['ffmpeg', '-i', encodeFilename(u
'ñ€ß\'.mp4')]
179 self
.assertEqual(shell_quote(args
), u
"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
181 def test_str_to_int(self
):
182 self
.assertEqual(str_to_int('123,456'), 123456)
183 self
.assertEqual(str_to_int('123.456'), 123456)
185 def test_url_basename(self
):
186 self
.assertEqual(url_basename(u
'http://foo.de/'), u
'')
187 self
.assertEqual(url_basename(u
'http://foo.de/bar/baz'), u
'baz')
188 self
.assertEqual(url_basename(u
'http://foo.de/bar/baz?x=y'), u
'baz')
189 self
.assertEqual(url_basename(u
'http://foo.de/bar/baz#x=y'), u
'baz')
190 self
.assertEqual(url_basename(u
'http://foo.de/bar/baz/'), u
'baz')
192 url_basename(u
'http://media.w3.org/2010/05/sintel/trailer.mp4'),
195 if __name__
== '__main__':