]> Raphaël G. Git Repositories - youtubedl/blob - test/test_utils.py
e5778cd83ee9ea74e4786243f1e6279aed3697d3
[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 get_meta_content,
20 orderedSet,
21 sanitize_filename,
22 shell_quote,
23 smuggle_url,
24 str_to_int,
25 timeconvert,
26 unescapeHTML,
27 unified_strdate,
28 unsmuggle_url,
29 url_basename,
30 xpath_with_ns,
31 )
32
33 if sys.version_info < (3, 0):
34 _compat_str = lambda b: b.decode('unicode-escape')
35 else:
36 _compat_str = lambda s: s
37
38
39 class TestUtil(unittest.TestCase):
40 def test_timeconvert(self):
41 self.assertTrue(timeconvert('') is None)
42 self.assertTrue(timeconvert('bougrg') is None)
43
44 def test_sanitize_filename(self):
45 self.assertEqual(sanitize_filename('abc'), 'abc')
46 self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
47
48 self.assertEqual(sanitize_filename('123'), '123')
49
50 self.assertEqual('abc_de', sanitize_filename('abc/de'))
51 self.assertFalse('/' in sanitize_filename('abc/de///'))
52
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'))
57
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)
63
64 forbidden = '"\0\\/'
65 for fc in forbidden:
66 for fbc in forbidden:
67 self.assertTrue(fbc not in sanitize_filename(fc))
68
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')
72
73 self.assertEqual(sanitize_filename('123', restricted=True), '123')
74
75 self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
76 self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
77
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))
82
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
86
87 forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
88 for fc in forbidden:
89 for fbc in forbidden:
90 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
91
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) != '')
98
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')
103
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])
110
111 def test_unescape_html(self):
112 self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
113
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)
121
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')
127
128 def test_find_xpath_attr(self):
129 testxml = u'''<root>
130 <node/>
131 <node x="a"/>
132 <node x="a" y="c" />
133 <node x="b" y="d" />
134 </root>'''
135 doc = xml.etree.ElementTree.fromstring(testxml)
136
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])
140
141 def test_meta_parser(self):
142 testhtml = u'''
143 <head>
144 <meta name="description" content="foo &amp; bar">
145 <meta content='Plato' name='author'/>
146 </head>
147 '''
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')
151
152 def test_xpath_with_ns(self):
153 testxml = u'''<root xmlns:media="http://example.com/">
154 <media:song>
155 <media:author>The Author</media:author>
156 <url>http://server.com/download.mp3</url>
157 </media:song>
158 </root>'''
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')
164
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)
172
173 res_url, res_data = unsmuggle_url(url)
174 self.assertEqual(res_url, url)
175 self.assertEqual(res_data, None)
176
177 def test_shell_quote(self):
178 args = ['ffmpeg', '-i', encodeFilename(u'ñ€ß\'.mp4')]
179 self.assertEqual(shell_quote(args), u"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
180
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)
184
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')
191 self.assertEqual(
192 url_basename(u'http://media.w3.org/2010/05/sintel/trailer.mp4'),
193 u'trailer.mp4')
194
195 if __name__ == '__main__':
196 unittest.main()