Files
DDUp/tests/conftest.py
liweiliang0905@gmail.com a8c9c87540 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>
2026-01-27 16:16:32 +08:00

113 lines
2.7 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.

"""Pytest 配置和共享 fixtures"""
import os
import tempfile
from datetime import date, time
from pathlib import Path
import pytest
# 设置测试数据库
@pytest.fixture(autouse=True)
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
# 重置连接池以使用新配置
database._connection_pool = None
# 初始化数据库
database.init_db()
# 清理测试数据(在测试前清空表)
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
def sample_exercise():
"""示例运动记录"""
from src.vitals.core.models import Exercise
return Exercise(
date=date(2026, 1, 18),
type="跑步",
duration=30,
calories=240,
distance=5.0,
heart_rate_avg=145,
source="手动",
notes="晨跑",
)
@pytest.fixture
def sample_meal():
"""示例饮食记录"""
from src.vitals.core.models import Meal
return Meal(
date=date(2026, 1, 18),
meal_type="午餐",
description="米饭+红烧肉+青菜",
calories=650,
protein=25.0,
carbs=80.0,
fat=20.0,
)
@pytest.fixture
def sample_sleep():
"""示例睡眠记录"""
from src.vitals.core.models import Sleep
return Sleep(
date=date(2026, 1, 18),
bedtime=time(23, 30),
wake_time=time(7, 0),
duration=7.5,
quality=4,
source="手动",
)
@pytest.fixture
def sample_weight():
"""示例体重记录"""
from src.vitals.core.models import Weight
return Weight(
date=date(2026, 1, 18),
weight_kg=72.5,
body_fat_pct=18.5,
muscle_mass=35.0,
)
@pytest.fixture
def sample_config():
"""示例用户配置"""
from src.vitals.core.models import UserConfig
return UserConfig(
age=28,
gender="male",
height=175.0,
weight=72.0,
activity_level="moderate",
goal="lose",
)