2026-01-22 12:57:26 +08:00
|
|
|
|
"""Pytest 配置和共享 fixtures"""
|
|
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
import tempfile
|
|
|
|
|
|
from datetime import date, time
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
2026-01-27 16:16:32 +08:00
|
|
|
|
# 设置测试数据库
|
2026-01-22 12:57:26 +08:00
|
|
|
|
@pytest.fixture(autouse=True)
|
2026-01-27 16:16:32 +08:00
|
|
|
|
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")
|
2026-01-22 12:57:26 +08:00
|
|
|
|
|
|
|
|
|
|
from src.vitals.core import database
|
2026-01-27 16:16:32 +08:00
|
|
|
|
|
|
|
|
|
|
# 重置连接池以使用新配置
|
|
|
|
|
|
database._connection_pool = None
|
2026-01-22 12:57:26 +08:00
|
|
|
|
|
|
|
|
|
|
# 初始化数据库
|
|
|
|
|
|
database.init_db()
|
|
|
|
|
|
|
2026-01-27 16:16:32 +08:00
|
|
|
|
# 清理测试数据(在测试前清空表)
|
|
|
|
|
|
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
|
2026-01-22 12:57:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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",
|
|
|
|
|
|
)
|