G41_TAPD_BUG_SYNC/src2/test_phase2.py

211 lines
5.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
任务二第二阶段验证脚本
测试链接解析和TAPD API功能
"""
import sys
from pathlib import Path
# 将项目根目录添加到 Python 路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
from src2.link_parser import parse_tapd_link, extract_story_id, is_valid_tapd_link
from src2.tapd_api import TAPDStoryApi, map_status, STATUS_MAPPING
def test_link_parser():
"""测试链接解析功能"""
print("=" * 60)
print("测试1: 链接解析器")
print("=" * 60)
test_cases = [
# 格式一:列表页弹窗链接
(
"https://www.tapd.cn/tapd_fe/58335167/story/list?dialog_preview_id=story_1158335167001044388",
True,
"1158335167001044388",
"dialog"
),
# 格式二:详情页链接
(
"https://www.tapd.cn/58335167/prong/stories/view/1158335167001044388",
True,
"1158335167001044388",
"view"
),
# 无效链接Bug链接
(
"https://www.tapd.cn/58335167/bugtrace/bugs/view/123456",
False,
None,
"unknown"
),
# 无效链接:其他网站
(
"https://www.google.com",
False,
None,
"unknown"
),
# 空链接
(
"",
False,
None,
"unknown"
),
]
passed = 0
failed = 0
for i, (url, expected_success, expected_id, expected_type) in enumerate(test_cases, 1):
success, result, link_type = parse_tapd_link(url)
# 检查结果
if success == expected_success and link_type == expected_type:
if success and result == expected_id:
print(f" [{i}] PASS: {url[:50]}...")
passed += 1
elif not success:
print(f" [{i}] PASS: 正确识别无效链接")
passed += 1
else:
print(f" [{i}] FAIL: 单号不匹配 (期望={expected_id}, 实际={result})")
failed += 1
else:
print(f" [{i}] FAIL: {url[:50]}...")
print(f" 期望: success={expected_success}, type={expected_type}")
print(f" 实际: success={success}, type={link_type}")
failed += 1
print(f"\n链接解析测试结果: {passed} 通过, {failed} 失败")
return failed == 0
def test_status_mapping():
"""测试状态映射功能"""
print("\n" + "=" * 60)
print("测试2: 状态映射")
print("=" * 60)
test_cases = [
("status_5", "进行中"),
("status_7", "未开始"),
("status_8", "已完成"),
("status_9", "待验收"),
("status_10", "联调"),
("status_12", "取消"),
("status_13", "待评审"),
("status_99", "status_99"), # 未知状态返回原值
("", "未知"),
(None, "未知"),
]
passed = 0
failed = 0
for status_code, expected in test_cases:
result = map_status(status_code)
if result == expected:
print(f" PASS: {status_code} -> {result}")
passed += 1
else:
print(f" FAIL: {status_code} -> {result} (期望: {expected})")
failed += 1
print(f"\n状态映射测试结果: {passed} 通过, {failed} 失败")
return failed == 0
def test_tapd_api(story_id: str = None):
"""测试TAPD API功能"""
print("\n" + "=" * 60)
print("测试3: TAPD API")
print("=" * 60)
# 从配置读取workspace_id
from src2.config import Task2ConfigManager
config = Task2ConfigManager()
tapd_config = config.get_tapd_config()
workspace_id = tapd_config['workspace_id']
print(f" workspace_id: {workspace_id}")
try:
# 初始化API
api = TAPDStoryApi(workspace_id, test_mode=True)
print(" ✓ API初始化成功")
except ValueError as e:
print(f" ✗ API初始化失败: {e}")
return False
if not story_id:
print("\n 跳过需求查询测试未提供story_id")
print(" 用法: python test_phase2.py <story_id>")
return True
# 测试获取需求详情
print(f"\n 测试获取需求: {story_id}")
try:
story = api.get_story(story_id)
print(f" ✓ 获取成功")
print(f" - ID: {story.get('id')}")
print(f" - 名称: {story.get('name')}")
print(f" - 状态: {story.get('status')}")
print(f" - 处理人: {story.get('owner')}")
print(f" - 预计开始: {story.get('begin')}")
print(f" - 预计结束: {story.get('due')}")
return True
except RuntimeError as e:
print(f" ✗ 获取失败: {e}")
return False
def main():
"""主函数"""
print("=" * 60)
print("任务二第二阶段验证")
print("=" * 60)
# 获取命令行参数
story_id = None
if len(sys.argv) > 1:
story_id = sys.argv[1]
results = []
# 测试1: 链接解析
results.append(("链接解析", test_link_parser()))
# 测试2: 状态映射
results.append(("状态映射", test_status_mapping()))
# 测试3: TAPD API
results.append(("TAPD API", test_tapd_api(story_id)))
# 汇总结果
print("\n" + "=" * 60)
print("验收结果汇总")
print("=" * 60)
all_passed = True
for name, passed in results:
status = "✓ PASS" if passed else "✗ FAIL"
print(f" {status}: {name}")
if not passed:
all_passed = False
if all_passed:
print("\n所有测试通过!第二阶段验收完成。")
else:
print("\n部分测试失败,请检查。")
return 0 if all_passed else 1
if __name__ == "__main__":
sys.exit(main())