第一次提交
This commit is contained in:
115
backend/tests/test_workflow_validator.py
Normal file
115
backend/tests/test_workflow_validator.py
Normal file
@@ -0,0 +1,115 @@
|
||||
"""
|
||||
工作流验证器测试
|
||||
"""
|
||||
import pytest
|
||||
from app.services.workflow_validator import WorkflowValidator
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
@pytest.mark.workflow
|
||||
class TestWorkflowValidator:
|
||||
"""工作流验证器测试"""
|
||||
|
||||
def test_validate_valid_workflow(self):
|
||||
"""测试验证有效工作流"""
|
||||
nodes = [
|
||||
{"id": "start-1", "type": "start"},
|
||||
{"id": "llm-1", "type": "llm"},
|
||||
{"id": "end-1", "type": "end"}
|
||||
]
|
||||
edges = [
|
||||
{"id": "e1", "source": "start-1", "target": "llm-1"},
|
||||
{"id": "e2", "source": "llm-1", "target": "end-1"}
|
||||
]
|
||||
|
||||
validator = WorkflowValidator(nodes, edges)
|
||||
is_valid, errors, warnings = validator.validate()
|
||||
|
||||
assert is_valid is True
|
||||
assert len(errors) == 0
|
||||
|
||||
def test_validate_no_start_node(self):
|
||||
"""测试验证缺少开始节点"""
|
||||
nodes = [
|
||||
{"id": "llm-1", "type": "llm"},
|
||||
{"id": "end-1", "type": "end"}
|
||||
]
|
||||
edges = [
|
||||
{"id": "e1", "source": "llm-1", "target": "end-1"}
|
||||
]
|
||||
|
||||
validator = WorkflowValidator(nodes, edges)
|
||||
is_valid, errors, warnings = validator.validate()
|
||||
|
||||
assert is_valid is False
|
||||
assert any("开始节点" in error for error in errors)
|
||||
|
||||
def test_validate_duplicate_node_id(self):
|
||||
"""测试验证重复节点ID"""
|
||||
nodes = [
|
||||
{"id": "start-1", "type": "start"},
|
||||
{"id": "start-1", "type": "llm"}, # 重复ID
|
||||
{"id": "end-1", "type": "end"}
|
||||
]
|
||||
edges = []
|
||||
|
||||
validator = WorkflowValidator(nodes, edges)
|
||||
is_valid, errors, warnings = validator.validate()
|
||||
|
||||
assert is_valid is False
|
||||
assert any("重复" in error for error in errors)
|
||||
|
||||
def test_validate_cycle_detection(self):
|
||||
"""测试循环检测"""
|
||||
nodes = [
|
||||
{"id": "start-1", "type": "start"},
|
||||
{"id": "node-1", "type": "llm"},
|
||||
{"id": "node-2", "type": "llm"}
|
||||
]
|
||||
edges = [
|
||||
{"id": "e1", "source": "start-1", "target": "node-1"},
|
||||
{"id": "e2", "source": "node-1", "target": "node-2"},
|
||||
{"id": "e3", "source": "node-2", "target": "node-1"} # 形成循环
|
||||
]
|
||||
|
||||
validator = WorkflowValidator(nodes, edges)
|
||||
is_valid, errors, warnings = validator.validate()
|
||||
|
||||
assert is_valid is False
|
||||
assert any("循环" in error for error in errors)
|
||||
|
||||
def test_validate_invalid_edge(self):
|
||||
"""测试验证无效边"""
|
||||
nodes = [
|
||||
{"id": "start-1", "type": "start"},
|
||||
{"id": "end-1", "type": "end"}
|
||||
]
|
||||
edges = [
|
||||
{"id": "e1", "source": "nonexistent", "target": "end-1"} # 源节点不存在
|
||||
]
|
||||
|
||||
validator = WorkflowValidator(nodes, edges)
|
||||
is_valid, errors, warnings = validator.validate()
|
||||
|
||||
assert is_valid is False
|
||||
assert any("不存在" in error for error in errors)
|
||||
|
||||
def test_validate_condition_branches(self):
|
||||
"""测试验证条件节点分支"""
|
||||
nodes = [
|
||||
{"id": "start-1", "type": "start"},
|
||||
{"id": "condition-1", "type": "condition"},
|
||||
{"id": "end-1", "type": "end"}
|
||||
]
|
||||
edges = [
|
||||
{"id": "e1", "source": "start-1", "target": "condition-1"},
|
||||
{"id": "e2", "source": "condition-1", "target": "end-1", "sourceHandle": "true"}
|
||||
# 缺少false分支
|
||||
]
|
||||
|
||||
validator = WorkflowValidator(nodes, edges)
|
||||
is_valid, errors, warnings = validator.validate()
|
||||
|
||||
# 缺少分支是警告,不是错误
|
||||
assert is_valid is True
|
||||
assert any("False分支" in warning for warning in warnings)
|
||||
Reference in New Issue
Block a user