-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cache.py
More file actions
119 lines (81 loc) 路 2.82 KB
/
Copy pathtest_cache.py
File metadata and controls
119 lines (81 loc) 路 2.82 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import pytest
import time
from src.cortex.storage.cache import SimpleCache, QueryCache, EmbeddingCache
def test_simple_cache():
"""Test basic cache operations."""
cache = SimpleCache(default_ttl=1.0)
# Set and get
cache.set("key1", "value1")
assert cache.get("key1") == "value1"
# Cache hit
assert cache.hits == 1
assert cache.misses == 0
# Cache miss
assert cache.get("nonexistent") is None
assert cache.misses == 1
def test_cache_expiration():
"""Test that cache entries expire after TTL."""
cache = SimpleCache(default_ttl=0.1) # 100ms TTL
cache.set("key1", "value1")
assert cache.get("key1") == "value1"
# Wait for expiration
time.sleep(0.15)
# Should be expired
assert cache.get("key1") is None
def test_cache_stats():
"""Test cache statistics."""
cache = SimpleCache()
cache.set("key1", "value1")
cache.set("key2", "value2")
cache.get("key1") # Hit
cache.get("key2") # Hit
cache.get("key3") # Miss
stats = cache.get_stats()
assert stats['size'] == 2
assert stats['hits'] == 2
assert stats['misses'] == 1
assert stats['hit_rate'] == pytest.approx(66.67, rel=0.1)
def test_cache_clear():
"""Test cache clearing."""
cache = SimpleCache()
cache.set("key1", "value1")
cache.set("key2", "value2")
assert cache.get_stats()['size'] == 2
cache.clear()
assert cache.get_stats()['size'] == 0
assert cache.get("key1") is None
def test_query_cache():
"""Test QueryCache with complex parameters."""
cache = QueryCache()
# Set query results
cache.set_query("test query", limit=5, results=["result1", "result2"])
# Get with same parameters
results = cache.get_query("test query", limit=5)
assert results == ["result1", "result2"]
# Different parameters = different cache entry
results = cache.get_query("test query", limit=10)
assert results is None
def test_embedding_cache():
"""Test EmbeddingCache."""
cache = EmbeddingCache()
# Set embedding
embedding = [0.1, 0.2, 0.3, 0.4]
cache.set_embedding("test text", embedding)
# Get embedding
cached = cache.get_embedding("test text")
assert cached == embedding
def test_cleanup_expired():
"""Test cleanup of expired entries."""
cache = SimpleCache(default_ttl=0.1)
cache.set("key1", "value1")
cache.set("key2", "value2", ttl=1.0) # Longer TTL
# Wait for first to expire
time.sleep(0.15)
# Before cleanup
assert cache.get_stats()['size'] == 2
# Cleanup
cache.cleanup_expired()
# After cleanup
assert cache.get_stats()['size'] == 1
assert cache.get("key2") == "value2" # Still there
assert cache.get("key1") is None # Gone