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