feat(api): 添加 BMI 分析端点 /api/bmi/analysis
- 新增 /api/bmi/analysis API 端点,提供 BMI 分析数据 - 返回当前体重、BMI、BMI状态、目标体重、预估达成天数等 - 基于近30天饮食和运动数据计算每日净消耗 - 更新测试框架支持 MySQL 和认证 - 修复其他 API 端点的用户隔离问题 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,13 +7,36 @@ from fastapi.testclient import TestClient
|
||||
|
||||
from src.vitals.core import database as db
|
||||
from src.vitals.core.models import Exercise, Meal, Sleep, Weight, UserConfig
|
||||
from src.vitals.core.auth import create_token
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
"""创建测试客户端"""
|
||||
"""创建测试客户端(带认证)"""
|
||||
from src.vitals.web.app import app
|
||||
return TestClient(app)
|
||||
|
||||
# 确保有测试用户
|
||||
active_user = db.get_active_user()
|
||||
if not active_user:
|
||||
# 获取或创建默认用户
|
||||
users = db.get_users()
|
||||
if users:
|
||||
active_user = users[0]
|
||||
db.set_active_user(active_user.id)
|
||||
else:
|
||||
# 创建默认测试用户
|
||||
from src.vitals.core.auth import hash_password
|
||||
user_id = db.create_user("test_user", hash_password("test123"))
|
||||
db.set_active_user(user_id)
|
||||
active_user = db.get_user_by_id(user_id)
|
||||
|
||||
# 创建认证 token
|
||||
token = create_token(active_user.id, active_user.name, active_user.is_admin)
|
||||
|
||||
# 创建带认证 cookie 的客户端
|
||||
test_client = TestClient(app)
|
||||
test_client.cookies.set("auth_token", token)
|
||||
return test_client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -282,3 +305,15 @@ class TestErrorHandling:
|
||||
"""测试无效参数"""
|
||||
response = client.get("/api/exercises?days=abc")
|
||||
assert response.status_code == 422
|
||||
|
||||
|
||||
def test_bmi_analysis_endpoint(client):
|
||||
"""测试 BMI 分析 API"""
|
||||
response = client.get("/api/bmi/analysis")
|
||||
assert response.status_code == 200
|
||||
data = response.json()
|
||||
assert "current_weight" in data
|
||||
assert "current_bmi" in data
|
||||
assert "bmi_status" in data
|
||||
assert "target_weight" in data
|
||||
assert "estimated_days" in data
|
||||
|
||||
Reference in New Issue
Block a user