feat(regulatory): 增加风险归并与核查报告导出

This commit is contained in:
2026-06-07 00:39:33 +08:00
parent ec89e62661
commit 4c28466fe4
6 changed files with 401 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
import json
import pytest
from review_agent.models import (
Conversation,
ExportedSummaryFile,
FileSummaryBatch,
RegulatoryIssue,
RegulatoryReviewBatch,
)
from review_agent.regulatory_review.services.export import export_review_results
pytestmark = pytest.mark.django_db
def test_export_review_results_creates_markdown_excel_and_json(settings, tmp_path, django_user_model):
settings.MEDIA_ROOT = tmp_path
user = django_user_model.objects.create_user(username="owner", password="pass")
conversation = Conversation.objects.create(user=user, title="会话")
summary = FileSummaryBatch.objects.create(conversation=conversation, user=user, batch_no="FS-OK")
batch = RegulatoryReviewBatch.objects.create(
conversation=conversation,
user=user,
source_summary_batch=summary,
batch_no="RR-EXPORT",
risk_summary={"blocking": 1},
)
RegulatoryIssue.objects.create(
batch=batch,
rule_code="registration_test_report",
category=RegulatoryIssue.Category.COMPLETENESS,
severity=RegulatoryIssue.Severity.BLOCKING,
title="缺少注册检验报告",
suggestion="请补充注册检验报告并复核。",
)
exports = export_review_results(batch)
assert {export.export_type for export in exports} == {
ExportedSummaryFile.ExportType.MARKDOWN,
ExportedSummaryFile.ExportType.EXCEL,
ExportedSummaryFile.ExportType.JSON,
}
json_export = next(export for export in exports if export.export_type == ExportedSummaryFile.ExportType.JSON)
payload = json.loads(open(json_export.storage_path, encoding="utf-8").read())
assert payload["batch_no"] == "RR-EXPORT"
assert payload["issues"][0]["title"] == "缺少注册检验报告"

View File

@@ -0,0 +1,35 @@
import pytest
from review_agent.models import Conversation, FileSummaryBatch, RegulatoryIssue, RegulatoryReviewBatch
from review_agent.regulatory_review.schemas import Finding
from review_agent.regulatory_review.services.risk_assess import persist_findings
pytestmark = pytest.mark.django_db
def test_persist_findings_deduplicates_and_updates_risk_summary(django_user_model):
user = django_user_model.objects.create_user(username="owner", password="pass")
conversation = Conversation.objects.create(user=user, title="会话")
summary = FileSummaryBatch.objects.create(conversation=conversation, user=user, batch_no="FS-OK")
batch = RegulatoryReviewBatch.objects.create(
conversation=conversation,
user=user,
source_summary_batch=summary,
batch_no="RR-RISK",
)
finding = Finding(
rule_code="registration_test_report",
category="completeness",
severity="blocking",
title="缺少注册检验报告",
suggestion="请补充注册检验报告并复核。",
citations=[{"source": "法规.doc", "text": "注册检验报告"}],
)
issues = persist_findings(batch, [finding, finding])
batch.refresh_from_db()
assert len(issues) == 1
assert RegulatoryIssue.objects.count() == 1
assert batch.risk_summary["blocking"] == 1

View File

@@ -2,8 +2,11 @@ import pytest
from review_agent.models import (
Conversation,
ExportedSummaryFile,
FileSummaryBatch,
FileSummaryItem,
Message,
RegulatoryIssue,
RegulatoryReviewBatch,
WorkflowEvent,
WorkflowNodeRun,
@@ -155,3 +158,43 @@ def test_stream_message_starts_regulatory_workflow(monkeypatch, settings, django
assert "workflow_started" in joined
assert "\"workflow_type\": \"regulatory_review\"" in joined
assert RegulatoryReviewBatch.objects.filter(conversation=conversation).exists()
def test_workflow_generates_issues_exports_and_assistant_summary(settings, tmp_path, django_user_model):
settings.MEDIA_ROOT = tmp_path
settings.REGULATORY_REVIEW_ASYNC = False
settings.REGULATORY_RAG_CHROMA_PATH = tmp_path / "missing-rag"
user = django_user_model.objects.create_user(username="owner", password="pass")
conversation = Conversation.objects.create(user=user, title="会话")
summary = FileSummaryBatch.objects.create(
conversation=conversation,
user=user,
batch_no="FS-OK",
status=FileSummaryBatch.Status.SUCCESS,
)
ifu_path = tmp_path / "ifu.txt"
ifu_path.write_text("产品名称:甲胎蛋白检测试剂盒\n样本要求:血清\n有效期12个月", encoding="utf-8")
FileSummaryItem.objects.create(
batch=summary,
file_index=1,
file_name="说明书.txt",
file_type="txt",
relative_path="说明书.txt",
storage_path=str(ifu_path),
)
batch = create_regulatory_review_batch(
conversation=conversation,
user=user,
source_summary_batch=summary,
)
start_regulatory_review_workflow(batch, async_run=False)
batch.refresh_from_db()
assert batch.status == RegulatoryReviewBatch.Status.SUCCESS
assert RegulatoryIssue.objects.filter(batch=batch, severity="blocking").exists()
assert ExportedSummaryFile.objects.filter(
workflow_type="regulatory_review",
workflow_batch_id=batch.pk,
).count() == 3
assert conversation.messages.filter(role=Message.Role.ASSISTANT, content__contains="已完成 NMPA").exists()