""" tests/services/admin/test_faq_service.py 用途:faq_service 全局常用问题 CRUD 单元测试。 """ import pytest from app import db from app.core.errors import NotFoundError, ValidationError from app.models import Agent, QuickQuestion from app.services.admin import faq_service def test_quick_question_crud(app): with app.app_context(): agent = Agent(name='faq-agent', type='api', dify_app_id='app-1', dify_api_key='key-1') db.session.add(agent) db.session.commit() created = faq_service.create_quick_question( { 'title': '请假', 'question': '如何请假?', 'agent_id': agent.id, 'answer': '在 OA 提交申请', 'cache_enabled': True, } ) qq_id = created['id'] items = faq_service.list_quick_questions(stale_only=False) assert len(items) == 1 assert items[0]['title'] == '请假' assert items[0]['agent_name'] == 'faq-agent' faq_service.update_quick_question(qq_id, {'title': '请假流程'}) qq = db.session.get(QuickQuestion, qq_id) assert qq.title == '请假流程' faq_service.delete_quick_question(qq_id) assert db.session.get(QuickQuestion, qq_id) is None def test_update_quick_question_not_found(app): with app.app_context(): with pytest.raises(NotFoundError): faq_service.update_quick_question(99999, {'title': 'x'}) def test_create_quick_question_requires_cache_answer(app): with app.app_context(): agent = Agent(name='faq-agent-2', type='api', dify_app_id='app-2', dify_api_key='key-2') db.session.add(agent) db.session.commit() with pytest.raises(ValidationError): faq_service.create_quick_question( { 'title': 't', 'question': 'q', 'agent_id': agent.id, 'cache_enabled': True, 'answer': '', } )