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