43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
from router_system.cache import RouterCache
|
|
|
|
|
|
def test_exact_hit():
|
|
c = RouterCache()
|
|
result = {"response": "hello", "domain": "general"}
|
|
assert c.get("query") is None
|
|
c.put("query", result)
|
|
level, got = c.get("query")
|
|
assert level == "exact"
|
|
assert got["response"] == "hello"
|
|
|
|
|
|
def test_semantic_hit():
|
|
c = RouterCache(semantic_enabled=True, similarity_threshold=0.5)
|
|
c.put("用python写一个快速排序", {"response": "code", "domain": "code"})
|
|
# 相似改写查询命中 L2 语义缓存
|
|
hit = c.get("用python写一个快速排序算法")
|
|
assert hit is not None
|
|
assert hit[0] == "semantic"
|
|
|
|
|
|
def test_promote_to_exact():
|
|
c = RouterCache(promote_frequency=3)
|
|
result = {"response": "x", "domain": "general"}
|
|
c.put("query", result)
|
|
# 语义命中 3 次后提升为精确缓存
|
|
for _ in range(3):
|
|
hit = c.get("query")
|
|
assert hit is not None
|
|
assert c.stats()["exact_size"] == 1
|
|
|
|
|
|
def test_stats():
|
|
c = RouterCache()
|
|
c.put("q", {"response": "r"})
|
|
c.get("q")
|
|
c.get("q")
|
|
c.get("miss")
|
|
s = c.stats()
|
|
assert s["exact_hits"] == 2
|
|
assert s["misses"] == 1
|