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