- 16 个业务模块,856 个测试用例,40 个 E2E 测试 - 单一 React 19 前端 + PlatformSwitcher 二合一切换 - Flask 后端支持 scenario/dcloud_module/dcloud_e2e/dcloud_all 四种任务类型 - E2E 测试面板:API 全链路(8条) + UI 点击(3条) + 分步测试(3个) - SSE 实时日志流 + 飞书告警 + 定时调度 - 完整还原原 ToB 平台 TestPanel E2E 模块
259 lines
6.8 KiB
Python
259 lines
6.8 KiB
Python
"""
|
||
测试数据工厂
|
||
|
||
提供各种业务对象的创建工厂,生成符合业务规则的测试数据。
|
||
|
||
特性:
|
||
- 动态命名(时间戳/UUID)避免冲突
|
||
- 可配置的默认值
|
||
- 支持自定义覆盖
|
||
"""
|
||
import time
|
||
import uuid
|
||
from typing import Dict, Any, Optional
|
||
from datetime import datetime
|
||
|
||
|
||
class TestDataGenerator:
|
||
"""测试数据生成器基类
|
||
|
||
提供通用的数据生成工具方法。
|
||
"""
|
||
|
||
@staticmethod
|
||
def generate_name(prefix: str = "test") -> str:
|
||
"""生成带时间戳的唯一名称
|
||
|
||
Args:
|
||
prefix: 名称前缀
|
||
|
||
Returns:
|
||
str: 唯一名称,格式为 {prefix}_{timestamp}
|
||
"""
|
||
return f"{prefix}_{int(time.time() * 1000)}"
|
||
|
||
@staticmethod
|
||
def generate_uuid() -> str:
|
||
"""生成 UUID
|
||
|
||
Returns:
|
||
str: UUID 字符串
|
||
"""
|
||
return str(uuid.uuid4())
|
||
|
||
@staticmethod
|
||
def generate_timestamp() -> int:
|
||
"""生成时间戳(毫秒)
|
||
|
||
Returns:
|
||
int: 当前时间戳(毫秒)
|
||
"""
|
||
return int(time.time() * 1000)
|
||
|
||
@staticmethod
|
||
def generate_datetime(fmt: str = "%Y-%m-%d %H:%M:%S") -> str:
|
||
"""生成格式化的日期时间字符串
|
||
|
||
Args:
|
||
fmt: 日期时间格式
|
||
|
||
Returns:
|
||
str: 格式化的日期时间字符串
|
||
"""
|
||
return datetime.now().strftime(fmt)
|
||
|
||
|
||
class DatasetFactory(TestDataGenerator):
|
||
"""数据集工厂
|
||
|
||
用于创建测试用的数据集数据。
|
||
"""
|
||
|
||
@classmethod
|
||
def create(
|
||
cls,
|
||
name: Optional[str] = None,
|
||
description: Optional[str] = None,
|
||
category: str = "object_detection",
|
||
**kwargs
|
||
) -> Dict[str, Any]:
|
||
"""创建数据集数据
|
||
|
||
Args:
|
||
name: 数据集名称,默认自动生成
|
||
description: 描述
|
||
category: 类型(object_detection/image_classification等)
|
||
**kwargs: 其他字段
|
||
|
||
Returns:
|
||
Dict[str, Any]: 数据集数据字典
|
||
"""
|
||
return {
|
||
"name": name or cls.generate_name("dataset"),
|
||
"description": description or f"测试数据集 {cls.generate_datetime()}",
|
||
"category": category,
|
||
"type": kwargs.get("type", "training"),
|
||
"labels": kwargs.get("labels", []),
|
||
"tags": kwargs.get("tags", ["自动化测试"]),
|
||
**kwargs
|
||
}
|
||
|
||
@classmethod
|
||
def create_for_api(cls, **kwargs) -> Dict[str, Any]:
|
||
"""创建 API 请求用的数据集数据
|
||
|
||
Returns:
|
||
Dict[str, Any]: API 请求数据
|
||
"""
|
||
data = cls.create(**kwargs)
|
||
# API 特定字段处理
|
||
if "labels" not in kwargs:
|
||
data["labels"] = ["label1", "label2"]
|
||
return data
|
||
|
||
|
||
class DataSpaceFactory(TestDataGenerator):
|
||
"""数据空间工厂
|
||
|
||
用于创建测试用的数据空间数据。
|
||
"""
|
||
|
||
@classmethod
|
||
def create(
|
||
cls,
|
||
name: Optional[str] = None,
|
||
description: Optional[str] = None,
|
||
space_type: str = "team",
|
||
**kwargs
|
||
) -> Dict[str, Any]:
|
||
"""创建数据空间数据
|
||
|
||
Args:
|
||
name: 空间名称,默认自动生成
|
||
description: 描述
|
||
space_type: 空间类型(team/personal)
|
||
**kwargs: 其他字段
|
||
|
||
Returns:
|
||
Dict[str, Any]: 数据空间数据字典
|
||
"""
|
||
return {
|
||
"name": name or cls.generate_name("space"),
|
||
"description": description or f"测试数据空间 {cls.generate_datetime()}",
|
||
"type": space_type,
|
||
"isPublic": kwargs.get("isPublic", False),
|
||
"members": kwargs.get("members", []),
|
||
**kwargs
|
||
}
|
||
|
||
|
||
class TaskFactory(TestDataGenerator):
|
||
"""任务工厂
|
||
|
||
用于创建测试用的任务数据。
|
||
"""
|
||
|
||
@classmethod
|
||
def create(
|
||
cls,
|
||
name: Optional[str] = None,
|
||
task_type: str = "annotation",
|
||
dataset_id: Optional[str] = None,
|
||
**kwargs
|
||
) -> Dict[str, Any]:
|
||
"""创建任务数据
|
||
|
||
Args:
|
||
name: 任务名称,默认自动生成
|
||
task_type: 任务类型(annotation/training/inference等)
|
||
dataset_id: 关联的数据集ID
|
||
**kwargs: 其他字段
|
||
|
||
Returns:
|
||
Dict[str, Any]: 任务数据字典
|
||
"""
|
||
return {
|
||
"name": name or cls.generate_name("task"),
|
||
"type": task_type,
|
||
"datasetId": dataset_id,
|
||
"priority": kwargs.get("priority", "normal"),
|
||
"assignee": kwargs.get("assignee"),
|
||
"dueDate": kwargs.get("dueDate"),
|
||
"labels": kwargs.get("labels", []),
|
||
"config": kwargs.get("config", {}),
|
||
**kwargs
|
||
}
|
||
|
||
@classmethod
|
||
def create_annotation_task(cls, dataset_id: str, **kwargs) -> Dict[str, Any]:
|
||
"""创建标注任务
|
||
|
||
Args:
|
||
dataset_id: 数据集ID
|
||
**kwargs: 其他字段
|
||
|
||
Returns:
|
||
Dict[str, Any]: 标注任务数据
|
||
"""
|
||
return cls.create(
|
||
task_type="annotation",
|
||
dataset_id=dataset_id,
|
||
**kwargs
|
||
)
|
||
|
||
@classmethod
|
||
def create_training_task(cls, **kwargs) -> Dict[str, Any]:
|
||
"""创建训练任务
|
||
|
||
Args:
|
||
**kwargs: 其他字段
|
||
|
||
Returns:
|
||
Dict[str, Any]: 训练任务数据
|
||
"""
|
||
return cls.create(
|
||
task_type="training",
|
||
**kwargs
|
||
)
|
||
|
||
|
||
class CloudDesktopFactory(TestDataGenerator):
|
||
"""云桌面工厂
|
||
|
||
用于创建测试用的云桌面数据。
|
||
"""
|
||
|
||
@classmethod
|
||
def create(
|
||
cls,
|
||
name: Optional[str] = None,
|
||
image_id: Optional[str] = None,
|
||
cpu: int = 4,
|
||
memory: int = 16,
|
||
gpu: int = 1,
|
||
**kwargs
|
||
) -> Dict[str, Any]:
|
||
"""创建云桌面数据
|
||
|
||
Args:
|
||
name: 桌面名称,默认自动生成
|
||
image_id: 镜像ID
|
||
cpu: CPU 核数
|
||
memory: 内存大小(GB)
|
||
gpu: GPU 数量
|
||
**kwargs: 其他字段
|
||
|
||
Returns:
|
||
Dict[str, Any]: 云桌面数据字典
|
||
"""
|
||
return {
|
||
"name": name or cls.generate_name("desktop"),
|
||
"imageId": image_id,
|
||
"cpu": cpu,
|
||
"memory": memory,
|
||
"gpu": gpu,
|
||
"diskSize": kwargs.get("diskSize", 100),
|
||
"networkType": kwargs.get("networkType", "private"),
|
||
**kwargs
|
||
}
|