2
2
from time import sleep
3
3
4
4
import pytest
5
+ from redis .exceptions import ConnectionError
5
6
6
7
from redisvl .extensions .llmcache import SemanticCache
7
8
from redisvl .index .index import SearchIndex
@@ -40,19 +41,17 @@ def cache_with_ttl(vectorizer, redis_url):
40
41
41
42
42
43
@pytest .fixture
43
- def cache_with_redis_client (vectorizer , client , redis_url ):
44
+ def cache_with_redis_client (vectorizer , client ):
44
45
cache_instance = SemanticCache (
45
46
vectorizer = vectorizer ,
46
47
redis_client = client ,
47
48
distance_threshold = 0.2 ,
48
- redis_url = redis_url ,
49
49
)
50
50
yield cache_instance
51
51
cache_instance .clear () # Clear cache after each test
52
52
cache_instance ._index .delete (True ) # Clean up index
53
53
54
54
55
- # # Test handling invalid input for check method
56
55
def test_bad_ttl (cache ):
57
56
with pytest .raises (ValueError ):
58
57
cache .set_ttl (2.5 )
@@ -76,7 +75,6 @@ def test_reset_ttl(cache):
76
75
assert cache .ttl is None
77
76
78
77
79
- # Test basic store and check functionality
80
78
def test_store_and_check (cache , vectorizer ):
81
79
prompt = "This is a test prompt."
82
80
response = "This is a test response."
@@ -91,7 +89,6 @@ def test_store_and_check(cache, vectorizer):
91
89
assert "metadata" not in check_result [0 ]
92
90
93
91
94
- # Test clearing the cache
95
92
def test_clear (cache , vectorizer ):
96
93
prompt = "This is a test prompt."
97
94
response = "This is a test response."
@@ -139,7 +136,6 @@ def test_check_no_match(cache, vectorizer):
139
136
assert len (check_result ) == 0
140
137
141
138
142
- # Test handling invalid input for check method
143
139
def test_check_invalid_input (cache ):
144
140
with pytest .raises (ValueError ):
145
141
cache .check ()
@@ -148,7 +144,15 @@ def test_check_invalid_input(cache):
148
144
cache .check (prompt = "test" , return_fields = "bad value" )
149
145
150
146
151
- # Test storing with metadata
147
+ def test_bad_connection_info (vectorizer ):
148
+ with pytest .raises (ConnectionError ):
149
+ SemanticCache (
150
+ vectorizer = vectorizer ,
151
+ distance_threshold = 0.2 ,
152
+ redis_url = "redis://localhost:6389" ,
153
+ )
154
+
155
+
152
156
def test_store_with_metadata (cache , vectorizer ):
153
157
prompt = "This is another test prompt."
154
158
response = "This is another test response."
@@ -165,7 +169,6 @@ def test_store_with_metadata(cache, vectorizer):
165
169
assert check_result [0 ]["prompt" ] == prompt
166
170
167
171
168
- # Test storing with invalid metadata
169
172
def test_store_with_invalid_metadata (cache , vectorizer ):
170
173
prompt = "This is another test prompt."
171
174
response = "This is another test response."
@@ -179,7 +182,6 @@ def test_store_with_invalid_metadata(cache, vectorizer):
179
182
cache .store (prompt , response , vector = vector , metadata = metadata )
180
183
181
184
182
- # Test setting and getting the distance threshold
183
185
def test_distance_threshold (cache ):
184
186
initial_threshold = cache .distance_threshold
185
187
new_threshold = 0.1
@@ -189,14 +191,12 @@ def test_distance_threshold(cache):
189
191
assert cache .distance_threshold != initial_threshold
190
192
191
193
192
- # Test out of range distance threshold
193
194
def test_distance_threshold_out_of_range (cache ):
194
195
out_of_range_threshold = - 1
195
196
with pytest .raises (ValueError ):
196
197
cache .set_threshold (out_of_range_threshold )
197
198
198
199
199
- # Test storing and retrieving multiple items
200
200
def test_multiple_items (cache , vectorizer ):
201
201
prompts_responses = {
202
202
"prompt1" : "response1" ,
@@ -217,12 +217,10 @@ def test_multiple_items(cache, vectorizer):
217
217
assert "metadata" not in check_result [0 ]
218
218
219
219
220
- # Test retrieving underlying SearchIndex for the cache.
221
220
def test_get_index (cache ):
222
221
assert isinstance (cache .index , SearchIndex )
223
222
224
223
225
- # Test basic functionality with cache created with user-provided Redis client
226
224
def test_store_and_check_with_provided_client (cache_with_redis_client , vectorizer ):
227
225
prompt = "This is a test prompt."
228
226
response = "This is a test response."
@@ -237,13 +235,11 @@ def test_store_and_check_with_provided_client(cache_with_redis_client, vectorize
237
235
assert "metadata" not in check_result [0 ]
238
236
239
237
240
- # Test deleting the cache
241
238
def test_delete (cache_no_cleanup ):
242
239
cache_no_cleanup .delete ()
243
240
assert not cache_no_cleanup .index .exists ()
244
241
245
242
246
- # Test we can only store and check vectors of correct dimensions
247
243
def test_vector_size (cache , vectorizer ):
248
244
prompt = "This is test prompt."
249
245
response = "This is a test response."
0 commit comments