test_hotel_group_brands.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 测试酒店集团品牌数据处理功能
  5. 该脚本用于测试parse_neo4j_process.py程序中新增的酒店集团品牌数据同步功能
  6. """
  7. import os
  8. import sys
  9. import subprocess
  10. import time
  11. def test_hotel_group_brands_functionality():
  12. """测试酒店集团品牌数据处理功能"""
  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("=" * 70)
  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. # 检查输出中是否包含酒店集团品牌相关功能信息
  43. if "酒店集团品牌数据" in result.stdout:
  44. print("✅ 检测到酒店集团品牌数据处理功能")
  45. else:
  46. print("⚠️ 未检测到酒店集团品牌数据处理功能")
  47. if "集团节点" in result.stdout and "品牌节点" in result.stdout:
  48. print("✅ 检测到集团和品牌节点创建功能")
  49. else:
  50. print("⚠️ 未检测到集团和品牌节点创建功能")
  51. if "品牌级别节点" in result.stdout:
  52. print("✅ 检测到品牌级别节点创建功能")
  53. else:
  54. print("⚠️ 未检测到品牌级别节点创建功能")
  55. if "BELONGS_TO" in result.stdout and "HAS_LEVEL" in result.stdout:
  56. print("✅ 检测到节点关系创建功能")
  57. else:
  58. print("⚠️ 未检测到节点关系创建功能")
  59. else:
  60. print(f"\n❌ 脚本执行失败,返回码: {result.returncode}")
  61. return False
  62. return True
  63. except Exception as e:
  64. print(f"执行脚本时发生错误: {e}")
  65. return False
  66. def test_import_enhanced_modules():
  67. """测试增强后的模块导入"""
  68. print("测试增强后的模块导入...")
  69. try:
  70. # 测试配置模块
  71. from app.config.config import config, current_env
  72. print("✅ 配置模块导入成功")
  73. # 测试Neo4j驱动模块
  74. from app.services.neo4j_driver import Neo4jDriver
  75. print("✅ Neo4j驱动模块导入成功")
  76. # 测试SQLAlchemy模块
  77. from sqlalchemy import create_engine, text
  78. print("✅ SQLAlchemy模块导入成功")
  79. # 测试增强后的处理器类
  80. from app.core.data_parse.parse_neo4j_process import HotelPositionNeo4jProcessor
  81. print("✅ 增强后的处理器类导入成功")
  82. return True
  83. except ImportError as e:
  84. print(f"❌ 模块导入失败: {e}")
  85. return False
  86. def test_hotel_group_brands_methods():
  87. """测试酒店集团品牌相关方法"""
  88. print("\n测试酒店集团品牌相关方法...")
  89. try:
  90. from app.core.data_parse.parse_neo4j_process import HotelPositionNeo4jProcessor
  91. # 创建处理器实例
  92. processor = HotelPositionNeo4jProcessor()
  93. print("✅ 成功创建处理器实例")
  94. # 检查酒店集团品牌相关方法是否存在
  95. if hasattr(processor, 'get_hotel_group_brands'):
  96. print("✅ 检测到获取酒店集团品牌数据方法")
  97. else:
  98. print("❌ 未检测到获取酒店集团品牌数据方法")
  99. if hasattr(processor, 'process_hotel_group_brands'):
  100. print("✅ 检测到处理酒店集团品牌数据方法")
  101. else:
  102. print("❌ 未检测到处理酒店集团品牌数据方法")
  103. if hasattr(processor, 'create_relationship'):
  104. print("✅ 检测到关系创建方法")
  105. else:
  106. print("❌ 未检测到关系创建方法")
  107. return True
  108. except Exception as e:
  109. print(f"❌ 测试酒店集团品牌方法时发生错误: {e}")
  110. return False
  111. def test_data_structure():
  112. """测试数据结构"""
  113. print("\n测试数据结构...")
  114. try:
  115. from app.core.data_parse.parse_neo4j_process import HotelPositionNeo4jProcessor
  116. processor = HotelPositionNeo4jProcessor()
  117. # 测试连接数据库(如果可能)
  118. if processor.connect_postgresql():
  119. print("✅ PostgreSQL数据库连接成功")
  120. # 尝试获取酒店集团品牌数据
  121. try:
  122. brands = processor.get_hotel_group_brands()
  123. print(f"✅ 成功获取酒店集团品牌数据,共 {len(brands)} 条记录")
  124. if brands:
  125. print("数据示例:")
  126. for i, brand in enumerate(brands[:3]):
  127. print(f" {i+1}. 集团: {brand['group_name_zh']}, 品牌: {brand['brand_name_zh']}, 级别: {brand['positioning_level_zh']}")
  128. else:
  129. print("⚠️ 未获取到酒店集团品牌数据,可能是表为空或查询条件不匹配")
  130. except Exception as e:
  131. print(f"⚠️ 获取酒店集团品牌数据时发生错误: {e}")
  132. print("这可能是正常的,如果数据库中没有相应的表或数据")
  133. else:
  134. print("⚠️ PostgreSQL数据库连接失败,跳过数据获取测试")
  135. return True
  136. except Exception as e:
  137. print(f"❌ 测试数据结构时发生错误: {e}")
  138. return False
  139. def main():
  140. """主函数"""
  141. print("酒店集团品牌数据处理功能测试")
  142. print("=" * 70)
  143. # 测试模块导入
  144. if not test_import_enhanced_modules():
  145. print("\n模块导入测试失败,跳过其他测试")
  146. return
  147. print("\n" + "=" * 70)
  148. # 测试酒店集团品牌相关方法
  149. if not test_hotel_group_brands_methods():
  150. print("\n酒店集团品牌方法测试失败,跳过其他测试")
  151. return
  152. print("\n" + "=" * 70)
  153. # 测试数据结构
  154. test_data_structure()
  155. print("\n" + "=" * 70)
  156. # 测试脚本执行
  157. if test_hotel_group_brands_functionality():
  158. print("\n🎉 酒店集团品牌功能测试通过!")
  159. else:
  160. print("\n💥 酒店集团品牌功能测试失败!")
  161. if __name__ == "__main__":
  162. main()