9598c7a1da
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
"""classify_cancelled_fallback / make_hit 单测。"""
|
|
from app.services.compare_alert import classify_cancelled_fallback, make_hit
|
|
|
|
|
|
class _Rec:
|
|
def __init__(self, **kw):
|
|
self.trace_id = kw.get("trace_id", "t")
|
|
self.status = kw.get("status", "cancelled")
|
|
self.total_ms = kw.get("total_ms")
|
|
self.step_count = kw.get("step_count")
|
|
self.fail_reason = kw.get("fail_reason")
|
|
self.information = kw.get("information")
|
|
self.app_version = kw.get("app_version")
|
|
self.created_at = kw.get("created_at")
|
|
self.trace_url = kw.get("trace_url")
|
|
self.user_id = kw.get("user_id")
|
|
|
|
|
|
def test_fallback_deep_by_ms():
|
|
hit = classify_cancelled_fallback(
|
|
_Rec(total_ms=95000, step_count=5),
|
|
cancelled_ms_threshold=90000, cancelled_step_threshold=30,
|
|
)
|
|
assert hit is not None and hit.alert_type == "T5" and "深度放弃" in hit.reason
|
|
|
|
|
|
def test_fallback_deep_by_step():
|
|
hit = classify_cancelled_fallback(
|
|
_Rec(total_ms=1000, step_count=35),
|
|
cancelled_ms_threshold=90000, cancelled_step_threshold=30,
|
|
)
|
|
assert hit is not None and hit.alert_type == "T5"
|
|
|
|
|
|
def test_fallback_shallow_none():
|
|
hit = classify_cancelled_fallback(
|
|
_Rec(total_ms=5000, step_count=3),
|
|
cancelled_ms_threshold=90000, cancelled_step_threshold=30,
|
|
)
|
|
assert hit is None
|
|
|
|
|
|
def test_make_hit_carries_fields():
|
|
hit = make_hit(_Rec(trace_id="tx", app_version="0.6.0"), "T5", "卡在 美团·加菜")
|
|
assert hit.trace_id == "tx"
|
|
assert hit.reason == "卡在 美团·加菜"
|
|
assert hit.app_version == "0.6.0"
|
|
|
|
|
|
def test_make_hit_carries_total_ms_and_step():
|
|
hit = make_hit(_Rec(trace_id="tx", total_ms=602000, step_count=157), "T5", "深度放弃")
|
|
assert hit.total_ms == 602000
|
|
assert hit.step_count == 157
|
|
|
|
|
|
def test_make_hit_total_ms_step_default_none():
|
|
# rec 没有这两个属性时安全降级为 None(不报错)
|
|
class _Bare:
|
|
trace_id = "b"; status = "cancelled"; reason = None
|
|
app_version = None; created_at = None; trace_url = None; user_id = None
|
|
hit = make_hit(_Bare(), "T1", "技术失败")
|
|
assert hit.total_ms is None and hit.step_count is None
|