98 lines
2.0 KiB
Python
98 lines
2.0 KiB
Python
"""Pytest 配置和共享 fixtures"""
|
|
|
|
import os
|
|
import tempfile
|
|
from datetime import date, time
|
|
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
|
|
|
|
from src.vitals.core import database
|
|
monkeypatch.setattr(database, "get_db_path", mock_get_db_path)
|
|
|
|
# 初始化数据库
|
|
database.init_db()
|
|
|
|
yield db_path
|
|
|
|
|
|
@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",
|
|
)
|