feat(training): 完善安全培训题库与考试交互
修复 PDF 导入判断和填空题解析,支持文本题判分、回顾改答及安全培训三级流程校验。
This commit is contained in:
1 parent
ec4ed9b67d
commit
e62e4961d4
19 files changed
+721
-48
No files matched your search
@@ -32,6 +32,21 @@ def test_parse_training_question_bank_text_supports_inline_answers():
|
||||
assert payload['question_bank'][0]['explanation'] == '工资固定在每月15号发放。'
|
||||
|
||||
|
||||
def test_parse_training_question_bank_text_supports_parenthesized_inline_answers():
|
||||
"""断言题干末尾的括号答案可被识别,避免格式差异导致整卷题库为空。"""
|
||||
text = """
|
||||
一、选择题
|
||||
1、发生火灾时应第一时间拨打哪个号码? A、110 B、119 C、120 D、122 (B)
|
||||
2、个人防护用品由谁提供? A、员工自费 B、公司免费提供 C、客户提供 (B)
|
||||
"""
|
||||
|
||||
payload = parse_training_question_bank_text(text)
|
||||
|
||||
# 关键断言:括号中的标准答案应与对应选项匹配并完整生成两道单选题。
|
||||
assert payload['parsed_count'] == 2
|
||||
assert [item['answer'] for item in payload['question_bank']] == ['b', 'b']
|
||||
|
||||
|
||||
def test_parse_training_question_bank_text_supports_answer_sheet_mapping():
|
||||
text = """
|
||||
1、发生火灾时应第一时间拨打哪个号码? A.110 B.119 C.120 D.122
|
||||
@@ -124,6 +139,73 @@ def test_parse_training_question_bank_text_supports_boolean_range_answer_sheet()
|
||||
assert [item['answer'] for item in payload['question_bank']] == ['c', 'b', 'true', 'true', 'false']
|
||||
|
||||
|
||||
def test_parse_training_question_bank_text_supports_fill_and_essay_sections_without_answers():
|
||||
"""关键断言:填空题和问答题即使没有标准答案也必须保留,供服务端按文本作答规则判卷。"""
|
||||
text = """
|
||||
三、填空题(1-10)
|
||||
1. 公司安全生产方针是:______。
|
||||
2. 发生火灾时应拨打______。
|
||||
四、问答题
|
||||
1. 请说明发现安全隐患后的处理流程。
|
||||
"""
|
||||
|
||||
payload = parse_training_question_bank_text(text)
|
||||
|
||||
assert payload['parsed_count'] == 3
|
||||
assert [item['type'] for item in payload['question_bank']] == ['fill', 'fill', 'essay']
|
||||
assert [item['answer'] for item in payload['question_bank']] == ['', '', '']
|
||||
|
||||
|
||||
def test_parse_training_question_bank_text_supports_boolean_section_and_answer_range_with_delimiter():
|
||||
"""关键断言:带“判断题”小节和“1-10、√”答案表时,判断题不得被填空区吞掉或跳过。"""
|
||||
text = """
|
||||
一、填空:(每题3分)
|
||||
1、公司安全生产方针是:______。
|
||||
|
||||
二、判断题:(每题2分)
|
||||
1、“预防为主”就是把安全工作的重点从事后处理转变为事前预防。()
|
||||
2、安全生产管理是企业管理人员的事,跟普通工人无关。()
|
||||
|
||||
三、问答题
|
||||
1、简要叙述下你所在岗位的安全风险?
|
||||
|
||||
参考答案:
|
||||
一、填空:1、厂级(公司级)、车间级、班组级
|
||||
二、判断题:1-2、√ ×
|
||||
"""
|
||||
|
||||
payload = parse_training_question_bank_text(text)
|
||||
|
||||
assert [item['type'] for item in payload['question_bank']] == ['fill', 'boolean', 'boolean', 'essay']
|
||||
assert [item['answer'] for item in payload['question_bank'][1:3]] == ['true', 'false']
|
||||
|
||||
|
||||
def test_parse_training_question_bank_text_preserves_fill_answers_and_ignores_repeated_answer_section():
|
||||
"""关键断言:填空答案区不能变成新题,且标准答案必须回填到题库用于前后端判分。"""
|
||||
text = """
|
||||
一、填空题
|
||||
1、三级教育是指 、 、 。
|
||||
2、操作牌是设备操作的 。
|
||||
二、判断题
|
||||
1、安全生产人人有责。()
|
||||
三、问答题
|
||||
1、说明岗位安全风险。
|
||||
|
||||
参考答案:
|
||||
一、填空题
|
||||
1、公司级、车间级、班组级
|
||||
2、唯一凭证
|
||||
二、判断题
|
||||
1、√
|
||||
"""
|
||||
|
||||
payload = parse_training_question_bank_text(text)
|
||||
|
||||
assert [item['type'] for item in payload['question_bank']] == ['fill', 'fill', 'boolean', 'essay']
|
||||
assert [item['answer'] for item in payload['question_bank'][:2]] == ['公司级、车间级、班组级', '唯一凭证']
|
||||
assert [item['prompt'].count('______') for item in payload['question_bank'][:2]] == [3, 1]
|
||||
|
||||
|
||||
def test_parse_training_question_bank_text_raises_when_no_usable_questions():
|
||||
with pytest.raises(ValidationError, match='未能从试题 PDF 中识别出可用题目'):
|
||||
parse_training_question_bank_text('这是一份扫描件占位文本,没有题号也没有答案。')
|
||||
|
||||
@@ -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