@@ -54,6 +54,24 @@ def cache_with_redis_client(vectorizer, client, redis_url):
54
54
cache_instance ._index .delete (True ) # Clean up index
55
55
56
56
57
+ # Test handling invalid input for check method
58
+ def test_bad_ttl (cache ):
59
+ with pytest .raises (ValueError ):
60
+ cache .set_ttl (2.5 )
61
+
62
+
63
+ def test_cache_ttl (cache_with_ttl ):
64
+ assert cache_with_ttl .ttl == 2
65
+ cache_with_ttl .set_ttl (5 )
66
+ assert cache_with_ttl .ttl == 5
67
+
68
+
69
+ def test_set_ttl (cache ):
70
+ assert cache .ttl == None
71
+ cache .set_ttl (5 )
72
+ assert cache .ttl == 5
73
+
74
+
57
75
# Test basic store and check functionality
58
76
def test_store_and_check (cache , vectorizer ):
59
77
prompt = "This is a test prompt."
@@ -95,6 +113,26 @@ def test_ttl_expiration(cache_with_ttl, vectorizer):
95
113
assert len (check_result ) == 0
96
114
97
115
116
+ def test_ttl_expiration_after_update (cache_with_ttl , vectorizer ):
117
+ prompt = "This is a test prompt."
118
+ response = "This is a test response."
119
+ vector = vectorizer .embed (prompt )
120
+ cache_with_ttl .set_ttl (5 )
121
+
122
+ cache_with_ttl .store (prompt , response , vector = vector )
123
+ sleep (2 )
124
+
125
+ check_result = cache_with_ttl .check (vector = vector )
126
+ assert len (check_result ) == 1
127
+ print (check_result , flush = True )
128
+ assert response == check_result [0 ]["response" ]
129
+ assert "metadata" not in check_result [0 ]
130
+
131
+ sleep (5 )
132
+ check_result = cache_with_ttl .check (vector = vector )
133
+ assert len (check_result ) == 0
134
+
135
+
98
136
# Test check behavior with no match
99
137
def test_check_no_match (cache , vectorizer ):
100
138
vector = vectorizer .embed ("Some random sentence." )
@@ -111,12 +149,6 @@ def test_check_invalid_input(cache):
111
149
cache .check (prompt = "test" , return_fields = "bad value" )
112
150
113
151
114
- # Test handling invalid input for check method
115
- def test_bad_ttl (cache ):
116
- with pytest .raises (ValueError ):
117
- cache .set_ttl (2.5 )
118
-
119
-
120
152
# Test storing with metadata
121
153
def test_store_with_metadata (cache , vectorizer ):
122
154
prompt = "This is another test prompt."
0 commit comments