22from time import sleep
33
44import pytest
5+ from redis .exceptions import ConnectionError
56
67from redisvl .extensions .llmcache import SemanticCache
78from redisvl .index .index import SearchIndex
@@ -40,19 +41,17 @@ def cache_with_ttl(vectorizer, redis_url):
4041
4142
4243@pytest .fixture
43- def cache_with_redis_client (vectorizer , client , redis_url ):
44+ def cache_with_redis_client (vectorizer , client ):
4445 cache_instance = SemanticCache (
4546 vectorizer = vectorizer ,
4647 redis_client = client ,
4748 distance_threshold = 0.2 ,
48- redis_url = redis_url ,
4949 )
5050 yield cache_instance
5151 cache_instance .clear () # Clear cache after each test
5252 cache_instance ._index .delete (True ) # Clean up index
5353
5454
55- # # Test handling invalid input for check method
5655def test_bad_ttl (cache ):
5756 with pytest .raises (ValueError ):
5857 cache .set_ttl (2.5 )
@@ -76,7 +75,6 @@ def test_reset_ttl(cache):
7675 assert cache .ttl is None
7776
7877
79- # Test basic store and check functionality
8078def test_store_and_check (cache , vectorizer ):
8179 prompt = "This is a test prompt."
8280 response = "This is a test response."
@@ -91,7 +89,6 @@ def test_store_and_check(cache, vectorizer):
9189 assert "metadata" not in check_result [0 ]
9290
9391
94- # Test clearing the cache
9592def test_clear (cache , vectorizer ):
9693 prompt = "This is a test prompt."
9794 response = "This is a test response."
@@ -139,7 +136,6 @@ def test_check_no_match(cache, vectorizer):
139136 assert len (check_result ) == 0
140137
141138
142- # Test handling invalid input for check method
143139def test_check_invalid_input (cache ):
144140 with pytest .raises (ValueError ):
145141 cache .check ()
@@ -148,7 +144,15 @@ def test_check_invalid_input(cache):
148144 cache .check (prompt = "test" , return_fields = "bad value" )
149145
150146
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+
152156def test_store_with_metadata (cache , vectorizer ):
153157 prompt = "This is another test prompt."
154158 response = "This is another test response."
@@ -165,7 +169,6 @@ def test_store_with_metadata(cache, vectorizer):
165169 assert check_result [0 ]["prompt" ] == prompt
166170
167171
168- # Test storing with invalid metadata
169172def test_store_with_invalid_metadata (cache , vectorizer ):
170173 prompt = "This is another test prompt."
171174 response = "This is another test response."
@@ -179,7 +182,6 @@ def test_store_with_invalid_metadata(cache, vectorizer):
179182 cache .store (prompt , response , vector = vector , metadata = metadata )
180183
181184
182- # Test setting and getting the distance threshold
183185def test_distance_threshold (cache ):
184186 initial_threshold = cache .distance_threshold
185187 new_threshold = 0.1
@@ -189,14 +191,12 @@ def test_distance_threshold(cache):
189191 assert cache .distance_threshold != initial_threshold
190192
191193
192- # Test out of range distance threshold
193194def test_distance_threshold_out_of_range (cache ):
194195 out_of_range_threshold = - 1
195196 with pytest .raises (ValueError ):
196197 cache .set_threshold (out_of_range_threshold )
197198
198199
199- # Test storing and retrieving multiple items
200200def test_multiple_items (cache , vectorizer ):
201201 prompts_responses = {
202202 "prompt1" : "response1" ,
@@ -217,12 +217,10 @@ def test_multiple_items(cache, vectorizer):
217217 assert "metadata" not in check_result [0 ]
218218
219219
220- # Test retrieving underlying SearchIndex for the cache.
221220def test_get_index (cache ):
222221 assert isinstance (cache .index , SearchIndex )
223222
224223
225- # Test basic functionality with cache created with user-provided Redis client
226224def test_store_and_check_with_provided_client (cache_with_redis_client , vectorizer ):
227225 prompt = "This is a test prompt."
228226 response = "This is a test response."
@@ -237,13 +235,11 @@ def test_store_and_check_with_provided_client(cache_with_redis_client, vectorize
237235 assert "metadata" not in check_result [0 ]
238236
239237
240- # Test deleting the cache
241238def test_delete (cache_no_cleanup ):
242239 cache_no_cleanup .delete ()
243240 assert not cache_no_cleanup .index .exists ()
244241
245242
246- # Test we can only store and check vectors of correct dimensions
247243def test_vector_size (cache , vectorizer ):
248244 prompt = "This is test prompt."
249245 response = "This is a test response."
0 commit comments