refactor: move all tests to backend/tests/ as black-box packages
All test files relocated from internal/X/ to tests/X/ and converted to package X_test, using only the public API of each package. - tests/auth/: jwt, service, handler integration tests - tests/middleware/: auth, request_id, recovery tests - tests/user/: calories, service, repository integration tests - tests/locale/: locale tests (already package locale_test, just moved) - tests/ingredient/: repository integration tests - tests/recipe/: repository integration tests mockUserRepo in tests/user/service_test.go redefined locally with fully-qualified user.* types. Unexported auth.refreshRequest replaced with a local testRefreshRequest struct in the integration test. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
113
backend/tests/user/calories_test.go
Normal file
113
backend/tests/user/calories_test.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/food-ai/backend/internal/user"
|
||||
)
|
||||
|
||||
func ptr[T any](v T) *T { return &v }
|
||||
|
||||
func TestAgeFromDOB_Nil(t *testing.T) {
|
||||
if user.AgeFromDOB(nil) != nil {
|
||||
t.Fatal("expected nil for nil input")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgeFromDOB_InvalidFormat(t *testing.T) {
|
||||
s := "not-a-date"
|
||||
if user.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 := user.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 := user.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 := user.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 := user.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 := user.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 := user.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 := user.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 := user.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 := user.CalculateDailyCalories(ptr(180), ptr(80.0), ptr(30), ptr("male"), ptr("extreme"), ptr("maintain"))
|
||||
if cal != nil {
|
||||
t.Fatal("expected nil for invalid activity")
|
||||
}
|
||||
}
|
||||
210
backend/tests/user/repository_integration_test.go
Normal file
210
backend/tests/user/repository_integration_test.go
Normal file
@@ -0,0 +1,210 @@
|
||||
//go:build integration
|
||||
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/food-ai/backend/internal/testutil"
|
||||
"github.com/food-ai/backend/internal/user"
|
||||
)
|
||||
|
||||
func TestRepository_UpsertByFirebaseUID_Insert(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
u, upsertError := repo.UpsertByFirebaseUID(ctx, "fb-123", "test@example.com", "Test User", "https://avatar.url")
|
||||
if upsertError != nil {
|
||||
t.Fatalf("unexpected error: %v", upsertError)
|
||||
}
|
||||
if u.FirebaseUID != "fb-123" {
|
||||
t.Errorf("expected fb-123, got %s", u.FirebaseUID)
|
||||
}
|
||||
if u.Email != "test@example.com" {
|
||||
t.Errorf("expected test@example.com, got %s", u.Email)
|
||||
}
|
||||
if u.Plan != "free" {
|
||||
t.Errorf("expected free, got %s", u.Plan)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_UpsertByFirebaseUID_Update(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
_, _ = repo.UpsertByFirebaseUID(ctx, "fb-123", "old@example.com", "Old Name", "")
|
||||
u, upsertError := repo.UpsertByFirebaseUID(ctx, "fb-123", "new@example.com", "New Name", "https://new-avatar.url")
|
||||
if upsertError != nil {
|
||||
t.Fatalf("unexpected error: %v", upsertError)
|
||||
}
|
||||
if u.Email != "new@example.com" {
|
||||
t.Errorf("expected new email, got %s", u.Email)
|
||||
}
|
||||
// Name should not be overwritten if already set
|
||||
if u.Name != "Old Name" {
|
||||
t.Errorf("expected name to be preserved as 'Old Name', got %s", u.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_GetByID(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-get", "get@example.com", "Get User", "")
|
||||
u, getError := repo.GetByID(ctx, created.ID)
|
||||
if getError != nil {
|
||||
t.Fatalf("unexpected error: %v", getError)
|
||||
}
|
||||
if u.ID != created.ID {
|
||||
t.Errorf("expected %s, got %s", created.ID, u.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_GetByID_NotFound(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
_, getError := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
|
||||
if getError == nil {
|
||||
t.Fatal("expected error for non-existent user")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_Update(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-upd", "upd@example.com", "Update User", "")
|
||||
height := 180
|
||||
u, updateError := repo.Update(ctx, created.ID, user.UpdateProfileRequest{HeightCM: &height})
|
||||
if updateError != nil {
|
||||
t.Fatalf("unexpected error: %v", updateError)
|
||||
}
|
||||
if u.HeightCM == nil || *u.HeightCM != 180 {
|
||||
t.Errorf("expected height 180, got %v", u.HeightCM)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_Update_MultipleFields(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-multi", "multi@example.com", "Multi User", "")
|
||||
height := 175
|
||||
weight := 70.5
|
||||
dob := "2001-03-09"
|
||||
gender := "male"
|
||||
activity := "moderate"
|
||||
goal := "maintain"
|
||||
u, updateError := repo.Update(ctx, created.ID, user.UpdateProfileRequest{
|
||||
HeightCM: &height,
|
||||
WeightKG: &weight,
|
||||
DateOfBirth: &dob,
|
||||
Gender: &gender,
|
||||
Activity: &activity,
|
||||
Goal: &goal,
|
||||
})
|
||||
if updateError != nil {
|
||||
t.Fatalf("unexpected error: %v", updateError)
|
||||
}
|
||||
if u.HeightCM == nil || *u.HeightCM != 175 {
|
||||
t.Errorf("expected 175, got %v", u.HeightCM)
|
||||
}
|
||||
if u.WeightKG == nil || *u.WeightKG != 70.5 {
|
||||
t.Errorf("expected 70.5, got %v", u.WeightKG)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_Update_Preferences(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-prefs", "prefs@example.com", "Prefs User", "")
|
||||
prefs := json.RawMessage(`{"cuisines":["russian","asian"]}`)
|
||||
u, updateError := repo.Update(ctx, created.ID, user.UpdateProfileRequest{Preferences: &prefs})
|
||||
if updateError != nil {
|
||||
t.Fatalf("unexpected error: %v", updateError)
|
||||
}
|
||||
var preferences map[string]interface{}
|
||||
json.Unmarshal(u.Preferences, &preferences)
|
||||
if preferences["cuisines"] == nil {
|
||||
t.Error("expected cuisines in preferences")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_SetAndFindRefreshToken(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-token", "token@example.com", "Token User", "")
|
||||
setError := repo.SetRefreshToken(ctx, created.ID, "refresh-token-123", time.Now().Add(24*time.Hour))
|
||||
if setError != nil {
|
||||
t.Fatalf("unexpected error setting token: %v", setError)
|
||||
}
|
||||
|
||||
u, findError := repo.FindByRefreshToken(ctx, "refresh-token-123")
|
||||
if findError != nil {
|
||||
t.Fatalf("unexpected error finding by token: %v", findError)
|
||||
}
|
||||
if u.ID != created.ID {
|
||||
t.Errorf("expected %s, got %s", created.ID, u.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_FindByRefreshToken_Expired(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-expired", "expired@example.com", "Expired User", "")
|
||||
_ = repo.SetRefreshToken(ctx, created.ID, "expired-token", time.Now().Add(-1*time.Hour))
|
||||
|
||||
_, findError := repo.FindByRefreshToken(ctx, "expired-token")
|
||||
if findError == nil {
|
||||
t.Fatal("expected error for expired token")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_ClearRefreshToken(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-clear", "clear@example.com", "Clear User", "")
|
||||
_ = repo.SetRefreshToken(ctx, created.ID, "token-to-clear", time.Now().Add(24*time.Hour))
|
||||
clearError := repo.ClearRefreshToken(ctx, created.ID)
|
||||
if clearError != nil {
|
||||
t.Fatalf("unexpected error: %v", clearError)
|
||||
}
|
||||
|
||||
_, findError := repo.FindByRefreshToken(ctx, "token-to-clear")
|
||||
if findError == nil {
|
||||
t.Fatal("expected error after clearing token")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_Update_NoFields(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-noop", "noop@example.com", "Noop User", "")
|
||||
u, updateError := repo.Update(ctx, created.ID, user.UpdateProfileRequest{})
|
||||
if updateError != nil {
|
||||
t.Fatalf("unexpected error: %v", updateError)
|
||||
}
|
||||
if u.ID != created.ID {
|
||||
t.Errorf("expected %s, got %s", created.ID, u.ID)
|
||||
}
|
||||
}
|
||||
262
backend/tests/user/service_test.go
Normal file
262
backend/tests/user/service_test.go
Normal file
@@ -0,0 +1,262 @@
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/food-ai/backend/internal/user"
|
||||
)
|
||||
|
||||
func ptrStr(s string) *string { return &s }
|
||||
func ptrInt(i int) *int { return &i }
|
||||
func ptrFloat(f float64) *float64 { return &f }
|
||||
|
||||
// mockUserRepo is an in-test mock implementing user.UserRepository.
|
||||
type mockUserRepo struct {
|
||||
getByIDFn func(ctx context.Context, id string) (*user.User, error)
|
||||
updateFn func(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error)
|
||||
updateInTxFn func(ctx context.Context, id string, profileReq user.UpdateProfileRequest, caloriesReq *user.UpdateProfileRequest) (*user.User, error)
|
||||
upsertByFirebaseUIDFn func(ctx context.Context, uid, email, name, avatarURL string) (*user.User, error)
|
||||
setRefreshTokenFn func(ctx context.Context, id, token string, expiresAt time.Time) error
|
||||
findByRefreshTokenFn func(ctx context.Context, token string) (*user.User, error)
|
||||
clearRefreshTokenFn func(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
func (m *mockUserRepo) UpsertByFirebaseUID(ctx context.Context, uid, email, name, avatarURL string) (*user.User, error) {
|
||||
return m.upsertByFirebaseUIDFn(ctx, uid, email, name, avatarURL)
|
||||
}
|
||||
func (m *mockUserRepo) GetByID(ctx context.Context, id string) (*user.User, error) {
|
||||
return m.getByIDFn(ctx, id)
|
||||
}
|
||||
func (m *mockUserRepo) Update(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error) {
|
||||
return m.updateFn(ctx, id, req)
|
||||
}
|
||||
func (m *mockUserRepo) UpdateInTx(ctx context.Context, id string, profileReq user.UpdateProfileRequest, caloriesReq *user.UpdateProfileRequest) (*user.User, error) {
|
||||
return m.updateInTxFn(ctx, id, profileReq, caloriesReq)
|
||||
}
|
||||
func (m *mockUserRepo) SetRefreshToken(ctx context.Context, id, token string, expiresAt time.Time) error {
|
||||
return m.setRefreshTokenFn(ctx, id, token, expiresAt)
|
||||
}
|
||||
func (m *mockUserRepo) FindByRefreshToken(ctx context.Context, token string) (*user.User, error) {
|
||||
return m.findByRefreshTokenFn(ctx, token)
|
||||
}
|
||||
func (m *mockUserRepo) ClearRefreshToken(ctx context.Context, id string) error {
|
||||
return m.clearRefreshTokenFn(ctx, id)
|
||||
}
|
||||
|
||||
func TestGetProfile_Success(t *testing.T) {
|
||||
repo := &mockUserRepo{
|
||||
getByIDFn: func(ctx context.Context, id string) (*user.User, error) {
|
||||
return &user.User{ID: id, Email: "test@example.com", Plan: "free"}, nil
|
||||
},
|
||||
}
|
||||
svc := user.NewService(repo)
|
||||
|
||||
u, getError := svc.GetProfile(context.Background(), "user-1")
|
||||
if getError != nil {
|
||||
t.Fatalf("unexpected error: %v", getError)
|
||||
}
|
||||
if u.ID != "user-1" {
|
||||
t.Errorf("expected user-1, got %s", u.ID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetProfile_NotFound(t *testing.T) {
|
||||
repo := &mockUserRepo{
|
||||
getByIDFn: func(ctx context.Context, id string) (*user.User, error) {
|
||||
return nil, fmt.Errorf("not found")
|
||||
},
|
||||
}
|
||||
svc := user.NewService(repo)
|
||||
|
||||
_, getError := svc.GetProfile(context.Background(), "nonexistent")
|
||||
if getError == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_NameOnly(t *testing.T) {
|
||||
repo := &mockUserRepo{
|
||||
updateFn: func(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error) {
|
||||
return &user.User{ID: id, Name: *req.Name, Plan: "free"}, nil
|
||||
},
|
||||
}
|
||||
svc := user.NewService(repo)
|
||||
|
||||
u, updateError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Name: ptrStr("New Name")})
|
||||
if updateError != nil {
|
||||
t.Fatalf("unexpected error: %v", updateError)
|
||||
}
|
||||
if u.Name != "New Name" {
|
||||
t.Errorf("expected 'New Name', got %q", u.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_WithCaloriesRecalculation(t *testing.T) {
|
||||
dob := time.Now().AddDate(-30, 0, 0).Format("2006-01-02")
|
||||
profileReq := user.UpdateProfileRequest{
|
||||
HeightCM: ptrInt(180),
|
||||
WeightKG: ptrFloat(80),
|
||||
DateOfBirth: &dob,
|
||||
Gender: ptrStr("male"),
|
||||
Activity: ptrStr("moderate"),
|
||||
Goal: ptrStr("maintain"),
|
||||
}
|
||||
finalUser := &user.User{
|
||||
ID: "user-1",
|
||||
HeightCM: ptrInt(180),
|
||||
WeightKG: ptrFloat(80),
|
||||
DateOfBirth: &dob,
|
||||
Gender: ptrStr("male"),
|
||||
Activity: ptrStr("moderate"),
|
||||
Goal: ptrStr("maintain"),
|
||||
DailyCalories: ptrInt(2759),
|
||||
Plan: "free",
|
||||
}
|
||||
|
||||
repo := &mockUserRepo{
|
||||
// Service calls GetByID first to merge current values for calorie calculation
|
||||
getByIDFn: func(ctx context.Context, id string) (*user.User, error) {
|
||||
return &user.User{ID: id, Plan: "free"}, nil
|
||||
},
|
||||
updateInTxFn: func(ctx context.Context, id string, pReq user.UpdateProfileRequest, cReq *user.UpdateProfileRequest) (*user.User, error) {
|
||||
if cReq == nil {
|
||||
t.Error("expected caloriesReq to be non-nil")
|
||||
} else if *cReq.DailyCalories != 2759 {
|
||||
t.Errorf("expected calories 2759, got %d", *cReq.DailyCalories)
|
||||
}
|
||||
return finalUser, nil
|
||||
},
|
||||
}
|
||||
svc := user.NewService(repo)
|
||||
|
||||
u, updateError := svc.UpdateProfile(context.Background(), "user-1", profileReq)
|
||||
if updateError != nil {
|
||||
t.Fatalf("unexpected error: %v", updateError)
|
||||
}
|
||||
if u.DailyCalories == nil {
|
||||
t.Fatal("expected daily_calories to be set")
|
||||
}
|
||||
if *u.DailyCalories != 2759 {
|
||||
t.Errorf("expected 2759, got %d", *u.DailyCalories)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidHeight_TooLow(t *testing.T) {
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{HeightCM: ptrInt(50)})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidHeight_TooHigh(t *testing.T) {
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{HeightCM: ptrInt(300)})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidWeight_TooLow(t *testing.T) {
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{WeightKG: ptrFloat(10)})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidWeight_TooHigh(t *testing.T) {
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{WeightKG: ptrFloat(400)})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidDOB_TooRecent(t *testing.T) {
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
dob := time.Now().AddDate(-5, 0, 0).Format("2006-01-02")
|
||||
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{DateOfBirth: &dob})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidDOB_TooOld(t *testing.T) {
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
dob := time.Now().AddDate(-150, 0, 0).Format("2006-01-02")
|
||||
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{DateOfBirth: &dob})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidDOB_BadFormat(t *testing.T) {
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
dob := "not-a-date"
|
||||
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{DateOfBirth: &dob})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidGender(t *testing.T) {
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Gender: ptrStr("other")})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidActivity(t *testing.T) {
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Activity: ptrStr("extreme")})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidGoal(t *testing.T) {
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Goal: ptrStr("bulk")})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateProfile_Preferences(t *testing.T) {
|
||||
prefs := json.RawMessage(`{"cuisines":["russian"]}`)
|
||||
repo := &mockUserRepo{
|
||||
updateFn: func(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error) {
|
||||
return &user.User{
|
||||
ID: id,
|
||||
Plan: "free",
|
||||
Preferences: *req.Preferences,
|
||||
UpdatedAt: time.Now(),
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
svc := user.NewService(repo)
|
||||
|
||||
u, updateError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Preferences: &prefs})
|
||||
if updateError != nil {
|
||||
t.Fatalf("unexpected error: %v", updateError)
|
||||
}
|
||||
if string(u.Preferences) != `{"cuisines":["russian"]}` {
|
||||
t.Errorf("unexpected preferences: %s", u.Preferences)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user