]> jfr.im git - yt-dlp.git/blame - test/test_cache.py
[build] Exclude `requests` from `py2exe` (#9982)
[yt-dlp.git] / test / test_cache.py
CommitLineData
cc52de43 1#!/usr/bin/env python3
54007a45 2
a0e07d31
PH
3# Allow direct execution
4import os
5import sys
6import unittest
f8271158 7
a0e07d31
PH
8sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10
54007a45 11import shutil
f8271158 12
54007a45 13from test.helper import FakeYDL
7a5c1cfe 14from yt_dlp.cache import Cache
a0e07d31
PH
15
16
17def _is_empty(d):
18 return not bool(os.listdir(d))
19
20
21def _mkdir(d):
22 if not os.path.exists(d):
23 os.mkdir(d)
24
25
26class 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]}
5df921b0
PH
44 self.assertEqual(c.load('test_cache', 'k.'), None)
45 c.store('test_cache', 'k.', obj)
a0e07d31
PH
46 self.assertEqual(c.load('test_cache', 'k2'), None)
47 self.assertFalse(_is_empty(self.test_dir))
5df921b0 48 self.assertEqual(c.load('test_cache', 'k.'), obj)
a0e07d31 49 self.assertEqual(c.load('test_cache', 'y'), None)
5df921b0 50 self.assertEqual(c.load('test_cache2', 'k.'), None)
a0e07d31
PH
51 c.remove()
52 self.assertFalse(os.path.exists(self.test_dir))
5df921b0 53 self.assertEqual(c.load('test_cache', 'k.'), None)
a0e07d31
PH
54
55
56if __name__ == '__main__':
57 unittest.main()