test_neo4j_connection.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 测试Neo4j连接脚本
  5. 用于验证修改后的Neo4jDriver类是否可以在没有Flask的情况下工作
  6. """
  7. import sys
  8. import os
  9. # 添加项目根目录到Python路径
  10. current_dir = os.path.dirname(os.path.abspath(__file__))
  11. project_root = current_dir
  12. sys.path.insert(0, project_root)
  13. try:
  14. from app.services.neo4j_driver import Neo4jDriver
  15. print("✅ 成功导入Neo4jDriver")
  16. # 测试Neo4j连接
  17. print("正在测试Neo4j连接...")
  18. neo4j_driver = Neo4jDriver(
  19. uri="bolt://192.168.3.143:7687",
  20. user="neo4j",
  21. password="cituneo4j",
  22. encrypted=False
  23. )
  24. if neo4j_driver.verify_connectivity():
  25. print("✅ Neo4j连接成功!")
  26. # 测试会话创建
  27. try:
  28. with neo4j_driver.get_session() as session:
  29. result = session.run("RETURN 1 as test")
  30. record = result.single()
  31. print(f"✅ 测试查询成功: {record['test']}")
  32. except Exception as e:
  33. print(f"❌ 测试查询失败: {e}")
  34. else:
  35. print("❌ Neo4j连接失败")
  36. except ImportError as e:
  37. print(f"❌ 导入失败: {e}")
  38. except Exception as e:
  39. print(f"❌ 其他错误: {e}")
  40. print("测试完成")