test_cache.py 1.5 KB

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