65 lines
1.6 KiB
Python
65 lines
1.6 KiB
Python
|
|
"""Tests for response cache."""
|
||
|
|
|
||
|
|
import time
|
||
|
|
|
||
|
|
from fusionagi.api.cache import ResponseCache
|
||
|
|
|
||
|
|
|
||
|
|
def test_cache_set_and_get():
|
||
|
|
cache = ResponseCache(max_size=10, ttl_seconds=60)
|
||
|
|
cache.set("hello", "s1", {"answer": "world"})
|
||
|
|
result = cache.get("hello", "s1")
|
||
|
|
assert result == {"answer": "world"}
|
||
|
|
|
||
|
|
|
||
|
|
def test_cache_miss():
|
||
|
|
cache = ResponseCache()
|
||
|
|
assert cache.get("nonexistent", "s1") is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_cache_ttl_expiry():
|
||
|
|
cache = ResponseCache(ttl_seconds=0.01)
|
||
|
|
cache.set("prompt", "s1", "cached")
|
||
|
|
time.sleep(0.02)
|
||
|
|
assert cache.get("prompt", "s1") is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_cache_invalidate():
|
||
|
|
cache = ResponseCache()
|
||
|
|
cache.set("p", "s", "val")
|
||
|
|
assert cache.invalidate("p", "s") is True
|
||
|
|
assert cache.get("p", "s") is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_cache_clear():
|
||
|
|
cache = ResponseCache()
|
||
|
|
cache.set("a", "s", 1)
|
||
|
|
cache.set("b", "s", 2)
|
||
|
|
count = cache.clear()
|
||
|
|
assert count == 2
|
||
|
|
assert cache.get("a", "s") is None
|
||
|
|
|
||
|
|
|
||
|
|
def test_cache_max_size():
|
||
|
|
cache = ResponseCache(max_size=2)
|
||
|
|
cache.set("a", "s", 1)
|
||
|
|
cache.set("b", "s", 2)
|
||
|
|
cache.set("c", "s", 3)
|
||
|
|
assert cache.stats()["total"] == 2
|
||
|
|
|
||
|
|
|
||
|
|
def test_cache_stats():
|
||
|
|
cache = ResponseCache(max_size=100)
|
||
|
|
cache.set("a", "s", 1)
|
||
|
|
stats = cache.stats()
|
||
|
|
assert stats["total"] == 1
|
||
|
|
assert stats["max_size"] == 100
|
||
|
|
|
||
|
|
|
||
|
|
def test_cache_tenant_isolation():
|
||
|
|
cache = ResponseCache()
|
||
|
|
cache.set("prompt", "s1", "tenant_a_result", tenant_id="a")
|
||
|
|
cache.set("prompt", "s1", "tenant_b_result", tenant_id="b")
|
||
|
|
assert cache.get("prompt", "s1", "a") == "tenant_a_result"
|
||
|
|
assert cache.get("prompt", "s1", "b") == "tenant_b_result"
|