-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cache.py
More file actions
191 lines (146 loc) · 5.28 KB
/
Copy pathtest_cache.py
File metadata and controls
191 lines (146 loc) · 5.28 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
"""
Test script for caching functionality
"""
import sys
import os
import time
# Add the project root to Python path
sys.path.insert(0, os.path.abspath('.'))
from cache import CacheManager, CacheConfig
def test_cache_config():
"""Test cache configuration"""
print("Testing cache configuration...")
config = CacheConfig()
print(f" Config: {config}")
assert config.enabled == True
assert config.ttl > 0
print("✓ Cache configuration test passed")
def test_cache_basic_operations():
"""Test basic cache operations"""
print("\nTesting basic cache operations...")
config = CacheConfig()
config.persistent = False # Use only memory cache for testing
cache = CacheManager(config)
# Test set and get
test_data = [{'uri': 'http://test.org/1', 'label': 'Test 1'}]
cache.set('test query', 'HP,NCIT', 'bioportal', test_data)
result = cache.get('test query', 'HP,NCIT', 'bioportal')
assert result is not None
assert len(result) == 1
assert result[0]['label'] == 'Test 1'
print("✓ Set and get test passed")
# Test cache hit
result2 = cache.get('test query', 'HP,NCIT', 'bioportal')
assert result2 is not None
stats = cache.get_stats()
assert stats['hits'] >= 1
print("✓ Cache hit test passed")
# Test cache miss
result3 = cache.get('nonexistent', 'HP', 'bioportal')
assert result3 is None
stats = cache.get_stats()
assert stats['misses'] >= 1
print("✓ Cache miss test passed")
# Test delete
deleted = cache.delete('test query', 'HP,NCIT', 'bioportal')
assert deleted == True
result4 = cache.get('test query', 'HP,NCIT', 'bioportal')
assert result4 is None
print("✓ Delete test passed")
def test_cache_key_generation():
"""Test cache key generation"""
print("\nTesting cache key generation...")
config = CacheConfig()
config.persistent = False
cache = CacheManager(config)
# Same query should produce same key
test_data = [{'uri': 'http://test.org/1', 'label': 'Test'}]
cache.set('cancer', 'MONDO,HP', 'bioportal', test_data)
# Case-insensitive and whitespace normalized
result1 = cache.get('cancer', 'MONDO,HP', 'bioportal')
result2 = cache.get('CANCER', 'mondo,hp', 'bioportal')
result3 = cache.get(' cancer ', ' MONDO,HP ', 'bioportal')
assert result1 is not None
assert result2 is not None
assert result3 is not None
print("✓ Cache key normalization test passed")
def test_cache_ttl():
"""Test cache TTL expiration"""
print("\nTesting cache TTL expiration...")
config = CacheConfig()
config.persistent = False
config.ttl = 1 # 1 second TTL
cache = CacheManager(config)
test_data = [{'uri': 'http://test.org/1', 'label': 'Test'}]
cache.set('expiring', 'HP', 'bioportal', test_data)
# Should be available immediately
result1 = cache.get('expiring', 'HP', 'bioportal')
assert result1 is not None
print("✓ Cache entry available immediately")
# Wait for expiration
print(" Waiting 2 seconds for TTL expiration...")
time.sleep(2)
# Should be expired
result2 = cache.get('expiring', 'HP', 'bioportal')
assert result2 is None
print("✓ Cache entry expired after TTL")
def test_cache_clear():
"""Test cache clear operation"""
print("\nTesting cache clear...")
config = CacheConfig()
config.persistent = False
cache = CacheManager(config)
# Add multiple entries
for i in range(5):
test_data = [{'uri': f'http://test.org/{i}', 'label': f'Test {i}'}]
cache.set(f'query{i}', 'HP', 'bioportal', test_data)
# Clear all
count = cache.clear()
assert count == 5
# Verify all cleared
for i in range(5):
result = cache.get(f'query{i}', 'HP', 'bioportal')
assert result is None
print("✓ Cache clear test passed")
def test_cache_stats():
"""Test cache statistics"""
print("\nTesting cache statistics...")
config = CacheConfig()
config.persistent = False
cache = CacheManager(config)
test_data = [{'uri': 'http://test.org/1', 'label': 'Test'}]
# Perform various operations
cache.set('query1', 'HP', 'bioportal', test_data)
cache.get('query1', 'HP', 'bioportal') # hit
cache.get('nonexistent', 'HP', 'bioportal') # miss
cache.delete('query1', 'HP', 'bioportal')
stats = cache.get_stats()
assert stats['hits'] >= 1
assert stats['misses'] >= 1
assert stats['sets'] >= 1
assert stats['deletes'] >= 1
assert stats['enabled'] == True
print(f" Stats: {stats}")
print("✓ Cache statistics test passed")
def main():
print("Testing Cache Functionality...")
print("=" * 50)
try:
test_cache_config()
test_cache_basic_operations()
test_cache_key_generation()
test_cache_ttl()
test_cache_clear()
test_cache_stats()
print("\n" + "=" * 50)
print("✅ All cache tests passed!")
return True
except Exception as e:
print(f"\n❌ Test failed: {e}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)