feat: tag validation logic + pytest-cov auto coverage
- Implement min_tags/max_tags validation in platform_rules.py - Support tags as list or comma-separated string - Add tags field to ContentValidateRequest schemas - Pass tags through API and service layers - Configure pytest-cov in pyproject.toml (auto coverage on pytest run)
This commit is contained in:
parent
4507afbbfd
commit
2a46f89a8a
|
|
@ -91,6 +91,7 @@ async def validate_content(req: ContentValidateRequest):
|
|||
content=req.content,
|
||||
title=req.title,
|
||||
platform=req.platform,
|
||||
tags=req.tags,
|
||||
)
|
||||
|
||||
return ContentValidateResponse(
|
||||
|
|
|
|||
|
|
@ -335,6 +335,7 @@ async def validate_content_for_platform(
|
|||
content=req.content,
|
||||
title=req.title,
|
||||
platform_id=platform_id,
|
||||
tags=req.tags,
|
||||
)
|
||||
|
||||
issues = [
|
||||
|
|
|
|||
|
|
@ -45,6 +45,7 @@ class ContentValidateRequest(BaseModel):
|
|||
content: str = Field(min_length=1)
|
||||
title: str = Field(min_length=1)
|
||||
platform: str
|
||||
tags: list[str] | None = Field(default=None, description="内容标签列表")
|
||||
|
||||
|
||||
class ValidationIssue(BaseModel):
|
||||
|
|
|
|||
|
|
@ -266,6 +266,7 @@ class ContentValidateRequest(BaseModel):
|
|||
content: str = Field(..., description="待验证内容")
|
||||
title: str = Field(..., description="标题")
|
||||
platform: str = Field(..., description="平台ID")
|
||||
tags: list[str] | None = Field(default=None, description="内容标签列表")
|
||||
|
||||
|
||||
# ============================================================
|
||||
|
|
|
|||
|
|
@ -942,7 +942,7 @@ class PlatformRuleEngine:
|
|||
"publish_rules",
|
||||
]
|
||||
|
||||
def validate_content(self, content: str, title: str, platform: str) -> dict:
|
||||
def validate_content(self, content: str, title: str, platform: str, tags: list[str] | str | None = None) -> dict:
|
||||
"""
|
||||
校验内容是否符合平台规则
|
||||
|
||||
|
|
@ -1012,7 +1012,29 @@ class PlatformRuleEngine:
|
|||
tag_rules = rules.get("tag_rules", {})
|
||||
min_tags = tag_rules.get("min_tags", 0)
|
||||
max_tags = tag_rules.get("max_tags", 10)
|
||||
# TODO: 需要传入标签数据进行验证
|
||||
|
||||
# 解析标签数量
|
||||
if tags is None:
|
||||
tag_count = 0
|
||||
elif isinstance(tags, str):
|
||||
tag_count = len([t.strip() for t in tags.split(",") if t.strip()])
|
||||
else:
|
||||
tag_count = len(tags)
|
||||
|
||||
if min_tags > 0 and tag_count < min_tags:
|
||||
issues.append({
|
||||
"severity": "medium",
|
||||
"message": f"标签数量 {tag_count} 低于最低要求 {min_tags}",
|
||||
"category": "tag_count",
|
||||
})
|
||||
elif max_tags > 0 and tag_count > max_tags:
|
||||
issues.append({
|
||||
"severity": "high",
|
||||
"message": f"标签数量 {tag_count} 超过限制 {max_tags}",
|
||||
"category": "tag_count",
|
||||
})
|
||||
else:
|
||||
passed.append(f"标签数量合规({tag_count})")
|
||||
|
||||
# --- AI敏感度检测 ---
|
||||
ai_sensitivity = rules.get("ai_sensitivity", {})
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ class PlatformRuleService:
|
|||
return self._engine.get_platform_rule(platform_id, rule_category)
|
||||
|
||||
def validate_content(
|
||||
self, content: str, title: str, platform_id: str
|
||||
self, content: str, title: str, platform_id: str, tags: list[str] | str | None = None
|
||||
) -> dict:
|
||||
"""验证内容是否符合平台规则
|
||||
|
||||
|
|
@ -67,11 +67,12 @@ class PlatformRuleService:
|
|||
content: 内容正文
|
||||
title: 标题
|
||||
platform_id: 平台标识
|
||||
tags: 内容标签列表
|
||||
|
||||
Returns:
|
||||
验证结果
|
||||
"""
|
||||
return self._engine.validate_content(content, title, platform_id)
|
||||
return self._engine.validate_content(content, title, platform_id, tags=tags)
|
||||
|
||||
def detect_ai_patterns(self, content: str, platform_id: str) -> list[dict]:
|
||||
"""检测内容中的AI写作模式
|
||||
|
|
|
|||
|
|
@ -15,3 +15,21 @@ testpaths = ["tests"]
|
|||
python_files = ["test_*.py"]
|
||||
python_classes = ["Test*"]
|
||||
python_functions = ["test_*"]
|
||||
addopts = "--cov=app --cov-report=term-missing --cov-report=html"
|
||||
|
||||
[tool.coverage.run]
|
||||
omit = [
|
||||
"tests/*",
|
||||
"migrations/*",
|
||||
"*/__pycache__/*",
|
||||
"app/conftest.py",
|
||||
]
|
||||
|
||||
[tool.coverage.report]
|
||||
exclude_lines = [
|
||||
"pragma: no cover",
|
||||
"if __name__ == .__main__.:",
|
||||
"pass",
|
||||
"raise NotImplementedError",
|
||||
"except ImportError:",
|
||||
]
|
||||
|
|
|
|||
Loading…
Reference in New Issue