Skip to content

Commit 5d3aa5f

Browse files
improve caching ttl tests
1 parent 9ca93ef commit 5d3aa5f

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

tests/integration/test_llmcache.py

+38-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ def cache_with_redis_client(vectorizer, client, redis_url):
5454
cache_instance._index.delete(True) # Clean up index
5555

5656

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+
5775
# Test basic store and check functionality
5876
def test_store_and_check(cache, vectorizer):
5977
prompt = "This is a test prompt."
@@ -95,6 +113,26 @@ def test_ttl_expiration(cache_with_ttl, vectorizer):
95113
assert len(check_result) == 0
96114

97115

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+
98136
# Test check behavior with no match
99137
def test_check_no_match(cache, vectorizer):
100138
vector = vectorizer.embed("Some random sentence.")
@@ -111,12 +149,6 @@ def test_check_invalid_input(cache):
111149
cache.check(prompt="test", return_fields="bad value")
112150

113151

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-
120152
# Test storing with metadata
121153
def test_store_with_metadata(cache, vectorizer):
122154
prompt = "This is another test prompt."

0 commit comments

Comments
 (0)