Release 2024.07.02
[yt-dlp.git] / test / test_cache.py
blobce1624b68b7ab2e3016f3290b77515de7b299409
1 #!/usr/bin/env python3
3 # Allow direct execution
4 import os
5 import sys
6 import unittest
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11 import shutil
13 from test.helper import FakeYDL
14 from yt_dlp.cache import Cache
17 def _is_empty(d):
18 return not bool(os.listdir(d))
21 def _mkdir(d):
22 if not os.path.exists(d):
23 os.mkdir(d)
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()
34 def tearDown(self):
35 if os.path.exists(self.test_dir):
36 shutil.rmtree(self.test_dir)
38 def test_cache(self):
39 ydl = FakeYDL({
40 'cachedir': self.test_dir,
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)
56 if __name__ == '__main__':
57 unittest.main()