"""Tests for cache.py.""" import json import tempfile from datetime import datetime, timedelta from pathlib import Path import pytest from gitignore_generator.cache import CacheManager class TestCacheManager: """Tests for CacheManager class.""" @pytest.fixture def cache_dir(self, tmp_path): """Create temp cache directory.""" return tmp_path @pytest.fixture def cache(self, cache_dir): """Create cache manager with temp directory.""" cache = CacheManager() cache._cache_dir = cache_dir return cache def test_set_and_get(self, cache): """Test setting and getting cached items.""" cache.set('python', 'python content') result = cache.get('python') assert result == 'python content' def test_get_nonexistent(self, cache): """Test getting non-existent item.""" result = cache.get('nonexistent_xyz_123') assert result is None def test_is_valid_fresh_cache(self, cache): """Test cache validity for fresh items.""" cache.set('python', 'content') assert cache.is_valid('python') is True def test_is_valid_expired_cache(self, cache): """Test cache validity for expired items.""" cache.set('python', 'content') metadata = cache._load_metadata() metadata['python']['cached_at'] = (datetime.now() - timedelta(days=10)).isoformat() cache._save_metadata(metadata) assert cache.is_valid('python') is False def test_invalidate(self, cache): """Test invalidating cached item.""" cache.set('python', 'content') assert cache.invalidate('python') is True assert cache.get('python') is None def test_invalidate_nonexistent(self, cache): """Test invalidating non-existent item.""" assert cache.invalidate('nonexistent_xyz_123') is False def test_clear(self, cache): """Test clearing all cache.""" cache.set('python', 'content') cache.set('node', 'content') cache.set('django', 'content') count = cache.clear() assert count == 3 assert cache.get('python') is None assert cache.get('node') is None assert cache.get('django') is None def test_get_stats(self, cache): """Test getting cache statistics.""" cache.set('python', 'content') cache.set('node', 'content') stats = cache.get_stats() assert stats['total_items'] == 2 assert stats['valid_items'] == 2 assert stats['expired_items'] == 0 def test_get_age(self, cache): """Test getting cache age.""" cache.set('python', 'content') age = cache.get_age('python') assert age is not None assert age >= 0 def test_get_age_nonexistent(self, cache): """Test getting age of non-existent item.""" age = cache.get_age('nonexistent_xyz_123') assert age is None def test_cleanup_expired(self, cache): """Test cleaning up expired entries.""" cache.set('fresh', 'content') metadata = cache._load_metadata() metadata['expired'] = { 'cached_at': (datetime.now() - timedelta(days=10)).isoformat(), 'size': 100 } cache._save_metadata(metadata) count = cache.cleanup_expired() assert count == 1 assert cache.get('expired') is None assert cache.get('fresh') == 'content' def test_cache_directory_creation(self, cache): """Test that cache directory is created.""" cache.set('test', 'content') assert cache._cache_dir.exists()