baoxiang/test_transactions.py
2025-12-16 21:39:32 +08:00

65 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试交易流水API接口
"""
import os
import sys
import django
# 添加项目路径
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
# 设置Django环境
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo07.settings')
try:
django.setup()
except Exception as e:
print(f"Django setup error: {e}")
import requests
import json
def test_transactions_api():
print("=" * 60)
print("测试交易流水API接口")
print("=" * 60)
# 这里需要替换为实际的API基础URL
base_url = "http://localhost:8000" # 假设后端运行在8000端口
# 测试获取交易流水接口
url = f"{base_url}/api/users/me/transactions"
print(f"测试接口: {url}")
try:
# 这里需要提供有效的认证token
# 在实际测试中你需要先登录获取token
headers = {
"Authorization": "Bearer YOUR_JWT_TOKEN_HERE"
}
response = requests.get(url, headers=headers, timeout=10)
print(f"HTTP状态码: {response.status_code}")
if response.status_code == 200:
data = response.json()
print(f"返回交易记录数量: {len(data)}")
if data:
print("第一条交易记录:")
print(json.dumps(data[0], indent=2, ensure_ascii=False))
else:
print("没有交易记录")
else:
print(f"错误响应: {response.text}")
except requests.exceptions.ConnectionError:
print("连接错误: 无法连接到后端服务,请确保后端服务正在运行")
except requests.exceptions.Timeout:
print("超时错误: 请求超时")
except Exception as e:
print(f"其他错误: {e}")
if __name__ == "__main__":
test_transactions_api()