44
55from redisvl .extensions .llmcache import SemanticCache
66from redisvl .utils .vectorize import HFTextVectorizer
7-
7+ from redisvl .index .index import SearchIndex
8+ from collections import namedtuple
89
910@pytest .fixture
1011def vectorizer ():
@@ -18,6 +19,10 @@ def cache(vectorizer):
1819 cache_instance .clear () # Clear cache after each test
1920 cache_instance ._index .delete (True ) # Clean up index
2021
22+ @pytest .fixture
23+ def cache_no_cleanup (vectorizer ):
24+ cache_instance = SemanticCache (vectorizer = vectorizer , distance_threshold = 0.2 )
25+ yield cache_instance
2126
2227@pytest .fixture
2328def cache_with_ttl (vectorizer ):
@@ -26,6 +31,12 @@ def cache_with_ttl(vectorizer):
2631 cache_instance .clear () # Clear cache after each test
2732 cache_instance ._index .delete (True ) # Clean up index
2833
34+ @pytest .fixture
35+ def cache_with_redis_client (vectorizer , client ):
36+ cache_instance = SemanticCache (vectorizer = vectorizer , redis_client = client , distance_threshold = 0.2 )
37+ yield cache_instance
38+ cache_instance .clear () # Clear cache after each test
39+ cache_instance ._index .delete (True ) # Clean up index
2940
3041# Test basic store and check functionality
3142def test_store_and_check (cache , vectorizer ):
@@ -83,6 +94,10 @@ def test_check_invalid_input(cache):
8394 with pytest .raises (TypeError ):
8495 cache .check (prompt = "test" , return_fields = "bad value" )
8596
97+ # Test handling invalid input for check method
98+ def test_bad_ttl (cache ):
99+ with pytest .raises (ValueError ):
100+ cache .set_ttl (2.5 )
86101
87102# Test storing with metadata
88103def test_store_with_metadata (cache , vectorizer ):
@@ -100,6 +115,16 @@ def test_store_with_metadata(cache, vectorizer):
100115 assert check_result [0 ]["metadata" ] == metadata
101116 assert check_result [0 ]["prompt" ] == prompt
102117
118+ # Test storing with invalid metadata
119+ def test_store_with_invalid_metadata (cache , vectorizer ):
120+ prompt = "This is another test prompt."
121+ response = "This is another test response."
122+ metadata = namedtuple ('metadata' , 'source' )(** {'source' : 'test' })
123+
124+ vector = vectorizer .embed (prompt )
125+
126+ with pytest .raises (TypeError , match = r"If specified, cached metadata must be a dictionary." ):
127+ cache .store (prompt , response , vector = vector , metadata = metadata )
103128
104129# Test setting and getting the distance threshold
105130def test_distance_threshold (cache ):
@@ -110,6 +135,11 @@ def test_distance_threshold(cache):
110135 assert cache .distance_threshold == new_threshold
111136 assert cache .distance_threshold != initial_threshold
112137
138+ # Test out of range distance threshold
139+ def test_distance_threshold_out_of_range (cache ):
140+ out_of_range_threshold = - 1
141+ with pytest .raises (ValueError ):
142+ cache .set_threshold (out_of_range_threshold )
113143
114144# Test storing and retrieving multiple items
115145def test_multiple_items (cache , vectorizer ):
@@ -130,3 +160,26 @@ def test_multiple_items(cache, vectorizer):
130160 print (check_result , flush = True )
131161 assert check_result [0 ]["response" ] == expected_response
132162 assert "metadata" not in check_result [0 ]
163+
164+ # Test retrieving underlying SearchIndex for the cache.
165+ def test_get_index (cache ):
166+ assert isinstance (cache .index , SearchIndex )
167+
168+ # Test basic functionality with cache created with user-provided Redis client
169+ def test_store_and_check_with_provided_client (cache_with_redis_client , vectorizer ):
170+ prompt = "This is a test prompt."
171+ response = "This is a test response."
172+ vector = vectorizer .embed (prompt )
173+
174+ cache_with_redis_client .store (prompt , response , vector = vector )
175+ check_result = cache_with_redis_client .check (vector = vector )
176+
177+ assert len (check_result ) == 1
178+ print (check_result , flush = True )
179+ assert response == check_result [0 ]["response" ]
180+ assert "metadata" not in check_result [0 ]
181+
182+ # Test deleting the cache
183+ def test_delete (cache_no_cleanup , vectorizer ):
184+ cache_no_cleanup .delete ()
185+ assert not cache_no_cleanup .index .exists ()
0 commit comments