test_cache.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  4. import shutil
  5. # Allow direct execution
  6. import os
  7. import sys
  8. import unittest
  9. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  10. from test.helper import FakeYDL
  11. from youtube_dl.cache import Cache
  12. def _is_empty(d):
  13. return not bool(os.listdir(d))
  14. def _mkdir(d):
  15. if not os.path.exists(d):
  16. os.mkdir(d)
  17. class TestCache(unittest.TestCase):
  18. def setUp(self):
  19. TEST_DIR = os.path.dirname(os.path.abspath(__file__))
  20. TESTDATA_DIR = os.path.join(TEST_DIR, 'testdata')
  21. _mkdir(TESTDATA_DIR)
  22. self.test_dir = os.path.join(TESTDATA_DIR, 'cache_test')
  23. self.tearDown()
  24. def tearDown(self):
  25. if os.path.exists(self.test_dir):
  26. shutil.rmtree(self.test_dir)
  27. def test_cache(self):
  28. ydl = FakeYDL({
  29. 'cachedir': self.test_dir,
  30. })
  31. c = Cache(ydl)
  32. obj = {'x': 1, 'y': ['ä', '\\a', True]}
  33. self.assertEqual(c.load('test_cache', 'k.'), None)
  34. c.store('test_cache', 'k.', obj)
  35. self.assertEqual(c.load('test_cache', 'k2'), None)
  36. self.assertFalse(_is_empty(self.test_dir))
  37. self.assertEqual(c.load('test_cache', 'k.'), obj)
  38. self.assertEqual(c.load('test_cache', 'y'), None)
  39. self.assertEqual(c.load('test_cache2', 'k.'), None)
  40. c.remove()
  41. self.assertFalse(os.path.exists(self.test_dir))
  42. self.assertEqual(c.load('test_cache', 'k.'), None)
  43. if __name__ == '__main__':
  44. unittest.main()