83 lines
3.7 KiB
Python
83 lines
3.7 KiB
Python
"""
|
|
backend/tests/services/test_feedback_notify.py
|
|
|
|
用途:锁定企微反馈通知 markdown 组装契约——有图反馈在「反馈内容」段之前
|
|
多一行「附图:N 张」(N>0 时),无图反馈不得出现「附图」字样,
|
|
且其余既有行内容与顺序零改动。
|
|
依赖:app 夹具(create_app('testing') + db.create_all,不走迁移)、
|
|
GeneralFeedback/GeneralFeedbackImage/User/FileAsset 模型、
|
|
app.services.feedback_notify.build_feedback_markdown。
|
|
逻辑:build_feedback_markdown 为纯组装函数(不发网络请求),测试直接落
|
|
User/FileAsset/GeneralFeedback/GeneralFeedbackImage 行后调用并断言文本,
|
|
覆盖有图(2 张)与无图两条路径。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from uuid import uuid4
|
|
|
|
from app import db
|
|
from app.models import FileAsset, GeneralFeedback, GeneralFeedbackImage, User
|
|
from app.services.feedback_notify import build_feedback_markdown
|
|
|
|
|
|
def _create_user(username: str) -> User:
|
|
"""按用户名幂等落一个测试用户(GeneralFeedback.user_id 外键依赖)。"""
|
|
user = db.session.query(User).filter_by(username=username).first()
|
|
if not user:
|
|
user = User(username=username, email=f"{username}@example.com", role="user", status="active")
|
|
user.set_password("x")
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
return user
|
|
|
|
|
|
def _make_asset(user_id: int) -> FileAsset:
|
|
"""直接落一条 ready/temporary 的图片 FileAsset 行(GeneralFeedbackImage.asset_id 外键依赖)。"""
|
|
asset = FileAsset(
|
|
asset_no=f"FA-TEST-{uuid4().hex[:8].upper()}", owner_user_id=int(user_id),
|
|
original_filename="screenshot.png", storage_provider="minio", storage_bucket="test",
|
|
storage_key=f"test/{user_id}/{uuid4().hex}", content_hash="0" * 64, file_size=1024,
|
|
mime_type="image/png", extension="png", upload_status="ready",
|
|
lifecycle_status="temporary", reference_count=0,
|
|
)
|
|
db.session.add(asset)
|
|
db.session.commit()
|
|
return asset
|
|
|
|
|
|
def _create_feedback(user: User, content: str) -> GeneralFeedback:
|
|
"""落一条最小可用的 GeneralFeedback 行(必填字段齐全)。"""
|
|
feedback = GeneralFeedback(
|
|
user_id=user.id, username=user.username,
|
|
feedback_type="问题反馈", feedback_module="知识库", content=content,
|
|
source_page="workspace",
|
|
)
|
|
db.session.add(feedback)
|
|
db.session.commit()
|
|
return feedback
|
|
|
|
|
|
def test_markdown_includes_image_count(app):
|
|
"""有图反馈:markdown 含「附图:2 张」行;无图反馈:不含「附图」。"""
|
|
with app.app_context():
|
|
user = _create_user("notifyuser")
|
|
# 有图反馈:2 条图片关联(asset_id 唯一,各自独立 FileAsset),走真实 images relationship
|
|
fb_with_images = _create_feedback(user, "这条反馈带两张截图")
|
|
db.session.add_all([
|
|
GeneralFeedbackImage(feedback_id=fb_with_images.id, asset_id=_make_asset(user.id).id, sort_order=0),
|
|
GeneralFeedbackImage(feedback_id=fb_with_images.id, asset_id=_make_asset(user.id).id, sort_order=1),
|
|
])
|
|
db.session.commit()
|
|
# 无图反馈:不落任何 GeneralFeedbackImage
|
|
fb_without_images = _create_feedback(user, "这条反馈没有截图")
|
|
|
|
md_with_images = build_feedback_markdown(fb_with_images, None)
|
|
md_without_images = build_feedback_markdown(fb_without_images, None)
|
|
|
|
# 有图:附图行存在,且必须位于「反馈内容」块之前
|
|
assert "附图:2 张" in md_with_images
|
|
assert md_with_images.index("附图:2 张") < md_with_images.index("**反馈内容:**")
|
|
# 无图:不得出现「附图」字样
|
|
assert "附图" not in md_without_images
|