72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
import pytest
|
||
|
||
from app.services.situational_alert_ai_brief import build_situational_alert_brief_request
|
||
|
||
|
||
class _SingleUseScalarResult:
|
||
def __init__(self, value=0, rows=None):
|
||
self.value = value
|
||
self.rows = rows or []
|
||
self.scalar_calls = 0
|
||
|
||
def scalar(self):
|
||
self.scalar_calls += 1
|
||
if self.scalar_calls > 1:
|
||
raise AssertionError("scalar result was consumed more than once")
|
||
return self.value
|
||
|
||
def fetchall(self):
|
||
return self.rows
|
||
|
||
def scalar_one_or_none(self):
|
||
return None
|
||
|
||
def scalars(self):
|
||
rows = self.rows
|
||
|
||
class _Scalars:
|
||
def all(self):
|
||
return rows
|
||
|
||
return _Scalars()
|
||
|
||
|
||
class _FakeBriefSession:
|
||
def __init__(self):
|
||
self._results = [
|
||
_SingleUseScalarResult(3),
|
||
_SingleUseScalarResult(2),
|
||
_SingleUseScalarResult(rows=[]),
|
||
_SingleUseScalarResult(rows=[]),
|
||
_SingleUseScalarResult(rows=[]),
|
||
_SingleUseScalarResult(4),
|
||
_SingleUseScalarResult(1),
|
||
_SingleUseScalarResult(rows=[]),
|
||
_SingleUseScalarResult(rows=[]),
|
||
_SingleUseScalarResult(5),
|
||
_SingleUseScalarResult(2),
|
||
_SingleUseScalarResult(rows=[]),
|
||
_SingleUseScalarResult(),
|
||
]
|
||
|
||
async def execute(self, _query):
|
||
return self._results.pop(0)
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_situational_alert_brief_builder_reuses_counts_without_reconsuming_results(monkeypatch):
|
||
monkeypatch.setattr(
|
||
"app.services.situational_alert_ai_brief.get_latest_bgp_brief_record",
|
||
lambda: None,
|
||
)
|
||
|
||
request, facts, context = await build_situational_alert_brief_request(_FakeBriefSession())
|
||
|
||
assert request.title == "态势告警 AI 简报"
|
||
assert "总告警 3 条,active 2 条" in facts[0]
|
||
assert "累计 incidents 4 条,active incidents 1 条" in facts[1]
|
||
assert "累计 anomalies 5 条,active anomalies 2 条" in facts[2]
|
||
assert context["active_system_alerts"] == 2
|
||
assert context["active_bgp_incidents"] == 1
|
||
assert context["active_bgp_anomalies"] == 2
|