test_enhanced_parse_neo4j.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 测试增强后的 parse_neo4j_process.py 程序
  5. 该脚本用于测试酒店职位数据Neo4j同步程序的增强功能,包括:
  6. - 职位名称和级别名称的同步
  7. - 节点关系的创建
  8. """
  9. import os
  10. import sys
  11. import subprocess
  12. import time
  13. def test_enhanced_parse_neo4j_process():
  14. """测试增强后的parse_neo4j_process.py程序"""
  15. # 获取脚本路径
  16. script_path = os.path.join('app', 'core', 'data_parse', 'parse_neo4j_process.py')
  17. if not os.path.exists(script_path):
  18. print(f"错误: 找不到脚本文件 {script_path}")
  19. return False
  20. print(f"开始测试增强版脚本: {script_path}")
  21. print("=" * 60)
  22. try:
  23. # 运行脚本
  24. start_time = time.time()
  25. result = subprocess.run(
  26. [sys.executable, script_path],
  27. capture_output=True,
  28. text=True,
  29. encoding='utf-8',
  30. cwd=os.getcwd()
  31. )
  32. end_time = time.time()
  33. # 输出结果
  34. print(f"脚本执行时间: {end_time - start_time:.2f} 秒")
  35. print(f"返回码: {result.returncode}")
  36. print("\n标准输出:")
  37. print(result.stdout)
  38. if result.stderr:
  39. print("\n标准错误:")
  40. print(result.stderr)
  41. # 判断执行结果
  42. if result.returncode == 0:
  43. print("\n✅ 增强版脚本执行成功!")
  44. # 检查输出中是否包含新的功能信息
  45. if "职位节点" in result.stdout and "级别节点" in result.stdout:
  46. print("✅ 检测到职位和级别节点创建功能")
  47. else:
  48. print("⚠️ 未检测到职位和级别节点创建功能")
  49. if "BELONGS_TO" in result.stdout and "HAS_LEVEL" in result.stdout:
  50. print("✅ 检测到节点关系创建功能")
  51. else:
  52. print("⚠️ 未检测到节点关系创建功能")
  53. else:
  54. print(f"\n❌ 增强版脚本执行失败,返回码: {result.returncode}")
  55. return False
  56. return True
  57. except Exception as e:
  58. print(f"执行增强版脚本时发生错误: {e}")
  59. return False
  60. def test_import_enhanced_modules():
  61. """测试增强后的模块导入"""
  62. print("测试增强后的模块导入...")
  63. try:
  64. # 测试配置模块
  65. from app.config.config import config, current_env
  66. print("✅ 配置模块导入成功")
  67. # 测试Neo4j驱动模块
  68. from app.services.neo4j_driver import Neo4jDriver
  69. print("✅ Neo4j驱动模块导入成功")
  70. # 测试SQLAlchemy模块
  71. from sqlalchemy import create_engine, text
  72. print("✅ SQLAlchemy模块导入成功")
  73. # 测试增强后的处理器类
  74. from app.core.data_parse.parse_neo4j_process import HotelPositionNeo4jProcessor
  75. print("✅ 增强后的处理器类导入成功")
  76. return True
  77. except ImportError as e:
  78. print(f"❌ 模块导入失败: {e}")
  79. return False
  80. def test_enhanced_functionality():
  81. """测试增强功能"""
  82. print("\n测试增强功能...")
  83. try:
  84. from app.core.data_parse.parse_neo4j_process import HotelPositionNeo4jProcessor
  85. # 创建处理器实例
  86. processor = HotelPositionNeo4jProcessor()
  87. print("✅ 成功创建处理器实例")
  88. # 检查方法是否存在
  89. if hasattr(processor, 'create_relationship'):
  90. print("✅ 检测到关系创建方法")
  91. else:
  92. print("❌ 未检测到关系创建方法")
  93. if hasattr(processor, 'process_hotel_data'):
  94. print("✅ 检测到酒店数据处理方法")
  95. else:
  96. print("❌ 未检测到酒店数据处理方法")
  97. return True
  98. except Exception as e:
  99. print(f"❌ 测试增强功能时发生错误: {e}")
  100. return False
  101. def main():
  102. """主函数"""
  103. print("酒店职位数据Neo4j同步程序增强功能测试")
  104. print("=" * 60)
  105. # 测试模块导入
  106. if not test_import_enhanced_modules():
  107. print("\n模块导入测试失败,跳过其他测试")
  108. return
  109. print("\n" + "=" * 60)
  110. # 测试增强功能
  111. if not test_enhanced_functionality():
  112. print("\n增强功能测试失败,跳过脚本执行测试")
  113. return
  114. print("\n" + "=" * 60)
  115. # 测试脚本执行
  116. if test_enhanced_parse_neo4j_process():
  117. print("\n🎉 所有增强功能测试通过!")
  118. else:
  119. print("\n💥 增强功能测试失败!")
  120. if __name__ == "__main__":
  121. main()