test_cache.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import unittest
  2. import os
  3. import tempfile
  4. import shutil
  5. import time
  6. from pdf2zh import cache
  7. class TestCache(unittest.TestCase):
  8. def setUp(self):
  9. # Create a temporary directory for testing
  10. self.test_cache_dir = os.path.join(tempfile.gettempdir(), "test_cache")
  11. self.original_cache_dir = cache.cache_dir
  12. cache.cache_dir = self.test_cache_dir
  13. os.makedirs(self.test_cache_dir, exist_ok=True)
  14. def tearDown(self):
  15. # Clean up the test directory
  16. shutil.rmtree(self.test_cache_dir)
  17. cache.cache_dir = self.original_cache_dir
  18. def test_deterministic_hash(self):
  19. # Test hash generation for different inputs
  20. test_input = "Hello World"
  21. hash1 = cache.deterministic_hash(test_input)
  22. hash2 = cache.deterministic_hash(test_input)
  23. self.assertEqual(hash1, hash2)
  24. self.assertEqual(len(hash1), 20)
  25. # Test different inputs produce different hashes
  26. hash3 = cache.deterministic_hash("Different input")
  27. self.assertNotEqual(hash1, hash3)
  28. def test_get_dirs(self):
  29. # Create test directories
  30. test_dirs = ["dir1", "dir2", "dir3"]
  31. for dir_name in test_dirs:
  32. os.makedirs(os.path.join(self.test_cache_dir, dir_name))
  33. # Create a file (should be ignored)
  34. with open(os.path.join(self.test_cache_dir, "test.txt"), "w") as f:
  35. f.write("test")
  36. dirs = cache.get_dirs()
  37. self.assertEqual(len(dirs), 3)
  38. for dir_path in dirs:
  39. self.assertTrue(os.path.isdir(dir_path))
  40. def test_get_time(self):
  41. # Create test directory with time file
  42. test_dir = os.path.join(self.test_cache_dir, "test_dir")
  43. os.makedirs(test_dir)
  44. test_time = 1234567890.0
  45. with open(os.path.join(test_dir, cache.time_filename), "w") as f:
  46. f.write(str(test_time))
  47. # Test reading time
  48. result = cache.get_time(test_dir)
  49. self.assertEqual(result, test_time)
  50. # Test non-existent directory
  51. non_existent_dir = os.path.join(self.test_cache_dir, "non_existent")
  52. result = cache.get_time(non_existent_dir)
  53. self.assertEqual(result, float("inf"))
  54. def test_write_time(self):
  55. test_dir = os.path.join(self.test_cache_dir, "test_dir")
  56. os.makedirs(test_dir)
  57. cache.write_time(test_dir)
  58. self.assertTrue(os.path.exists(os.path.join(test_dir, cache.time_filename)))
  59. with open(os.path.join(test_dir, cache.time_filename)) as f:
  60. time_value = float(f.read())
  61. self.assertIsInstance(time_value, float)
  62. def test_remove_extra(self):
  63. # Create more than max_cache directories
  64. for i in range(cache.max_cache + 2):
  65. dir_path = os.path.join(self.test_cache_dir, f"dir{i}")
  66. os.makedirs(dir_path)
  67. time.sleep(0.1) # Ensure different timestamps
  68. cache.write_time(dir_path)
  69. cache.remove_extra()
  70. remaining_dirs = cache.get_dirs()
  71. self.assertLessEqual(len(remaining_dirs), cache.max_cache)
  72. def test_cache_operations(self):
  73. test_hash = "test123hash"
  74. test_para_hash = "para456hash"
  75. test_content = "Test paragraph content"
  76. # Test cache creation
  77. self.assertFalse(cache.is_cached(test_hash))
  78. cache.create_cache(test_hash)
  79. self.assertTrue(cache.is_cached(test_hash))
  80. # Test paragraph operations
  81. self.assertIsNone(cache.load_paragraph(test_hash, test_para_hash))
  82. cache.write_paragraph(test_hash, test_para_hash, test_content)
  83. self.assertEqual(cache.load_paragraph(test_hash, test_para_hash), test_content)
  84. if __name__ == "__main__":
  85. unittest.main()