]>
Raphaël G. Git Repositories - youtubedl/blob - test/test_utils.py
f3fbff042ccc8193d8d08527fdc04421c9832305
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 (
29 if sys
.version_info
< (3, 0):
30 _compat_str
= lambda b
: b
.decode('unicode-escape')
32 _compat_str
= lambda s
: s
35 class TestUtil(unittest
.TestCase
):
36 def test_timeconvert(self
):
37 self
.assertTrue(timeconvert('') is None)
38 self
.assertTrue(timeconvert('bougrg') is None)
40 def test_sanitize_filename(self
):
41 self
.assertEqual(sanitize_filename('abc'), 'abc')
42 self
.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
44 self
.assertEqual(sanitize_filename('123'), '123')
46 self
.assertEqual('abc_de', sanitize_filename('abc/de'))
47 self
.assertFalse('/' in sanitize_filename('abc/de///'))
49 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
50 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
51 self
.assertEqual('yes no', sanitize_filename('yes? no'))
52 self
.assertEqual('this - that', sanitize_filename('this: that'))
54 self
.assertEqual(sanitize_filename('AT&T'), 'AT&T')
55 aumlaut
= _compat_str('\xe4')
56 self
.assertEqual(sanitize_filename(aumlaut
), aumlaut
)
57 tests
= _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
58 self
.assertEqual(sanitize_filename(tests
), tests
)
63 self
.assertTrue(fbc
not in sanitize_filename(fc
))
65 def test_sanitize_filename_restricted(self
):
66 self
.assertEqual(sanitize_filename('abc', restricted
=True), 'abc')
67 self
.assertEqual(sanitize_filename('abc_d-e', restricted
=True), 'abc_d-e')
69 self
.assertEqual(sanitize_filename('123', restricted
=True), '123')
71 self
.assertEqual('abc_de', sanitize_filename('abc/de', restricted
=True))
72 self
.assertFalse('/' in sanitize_filename('abc/de///', restricted
=True))
74 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted
=True))
75 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted
=True))
76 self
.assertEqual('yes_no', sanitize_filename('yes? no', restricted
=True))
77 self
.assertEqual('this_-_that', sanitize_filename('this: that', restricted
=True))
79 tests
= _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
80 self
.assertEqual(sanitize_filename(tests
, restricted
=True), 'a_b_c')
81 self
.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted
=True) != '') # No empty filename
83 forbidden
= '"\0\\/&!: \'\t\n()[]{}$;`^,#'
86 self
.assertTrue(fbc
not in sanitize_filename(fc
, restricted
=True))
88 # Handle a common case more neatly
89 self
.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted
=True), 'Song')
90 self
.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted
=True), 'Speech')
91 # .. but make sure the file name is never empty
92 self
.assertTrue(sanitize_filename('-', restricted
=True) != '')
93 self
.assertTrue(sanitize_filename(':', restricted
=True) != '')
95 def test_sanitize_ids(self
):
96 self
.assertEqual(sanitize_filename('_n_cd26wFpw', is_id
=True), '_n_cd26wFpw')
97 self
.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id
=True), '_BD_eEpuzXw')
98 self
.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id
=True), 'N0Y__7-UOdI')
100 def test_ordered_set(self
):
101 self
.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
102 self
.assertEqual(orderedSet([]), [])
103 self
.assertEqual(orderedSet([1]), [1])
104 #keep the list ordered
105 self
.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
107 def test_unescape_html(self
):
108 self
.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
110 def test_daterange(self
):
111 _20century
= DateRange("19000101","20000101")
112 self
.assertFalse("17890714" in _20century
)
113 _ac
= DateRange("00010101")
114 self
.assertTrue("19690721" in _ac
)
115 _firstmilenium
= DateRange(end
="10000101")
116 self
.assertTrue("07110427" in _firstmilenium
)
118 def test_unified_dates(self
):
119 self
.assertEqual(unified_strdate('December 21, 2010'), '20101221')
120 self
.assertEqual(unified_strdate('8/7/2009'), '20090708')
121 self
.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
122 self
.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
124 def test_find_xpath_attr(self
):
131 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
133 self
.assertEqual(find_xpath_attr(doc
, './/fourohfour', 'n', 'v'), None)
134 self
.assertEqual(find_xpath_attr(doc
, './/node', 'x', 'a'), doc
[1])
135 self
.assertEqual(find_xpath_attr(doc
, './/node', 'y', 'c'), doc
[2])
137 def test_meta_parser(self
):
140 <meta name="description" content="foo & bar">
141 <meta content='Plato' name='author'/>
144 get_meta
= lambda name
: get_meta_content(name
, testhtml
)
145 self
.assertEqual(get_meta('description'), u
'foo & bar')
146 self
.assertEqual(get_meta('author'), 'Plato')
148 def test_xpath_with_ns(self
):
149 testxml
= u
'''<root xmlns:media="http://example.com/">
151 <media:author>The Author</media:author>
152 <url>http://server.com/download.mp3</url>
155 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
156 find
= lambda p
: doc
.find(xpath_with_ns(p
, {'media': 'http://example.com/'}))
157 self
.assertTrue(find('media:song') is not None)
158 self
.assertEqual(find('media:song/media:author').text
, u
'The Author')
159 self
.assertEqual(find('media:song/url').text
, u
'http://server.com/download.mp3')
161 def test_smuggle_url(self
):
162 data
= {u
"ö": u
"ö", u
"abc": [3]}
163 url
= 'https://foo.bar/baz?x=y#a'
164 smug_url
= smuggle_url(url
, data
)
165 unsmug_url
, unsmug_data
= unsmuggle_url(smug_url
)
166 self
.assertEqual(url
, unsmug_url
)
167 self
.assertEqual(data
, unsmug_data
)
169 res_url
, res_data
= unsmuggle_url(url
)
170 self
.assertEqual(res_url
, url
)
171 self
.assertEqual(res_data
, None)
174 if __name__
== '__main__':