feat(training): 完善安全培训题库与考试交互
修复 PDF 导入判断和填空题解析,支持文本题判分、回顾改答及安全培训三级流程校验。
This commit is contained in:
1 parent
ec4ed9b67d
commit
e62e4961d4
19 files changed
+721
-48
No files matched your search
@@ -12,9 +12,13 @@ from datetime import date, datetime
|
||||
from io import BytesIO
|
||||
|
||||
from openpyxl import load_workbook
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
from app.core.errors import ValidationError
|
||||
from app.models import Agent, TrainingRecord, User
|
||||
from app.services import training_service
|
||||
from app.services.training_service import _grade_answer_details, _normalize_question_bank
|
||||
|
||||
|
||||
def _create_user(
|
||||
@@ -120,6 +124,35 @@ def _auth_headers(user: User) -> dict[str, str]:
|
||||
}
|
||||
|
||||
|
||||
def test_normalize_question_bank_accepts_judgment_and_qa_aliases():
|
||||
"""关键断言:题库历史类型别名必须映射为员工端可渲染、服务端可判卷的标准题型。"""
|
||||
question_bank = _normalize_question_bank([
|
||||
{"id": "q1", "type": "judgment", "prompt": "安全帽必须正确佩戴。", "answer": "true"},
|
||||
{"id": "q2", "type": "问答题", "prompt": "请说明发现隐患后的处理流程。", "answer": ""},
|
||||
])
|
||||
|
||||
assert [item["type"] for item in question_bank] == ["boolean", "essay"]
|
||||
|
||||
|
||||
def test_training_text_questions_apply_standard_answer_and_unanswered_scoring_rules():
|
||||
"""关键断言:文本题标准答案忽略空白与大小写;无答案题按非空 80%、超过 20 字 100% 判分。"""
|
||||
question_bank = _normalize_question_bank([
|
||||
{"id": "q1", "type": "fill", "prompt": "填写系统名称", "answer": "OA 系统"},
|
||||
{"id": "q2", "type": "essay", "prompt": "说明安全要求", "answer": "", "score_ratio": 2},
|
||||
{"id": "q3", "type": "fill", "prompt": "填写电话", "answer": "", "score_ratio": 1},
|
||||
])
|
||||
|
||||
details, score, passed = _grade_answer_details([
|
||||
{"question_id": "q1", "answer_text": " oa系统 "},
|
||||
{"question_id": "q2", "answer_text": "发现安全隐患后应立即停止作业并报告负责人,确认整改完成后再恢复作业。"},
|
||||
{"question_id": "q3", "answer_text": "119"},
|
||||
], question_bank, 90)
|
||||
|
||||
assert [item["score_ratio"] for item in details] == [1.0, 1.0, 0.8]
|
||||
assert score == 95
|
||||
assert passed is True
|
||||
|
||||
|
||||
def test_training_user_progress_round_trip_and_completion(client, app):
|
||||
"""员工侧培训进度应支持空记录读取、过程保存、结果结算与再次读取回显。"""
|
||||
with app.app_context():
|
||||
@@ -260,6 +293,45 @@ def test_safety_training_progress_round_trip(client, app):
|
||||
}
|
||||
|
||||
|
||||
def test_safety_exam_requires_all_configured_learning_resources(monkeypatch):
|
||||
"""断言安全培训缺少任一级资料完成记录时,后端拒绝直接进入考试或结算。"""
|
||||
monkeypatch.setattr(
|
||||
training_service,
|
||||
"get_published_config",
|
||||
lambda _training_type: {
|
||||
"resources": [
|
||||
{"id": 11, "resource_type": "agent_guide", "training_level": "company"},
|
||||
{"id": 12, "resource_type": "kb_document_version", "training_level": "department"},
|
||||
{"id": 13, "resource_type": "kb_document_version", "training_level": "team"},
|
||||
],
|
||||
},
|
||||
)
|
||||
progress = {"resource_progress": {"11": {"completed": True}, "12": {"completed": False}}}
|
||||
|
||||
# 关键断言:客户端伪造 exam 阶段不能跳过班组级学习资料。
|
||||
with pytest.raises(ValidationError):
|
||||
training_service._assert_safety_exam_prerequisites("exam", progress, "safety")
|
||||
|
||||
|
||||
def test_safety_exam_accepts_all_configured_learning_resources(monkeypatch):
|
||||
"""断言三级配置资料均完成时,后端允许安全培训进入考试阶段。"""
|
||||
monkeypatch.setattr(
|
||||
training_service,
|
||||
"get_published_config",
|
||||
lambda _training_type: {
|
||||
"resources": [
|
||||
{"id": 11, "resource_type": "agent_guide", "training_level": "company"},
|
||||
{"id": 12, "resource_type": "kb_document_version", "training_level": "department"},
|
||||
{"id": 13, "resource_type": "kb_document_version", "training_level": "team"},
|
||||
],
|
||||
},
|
||||
)
|
||||
progress = {"resource_progress": {str(resource_id): {"completed": True} for resource_id in (11, 12, 13)}}
|
||||
|
||||
# 关键断言:所有已配置资料完成后,不阻断正常开考。
|
||||
training_service._assert_safety_exam_prerequisites("exam", progress, "safety")
|
||||
|
||||
|
||||
def test_training_result_is_graded_by_server_question_bank(client, app):
|
||||
"""服务端应基于 Agent 题库重算分数与通过状态,不能信任前端伪造结果。"""
|
||||
with app.app_context():
|
||||
|
||||
Reference in new issue
Block a user