Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unittest for cache #302

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/python-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Test - Code format
run: |
black --check --diff --color pdf2zh/*.py
black --check --diff --color pdf2zh/*.py test/*.py
flake8 --ignore E203,E261,E501,W503,E741
- name: Test - Unit Test
Expand Down
108 changes: 108 additions & 0 deletions test/test_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import unittest
import os
import tempfile
import shutil
import time
from pdf2zh import cache


class TestCache(unittest.TestCase):
def setUp(self):
# Create a temporary directory for testing
self.test_cache_dir = os.path.join(tempfile.gettempdir(), "test_cache")
self.original_cache_dir = cache.cache_dir
cache.cache_dir = self.test_cache_dir
os.makedirs(self.test_cache_dir, exist_ok=True)

def tearDown(self):
# Clean up the test directory
shutil.rmtree(self.test_cache_dir)
cache.cache_dir = self.original_cache_dir

def test_deterministic_hash(self):
# Test hash generation for different inputs
test_input = "Hello World"
hash1 = cache.deterministic_hash(test_input)
hash2 = cache.deterministic_hash(test_input)
self.assertEqual(hash1, hash2)
self.assertEqual(len(hash1), 20)

# Test different inputs produce different hashes
hash3 = cache.deterministic_hash("Different input")
self.assertNotEqual(hash1, hash3)

def test_get_dirs(self):
# Create test directories
test_dirs = ["dir1", "dir2", "dir3"]
for dir_name in test_dirs:
os.makedirs(os.path.join(self.test_cache_dir, dir_name))

# Create a file (should be ignored)
with open(os.path.join(self.test_cache_dir, "test.txt"), "w") as f:
f.write("test")

dirs = cache.get_dirs()
self.assertEqual(len(dirs), 3)
for dir_path in dirs:
self.assertTrue(os.path.isdir(dir_path))

def test_get_time(self):
# Create test directory with time file
test_dir = os.path.join(self.test_cache_dir, "test_dir")
os.makedirs(test_dir)
test_time = 1234567890.0

with open(os.path.join(test_dir, cache.time_filename), "w") as f:
f.write(str(test_time))

# Test reading time
result = cache.get_time(test_dir)
self.assertEqual(result, test_time)

# Test non-existent directory
non_existent_dir = os.path.join(self.test_cache_dir, "non_existent")
result = cache.get_time(non_existent_dir)
self.assertEqual(result, float("inf"))

def test_write_time(self):
test_dir = os.path.join(self.test_cache_dir, "test_dir")
os.makedirs(test_dir)

cache.write_time(test_dir)

self.assertTrue(os.path.exists(os.path.join(test_dir, cache.time_filename)))
with open(os.path.join(test_dir, cache.time_filename)) as f:
time_value = float(f.read())
self.assertIsInstance(time_value, float)

def test_remove_extra(self):
# Create more than max_cache directories
for i in range(cache.max_cache + 2):
dir_path = os.path.join(self.test_cache_dir, f"dir{i}")
os.makedirs(dir_path)
time.sleep(0.1) # Ensure different timestamps
cache.write_time(dir_path)

cache.remove_extra()

remaining_dirs = cache.get_dirs()
self.assertLessEqual(len(remaining_dirs), cache.max_cache)

def test_cache_operations(self):
test_hash = "test123hash"
test_para_hash = "para456hash"
test_content = "Test paragraph content"

# Test cache creation
self.assertFalse(cache.is_cached(test_hash))
cache.create_cache(test_hash)
self.assertTrue(cache.is_cached(test_hash))

# Test paragraph operations
self.assertIsNone(cache.load_paragraph(test_hash, test_para_hash))
cache.write_paragraph(test_hash, test_para_hash, test_content)
self.assertEqual(cache.load_paragraph(test_hash, test_para_hash), test_content)


if __name__ == "__main__":
unittest.main()
Loading