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