128 lines
3.8 KiB
Python
128 lines
3.8 KiB
Python
"""Tests for the cache manager."""
|
|
|
|
import tempfile
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from src.cache_manager import CacheManager
|
|
|
|
|
|
class TestCacheManager:
|
|
"""Tests for CacheManager class."""
|
|
|
|
@pytest.fixture
|
|
def temp_cache_dir(self):
|
|
"""Create a temporary cache directory."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
yield Path(tmpdir)
|
|
|
|
@pytest.fixture
|
|
def cache_manager(self, temp_cache_dir):
|
|
"""Create a cache manager with temp directory."""
|
|
return CacheManager(cache_dir=temp_cache_dir, ttl=60)
|
|
|
|
def test_set_and_get(self, cache_manager):
|
|
"""Test setting and getting values."""
|
|
cache_manager.set("test_key", "test_value")
|
|
result = cache_manager.get("test_key")
|
|
|
|
assert result == "test_value"
|
|
|
|
def test_get_nonexistent_key(self, cache_manager):
|
|
"""Test getting a key that doesn't exist."""
|
|
result = cache_manager.get("nonexistent_key")
|
|
|
|
assert result is None
|
|
|
|
def test_set_complex_value(self, cache_manager):
|
|
"""Test setting and getting complex values."""
|
|
data = {
|
|
"repo": "test/repo",
|
|
"matches": ["match1", "match2"],
|
|
"count": 5,
|
|
}
|
|
cache_manager.set("complex_key", data)
|
|
result = cache_manager.get("complex_key")
|
|
|
|
assert result == data
|
|
|
|
def test_delete(self, cache_manager):
|
|
"""Test deleting a value."""
|
|
cache_manager.set("delete_key", "value")
|
|
assert cache_manager.get("delete_key") == "value"
|
|
|
|
result = cache_manager.delete("delete_key")
|
|
assert result is True
|
|
assert cache_manager.get("delete_key") is None
|
|
|
|
def test_delete_nonexistent(self, cache_manager):
|
|
"""Test deleting a key that doesn't exist."""
|
|
result = cache_manager.delete("nonexistent")
|
|
assert result is False
|
|
|
|
def test_clear(self, cache_manager):
|
|
"""Test clearing all cache values."""
|
|
cache_manager.set("key1", "value1")
|
|
cache_manager.set("key2", "value2")
|
|
|
|
result = cache_manager.clear()
|
|
assert result is True
|
|
assert cache_manager.get("key1") is None
|
|
assert cache_manager.get("key2") is None
|
|
|
|
def test_get_stats(self, cache_manager):
|
|
"""Test getting cache statistics."""
|
|
cache_manager.set("key", "value")
|
|
stats = cache_manager.get_stats()
|
|
|
|
assert "size" in stats
|
|
assert "hits" in stats
|
|
assert "misses" in stats
|
|
assert "cache_size_mb" in stats
|
|
assert isinstance(stats["cache_size_mb"], float)
|
|
|
|
def test_get_all(self, cache_manager):
|
|
"""Test getting all cached entries."""
|
|
cache_manager.set("key1", "value1")
|
|
cache_manager.set("key2", "value2")
|
|
|
|
entries = cache_manager.get_all()
|
|
|
|
assert len(entries) >= 2
|
|
|
|
def test_key_is_hashed(self, cache_manager):
|
|
"""Test that keys are hashed."""
|
|
cache_manager.set("test_key", "value")
|
|
entries = cache_manager.get_all()
|
|
|
|
assert "test_key" not in entries
|
|
|
|
def test_cleanup(self, cache_manager):
|
|
"""Test cleaning up expired entries."""
|
|
cache_manager.set("key", "value")
|
|
removed = cache_manager.cleanup()
|
|
|
|
assert isinstance(removed, int)
|
|
|
|
|
|
class TestCacheManagerTTL:
|
|
"""Tests for cache TTL functionality."""
|
|
|
|
@pytest.fixture
|
|
def temp_cache_dir(self):
|
|
"""Create a temporary cache directory."""
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
yield Path(tmpdir)
|
|
|
|
def test_ttl_expiration(self, temp_cache_dir):
|
|
"""Test that cached values expire after TTL."""
|
|
cache_manager = CacheManager(cache_dir=temp_cache_dir, ttl=1)
|
|
|
|
cache_manager.set("expiring_key", "value")
|
|
assert cache_manager.get("expiring_key") == "value"
|
|
|
|
time.sleep(1.5)
|
|
|
|
assert cache_manager.get("expiring_key") is None
|