feat(v2): T8 人工检验队列 ReviewQueue(sqlite/抽样/审核)
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
"""T8 人工检验队列单测(封闭:临时 sqlite)。"""
|
||||
import pytest
|
||||
|
||||
from router_system.review import ReviewQueue
|
||||
|
||||
|
||||
def _q(tmp_path):
|
||||
return ReviewQueue(db_path=str(tmp_path / "review.sqlite3"))
|
||||
|
||||
|
||||
def test_enqueue_and_get(tmp_path):
|
||||
q = _q(tmp_path)
|
||||
rid = q.enqueue("req1", "问", "答", tags=["code"], reason="sample")
|
||||
assert rid == 1
|
||||
row = q.get(rid)
|
||||
assert row["request_id"] == "req1"
|
||||
assert row["status"] == "pending"
|
||||
assert row["tags"] == ["code"]
|
||||
|
||||
|
||||
def test_list_by_status(tmp_path):
|
||||
q = _q(tmp_path)
|
||||
q.enqueue("r1", "q", "a", tags=["code"])
|
||||
q.enqueue("r2", "q", "a", tags=["safety"])
|
||||
assert q.count() == 2
|
||||
assert q.count(status="pending") == 2
|
||||
q.submit(1, "approve")
|
||||
assert q.count(status="pending") == 1
|
||||
pending = q.list(status="pending")
|
||||
assert len(pending) == 1
|
||||
assert pending[0]["request_id"] == "r2"
|
||||
|
||||
|
||||
def test_submit_verdicts(tmp_path):
|
||||
q = _q(tmp_path)
|
||||
rid = q.enqueue("r1", "q", "a")
|
||||
assert q.submit(rid, "edit", correction="修正文本") is True
|
||||
row = q.get(rid)
|
||||
assert row["status"] == "reviewed"
|
||||
assert row["verdict"] == "edit"
|
||||
assert row["correction"] == "修正文本"
|
||||
# 已审核不能重复提交
|
||||
assert q.submit(rid, "approve") is False
|
||||
|
||||
|
||||
def test_submit_invalid_verdict(tmp_path):
|
||||
q = _q(tmp_path)
|
||||
rid = q.enqueue("r1", "q", "a")
|
||||
with pytest.raises(ValueError):
|
||||
q.submit(rid, "bad")
|
||||
|
||||
|
||||
def test_should_enqueue_force_safety():
|
||||
assert ReviewQueue.should_enqueue(["safety"], sample_rate=0.0,
|
||||
force_tags=["safety"]) is True
|
||||
assert ReviewQueue.should_enqueue(["code"], sample_rate=0.0,
|
||||
force_tags=["safety"]) is False
|
||||
|
||||
|
||||
def test_should_enqueue_sample_rate():
|
||||
import random
|
||||
# 固定随机种子下按 10% 抽样应命中/不命中可控
|
||||
rng = random.Random(42)
|
||||
hit = sum(ReviewQueue.should_enqueue(["code"], sample_rate=0.0, force_tags=[], rng=rng) for _ in range(1000))
|
||||
assert hit == 0 # sample_rate=0 -> 永不抽样
|
||||
rng = random.Random(1)
|
||||
hit = sum(ReviewQueue.should_enqueue(["code"], sample_rate=1.0, force_tags=[], rng=rng) for _ in range(10))
|
||||
assert hit == 10 # sample_rate=1 -> 全抽样
|
||||
|
||||
|
||||
def test_db_recreated(tmp_path):
|
||||
q1 = _q(tmp_path)
|
||||
q1.enqueue("r1", "q", "a")
|
||||
# 重新打开同一 db,数据仍在
|
||||
q2 = ReviewQueue(db_path=str(tmp_path / "review.sqlite3"))
|
||||
assert q2.count() == 1
|
||||
Reference in New Issue
Block a user