test_parse_neo4j_process.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 测试 parse_neo4j_process.py 程序
  5. 该脚本用于测试酒店职位数据Neo4j同步程序的功能
  6. """
  7. import os
  8. import sys
  9. import subprocess
  10. import time
  11. def test_parse_neo4j_process():
  12. """测试parse_neo4j_process.py程序"""
  13. # 获取脚本路径
  14. script_path = os.path.join('app', 'core', 'data_parse', 'parse_neo4j_process.py')
  15. if not os.path.exists(script_path):
  16. print(f"错误: 找不到脚本文件 {script_path}")
  17. return False
  18. print(f"开始测试脚本: {script_path}")
  19. print("=" * 50)
  20. try:
  21. # 运行脚本
  22. start_time = time.time()
  23. result = subprocess.run(
  24. [sys.executable, script_path],
  25. capture_output=True,
  26. text=True,
  27. encoding='utf-8',
  28. cwd=os.getcwd()
  29. )
  30. end_time = time.time()
  31. # 输出结果
  32. print(f"脚本执行时间: {end_time - start_time:.2f} 秒")
  33. print(f"返回码: {result.returncode}")
  34. print("\n标准输出:")
  35. print(result.stdout)
  36. if result.stderr:
  37. print("\n标准错误:")
  38. print(result.stderr)
  39. # 判断执行结果
  40. if result.returncode == 0:
  41. print("\n✅ 脚本执行成功!")
  42. return True
  43. else:
  44. print(f"\n❌ 脚本执行失败,返回码: {result.returncode}")
  45. return False
  46. except Exception as e:
  47. print(f"执行脚本时发生错误: {e}")
  48. return False
  49. def test_import_modules():
  50. """测试模块导入"""
  51. print("测试模块导入...")
  52. try:
  53. # 测试配置模块
  54. from app.config.config import config, current_env
  55. print("✅ 配置模块导入成功")
  56. # 测试Neo4j驱动模块
  57. from app.services.neo4j_driver import Neo4jDriver
  58. print("✅ Neo4j驱动模块导入成功")
  59. # 测试SQLAlchemy模块
  60. from sqlalchemy import create_engine, text
  61. print("✅ SQLAlchemy模块导入成功")
  62. return True
  63. except ImportError as e:
  64. print(f"❌ 模块导入失败: {e}")
  65. return False
  66. def main():
  67. """主函数"""
  68. print("酒店职位数据Neo4j同步程序测试")
  69. print("=" * 50)
  70. # 测试模块导入
  71. if not test_import_modules():
  72. print("\n模块导入测试失败,跳过脚本执行测试")
  73. return
  74. print("\n" + "=" * 50)
  75. # 测试脚本执行
  76. if test_parse_neo4j_process():
  77. print("\n🎉 所有测试通过!")
  78. else:
  79. print("\n💥 测试失败!")
  80. if __name__ == "__main__":
  81. main()