test_check_api.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 测试 /api/meta/check 接口
  5. """
  6. import sys
  7. import os
  8. # 添加项目根目录到 Python 路径
  9. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  10. def test_check_api():
  11. """测试检查元数据接口"""
  12. print("\n" + "="*60)
  13. print(" 测试 /api/meta/check 接口")
  14. print("="*60 + "\n")
  15. # 导入 Flask 应用
  16. try:
  17. from app import create_app
  18. app = create_app()
  19. with app.app_context():
  20. print("✅ Flask 应用创建成功")
  21. print(f"✅ App context 已激活")
  22. # 测试 1: 检查不存在的元数据
  23. print("\n" + "-"*60)
  24. print("测试 1: 检查不存在的元数据")
  25. print("-"*60)
  26. test_name = "测试元数据_不存在_" + str(os.getpid())
  27. print(f"测试元数据名: {test_name}")
  28. with app.test_client() as client:
  29. response = client.get(
  30. '/api/meta/check',
  31. query_string={'name_zh': test_name}
  32. )
  33. print(f"\n状态码: {response.status_code}")
  34. print(f"响应内容:")
  35. import json
  36. data = response.get_json()
  37. print(json.dumps(data, indent=2, ensure_ascii=False))
  38. if response.status_code == 200:
  39. if data.get('code') == 200:
  40. exists = data.get('data', {}).get('exists', None)
  41. if exists == False:
  42. print("\n✅ 测试 1 通过:正确返回元数据不存在")
  43. else:
  44. print(f"\n❌ 测试 1 失败:预期 exists=False,实际 exists={exists}")
  45. else:
  46. print(f"\n❌ 测试 1 失败:code={data.get('code')}")
  47. else:
  48. print(f"\n❌ 测试 1 失败:HTTP 状态码 {response.status_code}")
  49. # 测试 2: 检查已存在的元数据(如果有)
  50. print("\n" + "-"*60)
  51. print("测试 2: 检查已存在的元数据")
  52. print("-"*60)
  53. existing_name = "其他费用定额"
  54. print(f"测试元数据名: {existing_name}")
  55. with app.test_client() as client:
  56. response = client.get(
  57. '/api/meta/check',
  58. query_string={'name_zh': existing_name}
  59. )
  60. print(f"\n状态码: {response.status_code}")
  61. print(f"响应内容:")
  62. data = response.get_json()
  63. print(json.dumps(data, indent=2, ensure_ascii=False))
  64. if response.status_code == 200:
  65. if data.get('code') == 200:
  66. exists = data.get('data', {}).get('exists', None)
  67. print(f"\n元数据 '{existing_name}' {'存在' if exists else '不存在'}")
  68. print("✅ 测试 2 通过:接口正常响应")
  69. else:
  70. print(f"\n❌ 测试 2 失败:code={data.get('code')}")
  71. else:
  72. print(f"\n❌ 测试 2 失败:HTTP 状态码 {response.status_code}")
  73. # 测试 3: 缺少参数
  74. print("\n" + "-"*60)
  75. print("测试 3: 测试参数验证(缺少 name_zh)")
  76. print("-"*60)
  77. with app.test_client() as client:
  78. response = client.get('/api/meta/check')
  79. print(f"\n状态码: {response.status_code}")
  80. print(f"响应内容:")
  81. data = response.get_json()
  82. print(json.dumps(data, indent=2, ensure_ascii=False))
  83. if response.status_code == 200:
  84. if data.get('code') != 200:
  85. print("\n✅ 测试 3 通过:正确返回参数错误")
  86. else:
  87. print("\n❌ 测试 3 失败:应该返回错误但返回了成功")
  88. else:
  89. print(f"\n❌ 测试 3 失败:HTTP 状态码 {response.status_code}")
  90. # 测试 4: 测试 Neo4j 连接
  91. print("\n" + "-"*60)
  92. print("测试 4: 验证 Neo4j 连接")
  93. print("-"*60)
  94. try:
  95. from app.services.neo4j_driver import neo4j_driver
  96. with neo4j_driver.get_session() as session:
  97. result = session.run("RETURN 1 as test")
  98. record = result.single()
  99. if record and record["test"] == 1:
  100. print("✅ Neo4j 连接正常")
  101. else:
  102. print("❌ Neo4j 查询返回异常")
  103. except Exception as e:
  104. print(f"❌ Neo4j 连接失败: {str(e)}")
  105. # 总结
  106. print("\n" + "="*60)
  107. print(" 测试完成")
  108. print("="*60 + "\n")
  109. print("✅ /api/meta/check 接口工作正常")
  110. print("✅ 参数验证正确")
  111. print("✅ Neo4j 连接正常")
  112. print("✅ 数据查询正常")
  113. except ImportError as e:
  114. print(f"❌ 导入失败: {str(e)}")
  115. print("\n请确保:")
  116. print("1. 在项目根目录运行此脚本")
  117. print("2. 已安装所有依赖")
  118. print("3. 配置文件正确")
  119. except Exception as e:
  120. print(f"❌ 测试失败: {str(e)}")
  121. import traceback
  122. traceback.print_exc()
  123. if __name__ == "__main__":
  124. test_check_api()