]> Raphaël G. Git Repositories - youtubedl/blob - test/test_utils.py
f3fbff042ccc8193d8d08527fdc04421c9832305
[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 timeconvert,
17 sanitize_filename,
18 unescapeHTML,
19 orderedSet,
20 DateRange,
21 unified_strdate,
22 find_xpath_attr,
23 get_meta_content,
24 xpath_with_ns,
25 smuggle_url,
26 unsmuggle_url,
27 )
28
29 if sys.version_info < (3, 0):
30 _compat_str = lambda b: b.decode('unicode-escape')
31 else:
32 _compat_str = lambda s: s
33
34
35 class TestUtil(unittest.TestCase):
36 def test_timeconvert(self):
37 self.assertTrue(timeconvert('') is None)
38 self.assertTrue(timeconvert('bougrg') is None)
39
40 def test_sanitize_filename(self):
41 self.assertEqual(sanitize_filename('abc'), 'abc')
42 self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
43
44 self.assertEqual(sanitize_filename('123'), '123')
45
46 self.assertEqual('abc_de', sanitize_filename('abc/de'))
47 self.assertFalse('/' in sanitize_filename('abc/de///'))
48
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'))
53
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)
59
60 forbidden = '"\0\\/'
61 for fc in forbidden:
62 for fbc in forbidden:
63 self.assertTrue(fbc not in sanitize_filename(fc))
64
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')
68
69 self.assertEqual(sanitize_filename('123', restricted=True), '123')
70
71 self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
72 self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
73
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))
78
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
82
83 forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
84 for fc in forbidden:
85 for fbc in forbidden:
86 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
87
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) != '')
94
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')
99
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])
106
107 def test_unescape_html(self):
108 self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
109
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)
117
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')
123
124 def test_find_xpath_attr(self):
125 testxml = u'''<root>
126 <node/>
127 <node x="a"/>
128 <node x="a" y="c" />
129 <node x="b" y="d" />
130 </root>'''
131 doc = xml.etree.ElementTree.fromstring(testxml)
132
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])
136
137 def test_meta_parser(self):
138 testhtml = u'''
139 <head>
140 <meta name="description" content="foo &amp; bar">
141 <meta content='Plato' name='author'/>
142 </head>
143 '''
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')
147
148 def test_xpath_with_ns(self):
149 testxml = u'''<root xmlns:media="http://example.com/">
150 <media:song>
151 <media:author>The Author</media:author>
152 <url>http://server.com/download.mp3</url>
153 </media:song>
154 </root>'''
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')
160
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)
168
169 res_url, res_data = unsmuggle_url(url)
170 self.assertEqual(res_url, url)
171 self.assertEqual(res_data, None)
172
173
174 if __name__ == '__main__':
175 unittest.main()