Files
food-ai/backend/internal/user/calories_test.go
dbastrikin c9ddb708b1 feat: replace age integer with date_of_birth across backend and client
Store date_of_birth (DATE) instead of a static age integer so that age
is always computed dynamically from the stored date of birth.

- Migration 011: adds date_of_birth, backfills from age, drops age
- AgeFromDOB helper computes current age from YYYY-MM-DD string
- User model, repository SQL, and service validation updated
- Flutter: User.age becomes a computed getter; profile edit screen
  uses a date picker bounded to [today-120y, today-10y]

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-09 23:37:58 +02:00

112 lines
3.1 KiB
Go

package user
import (
"testing"
"time"
)
func ptr[T any](v T) *T { return &v }
func TestAgeFromDOB_Nil(t *testing.T) {
if AgeFromDOB(nil) != nil {
t.Fatal("expected nil for nil input")
}
}
func TestAgeFromDOB_InvalidFormat(t *testing.T) {
s := "not-a-date"
if AgeFromDOB(&s) != nil {
t.Fatal("expected nil for invalid format")
}
}
func TestAgeFromDOB_ExactAge(t *testing.T) {
dob := time.Now().AddDate(-30, 0, 0).Format("2006-01-02")
age := AgeFromDOB(&dob)
if age == nil {
t.Fatal("expected non-nil result")
}
if *age != 30 {
t.Errorf("expected 30, got %d", *age)
}
}
func TestAgeFromDOB_BeforeBirthday(t *testing.T) {
// Birthday is one day in the future relative to today-25y, so age should be 24
now := time.Now()
dob := time.Date(now.Year()-25, now.Month(), now.Day()+1, 0, 0, 0, 0, time.UTC).Format("2006-01-02")
age := AgeFromDOB(&dob)
if age == nil {
t.Fatal("expected non-nil result")
}
if *age != 24 {
t.Errorf("expected 24, got %d", *age)
}
}
func TestCalculateDailyCalories_MaleMaintain(t *testing.T) {
cal := CalculateDailyCalories(ptr(180), ptr(80.0), ptr(30), ptr("male"), ptr("moderate"), ptr("maintain"))
if cal == nil {
t.Fatal("expected non-nil result")
}
// BMR = 10*80 + 6.25*180 - 5*30 + 5 = 800 + 1125 - 150 + 5 = 1780
// TDEE = 1780 * 1.55 = 2759
if *cal != 2759 {
t.Errorf("expected 2759, got %d", *cal)
}
}
func TestCalculateDailyCalories_FemaleLose(t *testing.T) {
cal := CalculateDailyCalories(ptr(165), ptr(60.0), ptr(25), ptr("female"), ptr("low"), ptr("lose"))
if cal == nil {
t.Fatal("expected non-nil result")
}
// BMR = 10*60 + 6.25*165 - 5*25 - 161 = 600 + 1031.25 - 125 - 161 = 1345.25
// TDEE = 1345.25 * 1.375 = 1849.72
// Goal: -500 = 1349.72 → 1350
if *cal != 1350 {
t.Errorf("expected 1350, got %d", *cal)
}
}
func TestCalculateDailyCalories_MaleGain(t *testing.T) {
cal := CalculateDailyCalories(ptr(175), ptr(70.0), ptr(28), ptr("male"), ptr("high"), ptr("gain"))
if cal == nil {
t.Fatal("expected non-nil result")
}
// BMR = 10*70 + 6.25*175 - 5*28 + 5 = 700 + 1093.75 - 140 + 5 = 1658.75
// TDEE = 1658.75 * 1.725 = 2861.34
// Goal: +300 = 3161.34 → 3161
if *cal != 3161 {
t.Errorf("expected 3161, got %d", *cal)
}
}
func TestCalculateDailyCalories_NilHeight(t *testing.T) {
cal := CalculateDailyCalories(nil, ptr(80.0), ptr(30), ptr("male"), ptr("moderate"), ptr("maintain"))
if cal != nil {
t.Fatal("expected nil when height is nil")
}
}
func TestCalculateDailyCalories_NilWeight(t *testing.T) {
cal := CalculateDailyCalories(ptr(180), nil, ptr(30), ptr("male"), ptr("moderate"), ptr("maintain"))
if cal != nil {
t.Fatal("expected nil when weight is nil")
}
}
func TestCalculateDailyCalories_InvalidGender(t *testing.T) {
cal := CalculateDailyCalories(ptr(180), ptr(80.0), ptr(30), ptr("other"), ptr("moderate"), ptr("maintain"))
if cal != nil {
t.Fatal("expected nil for invalid gender")
}
}
func TestCalculateDailyCalories_InvalidActivity(t *testing.T) {
cal := CalculateDailyCalories(ptr(180), ptr(80.0), ptr(30), ptr("male"), ptr("extreme"), ptr("maintain"))
if cal != nil {
t.Fatal("expected nil for invalid activity")
}
}