208 lines
6.6 KiB
Python
208 lines
6.6 KiB
Python
"""内容生成Pipeline测试"""
|
|
import pytest
|
|
from app.services.content.content_pipeline import ContentPipeline, PipelineResponse
|
|
|
|
|
|
class TestContentPipeline:
|
|
"""内容Pipeline测试"""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_complete_run(self):
|
|
"""测试完整Pipeline执行"""
|
|
pipeline = ContentPipeline()
|
|
request = {
|
|
"content": "这是一篇关于华为手机评测的深度文章,"
|
|
"详细介绍了华为Mate系列的特点和性能。",
|
|
"title": "华为手机评测",
|
|
"platform": "zhihu",
|
|
"optimize_for": ["validation", "sensitive", "seo"],
|
|
"output_formats": ["html", "markdown", "plain"],
|
|
"keyword": "华为手机"
|
|
}
|
|
|
|
result = await pipeline.run(request)
|
|
|
|
# 验证返回类型
|
|
assert isinstance(result, PipelineResponse)
|
|
assert result.stages is not None
|
|
assert len(result.stages) > 0
|
|
|
|
# 验证输出
|
|
assert result.outputs is not None
|
|
assert result.outputs.html is not None or result.outputs.markdown is not None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_validation_only(self):
|
|
"""测试仅校验模式"""
|
|
pipeline = ContentPipeline()
|
|
request = {
|
|
"content": "这是一篇测试文章内容",
|
|
"title": "测试标题",
|
|
"platform": "zhihu",
|
|
"optimize_for": ["validation"]
|
|
}
|
|
|
|
result = await pipeline.run(request)
|
|
|
|
assert result.stages is not None
|
|
# 仅校验模式不生成输出
|
|
validation_stage = next((s for s in result.stages if s.name == "validation"), None)
|
|
assert validation_stage is not None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_sensitive_filter_only(self):
|
|
"""测试仅敏感词过滤模式"""
|
|
pipeline = ContentPipeline()
|
|
request = {
|
|
"content": "这是一篇正常内容的文章",
|
|
"title": "测试标题",
|
|
"platform": "zhihu",
|
|
"optimize_for": ["sensitive"]
|
|
}
|
|
|
|
result = await pipeline.run(request)
|
|
|
|
assert result.stages is not None
|
|
assert len(result.stages) >= 1
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_seo_only(self):
|
|
"""测试仅SEO优化模式"""
|
|
pipeline = ContentPipeline()
|
|
request = {
|
|
"content": "华为手机是知名的手机品牌",
|
|
"title": "华为手机评测",
|
|
"platform": "zhihu",
|
|
"optimize_for": ["seo"],
|
|
"keyword": "华为手机"
|
|
}
|
|
|
|
result = await pipeline.run(request)
|
|
|
|
assert result.stages is not None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_with_validation_fail(self):
|
|
"""测试校验失败场景"""
|
|
pipeline = ContentPipeline()
|
|
request = {
|
|
"content": "内容",
|
|
"title": "这个标题太长了超过了三十个字符的限制了哈哈哈啊",
|
|
"platform": "wechat",
|
|
"optimize_for": ["validation"]
|
|
}
|
|
|
|
result = await pipeline.run(request)
|
|
|
|
# 校验失败时不应继续执行后续阶段
|
|
validation_stage = next((s for s in result.stages if s.name == "validation"), None)
|
|
assert validation_stage is not None
|
|
assert validation_stage.passed is False
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_stage_timing(self):
|
|
"""测试各阶段耗时记录"""
|
|
pipeline = ContentPipeline()
|
|
request = {
|
|
"content": "这是一篇测试文章内容",
|
|
"title": "测试标题",
|
|
"platform": "zhihu",
|
|
"optimize_for": ["validation", "sensitive", "seo"]
|
|
}
|
|
|
|
result = await pipeline.run(request)
|
|
|
|
for stage in result.stages:
|
|
assert stage.name is not None
|
|
assert hasattr(stage, 'duration')
|
|
assert stage.duration >= 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_multi_platform(self):
|
|
"""测试多平台适配"""
|
|
pipeline = ContentPipeline()
|
|
|
|
platforms = ["zhihu", "wechat", "xiaohongshu"]
|
|
results = []
|
|
|
|
for platform in platforms:
|
|
result = await pipeline.run({
|
|
"content": "<p>测试内容</p>",
|
|
"title": "测试标题",
|
|
"platform": platform,
|
|
"optimize_for": ["validation", "sensitive"]
|
|
})
|
|
results.append(result)
|
|
assert result.stages is not None
|
|
|
|
# 各平台结果都应成功
|
|
assert len(results) == len(platforms)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_output_formats(self):
|
|
"""测试不同输出格式"""
|
|
pipeline = ContentPipeline()
|
|
request = {
|
|
"content": "测试内容",
|
|
"title": "测试标题",
|
|
"platform": "zhihu",
|
|
"optimize_for": [],
|
|
"output_formats": ["html", "markdown", "plain"]
|
|
}
|
|
|
|
result = await pipeline.run(request)
|
|
|
|
assert result.outputs is not None
|
|
# HTML生成是默认的
|
|
assert result.outputs.html is not None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_empty_optimize_for(self):
|
|
"""测试空的optimize_for参数"""
|
|
pipeline = ContentPipeline()
|
|
request = {
|
|
"content": "测试内容",
|
|
"title": "测试标题",
|
|
"platform": "zhihu",
|
|
"optimize_for": [],
|
|
"output_formats": ["html"]
|
|
}
|
|
|
|
result = await pipeline.run(request)
|
|
|
|
# 无优化阶段但应有HTML生成阶段
|
|
assert result.outputs is not None
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_error_handling(self):
|
|
"""测试无效平台错误处理"""
|
|
pipeline = ContentPipeline()
|
|
|
|
try:
|
|
result = await pipeline.run({
|
|
"content": "内容",
|
|
"title": "标题",
|
|
"platform": "invalid_platform"
|
|
})
|
|
# 应该返回错误
|
|
assert result.error is not None or len(result.stages) > 0
|
|
except ValueError as e:
|
|
assert "不支持的平台" in str(e)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pipeline_validate_only_method(self):
|
|
"""测试validate_only方法"""
|
|
pipeline = ContentPipeline()
|
|
|
|
result = await pipeline.validate_only(
|
|
content="测试内容",
|
|
title="测试标题",
|
|
platform="zhihu"
|
|
)
|
|
|
|
assert result is not None
|
|
assert hasattr(result, 'is_valid')
|
|
assert hasattr(result, 'score')
|
|
assert hasattr(result, 'issues')
|
|
assert hasattr(result, 'passed')
|