]>
Raphaël G. Git Repositories - youtubedl/blob - test/test_utils.py
e9e590e749f131a0950c79bcf4fee1e9fb9004c2
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 (
31 if sys
.version_info
< (3, 0):
32 _compat_str
= lambda b
: b
.decode('unicode-escape')
34 _compat_str
= lambda s
: s
37 class TestUtil(unittest
.TestCase
):
38 def test_timeconvert(self
):
39 self
.assertTrue(timeconvert('') is None)
40 self
.assertTrue(timeconvert('bougrg') is None)
42 def test_sanitize_filename(self
):
43 self
.assertEqual(sanitize_filename('abc'), 'abc')
44 self
.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
46 self
.assertEqual(sanitize_filename('123'), '123')
48 self
.assertEqual('abc_de', sanitize_filename('abc/de'))
49 self
.assertFalse('/' in sanitize_filename('abc/de///'))
51 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
52 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
53 self
.assertEqual('yes no', sanitize_filename('yes? no'))
54 self
.assertEqual('this - that', sanitize_filename('this: that'))
56 self
.assertEqual(sanitize_filename('AT&T'), 'AT&T')
57 aumlaut
= _compat_str('\xe4')
58 self
.assertEqual(sanitize_filename(aumlaut
), aumlaut
)
59 tests
= _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
60 self
.assertEqual(sanitize_filename(tests
), tests
)
65 self
.assertTrue(fbc
not in sanitize_filename(fc
))
67 def test_sanitize_filename_restricted(self
):
68 self
.assertEqual(sanitize_filename('abc', restricted
=True), 'abc')
69 self
.assertEqual(sanitize_filename('abc_d-e', restricted
=True), 'abc_d-e')
71 self
.assertEqual(sanitize_filename('123', restricted
=True), '123')
73 self
.assertEqual('abc_de', sanitize_filename('abc/de', restricted
=True))
74 self
.assertFalse('/' in sanitize_filename('abc/de///', restricted
=True))
76 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted
=True))
77 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted
=True))
78 self
.assertEqual('yes_no', sanitize_filename('yes? no', restricted
=True))
79 self
.assertEqual('this_-_that', sanitize_filename('this: that', restricted
=True))
81 tests
= _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
82 self
.assertEqual(sanitize_filename(tests
, restricted
=True), 'a_b_c')
83 self
.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted
=True) != '') # No empty filename
85 forbidden
= '"\0\\/&!: \'\t\n()[]{}$;`^,#'
88 self
.assertTrue(fbc
not in sanitize_filename(fc
, restricted
=True))
90 # Handle a common case more neatly
91 self
.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted
=True), 'Song')
92 self
.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted
=True), 'Speech')
93 # .. but make sure the file name is never empty
94 self
.assertTrue(sanitize_filename('-', restricted
=True) != '')
95 self
.assertTrue(sanitize_filename(':', restricted
=True) != '')
97 def test_sanitize_ids(self
):
98 self
.assertEqual(sanitize_filename('_n_cd26wFpw', is_id
=True), '_n_cd26wFpw')
99 self
.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id
=True), '_BD_eEpuzXw')
100 self
.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id
=True), 'N0Y__7-UOdI')
102 def test_ordered_set(self
):
103 self
.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
104 self
.assertEqual(orderedSet([]), [])
105 self
.assertEqual(orderedSet([1]), [1])
106 #keep the list ordered
107 self
.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
109 def test_unescape_html(self
):
110 self
.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
112 def test_daterange(self
):
113 _20century
= DateRange("19000101","20000101")
114 self
.assertFalse("17890714" in _20century
)
115 _ac
= DateRange("00010101")
116 self
.assertTrue("19690721" in _ac
)
117 _firstmilenium
= DateRange(end
="10000101")
118 self
.assertTrue("07110427" in _firstmilenium
)
120 def test_unified_dates(self
):
121 self
.assertEqual(unified_strdate('December 21, 2010'), '20101221')
122 self
.assertEqual(unified_strdate('8/7/2009'), '20090708')
123 self
.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
124 self
.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
126 def test_find_xpath_attr(self
):
133 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
135 self
.assertEqual(find_xpath_attr(doc
, './/fourohfour', 'n', 'v'), None)
136 self
.assertEqual(find_xpath_attr(doc
, './/node', 'x', 'a'), doc
[1])
137 self
.assertEqual(find_xpath_attr(doc
, './/node', 'y', 'c'), doc
[2])
139 def test_meta_parser(self
):
142 <meta name="description" content="foo & bar">
143 <meta content='Plato' name='author'/>
146 get_meta
= lambda name
: get_meta_content(name
, testhtml
)
147 self
.assertEqual(get_meta('description'), u
'foo & bar')
148 self
.assertEqual(get_meta('author'), 'Plato')
150 def test_xpath_with_ns(self
):
151 testxml
= u
'''<root xmlns:media="http://example.com/">
153 <media:author>The Author</media:author>
154 <url>http://server.com/download.mp3</url>
157 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
158 find
= lambda p
: doc
.find(xpath_with_ns(p
, {'media': 'http://example.com/'}))
159 self
.assertTrue(find('media:song') is not None)
160 self
.assertEqual(find('media:song/media:author').text
, u
'The Author')
161 self
.assertEqual(find('media:song/url').text
, u
'http://server.com/download.mp3')
163 def test_smuggle_url(self
):
164 data
= {u
"ö": u
"ö", u
"abc": [3]}
165 url
= 'https://foo.bar/baz?x=y#a'
166 smug_url
= smuggle_url(url
, data
)
167 unsmug_url
, unsmug_data
= unsmuggle_url(smug_url
)
168 self
.assertEqual(url
, unsmug_url
)
169 self
.assertEqual(data
, unsmug_data
)
171 res_url
, res_data
= unsmuggle_url(url
)
172 self
.assertEqual(res_url
, url
)
173 self
.assertEqual(res_data
, None)
175 def test_shell_quote(self
):
176 args
= ['ffmpeg', '-i', encodeFilename(u
'ñ€ß\'.mp4')]
177 self
.assertEqual(shell_quote(args
), u
"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
180 if __name__
== '__main__':