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:
2026-01-27 16:16:32 +08:00
parent 295bc6e736
commit a8c9c87540
3 changed files with 228 additions and 19 deletions

View File

@@ -7,23 +7,38 @@ from pathlib import Path
import pytest
# 设置测试数据库路径
# 设置测试数据库
@pytest.fixture(autouse=True)
def test_db(tmp_path, monkeypatch):
"""使用临时数据库进行测试"""
db_path = tmp_path / "test_vitals.db"
# 覆盖 get_db_path 函数
def mock_get_db_path():
return db_path
def test_db(monkeypatch):
"""使用 MySQL 测试数据库进行测试"""
# 设置 MySQL 环境变量(使用本地 Docker MySQL
monkeypatch.setenv("MYSQL_HOST", "localhost")
monkeypatch.setenv("MYSQL_PORT", "3306")
monkeypatch.setenv("MYSQL_USER", "vitals")
monkeypatch.setenv("MYSQL_PASSWORD", "vitalspassword")
monkeypatch.setenv("MYSQL_DATABASE", "vitals")
from src.vitals.core import database
monkeypatch.setattr(database, "get_db_path", mock_get_db_path)
# 重置连接池以使用新配置
database._connection_pool = None
# 初始化数据库
database.init_db()
yield db_path
# 清理测试数据(在测试前清空表)
with database.get_connection() as (conn, cursor):
cursor.execute("DELETE FROM exercise")
cursor.execute("DELETE FROM meal")
cursor.execute("DELETE FROM sleep")
cursor.execute("DELETE FROM weight")
cursor.execute("DELETE FROM reading")
# 不删除 users 和 config只是确保有默认用户
yield
# 测试后重置连接池
database._connection_pool = None
@pytest.fixture