File size: 2,747 Bytes
69066c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
Tests for cache_manager module.
"""

import unittest
import time
from cache_manager import LRUCache


class TestCacheManager(unittest.TestCase):
    """Test cases for cache management."""
    
    def setUp(self):
        """Set up test fixtures."""
        self.cache = LRUCache(max_size=5, ttl_seconds=1)
    
    def test_cache_set_get(self):
        """Test basic cache set and get operations."""
        self.cache.set("key1", "value1")
        value = self.cache.get("key1")
        self.assertEqual(value, "value1")
    
    def test_cache_miss(self):
        """Test cache miss scenario."""
        value = self.cache.get("nonexistent")
        self.assertIsNone(value)
    
    def test_cache_size_limit(self):
        """Test that cache respects size limits."""
        # Fill cache beyond max_size
        for i in range(10):
            self.cache.set(f"key{i}", f"value{i}")
        
        # Oldest entries should be evicted
        self.assertIsNone(self.cache.get("key0"))
        self.assertIsNotNone(self.cache.get("key9"))
    
    def test_cache_lru_eviction(self):
        """Test LRU eviction policy."""
        # Fill cache
        for i in range(5):
            self.cache.set(f"key{i}", f"value{i}")
        
        # Access key0 to make it recently used
        self.cache.get("key0")
        
        # Add new entry - should evict least recently used (key1)
        self.cache.set("key5", "value5")
        
        self.assertIsNotNone(self.cache.get("key0"))  # Still in cache
        self.assertIsNone(self.cache.get("key1"))  # Evicted
    
    def test_cache_ttl_expiration(self):
        """Test cache TTL expiration."""
        self.cache.set("key1", "value1")
        
        # Value should be available immediately
        self.assertIsNotNone(self.cache.get("key1"))
        
        # Wait for expiration
        time.sleep(1.1)
        
        # Value should be expired
        self.assertIsNone(self.cache.get("key1"))
    
    def test_cache_clear(self):
        """Test cache clear operation."""
        self.cache.set("key1", "value1")
        self.cache.set("key2", "value2")
        
        self.assertEqual(self.cache.size(), 2)
        
        self.cache.clear()
        
        self.assertEqual(self.cache.size(), 0)
        self.assertIsNone(self.cache.get("key1"))
    
    def test_cache_stats(self):
        """Test cache statistics."""
        self.cache.set("key1", "value1")
        stats = self.cache.stats()
        
        self.assertIn("size", stats)
        self.assertIn("max_size", stats)
        self.assertIn("ttl_seconds", stats)
        self.assertIn("usage_percent", stats)
        self.assertEqual(stats["size"], 1)


if __name__ == '__main__':
    unittest.main()