aboutsummaryrefslogtreecommitdiff
path: root/test/test_cache.py
diff options
context:
space:
mode:
authorPhilipp Hagemeister <phihag@phihag.de>2014-09-03 12:41:05 +0200
committerPhilipp Hagemeister <phihag@phihag.de>2014-09-03 17:29:19 +0200
commita0e07d31616102ac905c0519474d2c01db7ee392 (patch)
treedc069a812353d4a3810e42e247d41ddaebadb843 /test/test_cache.py
parent88fc294f7f03ce6af9787c6d9c3bad7fc5fade10 (diff)
[youtube] Move cache into its own module
Diffstat (limited to 'test/test_cache.py')
-rw-r--r--test/test_cache.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/test/test_cache.py b/test/test_cache.py
new file mode 100644
index 000000000..dbefad109
--- /dev/null
+++ b/test/test_cache.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python
+# coding: utf-8
+
+from __future__ import unicode_literals
+
+import shutil
+
+# Allow direct execution
+import os
+import sys
+import unittest
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+
+from test.helper import FakeYDL
+from youtube_dl.cache import Cache
+
+
+def _is_empty(d):
+ return not bool(os.listdir(d))
+
+
+def _mkdir(d):
+ if not os.path.exists(d):
+ os.mkdir(d)
+
+
+class TestCache(unittest.TestCase):
+ def setUp(self):
+ TEST_DIR = os.path.dirname(os.path.abspath(__file__))
+ TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata')
+ _mkdir(TESTDATA_DIR)
+ self.test_dir = os.path.join(TESTDATA_DIR, 'cache_test')
+ self.tearDown()
+
+ def tearDown(self):
+ if os.path.exists(self.test_dir):
+ shutil.rmtree(self.test_dir)
+
+ def test_cache(self):
+ ydl = FakeYDL({
+ 'cachedir': self.test_dir,
+ })
+ c = Cache(ydl)
+ obj = {'x': 1, 'y': ['รค', '\\a', True]}
+ self.assertEqual(c.load('test_cache', 'k'), None)
+ c.store('test_cache', 'k', obj)
+ self.assertEqual(c.load('test_cache', 'k2'), None)
+ self.assertFalse(_is_empty(self.test_dir))
+ self.assertEqual(c.load('test_cache', 'k'), obj)
+ self.assertEqual(c.load('test_cache', 'y'), None)
+ self.assertEqual(c.load('test_cache2', 'k'), None)
+ c.remove()
+ self.assertFalse(os.path.exists(self.test_dir))
+ self.assertEqual(c.load('test_cache', 'k'), None)
+
+
+if __name__ == '__main__':
+ unittest.main()