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