]> Raphaël G. Git Repositories - youtubedl/blob - test/test_cache.py
Start new release.
[youtubedl] / test / test_cache.py
1 #!/usr/bin/env python
2 # coding: utf-8
3
4 from __future__ import unicode_literals
5
6 import shutil
7
8 # Allow direct execution
9 import os
10 import sys
11 import unittest
12 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13
14
15 from test.helper import FakeYDL
16 from youtube_dl.cache import Cache
17
18
19 def _is_empty(d):
20 return not bool(os.listdir(d))
21
22
23 def _mkdir(d):
24 if not os.path.exists(d):
25 os.mkdir(d)
26
27
28 class TestCache(unittest.TestCase):
29 def setUp(self):
30 TEST_DIR = os.path.dirname(os.path.abspath(__file__))
31 TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata')
32 _mkdir(TESTDATA_DIR)
33 self.test_dir = os.path.join(TESTDATA_DIR, 'cache_test')
34 self.tearDown()
35
36 def tearDown(self):
37 if os.path.exists(self.test_dir):
38 shutil.rmtree(self.test_dir)
39
40 def test_cache(self):
41 ydl = FakeYDL({
42 'cachedir': self.test_dir,
43 })
44 c = Cache(ydl)
45 obj = {'x': 1, 'y': ['ä', '\\a', True]}
46 self.assertEqual(c.load('test_cache', 'k.'), None)
47 c.store('test_cache', 'k.', obj)
48 self.assertEqual(c.load('test_cache', 'k2'), None)
49 self.assertFalse(_is_empty(self.test_dir))
50 self.assertEqual(c.load('test_cache', 'k.'), obj)
51 self.assertEqual(c.load('test_cache', 'y'), None)
52 self.assertEqual(c.load('test_cache2', 'k.'), None)
53 c.remove()
54 self.assertFalse(os.path.exists(self.test_dir))
55 self.assertEqual(c.load('test_cache', 'k.'), None)
56
57
58 if __name__ == '__main__':
59 unittest.main()