]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_utils.py
Update changelog.
[youtubedl] / test / test_utils.py
1 #!/usr/bin/env python
2
3 # Various small unit tests
4
5 import sys
6 import unittest
7 import xml.etree.ElementTree
8
9 # Allow direct execution
10 import os
11 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12
13 #from youtube_dl.utils import htmlentity_transform
14 from youtube_dl.utils import timeconvert
15 from youtube_dl.utils import sanitize_filename
16 from youtube_dl.utils import unescapeHTML
17 from youtube_dl.utils import orderedSet
18 from youtube_dl.utils import DateRange
19 from youtube_dl.utils import unified_strdate
20 from youtube_dl.utils import find_xpath_attr
21
22 if sys.version_info < (3, 0):
23 _compat_str = lambda b: b.decode('unicode-escape')
24 else:
25 _compat_str = lambda s: s
26
27
28 class TestUtil(unittest.TestCase):
29 def test_timeconvert(self):
30 self.assertTrue(timeconvert('') is None)
31 self.assertTrue(timeconvert('bougrg') is None)
32
33 def test_sanitize_filename(self):
34 self.assertEqual(sanitize_filename('abc'), 'abc')
35 self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
36
37 self.assertEqual(sanitize_filename('123'), '123')
38
39 self.assertEqual('abc_de', sanitize_filename('abc/de'))
40 self.assertFalse('/' in sanitize_filename('abc/de///'))
41
42 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
43 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
44 self.assertEqual('yes no', sanitize_filename('yes? no'))
45 self.assertEqual('this - that', sanitize_filename('this: that'))
46
47 self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
48 aumlaut = _compat_str('\xe4')
49 self.assertEqual(sanitize_filename(aumlaut), aumlaut)
50 tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
51 self.assertEqual(sanitize_filename(tests), tests)
52
53 forbidden = '"\0\\/'
54 for fc in forbidden:
55 for fbc in forbidden:
56 self.assertTrue(fbc not in sanitize_filename(fc))
57
58 def test_sanitize_filename_restricted(self):
59 self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
60 self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
61
62 self.assertEqual(sanitize_filename('123', restricted=True), '123')
63
64 self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
65 self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
66
67 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
68 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
69 self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
70 self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
71
72 tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
73 self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
74 self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename
75
76 forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
77 for fc in forbidden:
78 for fbc in forbidden:
79 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
80
81 # Handle a common case more neatly
82 self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
83 self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
84 # .. but make sure the file name is never empty
85 self.assertTrue(sanitize_filename('-', restricted=True) != '')
86 self.assertTrue(sanitize_filename(':', restricted=True) != '')
87
88 def test_sanitize_ids(self):
89 self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
90 self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
91 self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
92
93 def test_ordered_set(self):
94 self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
95 self.assertEqual(orderedSet([]), [])
96 self.assertEqual(orderedSet([1]), [1])
97 #keep the list ordered
98 self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
99
100 def test_unescape_html(self):
101 self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
102
103 def test_daterange(self):
104 _20century = DateRange("19000101","20000101")
105 self.assertFalse("17890714" in _20century)
106 _ac = DateRange("00010101")
107 self.assertTrue("19690721" in _ac)
108 _firstmilenium = DateRange(end="10000101")
109 self.assertTrue("07110427" in _firstmilenium)
110
111 def test_unified_dates(self):
112 self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
113 self.assertEqual(unified_strdate('8/7/2009'), '20090708')
114 self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
115 self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
116
117 def test_find_xpath_attr(self):
118 testxml = u'''<root>
119 <node/>
120 <node x="a"/>
121 <node x="a" y="c" />
122 <node x="b" y="d" />
123 </root>'''
124 doc = xml.etree.ElementTree.fromstring(testxml)
125
126 self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
127 self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
128 self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
129
130 if __name__ == '__main__':
131 unittest.main()