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