-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cache.py
More file actions
78 lines (61 loc) · 2.29 KB
/
test_cache.py
File metadata and controls
78 lines (61 loc) · 2.29 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
# filename: tests/test_cache.py
import unittest
import time
from promptshield.cache import InMemoryCache
class TestInMemoryCache(unittest.TestCase):
def setUp(self):
self.cache = InMemoryCache(ttl=1) # 1 second TTL for testing
def test_set_get(self):
# Set a value
self.cache.set("test_key", {"value": "test_value"})
# Get the value
value = self.cache.get("test_key")
self.assertEqual(value["value"], "test_value")
def test_ttl(self):
# Set a value
self.cache.set("test_key", {"value": "test_value"})
# Wait for TTL to expire
time.sleep(1.1)
# Get the value (should be None)
value = self.cache.get("test_key")
self.assertIsNone(value)
def test_nonexistent_key(self):
# Get a nonexistent key
value = self.cache.get("nonexistent_key")
self.assertIsNone(value)
try:
from promptshield.redis_cache import RedisCache
class TestRedisCache(unittest.TestCase):
def setUp(self):
try:
self.cache = RedisCache(ttl=1) # 1 second TTL for testing
self.cache.flush() # Clear the cache before testing
except:
self.skipTest("Redis is not available")
def test_set_get(self):
# Set a value
self.cache.set("test_key", {"value": "test_value"})
# Get the value
value = self.cache.get("test_key")
self.assertEqual(value["value"], "test_value")
def test_ttl(self):
# Set a value
self.cache.set("test_key", {"value": "test_value"})
# Wait for TTL to expire
time.sleep(1.1)
# Get the value (should be None)
value = self.cache.get("test_key")
self.assertIsNone(value)
def test_nonexistent_key(self):
# Get a nonexistent key
value = self.cache.get("nonexistent_key")
self.assertIsNone(value)
def tearDown(self):
try:
self.cache.flush()
except:
pass
except ImportError:
pass # Redis is not available
if __name__ == "__main__":
unittest.main()