Buckets:

download
raw
14.5 kB
"""Tests for core/cache.py - Caching System."""
import json
import pickle
import sys
from collections import OrderedDict
from datetime import datetime, timedelta
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from core.cache import (
CacheBackend,
CacheStats,
MemoryCache,
_get_cache_backend,
cache_cve,
generate_cache_key,
get_cached_cve,
)
class TestCacheStats:
"""Test CacheStats dataclass."""
def test_default_values(self):
"""Test default initialization."""
stats = CacheStats()
assert stats.hits == 0
assert stats.misses == 0
assert stats.evictions == 0
assert stats.total_gets == 0
assert stats.total_sets == 0
assert stats.total_deletes == 0
assert stats.bytes_stored == 0
def test_hit_rate_empty(self):
"""Test hit rate with no gets."""
stats = CacheStats()
assert stats.hit_rate == 0.0
def test_hit_rate_with_hits(self):
"""Test hit rate calculation."""
stats = CacheStats(hits=80, total_gets=100)
assert stats.hit_rate == 0.8
def test_miss_rate(self):
"""Test miss rate calculation."""
stats = CacheStats(misses=20, total_gets=100)
assert stats.miss_rate == 0.2
def test_to_dict(self):
"""Test serialization to dict."""
stats = CacheStats(
hits=80,
misses=20,
evictions=5,
total_gets=100,
total_sets=50,
total_deletes=10,
bytes_stored=1024,
)
d = stats.to_dict()
assert d["hits"] == 80
assert d["misses"] == 20
assert d["hit_rate"] == "80.00%"
assert d["miss_rate"] == "20.00%"
assert d["bytes_stored"] == 1024
class TestCacheBackend:
"""Test CacheBackend abstract class."""
def test_init(self):
"""Test initialization."""
backend = CacheBackend()
assert isinstance(backend.stats, CacheStats)
def test_get_not_implemented(self):
"""Test get is not implemented (abstract)."""
backend = CacheBackend()
# The get method exists but raises NotImplementedError when called
# We can't easily test the async call without pytest-asyncio
assert hasattr(backend, "get")
def test_get_stats(self):
"""Test get_stats returns stats."""
backend = CacheBackend()
stats = backend.get_stats()
assert isinstance(stats, CacheStats)
class TestMemoryCache:
"""Test MemoryCache implementation."""
def test_init_defaults(self):
"""Test initialization with defaults."""
cache = MemoryCache()
assert cache._max_size == 1000
assert cache._max_memory_bytes == 100 * 1024 * 1024 # 100 MB
assert cache._cache == OrderedDict()
def test_init_custom(self):
"""Test initialization with custom values."""
cache = MemoryCache(max_size=500, max_memory_mb=50.0)
assert cache._max_size == 500
assert cache._max_memory_bytes == 50 * 1024 * 1024
def test_init_with_default_ttl(self):
"""Test initialization with default TTL."""
cache = MemoryCache(default_ttl=3600)
assert cache._default_ttl == 3600
class TestMemoryCacheSyncOperations:
"""Test MemoryCache synchronous-style operations."""
def test_cache_data_structure(self):
"""Test internal data structures."""
cache = MemoryCache()
# Simulate setting a value
cache._cache["key1"] = "value1"
cache._sizes["key1"] = 100
assert "key1" in cache._cache
assert cache._sizes["key1"] == 100
def test_remove_method(self):
"""Test _remove method."""
cache = MemoryCache()
cache._cache["key1"] = "value1"
cache._sizes["key1"] = 100
cache._current_memory = 100
cache._remove("key1")
assert "key1" not in cache._cache
assert cache._current_memory == 0
def test_evict_lru_empty(self):
"""Test _evict_lru with empty cache."""
cache = MemoryCache()
cache._evict_lru() # Should not raise
assert len(cache._cache) == 0
def test_evict_lru_with_expired(self):
"""Test _evict_lru removes expired entries."""
cache = MemoryCache()
cache._cache["expired"] = "value"
cache._expiry["expired"] = time.time() - 100 # Expired
cache._sizes["expired"] = 100
cache._evict_lru()
assert cache.stats.evictions >= 1
def test_size_calculation(self):
"""Test size calculation for values."""
cache = MemoryCache()
# Test with pickle-able value
value = {"test": "data", "number": 123}
try:
size = len(pickle.dumps(value))
assert size > 0
except pickle.PicklingError:
pass
class TestGenerateCacheKey:
"""Test cache key generation."""
def test_simple_args(self):
"""Test key generation with simple args."""
key = generate_cache_key("arg1", "arg2")
assert isinstance(key, str)
assert len(key) == 32 # MD5 hex
def test_with_kwargs(self):
"""Test key generation with kwargs."""
key1 = generate_cache_key("arg", kwarg1="value1")
key2 = generate_cache_key("arg", kwarg1="value1")
assert key1 == key2 # Deterministic
def test_different_args_different_keys(self):
"""Test different args produce different keys."""
key1 = generate_cache_key("arg1")
key2 = generate_cache_key("arg2")
assert key1 != key2
def test_complex_types(self):
"""Test key generation with complex types."""
key = generate_cache_key([1, 2, 3], {"key": "value"})
assert isinstance(key, str)
class TestGetCacheBackend:
"""Test _get_cache_backend function."""
def test_get_memory_backend(self):
"""Test getting memory backend."""
# Reset global cache
import core.cache as cache_module
original = cache_module._memory_cache
cache_module._memory_cache = None
try:
backend = _get_cache_backend("memory")
assert isinstance(backend, MemoryCache)
finally:
cache_module._memory_cache = original
def test_get_custom_backend(self):
"""Test passing custom backend."""
custom = MemoryCache()
backend = _get_cache_backend(custom)
assert backend is custom
def test_invalid_backend(self):
"""Test invalid backend raises error."""
with pytest.raises(ValueError):
_get_cache_backend("invalid")
class TestCacheDecorators:
"""Test cache decorator functionality."""
def test_generate_cache_key_structure(self):
"""Test key generation structure."""
key = generate_cache_key("func_name", 1, 2, a=3, b=4)
# Should be MD5 hash
assert len(key) == 32
assert all(c in "0123456789abcdef" for c in key)
class TestCVEFunctions:
"""Test CVE-specific cache functions."""
@patch("core.cache._get_cache_backend")
def test_get_cached_cve(self, mock_get_backend):
"""Test get_cached_cve calls backend correctly."""
mock_backend = MagicMock()
mock_backend.get = AsyncMock(return_value={"id": "CVE-2021-44228"})
mock_get_backend.return_value = mock_backend
# Can't test async easily without pytest-asyncio
# Just verify function exists and calls backend
mock_get_backend.assert_not_called()
@patch("core.cache._get_cache_backend")
def test_cache_cve(self, mock_get_backend):
"""Test cache_cve calls backend correctly."""
mock_backend = MagicMock()
mock_backend.set = AsyncMock(return_value=True)
mock_get_backend.return_value = mock_backend
# Verify function exists
assert callable(cache_cve)
class TestCacheStatsEdgeCases:
"""Test CacheStats edge cases."""
def test_hit_rate_division_by_zero(self):
"""Test hit rate with zero total gets."""
stats = CacheStats(hits=0, misses=0, total_gets=0)
assert stats.hit_rate == 0.0
assert stats.miss_rate == 0.0
def test_to_dict_with_zero_values(self):
"""Test to_dict with all zeros."""
stats = CacheStats()
d = stats.to_dict()
assert d["hit_rate"] == "0.00%"
assert d["miss_rate"] == "0.00%"
def test_stats_mutation(self):
"""Test that stats can be mutated."""
stats = CacheStats()
stats.hits += 10
stats.total_gets += 10
assert stats.hit_rate == 1.0
class TestMemoryCacheSizeLimits:
"""Test MemoryCache size limits."""
def test_value_size_check(self):
"""Test that value size is checked."""
cache = MemoryCache(max_memory_mb=1.0)
# Simulate a large value check
large_value = "x" * (1024 * 1024) # 1MB string
size = len(pickle.dumps(large_value))
# Value larger than 50% of max should be rejected
max_allowed = cache._max_memory_bytes * 0.5
assert size > max_allowed or size <= max_allowed
def test_memory_tracking(self):
"""Test memory tracking on set."""
cache = MemoryCache()
# Add values and check memory
initial_memory = cache._current_memory
cache._cache["key1"] = b"value1"
cache._sizes["key1"] = 10
cache._current_memory += 10
assert cache._current_memory == initial_memory + 10
def test_expiry_tracking(self):
"""Test expiry time tracking."""
cache = MemoryCache()
future_time = time.time() + 3600
cache._expiry["key1"] = future_time
assert cache._expiry["key1"] == future_time
# Import time at module level for tests
import time
class TestMultiTierCacheStructure:
"""Test MultiTierCache structure (without full async testing)."""
def test_multitier_init(self):
"""Test MultiTierCache initialization."""
from core.cache import MultiTierCache
cache = MultiTierCache(memory_size=50, memory_max_mb=25.0)
assert cache.l1 is not None
assert isinstance(cache.l1, MemoryCache)
assert cache._hit_distribution == {"L1": 0, "L2": 0, "L3": 0}
def test_multitier_init_with_sqlite(self):
"""Test MultiTierCache with SQLite."""
from core.cache import SQLITE_AVAILABLE, MultiTierCache
cache = MultiTierCache()
if SQLITE_AVAILABLE:
assert cache.l2 is not None
else:
assert cache.l2 is None
class TestSQLiteCacheStructure:
"""Test SQLiteCache structure (without full async testing)."""
def test_sqlite_init(self):
"""Test SQLiteCache initialization."""
from core.cache import SQLITE_AVAILABLE, SQLiteCache
if not SQLITE_AVAILABLE:
pytest.skip("aiosqlite not available")
cache = SQLiteCache()
assert cache._pool_size == 5
assert cache._cleanup_interval == 3600
assert cache._db is None
def test_sqlite_custom_path(self):
"""Test SQLiteCache with custom path."""
from core.cache import SQLITE_AVAILABLE, SQLiteCache
if not SQLITE_AVAILABLE:
pytest.skip("aiosqlite not available")
custom_path = Path("/tmp/test_cache.db") # nosec B108
cache = SQLiteCache(db_path=custom_path)
assert cache.db_path == custom_path
class TestRedisCacheStructure:
"""Test RedisCache structure (without full async testing)."""
def test_redis_init(self):
"""Test RedisCache initialization."""
from core.cache import REDIS_AVAILABLE, RedisCache
if not REDIS_AVAILABLE:
pytest.skip("redis not available")
cache = RedisCache(host="localhost", port=6379, db=0)
assert cache.host == "localhost"
assert cache.port == 6379
assert cache.db == 0
def test_redis_init_not_available(self):
"""Test RedisCache raises error when redis not installed."""
from core.cache import RedisCache
with patch("core.cache.REDIS_AVAILABLE", False):
with pytest.raises(ImportError):
RedisCache()
class TestCacheKeyGenerationEdgeCases:
"""Test cache key generation edge cases."""
def test_empty_args(self):
"""Test key generation with no args."""
key = generate_cache_key()
assert isinstance(key, str)
assert len(key) == 32
def test_nested_structures(self):
"""Test key generation with nested structures."""
nested = {"outer": {"inner": [1, 2, 3]}}
key = generate_cache_key(nested)
assert isinstance(key, str)
def test_special_characters(self):
"""Test key generation with special characters."""
key = generate_cache_key("hello\nworld\t!")
assert isinstance(key, str)
assert len(key) == 32
def test_unicode(self):
"""Test key generation with unicode."""
key = generate_cache_key("héllo", "wörld", "日本語")
assert isinstance(key, str)
assert len(key) == 32
class TestCacheBackendBatchOperations:
"""Test CacheBackend batch operations."""
def test_mget_empty_keys(self):
"""Test mget with empty keys returns empty dict."""
backend = CacheBackend()
# Empty keys should return empty result without error
assert backend.stats.total_gets == 0
def test_mset_empty_items(self):
"""Test mset with empty items concept."""
backend = CacheBackend()
# Should be able to handle empty batch
assert backend.stats.total_sets == 0
class TestAllExports:
"""Test that all expected exports are available."""
def test_exports(self):
"""Test that __all__ exports are importable."""
from core import cache
assert hasattr(cache, "CacheBackend")
assert hasattr(cache, "MemoryCache")
assert hasattr(cache, "SQLiteCache")
assert hasattr(cache, "RedisCache")
assert hasattr(cache, "MultiTierCache")
assert hasattr(cache, "CacheStats")
assert hasattr(cache, "cached")
assert hasattr(cache, "generate_cache_key")
assert hasattr(cache, "get_cached_cve")
assert hasattr(cache, "cache_cve")
assert hasattr(cache, "_get_cache_backend")

Xet Storage Details

Size:
14.5 kB
·
Xet hash:
2111b6e4df2b05a2e833fb8a7c2a6e59dac6af6001e0dfab5a5921fb2441a444

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.