242 lines
11 KiB
Python
242 lines
11 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
测试orchestrator修复的脚本
|
||
"""
|
||
|
||
import sys
|
||
import os
|
||
import logging
|
||
|
||
# 添加项目根目录到Python路径
|
||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||
|
||
def test_orchestrator_fix():
|
||
"""测试orchestrator修复"""
|
||
|
||
# 设置日志
|
||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||
logger = logging.getLogger(__name__)
|
||
|
||
logger.info("开始测试orchestrator修复...")
|
||
|
||
try:
|
||
# 导入必要的模块
|
||
from ddms_compliance_suite.test_orchestrator import APITestOrchestrator
|
||
from ddms_compliance_suite.input_parser.parser import ParsedDMSSpec, DMSEndpoint
|
||
from custom_stages.dms_crud_scenario_stage import DmsCrudScenarioStage
|
||
|
||
# 创建一个简单的API规范
|
||
simple_api_spec = {
|
||
"dms_api_list": [
|
||
{
|
||
"method": "POST",
|
||
"path": "/api/dms/well_kd_wellbore_ideas01/v1/dr_ach_survey_inc",
|
||
"title": "Create dr_ach_survey_inc",
|
||
"summary": "Create a new dr_ach_survey_inc record",
|
||
"description": "Create a new dr_ach_survey_inc record",
|
||
"operationId": "create_dr_ach_survey_inc",
|
||
"tags": ["dr_ach_survey_inc"],
|
||
"parameters": [],
|
||
"requestBody": {
|
||
"required": True,
|
||
"content": {
|
||
"application/json": {
|
||
"schema": {
|
||
"type": "object",
|
||
"properties": {
|
||
"version": {"type": "string", "example": "1.0.0"},
|
||
"act": {"type": "integer", "example": -1},
|
||
"data": {
|
||
"type": "array",
|
||
"items": {
|
||
"type": "object",
|
||
"properties": {
|
||
"dsid": {"type": "string", "title": "数据ID"},
|
||
"wellId": {"type": "string", "title": "井标识符"},
|
||
"wellboreId": {"type": "string", "title": "井筒唯一标识符"},
|
||
"wellCommonName": {"type": "string", "title": "井名"},
|
||
"dataRegion": {"type": "string", "title": "数据标识"},
|
||
"description": {"type": "string", "title": "测斜描述"},
|
||
"bsflag": {"type": "number", "title": "逻辑删除标识"}
|
||
},
|
||
"required": ["dsid", "dataRegion", "bsflag"]
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"responses": {
|
||
"200": {
|
||
"description": "Success",
|
||
"content": {
|
||
"application/json": {
|
||
"schema": {
|
||
"type": "object",
|
||
"properties": {
|
||
"code": {"type": "integer"},
|
||
"message": {"type": "string"},
|
||
"data": {"type": "object"}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"_test_mode": "scenario_only",
|
||
"_dms_model_pk_name": "dsid"
|
||
}
|
||
]
|
||
}
|
||
|
||
# 创建DMSEndpoint
|
||
endpoint = DMSEndpoint(
|
||
method="POST",
|
||
path="/api/dms/well_kd_wellbore_ideas01/v1/dr_ach_survey_inc",
|
||
title="Create dr_ach_survey_inc",
|
||
request_body=simple_api_spec["dms_api_list"][0]["requestBody"],
|
||
responses=simple_api_spec["dms_api_list"][0]["responses"],
|
||
parameters=[],
|
||
test_mode="scenario_only",
|
||
model_pk_name="dsid"
|
||
)
|
||
|
||
# 创建ParsedDMSSpec
|
||
parsed_spec = ParsedDMSSpec(endpoints=[endpoint], spec=simple_api_spec)
|
||
|
||
# 创建测试编排器
|
||
orchestrator = APITestOrchestrator(
|
||
base_url="https://www.dev.ideas.cnpc",
|
||
enable_well_data=True
|
||
)
|
||
|
||
logger.info(f"测试编排器创建成功")
|
||
logger.info(f"orchestrator.well_data_manager: {getattr(orchestrator, 'well_data_manager', 'NOT_FOUND')}")
|
||
logger.info(f"type(orchestrator.well_data_manager): {type(getattr(orchestrator, 'well_data_manager', None))}")
|
||
|
||
# 初始化井数据
|
||
if orchestrator.well_data_manager:
|
||
logger.info("开始初始化井数据...")
|
||
well_data_success = orchestrator.initialize_well_data()
|
||
if well_data_success:
|
||
logger.info("井数据初始化成功")
|
||
else:
|
||
logger.warning("井数据初始化失败")
|
||
else:
|
||
logger.warning("井数据管理器不可用")
|
||
|
||
# 手动创建CRUD场景并传递orchestrator
|
||
crud_stage = DmsCrudScenarioStage(
|
||
api_group_metadata={"name": "test_group", "description": "测试分组"},
|
||
apis_in_group=[endpoint],
|
||
global_api_spec=parsed_spec,
|
||
llm_service=orchestrator.llm_service,
|
||
stage_llm_config=orchestrator.stage_llm_config,
|
||
orchestrator=orchestrator # 🔑 关键:传递orchestrator
|
||
)
|
||
|
||
logger.info(f"CRUD场景创建成功")
|
||
logger.info(f"crud_stage.orchestrator: {crud_stage.orchestrator}")
|
||
logger.info(f"hasattr(crud_stage, 'orchestrator'): {hasattr(crud_stage, 'orchestrator')}")
|
||
|
||
if hasattr(crud_stage, 'orchestrator'):
|
||
logger.info(f"hasattr(crud_stage.orchestrator, 'well_data_manager'): {hasattr(crud_stage.orchestrator, 'well_data_manager')}")
|
||
if hasattr(crud_stage.orchestrator, 'well_data_manager'):
|
||
logger.info(f"crud_stage.orchestrator.well_data_manager: {crud_stage.orchestrator.well_data_manager}")
|
||
logger.info(f"type(crud_stage.orchestrator.well_data_manager): {type(crud_stage.orchestrator.well_data_manager)}")
|
||
logger.info(f"bool(crud_stage.orchestrator.well_data_manager): {bool(crud_stage.orchestrator.well_data_manager)}")
|
||
|
||
# 测试条件判断
|
||
condition_result = (hasattr(crud_stage, 'orchestrator') and
|
||
hasattr(crud_stage.orchestrator, 'well_data_manager') and
|
||
crud_stage.orchestrator.well_data_manager)
|
||
|
||
logger.info(f"完整条件判断结果: {condition_result}")
|
||
|
||
if condition_result:
|
||
logger.info("✅ 条件判断通过,井数据管理器可用")
|
||
|
||
# 测试井数据增强
|
||
test_data = {
|
||
'dsid': 'test-123',
|
||
'wellId': 'WELL001',
|
||
'wellboreId': 'WB001',
|
||
'wellCommonName': '大庆1井',
|
||
'dataRegion': '大庆油田',
|
||
'description': '测试数据',
|
||
'bsflag': 1
|
||
}
|
||
|
||
logger.info(f"原始测试数据: {test_data}")
|
||
enhanced_data = crud_stage.orchestrator.well_data_manager.enhance_data_with_well_values(test_data)
|
||
logger.info(f"增强后数据: {enhanced_data}")
|
||
|
||
# 验证增强效果
|
||
expected_changes = {
|
||
'wellId': ['HB00019975', 'WELLHB100293168', 'WELLHB100295876', 'WELLHB100294095', 'WELLHB100293950'],
|
||
'wellboreId': ['WEBHHB100083169', 'WEBHHB100085743', 'WEBHHB100083191', 'WEBHHB100082983', 'WEBHHB100083194'],
|
||
'wellCommonName': ['郑4-106', '沁362', '新蔡39-169', '郑4-2', '郑4-3']
|
||
}
|
||
|
||
all_correct = True
|
||
for field, expected_values in expected_changes.items():
|
||
actual_value = enhanced_data.get(field)
|
||
if actual_value in expected_values:
|
||
logger.info(f"✅ {field} 正确增强为真实值: {actual_value}")
|
||
else:
|
||
logger.error(f"❌ {field} 增强失败,期望值之一: {expected_values}, 实际: {actual_value}")
|
||
all_correct = False
|
||
|
||
if all_correct:
|
||
logger.info("🎉 井数据增强功能正常工作")
|
||
return True
|
||
else:
|
||
logger.error("❌ 井数据增强功能有问题")
|
||
return False
|
||
|
||
else:
|
||
logger.warning("❌ 条件判断失败,井数据管理器不可用")
|
||
|
||
# 检查井数据管理器初始化
|
||
logger.info("尝试手动初始化井数据管理器...")
|
||
try:
|
||
orchestrator._initialize_well_data_manager()
|
||
logger.info(f"手动初始化后 - orchestrator.well_data_manager: {orchestrator.well_data_manager}")
|
||
logger.info(f"手动初始化后 - type: {type(orchestrator.well_data_manager)}")
|
||
logger.info(f"手动初始化后 - bool: {bool(orchestrator.well_data_manager)}")
|
||
|
||
# 重新测试条件判断
|
||
condition_result_after_init = (hasattr(crud_stage, 'orchestrator') and
|
||
hasattr(crud_stage.orchestrator, 'well_data_manager') and
|
||
crud_stage.orchestrator.well_data_manager)
|
||
|
||
logger.info(f"初始化后的条件判断结果: {condition_result_after_init}")
|
||
|
||
if condition_result_after_init:
|
||
logger.info("✅ 手动初始化后条件判断通过")
|
||
return True
|
||
else:
|
||
logger.error("❌ 手动初始化后条件判断仍然失败")
|
||
return False
|
||
|
||
except Exception as e:
|
||
logger.error(f"手动初始化井数据管理器失败: {e}")
|
||
return False
|
||
|
||
except Exception as e:
|
||
logger.error(f"测试失败: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
return False
|
||
|
||
if __name__ == "__main__":
|
||
try:
|
||
success = test_orchestrator_fix()
|
||
sys.exit(0 if success else 1)
|
||
except Exception as e:
|
||
print(f"❌ 测试失败: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
sys.exit(1)
|