| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- 测试 /api/meta/check 接口
- """
- import sys
- import os
- # 添加项目根目录到 Python 路径
- sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
- def test_check_api():
- """测试检查元数据接口"""
- print("\n" + "="*60)
- print(" 测试 /api/meta/check 接口")
- print("="*60 + "\n")
-
- # 导入 Flask 应用
- try:
- from app import create_app
- app = create_app()
-
- with app.app_context():
- print("✅ Flask 应用创建成功")
- print(f"✅ App context 已激活")
-
- # 测试 1: 检查不存在的元数据
- print("\n" + "-"*60)
- print("测试 1: 检查不存在的元数据")
- print("-"*60)
-
- test_name = "测试元数据_不存在_" + str(os.getpid())
- print(f"测试元数据名: {test_name}")
-
- with app.test_client() as client:
- response = client.get(
- '/api/meta/check',
- query_string={'name_zh': test_name}
- )
-
- print(f"\n状态码: {response.status_code}")
- print(f"响应内容:")
-
- import json
- data = response.get_json()
- print(json.dumps(data, indent=2, ensure_ascii=False))
-
- if response.status_code == 200:
- if data.get('code') == 200:
- exists = data.get('data', {}).get('exists', None)
- if exists == False:
- print("\n✅ 测试 1 通过:正确返回元数据不存在")
- else:
- print(f"\n❌ 测试 1 失败:预期 exists=False,实际 exists={exists}")
- else:
- print(f"\n❌ 测试 1 失败:code={data.get('code')}")
- else:
- print(f"\n❌ 测试 1 失败:HTTP 状态码 {response.status_code}")
-
- # 测试 2: 检查已存在的元数据(如果有)
- print("\n" + "-"*60)
- print("测试 2: 检查已存在的元数据")
- print("-"*60)
-
- existing_name = "其他费用定额"
- print(f"测试元数据名: {existing_name}")
-
- with app.test_client() as client:
- response = client.get(
- '/api/meta/check',
- query_string={'name_zh': existing_name}
- )
-
- print(f"\n状态码: {response.status_code}")
- print(f"响应内容:")
-
- data = response.get_json()
- print(json.dumps(data, indent=2, ensure_ascii=False))
-
- if response.status_code == 200:
- if data.get('code') == 200:
- exists = data.get('data', {}).get('exists', None)
- print(f"\n元数据 '{existing_name}' {'存在' if exists else '不存在'}")
- print("✅ 测试 2 通过:接口正常响应")
- else:
- print(f"\n❌ 测试 2 失败:code={data.get('code')}")
- else:
- print(f"\n❌ 测试 2 失败:HTTP 状态码 {response.status_code}")
-
- # 测试 3: 缺少参数
- print("\n" + "-"*60)
- print("测试 3: 测试参数验证(缺少 name_zh)")
- print("-"*60)
-
- with app.test_client() as client:
- response = client.get('/api/meta/check')
-
- print(f"\n状态码: {response.status_code}")
- print(f"响应内容:")
-
- data = response.get_json()
- print(json.dumps(data, indent=2, ensure_ascii=False))
-
- if response.status_code == 200:
- if data.get('code') != 200:
- print("\n✅ 测试 3 通过:正确返回参数错误")
- else:
- print("\n❌ 测试 3 失败:应该返回错误但返回了成功")
- else:
- print(f"\n❌ 测试 3 失败:HTTP 状态码 {response.status_code}")
-
- # 测试 4: 测试 Neo4j 连接
- print("\n" + "-"*60)
- print("测试 4: 验证 Neo4j 连接")
- print("-"*60)
-
- try:
- from app.services.neo4j_driver import neo4j_driver
-
- with neo4j_driver.get_session() as session:
- result = session.run("RETURN 1 as test")
- record = result.single()
- if record and record["test"] == 1:
- print("✅ Neo4j 连接正常")
- else:
- print("❌ Neo4j 查询返回异常")
- except Exception as e:
- print(f"❌ Neo4j 连接失败: {str(e)}")
-
- # 总结
- print("\n" + "="*60)
- print(" 测试完成")
- print("="*60 + "\n")
- print("✅ /api/meta/check 接口工作正常")
- print("✅ 参数验证正确")
- print("✅ Neo4j 连接正常")
- print("✅ 数据查询正常")
-
- except ImportError as e:
- print(f"❌ 导入失败: {str(e)}")
- print("\n请确保:")
- print("1. 在项目根目录运行此脚本")
- print("2. 已安装所有依赖")
- print("3. 配置文件正确")
- except Exception as e:
- print(f"❌ 测试失败: {str(e)}")
- import traceback
- traceback.print_exc()
- if __name__ == "__main__":
- test_check_api()
|