fixed dynamic TDEE

This commit is contained in:
2026-06-16 08:04:15 +03:00
parent a3f01cd850
commit 0f2827030b
11 changed files with 603 additions and 18 deletions
+80
View File
@@ -0,0 +1,80 @@
import unittest
from datetime import date, timedelta
from app.fitness.calculators import (
compute_daily_targets,
compute_expected_targets,
resolve_expected_activity,
)
PROFILE_LOSE = {
"sex": "male",
"age": 30,
"height_cm": 180,
"weight_kg": 86,
"goal": "lose",
"neat_base_kcal": 200,
"activity_level": "moderate",
"weekly_workouts": 3,
"baseline_steps": None,
"baseline_workout_kcal": None,
}
class ExpectedActivityTests(unittest.TestCase):
def test_weekly_avg_with_enough_history(self) -> None:
history = [
{"steps": 8000, "workout_kcal": 400.0},
{"steps": 9000, "workout_kcal": 500.0},
{"steps": 7000, "workout_kcal": 450.0},
{"steps": 0, "workout_kcal": 0.0},
]
steps, workout_kcal, source, days = resolve_expected_activity(PROFILE_LOSE, history=history)
self.assertEqual(source, "weekly_avg")
self.assertEqual(days, 3)
self.assertEqual(steps, 6000)
self.assertEqual(workout_kcal, 337.5)
def test_baseline_fallback(self) -> None:
profile = {
**PROFILE_LOSE,
"baseline_steps": 10000,
"baseline_workout_kcal": 2100.0,
}
history = [{"steps": 1000, "workout_kcal": 50.0}]
steps, workout_kcal, source, _ = resolve_expected_activity(profile, history=history)
self.assertEqual(source, "baseline")
self.assertEqual(steps, 10000)
self.assertEqual(workout_kcal, 300.0)
def test_defaults_fallback(self) -> None:
history = [{"steps": 0, "workout_kcal": 0.0}] * 7
steps, workout_kcal, source, _ = resolve_expected_activity(PROFILE_LOSE, history=history)
self.assertEqual(source, "defaults")
self.assertEqual(steps, 8000)
self.assertAlmostEqual(workout_kcal, 150.0, delta=0.1)
def test_history_excludes_current_day_for_expected(self) -> None:
"""Expected for day D uses only prior days in history list."""
day = date(2026, 6, 16)
prior_days = []
cursor = day - timedelta(days=7)
while cursor < day:
prior_days.append({"steps": 8000, "workout_kcal": 450.0})
cursor += timedelta(days=1)
expected = compute_expected_targets(PROFILE_LOSE, history=prior_days)
rest = compute_daily_targets(PROFILE_LOSE, steps_total=0, workouts=[])
self.assertEqual(expected["source"], "weekly_avg")
self.assertGreater(expected["tdee"], rest["tdee"])
self.assertGreater(expected["carbs_g"], rest["carbs_g"])
def test_morning_carbs_scenario(self) -> None:
rest = compute_daily_targets(PROFILE_LOSE, steps_total=0, workouts=[])
history = [{"steps": 8000, "workout_kcal": 450.0}] * 7
expected = compute_expected_targets(PROFILE_LOSE, history=history)
self.assertLess(rest["carbs_g"], 10)
self.assertGreater(expected["carbs_g"], 100)
if __name__ == "__main__":
unittest.main()