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:
@@ -1,6 +1,6 @@
|
||||
//go:build integration
|
||||
|
||||
package auth
|
||||
package auth_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/food-ai/backend/internal/auth"
|
||||
"github.com/food-ai/backend/internal/auth/mocks"
|
||||
"github.com/food-ai/backend/internal/middleware"
|
||||
"github.com/food-ai/backend/internal/testutil"
|
||||
@@ -18,20 +19,25 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
// testValidator adapts JWTManager to middleware.AccessTokenValidator for tests.
|
||||
// testRefreshRequest mirrors the unexported handler request type for test marshalling.
|
||||
type testRefreshRequest struct {
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
}
|
||||
|
||||
// testValidator adapts auth.JWTManager to middleware.AccessTokenValidator for tests.
|
||||
type testValidator struct {
|
||||
jm *JWTManager
|
||||
jm *auth.JWTManager
|
||||
}
|
||||
|
||||
func (v *testValidator) ValidateAccessToken(tokenStr string) (*middleware.TokenClaims, error) {
|
||||
claims, err := v.jm.ValidateAccessToken(tokenStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
claims, validateError := v.jm.ValidateAccessToken(tokenStr)
|
||||
if validateError != nil {
|
||||
return nil, validateError
|
||||
}
|
||||
return &middleware.TokenClaims{UserID: claims.UserID, Plan: claims.Plan}, nil
|
||||
}
|
||||
|
||||
func setupIntegrationTest(t *testing.T) (*chi.Mux, *JWTManager) {
|
||||
func setupIntegrationTest(t *testing.T) (*chi.Mux, *auth.JWTManager) {
|
||||
t.Helper()
|
||||
pool := testutil.SetupTestDB(t)
|
||||
|
||||
@@ -41,10 +47,10 @@ func setupIntegrationTest(t *testing.T) (*chi.Mux, *JWTManager) {
|
||||
},
|
||||
}
|
||||
|
||||
jm := NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
repo := user.NewRepository(pool)
|
||||
svc := NewService(verifier, repo, jm)
|
||||
handler := NewHandler(svc)
|
||||
svc := auth.NewService(verifier, repo, jm)
|
||||
handler := auth.NewHandler(svc)
|
||||
|
||||
r := chi.NewRouter()
|
||||
r.Post("/auth/login", handler.Login)
|
||||
@@ -70,7 +76,7 @@ func TestIntegration_Login(t *testing.T) {
|
||||
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
|
||||
var resp LoginResponse
|
||||
var resp auth.LoginResponse
|
||||
json.NewDecoder(rr.Body).Decode(&resp)
|
||||
if resp.AccessToken == "" {
|
||||
t.Error("expected non-empty access token")
|
||||
@@ -120,11 +126,11 @@ func TestIntegration_Refresh(t *testing.T) {
|
||||
loginRR := httptest.NewRecorder()
|
||||
router.ServeHTTP(loginRR, loginReq)
|
||||
|
||||
var loginResp LoginResponse
|
||||
var loginResp auth.LoginResponse
|
||||
json.NewDecoder(loginRR.Body).Decode(&loginResp)
|
||||
|
||||
// Then refresh
|
||||
refreshBody, _ := json.Marshal(refreshRequest{RefreshToken: loginResp.RefreshToken})
|
||||
refreshBody, _ := json.Marshal(testRefreshRequest{RefreshToken: loginResp.RefreshToken})
|
||||
refreshReq := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBuffer(refreshBody))
|
||||
refreshReq.Header.Set("Content-Type", "application/json")
|
||||
refreshRR := httptest.NewRecorder()
|
||||
@@ -134,7 +140,7 @@ func TestIntegration_Refresh(t *testing.T) {
|
||||
t.Fatalf("expected 200, got %d: %s", refreshRR.Code, refreshRR.Body.String())
|
||||
}
|
||||
|
||||
var resp RefreshResponse
|
||||
var resp auth.RefreshResponse
|
||||
json.NewDecoder(refreshRR.Body).Decode(&resp)
|
||||
if resp.AccessToken == "" {
|
||||
t.Error("expected non-empty access token")
|
||||
@@ -182,7 +188,7 @@ func TestIntegration_Logout(t *testing.T) {
|
||||
loginRR := httptest.NewRecorder()
|
||||
router.ServeHTTP(loginRR, loginReq)
|
||||
|
||||
var loginResp LoginResponse
|
||||
var loginResp auth.LoginResponse
|
||||
json.NewDecoder(loginRR.Body).Decode(&loginResp)
|
||||
|
||||
// Logout
|
||||
@@ -218,7 +224,7 @@ func TestIntegration_RefreshAfterLogout(t *testing.T) {
|
||||
loginRR := httptest.NewRecorder()
|
||||
router.ServeHTTP(loginRR, loginReq)
|
||||
|
||||
var loginResp LoginResponse
|
||||
var loginResp auth.LoginResponse
|
||||
json.NewDecoder(loginRR.Body).Decode(&loginResp)
|
||||
|
||||
// Logout
|
||||
@@ -228,7 +234,7 @@ func TestIntegration_RefreshAfterLogout(t *testing.T) {
|
||||
router.ServeHTTP(logoutRR, logoutReq)
|
||||
|
||||
// Try to refresh with old token
|
||||
refreshBody, _ := json.Marshal(refreshRequest{RefreshToken: loginResp.RefreshToken})
|
||||
refreshBody, _ := json.Marshal(testRefreshRequest{RefreshToken: loginResp.RefreshToken})
|
||||
refreshReq := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBuffer(refreshBody))
|
||||
refreshReq.Header.Set("Content-Type", "application/json")
|
||||
refreshRR := httptest.NewRecorder()
|
||||
@@ -249,12 +255,12 @@ func TestIntegration_OldRefreshTokenInvalid(t *testing.T) {
|
||||
loginRR := httptest.NewRecorder()
|
||||
router.ServeHTTP(loginRR, loginReq)
|
||||
|
||||
var loginResp LoginResponse
|
||||
var loginResp auth.LoginResponse
|
||||
json.NewDecoder(loginRR.Body).Decode(&loginResp)
|
||||
oldRefreshToken := loginResp.RefreshToken
|
||||
|
||||
// Refresh (rotates token)
|
||||
refreshBody, _ := json.Marshal(refreshRequest{RefreshToken: oldRefreshToken})
|
||||
refreshBody, _ := json.Marshal(testRefreshRequest{RefreshToken: oldRefreshToken})
|
||||
refreshReq := httptest.NewRequest("POST", "/auth/refresh", bytes.NewBuffer(refreshBody))
|
||||
refreshReq.Header.Set("Content-Type", "application/json")
|
||||
refreshRR := httptest.NewRecorder()
|
||||
@@ -1,16 +1,18 @@
|
||||
package auth
|
||||
package auth_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/food-ai/backend/internal/auth"
|
||||
)
|
||||
|
||||
func TestGenerateAccessToken(t *testing.T) {
|
||||
jm := NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
|
||||
token, err := jm.GenerateAccessToken("user-123", "free")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
token, tokenError := jm.GenerateAccessToken("user-123", "free")
|
||||
if tokenError != nil {
|
||||
t.Fatalf("unexpected error: %v", tokenError)
|
||||
}
|
||||
if token == "" {
|
||||
t.Fatal("expected non-empty token")
|
||||
@@ -18,12 +20,12 @@ func TestGenerateAccessToken(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestValidateAccessToken_Valid(t *testing.T) {
|
||||
jm := NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
|
||||
token, _ := jm.GenerateAccessToken("user-123", "free")
|
||||
claims, err := jm.ValidateAccessToken(token)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
claims, validateError := jm.ValidateAccessToken(token)
|
||||
if validateError != nil {
|
||||
t.Fatalf("unexpected error: %v", validateError)
|
||||
}
|
||||
if claims.UserID != "user-123" {
|
||||
t.Errorf("expected user_id 'user-123', got %q", claims.UserID)
|
||||
@@ -34,37 +36,37 @@ func TestValidateAccessToken_Valid(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestValidateAccessToken_Expired(t *testing.T) {
|
||||
jm := NewJWTManager("test-secret", -1*time.Second, 720*time.Hour)
|
||||
jm := auth.NewJWTManager("test-secret", -1*time.Second, 720*time.Hour)
|
||||
|
||||
token, _ := jm.GenerateAccessToken("user-123", "free")
|
||||
_, err := jm.ValidateAccessToken(token)
|
||||
if err == nil {
|
||||
_, validateError := jm.ValidateAccessToken(token)
|
||||
if validateError == nil {
|
||||
t.Fatal("expected error for expired token")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAccessToken_WrongSecret(t *testing.T) {
|
||||
jm1 := NewJWTManager("secret-1", 15*time.Minute, 720*time.Hour)
|
||||
jm2 := NewJWTManager("secret-2", 15*time.Minute, 720*time.Hour)
|
||||
jm1 := auth.NewJWTManager("secret-1", 15*time.Minute, 720*time.Hour)
|
||||
jm2 := auth.NewJWTManager("secret-2", 15*time.Minute, 720*time.Hour)
|
||||
|
||||
token, _ := jm1.GenerateAccessToken("user-123", "free")
|
||||
_, err := jm2.ValidateAccessToken(token)
|
||||
if err == nil {
|
||||
_, validateError := jm2.ValidateAccessToken(token)
|
||||
if validateError == nil {
|
||||
t.Fatal("expected error for wrong secret")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAccessToken_InvalidToken(t *testing.T) {
|
||||
jm := NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
|
||||
_, err := jm.ValidateAccessToken("invalid-token")
|
||||
if err == nil {
|
||||
_, validateError := jm.ValidateAccessToken("invalid-token")
|
||||
if validateError == nil {
|
||||
t.Fatal("expected error for invalid token")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateRefreshToken(t *testing.T) {
|
||||
jm := NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
|
||||
token, expiresAt := jm.GenerateRefreshToken()
|
||||
if token == "" {
|
||||
@@ -76,7 +78,7 @@ func TestGenerateRefreshToken(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGenerateRefreshToken_Unique(t *testing.T) {
|
||||
jm := NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
|
||||
token1, _ := jm.GenerateRefreshToken()
|
||||
token2, _ := jm.GenerateRefreshToken()
|
||||
@@ -1,4 +1,4 @@
|
||||
package auth
|
||||
package auth_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -6,14 +6,15 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/food-ai/backend/internal/auth"
|
||||
"github.com/food-ai/backend/internal/auth/mocks"
|
||||
"github.com/food-ai/backend/internal/user"
|
||||
umocks "github.com/food-ai/backend/internal/user/mocks"
|
||||
)
|
||||
|
||||
func newTestService(verifier *mocks.MockTokenVerifier, repo *umocks.MockUserRepository) *Service {
|
||||
jm := NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
return NewService(verifier, repo, jm)
|
||||
func newTestService(verifier *mocks.MockTokenVerifier, repo *umocks.MockUserRepository) *auth.Service {
|
||||
jm := auth.NewJWTManager("test-secret", 15*time.Minute, 720*time.Hour)
|
||||
return auth.NewService(verifier, repo, jm)
|
||||
}
|
||||
|
||||
func TestLogin_Success(t *testing.T) {
|
||||
@@ -32,9 +33,9 @@ func TestLogin_Success(t *testing.T) {
|
||||
}
|
||||
|
||||
svc := newTestService(verifier, repo)
|
||||
resp, err := svc.Login(context.Background(), "firebase-token")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
resp, loginError := svc.Login(context.Background(), "firebase-token")
|
||||
if loginError != nil {
|
||||
t.Fatalf("unexpected error: %v", loginError)
|
||||
}
|
||||
if resp.AccessToken == "" {
|
||||
t.Error("expected non-empty access token")
|
||||
@@ -56,8 +57,8 @@ func TestLogin_InvalidFirebaseToken(t *testing.T) {
|
||||
repo := &umocks.MockUserRepository{}
|
||||
|
||||
svc := newTestService(verifier, repo)
|
||||
_, err := svc.Login(context.Background(), "bad-token")
|
||||
if err == nil {
|
||||
_, loginError := svc.Login(context.Background(), "bad-token")
|
||||
if loginError == nil {
|
||||
t.Fatal("expected error for invalid firebase token")
|
||||
}
|
||||
}
|
||||
@@ -75,8 +76,8 @@ func TestLogin_UpsertError(t *testing.T) {
|
||||
}
|
||||
|
||||
svc := newTestService(verifier, repo)
|
||||
_, err := svc.Login(context.Background(), "token")
|
||||
if err == nil {
|
||||
_, loginError := svc.Login(context.Background(), "token")
|
||||
if loginError == nil {
|
||||
t.Fatal("expected error for upsert failure")
|
||||
}
|
||||
}
|
||||
@@ -97,8 +98,8 @@ func TestLogin_SetRefreshTokenError(t *testing.T) {
|
||||
}
|
||||
|
||||
svc := newTestService(verifier, repo)
|
||||
_, err := svc.Login(context.Background(), "token")
|
||||
if err == nil {
|
||||
_, loginError := svc.Login(context.Background(), "token")
|
||||
if loginError == nil {
|
||||
t.Fatal("expected error for set refresh token failure")
|
||||
}
|
||||
}
|
||||
@@ -115,9 +116,9 @@ func TestRefresh_Success(t *testing.T) {
|
||||
verifier := &mocks.MockTokenVerifier{}
|
||||
|
||||
svc := newTestService(verifier, repo)
|
||||
resp, err := svc.Refresh(context.Background(), "valid-refresh-token")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
resp, refreshError := svc.Refresh(context.Background(), "valid-refresh-token")
|
||||
if refreshError != nil {
|
||||
t.Fatalf("unexpected error: %v", refreshError)
|
||||
}
|
||||
if resp.AccessToken == "" {
|
||||
t.Error("expected non-empty access token")
|
||||
@@ -136,8 +137,8 @@ func TestRefresh_InvalidToken(t *testing.T) {
|
||||
verifier := &mocks.MockTokenVerifier{}
|
||||
|
||||
svc := newTestService(verifier, repo)
|
||||
_, err := svc.Refresh(context.Background(), "bad-token")
|
||||
if err == nil {
|
||||
_, refreshError := svc.Refresh(context.Background(), "bad-token")
|
||||
if refreshError == nil {
|
||||
t.Fatal("expected error for invalid refresh token")
|
||||
}
|
||||
}
|
||||
@@ -154,8 +155,8 @@ func TestRefresh_SetRefreshTokenError(t *testing.T) {
|
||||
verifier := &mocks.MockTokenVerifier{}
|
||||
|
||||
svc := newTestService(verifier, repo)
|
||||
_, err := svc.Refresh(context.Background(), "valid-token")
|
||||
if err == nil {
|
||||
_, refreshError := svc.Refresh(context.Background(), "valid-token")
|
||||
if refreshError == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
@@ -169,9 +170,9 @@ func TestLogout_Success(t *testing.T) {
|
||||
verifier := &mocks.MockTokenVerifier{}
|
||||
|
||||
svc := newTestService(verifier, repo)
|
||||
err := svc.Logout(context.Background(), "user-1")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
logoutError := svc.Logout(context.Background(), "user-1")
|
||||
if logoutError != nil {
|
||||
t.Fatalf("unexpected error: %v", logoutError)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,8 +185,8 @@ func TestLogout_Error(t *testing.T) {
|
||||
verifier := &mocks.MockTokenVerifier{}
|
||||
|
||||
svc := newTestService(verifier, repo)
|
||||
err := svc.Logout(context.Background(), "user-1")
|
||||
if err == nil {
|
||||
logoutError := svc.Logout(context.Background(), "user-1")
|
||||
if logoutError == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
}
|
||||
@@ -1,34 +1,35 @@
|
||||
//go:build integration
|
||||
|
||||
package ingredient
|
||||
package ingredient_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/food-ai/backend/internal/ingredient"
|
||||
"github.com/food-ai/backend/internal/locale"
|
||||
"github.com/food-ai/backend/internal/testutil"
|
||||
)
|
||||
|
||||
func TestIngredientRepository_Upsert_Insert(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := ingredient.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
cat := "produce"
|
||||
unit := "g"
|
||||
cal := 52.0
|
||||
|
||||
m := &IngredientMapping{
|
||||
mapping := &ingredient.IngredientMapping{
|
||||
CanonicalName: "apple",
|
||||
Category: &cat,
|
||||
DefaultUnit: &unit,
|
||||
CaloriesPer100g: &cal,
|
||||
}
|
||||
|
||||
got, err := repo.Upsert(ctx, m)
|
||||
if err != nil {
|
||||
t.Fatalf("upsert: %v", err)
|
||||
got, upsertError := repo.Upsert(ctx, mapping)
|
||||
if upsertError != nil {
|
||||
t.Fatalf("upsert: %v", upsertError)
|
||||
}
|
||||
if got.ID == "" {
|
||||
t.Error("expected non-empty ID")
|
||||
@@ -43,32 +44,32 @@ func TestIngredientRepository_Upsert_Insert(t *testing.T) {
|
||||
|
||||
func TestIngredientRepository_Upsert_ConflictUpdates(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := ingredient.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
cat := "produce"
|
||||
unit := "g"
|
||||
|
||||
first := &IngredientMapping{
|
||||
first := &ingredient.IngredientMapping{
|
||||
CanonicalName: "banana",
|
||||
Category: &cat,
|
||||
DefaultUnit: &unit,
|
||||
}
|
||||
got1, err := repo.Upsert(ctx, first)
|
||||
if err != nil {
|
||||
t.Fatalf("first upsert: %v", err)
|
||||
got1, firstUpsertError := repo.Upsert(ctx, first)
|
||||
if firstUpsertError != nil {
|
||||
t.Fatalf("first upsert: %v", firstUpsertError)
|
||||
}
|
||||
|
||||
cal := 89.0
|
||||
second := &IngredientMapping{
|
||||
second := &ingredient.IngredientMapping{
|
||||
CanonicalName: "banana",
|
||||
Category: &cat,
|
||||
DefaultUnit: &unit,
|
||||
CaloriesPer100g: &cal,
|
||||
}
|
||||
got2, err := repo.Upsert(ctx, second)
|
||||
if err != nil {
|
||||
t.Fatalf("second upsert: %v", err)
|
||||
got2, secondUpsertError := repo.Upsert(ctx, second)
|
||||
if secondUpsertError != nil {
|
||||
t.Fatalf("second upsert: %v", secondUpsertError)
|
||||
}
|
||||
|
||||
if got1.ID != got2.ID {
|
||||
@@ -84,24 +85,24 @@ func TestIngredientRepository_Upsert_ConflictUpdates(t *testing.T) {
|
||||
|
||||
func TestIngredientRepository_GetByID_Found(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := ingredient.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
cat := "dairy"
|
||||
unit := "g"
|
||||
|
||||
saved, err := repo.Upsert(ctx, &IngredientMapping{
|
||||
saved, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
|
||||
CanonicalName: "cheese",
|
||||
Category: &cat,
|
||||
DefaultUnit: &unit,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("upsert: %v", err)
|
||||
if upsertError != nil {
|
||||
t.Fatalf("upsert: %v", upsertError)
|
||||
}
|
||||
|
||||
got, err := repo.GetByID(ctx, saved.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
got, getError := repo.GetByID(ctx, saved.ID)
|
||||
if getError != nil {
|
||||
t.Fatalf("get: %v", getError)
|
||||
}
|
||||
if got == nil {
|
||||
t.Fatal("expected non-nil result")
|
||||
@@ -113,12 +114,12 @@ func TestIngredientRepository_GetByID_Found(t *testing.T) {
|
||||
|
||||
func TestIngredientRepository_GetByID_NotFound(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := ingredient.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
got, err := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
got, getError := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
|
||||
if getError != nil {
|
||||
t.Fatalf("unexpected error: %v", getError)
|
||||
}
|
||||
if got != nil {
|
||||
t.Error("expected nil result for missing ID")
|
||||
@@ -127,7 +128,7 @@ func TestIngredientRepository_GetByID_NotFound(t *testing.T) {
|
||||
|
||||
func TestIngredientRepository_ListMissingTranslation(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := ingredient.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
cat := "produce"
|
||||
@@ -135,36 +136,36 @@ func TestIngredientRepository_ListMissingTranslation(t *testing.T) {
|
||||
|
||||
// Insert 3 without any translation.
|
||||
for _, name := range []string{"carrot", "onion", "garlic"} {
|
||||
_, err := repo.Upsert(ctx, &IngredientMapping{
|
||||
_, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
|
||||
CanonicalName: name,
|
||||
Category: &cat,
|
||||
DefaultUnit: &unit,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("upsert %s: %v", name, err)
|
||||
if upsertError != nil {
|
||||
t.Fatalf("upsert %s: %v", name, upsertError)
|
||||
}
|
||||
}
|
||||
|
||||
// Insert 1 and add a translation — should not appear in the result.
|
||||
saved, err := repo.Upsert(ctx, &IngredientMapping{
|
||||
saved, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
|
||||
CanonicalName: "tomato",
|
||||
Category: &cat,
|
||||
DefaultUnit: &unit,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("upsert tomato: %v", err)
|
||||
if upsertError != nil {
|
||||
t.Fatalf("upsert tomato: %v", upsertError)
|
||||
}
|
||||
if err := repo.UpsertTranslation(ctx, saved.ID, "ru", "помидор"); err != nil {
|
||||
t.Fatalf("upsert translation: %v", err)
|
||||
if translationError := repo.UpsertTranslation(ctx, saved.ID, "ru", "помидор"); translationError != nil {
|
||||
t.Fatalf("upsert translation: %v", translationError)
|
||||
}
|
||||
|
||||
missing, err := repo.ListMissingTranslation(ctx, "ru", 10, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("list missing translation: %v", err)
|
||||
missing, listError := repo.ListMissingTranslation(ctx, "ru", 10, 0)
|
||||
if listError != nil {
|
||||
t.Fatalf("list missing translation: %v", listError)
|
||||
}
|
||||
|
||||
for _, m := range missing {
|
||||
if m.CanonicalName == "tomato" {
|
||||
for _, mapping := range missing {
|
||||
if mapping.CanonicalName == "tomato" {
|
||||
t.Error("translated ingredient should not appear in ListMissingTranslation")
|
||||
}
|
||||
}
|
||||
@@ -175,78 +176,78 @@ func TestIngredientRepository_ListMissingTranslation(t *testing.T) {
|
||||
|
||||
func TestIngredientRepository_UpsertTranslation(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := ingredient.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
cat := "meat"
|
||||
unit := "g"
|
||||
|
||||
saved, err := repo.Upsert(ctx, &IngredientMapping{
|
||||
saved, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
|
||||
CanonicalName: "chicken_breast",
|
||||
Category: &cat,
|
||||
DefaultUnit: &unit,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("upsert: %v", err)
|
||||
if upsertError != nil {
|
||||
t.Fatalf("upsert: %v", upsertError)
|
||||
}
|
||||
|
||||
if err := repo.UpsertTranslation(ctx, saved.ID, "ru", "куриная грудка"); err != nil {
|
||||
t.Fatalf("upsert translation: %v", err)
|
||||
if translationError := repo.UpsertTranslation(ctx, saved.ID, "ru", "куриная грудка"); translationError != nil {
|
||||
t.Fatalf("upsert translation: %v", translationError)
|
||||
}
|
||||
|
||||
// Retrieve with Russian context — CanonicalName should be the Russian name.
|
||||
ruCtx := locale.WithLang(ctx, "ru")
|
||||
got, err := repo.GetByID(ruCtx, saved.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get by id: %v", err)
|
||||
russianContext := locale.WithLang(ctx, "ru")
|
||||
got, getError := repo.GetByID(russianContext, saved.ID)
|
||||
if getError != nil {
|
||||
t.Fatalf("get by id: %v", getError)
|
||||
}
|
||||
if got.CanonicalName != "куриная грудка" {
|
||||
t.Errorf("expected CanonicalName='куриная грудка', got %q", got.CanonicalName)
|
||||
}
|
||||
|
||||
// Retrieve with English context (default) — CanonicalName should be the English name.
|
||||
enCtx := locale.WithLang(ctx, "en")
|
||||
gotEn, err := repo.GetByID(enCtx, saved.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get by id (en): %v", err)
|
||||
englishContext := locale.WithLang(ctx, "en")
|
||||
gotEnglish, getEnglishError := repo.GetByID(englishContext, saved.ID)
|
||||
if getEnglishError != nil {
|
||||
t.Fatalf("get by id (en): %v", getEnglishError)
|
||||
}
|
||||
if gotEn.CanonicalName != "chicken_breast" {
|
||||
t.Errorf("expected English CanonicalName='chicken_breast', got %q", gotEn.CanonicalName)
|
||||
if gotEnglish.CanonicalName != "chicken_breast" {
|
||||
t.Errorf("expected English CanonicalName='chicken_breast', got %q", gotEnglish.CanonicalName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIngredientRepository_UpsertAliases(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := ingredient.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
cat := "produce"
|
||||
unit := "g"
|
||||
|
||||
saved, err := repo.Upsert(ctx, &IngredientMapping{
|
||||
saved, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
|
||||
CanonicalName: "apple_test_aliases",
|
||||
Category: &cat,
|
||||
DefaultUnit: &unit,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("upsert: %v", err)
|
||||
if upsertError != nil {
|
||||
t.Fatalf("upsert: %v", upsertError)
|
||||
}
|
||||
|
||||
aliases := []string{"apple", "apples", "red apple"}
|
||||
if err := repo.UpsertAliases(ctx, saved.ID, "en", aliases); err != nil {
|
||||
t.Fatalf("upsert aliases: %v", err)
|
||||
if aliasError := repo.UpsertAliases(ctx, saved.ID, "en", aliases); aliasError != nil {
|
||||
t.Fatalf("upsert aliases: %v", aliasError)
|
||||
}
|
||||
|
||||
// Idempotent — second call should not fail.
|
||||
if err := repo.UpsertAliases(ctx, saved.ID, "en", aliases); err != nil {
|
||||
t.Fatalf("second upsert aliases: %v", err)
|
||||
if aliasError := repo.UpsertAliases(ctx, saved.ID, "en", aliases); aliasError != nil {
|
||||
t.Fatalf("second upsert aliases: %v", aliasError)
|
||||
}
|
||||
|
||||
// Retrieve with English context — aliases should appear.
|
||||
enCtx := locale.WithLang(ctx, "en")
|
||||
got, err := repo.GetByID(enCtx, saved.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get by id: %v", err)
|
||||
englishContext := locale.WithLang(ctx, "en")
|
||||
got, getError := repo.GetByID(englishContext, saved.ID)
|
||||
if getError != nil {
|
||||
t.Fatalf("get by id: %v", getError)
|
||||
}
|
||||
if string(got.Aliases) == "[]" || string(got.Aliases) == "null" {
|
||||
t.Errorf("expected non-empty aliases, got %s", got.Aliases)
|
||||
@@ -255,42 +256,42 @@ func TestIngredientRepository_UpsertAliases(t *testing.T) {
|
||||
|
||||
func TestIngredientRepository_UpsertCategoryTranslation(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := ingredient.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
// Upsert an ingredient with a known category.
|
||||
cat := "dairy"
|
||||
unit := "g"
|
||||
saved, err := repo.Upsert(ctx, &IngredientMapping{
|
||||
saved, upsertError := repo.Upsert(ctx, &ingredient.IngredientMapping{
|
||||
CanonicalName: "milk_test_category",
|
||||
Category: &cat,
|
||||
DefaultUnit: &unit,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("upsert: %v", err)
|
||||
if upsertError != nil {
|
||||
t.Fatalf("upsert: %v", upsertError)
|
||||
}
|
||||
|
||||
// The migration already inserted Russian translations for known categories.
|
||||
// Retrieve with Russian context — CategoryName should be set.
|
||||
ruCtx := locale.WithLang(ctx, "ru")
|
||||
got, err := repo.GetByID(ruCtx, saved.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get by id: %v", err)
|
||||
russianContext := locale.WithLang(ctx, "ru")
|
||||
got, getError := repo.GetByID(russianContext, saved.ID)
|
||||
if getError != nil {
|
||||
t.Fatalf("get by id: %v", getError)
|
||||
}
|
||||
if got.CategoryName == nil || *got.CategoryName == "" {
|
||||
t.Error("expected non-empty CategoryName for 'dairy' in Russian")
|
||||
}
|
||||
|
||||
// Upsert a new translation and verify it is returned.
|
||||
if err := repo.UpsertCategoryTranslation(ctx, "dairy", "de", "Milchprodukte"); err != nil {
|
||||
t.Fatalf("upsert category translation: %v", err)
|
||||
if translationError := repo.UpsertCategoryTranslation(ctx, "dairy", "de", "Milchprodukte"); translationError != nil {
|
||||
t.Fatalf("upsert category translation: %v", translationError)
|
||||
}
|
||||
deCtx := locale.WithLang(ctx, "de")
|
||||
gotDe, err := repo.GetByID(deCtx, saved.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("get by id (de): %v", err)
|
||||
germanContext := locale.WithLang(ctx, "de")
|
||||
gotGerman, getGermanError := repo.GetByID(germanContext, saved.ID)
|
||||
if getGermanError != nil {
|
||||
t.Fatalf("get by id (de): %v", getGermanError)
|
||||
}
|
||||
if gotDe.CategoryName == nil || *gotDe.CategoryName != "Milchprodukte" {
|
||||
t.Errorf("expected CategoryName='Milchprodukte', got %v", gotDe.CategoryName)
|
||||
if gotGerman.CategoryName == nil || *gotGerman.CategoryName != "Milchprodukte" {
|
||||
t.Errorf("expected CategoryName='Milchprodukte', got %v", gotGerman.CategoryName)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package middleware
|
||||
package middleware_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/food-ai/backend/internal/middleware"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
@@ -27,36 +28,36 @@ func generateTestToken(secret string, userID, plan string, duration time.Duratio
|
||||
},
|
||||
}
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
s, _ := token.SignedString([]byte(secret))
|
||||
return s
|
||||
tokenString, _ := token.SignedString([]byte(secret))
|
||||
return tokenString
|
||||
}
|
||||
|
||||
// testValidator implements AccessTokenValidator for tests.
|
||||
// testAccessValidator implements middleware.AccessTokenValidator for tests.
|
||||
type testAccessValidator struct {
|
||||
secret string
|
||||
}
|
||||
|
||||
func (v *testAccessValidator) ValidateAccessToken(tokenStr string) (*TokenClaims, error) {
|
||||
token, err := jwt.ParseWithClaims(tokenStr, &testJWTClaims{}, func(t *jwt.Token) (interface{}, error) {
|
||||
func (v *testAccessValidator) ValidateAccessToken(tokenStr string) (*middleware.TokenClaims, error) {
|
||||
token, parseError := jwt.ParseWithClaims(tokenStr, &testJWTClaims{}, func(t *jwt.Token) (interface{}, error) {
|
||||
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method")
|
||||
}
|
||||
return []byte(v.secret), nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if parseError != nil {
|
||||
return nil, parseError
|
||||
}
|
||||
claims, ok := token.Claims.(*testJWTClaims)
|
||||
if !ok || !token.Valid {
|
||||
return nil, fmt.Errorf("invalid token")
|
||||
}
|
||||
return &TokenClaims{UserID: claims.UserID, Plan: claims.Plan}, nil
|
||||
return &middleware.TokenClaims{UserID: claims.UserID, Plan: claims.Plan}, nil
|
||||
}
|
||||
|
||||
// failingValidator always returns an error.
|
||||
type failingValidator struct{}
|
||||
|
||||
func (v *failingValidator) ValidateAccessToken(tokenStr string) (*TokenClaims, error) {
|
||||
func (v *failingValidator) ValidateAccessToken(tokenStr string) (*middleware.TokenClaims, error) {
|
||||
return nil, fmt.Errorf("invalid token")
|
||||
}
|
||||
|
||||
@@ -64,12 +65,12 @@ func TestAuth_ValidToken(t *testing.T) {
|
||||
validator := &testAccessValidator{secret: "test-secret"}
|
||||
token := generateTestToken("test-secret", "user-1", "free", 15*time.Minute)
|
||||
|
||||
handler := Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
userID := UserIDFromCtx(r.Context())
|
||||
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.UserIDFromCtx(r.Context())
|
||||
if userID != "user-1" {
|
||||
t.Errorf("expected user-1, got %s", userID)
|
||||
}
|
||||
plan := UserPlanFromCtx(r.Context())
|
||||
plan := middleware.UserPlanFromCtx(r.Context())
|
||||
if plan != "free" {
|
||||
t.Errorf("expected free, got %s", plan)
|
||||
}
|
||||
@@ -89,7 +90,7 @@ func TestAuth_ValidToken(t *testing.T) {
|
||||
func TestAuth_MissingHeader(t *testing.T) {
|
||||
validator := &testAccessValidator{secret: "test-secret"}
|
||||
|
||||
handler := Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Error("handler should not be called")
|
||||
}))
|
||||
|
||||
@@ -105,7 +106,7 @@ func TestAuth_MissingHeader(t *testing.T) {
|
||||
func TestAuth_InvalidBearerFormat(t *testing.T) {
|
||||
validator := &testAccessValidator{secret: "test-secret"}
|
||||
|
||||
handler := Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Error("handler should not be called")
|
||||
}))
|
||||
|
||||
@@ -123,7 +124,7 @@ func TestAuth_ExpiredToken(t *testing.T) {
|
||||
validator := &testAccessValidator{secret: "test-secret"}
|
||||
token := generateTestToken("test-secret", "user-1", "free", -1*time.Second)
|
||||
|
||||
handler := Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Error("handler should not be called")
|
||||
}))
|
||||
|
||||
@@ -140,7 +141,7 @@ func TestAuth_ExpiredToken(t *testing.T) {
|
||||
func TestAuth_InvalidToken(t *testing.T) {
|
||||
validator := &testAccessValidator{secret: "test-secret"}
|
||||
|
||||
handler := Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Error("handler should not be called")
|
||||
}))
|
||||
|
||||
@@ -158,8 +159,8 @@ func TestAuth_PaidPlan(t *testing.T) {
|
||||
validator := &testAccessValidator{secret: "test-secret"}
|
||||
token := generateTestToken("test-secret", "user-1", "paid", 15*time.Minute)
|
||||
|
||||
handler := Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
plan := UserPlanFromCtx(r.Context())
|
||||
handler := middleware.Auth(validator)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
plan := middleware.UserPlanFromCtx(r.Context())
|
||||
if plan != "paid" {
|
||||
t.Errorf("expected paid, got %s", plan)
|
||||
}
|
||||
@@ -177,7 +178,7 @@ func TestAuth_PaidPlan(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestAuth_EmptyBearer(t *testing.T) {
|
||||
handler := Auth(&failingValidator{})(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
handler := middleware.Auth(&failingValidator{})(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
t.Error("handler should not be called")
|
||||
}))
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package middleware
|
||||
package middleware_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/food-ai/backend/internal/middleware"
|
||||
)
|
||||
|
||||
func TestRecovery_NoPanic(t *testing.T) {
|
||||
handler := Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
handler := middleware.Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
@@ -21,7 +23,7 @@ func TestRecovery_NoPanic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRecovery_CatchesPanic(t *testing.T) {
|
||||
handler := Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
handler := middleware.Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
panic("test panic")
|
||||
}))
|
||||
|
||||
@@ -35,7 +37,7 @@ func TestRecovery_CatchesPanic(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRecovery_CatchesPanicWithError(t *testing.T) {
|
||||
handler := Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
handler := middleware.Recovery(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
panic(42)
|
||||
}))
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
package middleware
|
||||
package middleware_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/food-ai/backend/internal/middleware"
|
||||
)
|
||||
|
||||
func TestRequestID_GeneratesNew(t *testing.T) {
|
||||
handler := RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := RequestIDFromCtx(r.Context())
|
||||
handler := middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := middleware.RequestIDFromCtx(r.Context())
|
||||
if id == "" {
|
||||
t.Error("expected non-empty request ID in context")
|
||||
}
|
||||
@@ -24,8 +26,8 @@ func TestRequestID_GeneratesNew(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRequestID_PreservesExisting(t *testing.T) {
|
||||
handler := RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := RequestIDFromCtx(r.Context())
|
||||
handler := middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := middleware.RequestIDFromCtx(r.Context())
|
||||
if id != "existing-id" {
|
||||
t.Errorf("expected 'existing-id', got %q", id)
|
||||
}
|
||||
@@ -43,8 +45,8 @@ func TestRequestID_PreservesExisting(t *testing.T) {
|
||||
|
||||
func TestRequestID_UniquePerRequest(t *testing.T) {
|
||||
var ids []string
|
||||
handler := RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ids = append(ids, RequestIDFromCtx(r.Context()))
|
||||
handler := middleware.RequestID(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ids = append(ids, middleware.RequestIDFromCtx(r.Context()))
|
||||
}))
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
@@ -1,22 +1,23 @@
|
||||
//go:build integration
|
||||
|
||||
package recipe
|
||||
package recipe_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/food-ai/backend/internal/recipe"
|
||||
"github.com/food-ai/backend/internal/testutil"
|
||||
)
|
||||
|
||||
func TestRecipeRepository_GetByID_NotFound(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := recipe.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
got, err := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
got, getError := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
|
||||
if getError != nil {
|
||||
t.Fatalf("unexpected error: %v", getError)
|
||||
}
|
||||
if got != nil {
|
||||
t.Error("expected nil for non-existent ID")
|
||||
@@ -25,11 +26,11 @@ func TestRecipeRepository_GetByID_NotFound(t *testing.T) {
|
||||
|
||||
func TestRecipeRepository_Count(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := recipe.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := repo.Count(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("count: %v", err)
|
||||
_, countError := repo.Count(ctx)
|
||||
if countError != nil {
|
||||
t.Fatalf("count: %v", countError)
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,30 @@
|
||||
package user
|
||||
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 AgeFromDOB(nil) != nil {
|
||||
if user.AgeFromDOB(nil) != nil {
|
||||
t.Fatal("expected nil for nil input")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgeFromDOB_InvalidFormat(t *testing.T) {
|
||||
s := "not-a-date"
|
||||
if AgeFromDOB(&s) != nil {
|
||||
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 := AgeFromDOB(&dob)
|
||||
age := user.AgeFromDOB(&dob)
|
||||
if age == nil {
|
||||
t.Fatal("expected non-nil result")
|
||||
}
|
||||
@@ -35,7 +37,7 @@ 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)
|
||||
age := user.AgeFromDOB(&dob)
|
||||
if age == nil {
|
||||
t.Fatal("expected non-nil result")
|
||||
}
|
||||
@@ -45,7 +47,7 @@ func TestAgeFromDOB_BeforeBirthday(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCalculateDailyCalories_MaleMaintain(t *testing.T) {
|
||||
cal := CalculateDailyCalories(ptr(180), ptr(80.0), ptr(30), ptr("male"), ptr("moderate"), ptr("maintain"))
|
||||
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")
|
||||
}
|
||||
@@ -57,7 +59,7 @@ func TestCalculateDailyCalories_MaleMaintain(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCalculateDailyCalories_FemaleLose(t *testing.T) {
|
||||
cal := CalculateDailyCalories(ptr(165), ptr(60.0), ptr(25), ptr("female"), ptr("low"), ptr("lose"))
|
||||
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")
|
||||
}
|
||||
@@ -70,7 +72,7 @@ func TestCalculateDailyCalories_FemaleLose(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCalculateDailyCalories_MaleGain(t *testing.T) {
|
||||
cal := CalculateDailyCalories(ptr(175), ptr(70.0), ptr(28), ptr("male"), ptr("high"), ptr("gain"))
|
||||
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")
|
||||
}
|
||||
@@ -83,28 +85,28 @@ func TestCalculateDailyCalories_MaleGain(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCalculateDailyCalories_NilHeight(t *testing.T) {
|
||||
cal := CalculateDailyCalories(nil, ptr(80.0), ptr(30), ptr("male"), ptr("moderate"), ptr("maintain"))
|
||||
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 := CalculateDailyCalories(ptr(180), nil, ptr(30), ptr("male"), ptr("moderate"), ptr("maintain"))
|
||||
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 := CalculateDailyCalories(ptr(180), ptr(80.0), ptr(30), ptr("other"), ptr("moderate"), ptr("maintain"))
|
||||
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 := CalculateDailyCalories(ptr(180), ptr(80.0), ptr(30), ptr("male"), ptr("extreme"), ptr("maintain"))
|
||||
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")
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build integration
|
||||
|
||||
package user
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -9,16 +9,17 @@ import (
|
||||
"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 := NewRepository(pool)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
u, err := repo.UpsertByFirebaseUID(ctx, "fb-123", "test@example.com", "Test User", "https://avatar.url")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
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)
|
||||
@@ -33,13 +34,13 @@ func TestRepository_UpsertByFirebaseUID_Insert(t *testing.T) {
|
||||
|
||||
func TestRepository_UpsertByFirebaseUID_Update(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
_, _ = repo.UpsertByFirebaseUID(ctx, "fb-123", "old@example.com", "Old Name", "")
|
||||
u, err := repo.UpsertByFirebaseUID(ctx, "fb-123", "new@example.com", "New Name", "https://new-avatar.url")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
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)
|
||||
@@ -52,13 +53,13 @@ func TestRepository_UpsertByFirebaseUID_Update(t *testing.T) {
|
||||
|
||||
func TestRepository_GetByID(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-get", "get@example.com", "Get User", "")
|
||||
u, err := repo.GetByID(ctx, created.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
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)
|
||||
@@ -67,25 +68,25 @@ func TestRepository_GetByID(t *testing.T) {
|
||||
|
||||
func TestRepository_GetByID_NotFound(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
_, err := repo.GetByID(ctx, "00000000-0000-0000-0000-000000000000")
|
||||
if err == nil {
|
||||
_, 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 := NewRepository(pool)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-upd", "upd@example.com", "Update User", "")
|
||||
height := 180
|
||||
u, err := repo.Update(ctx, created.ID, UpdateProfileRequest{HeightCM: &height})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
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)
|
||||
@@ -94,7 +95,7 @@ func TestRepository_Update(t *testing.T) {
|
||||
|
||||
func TestRepository_Update_MultipleFields(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-multi", "multi@example.com", "Multi User", "")
|
||||
@@ -104,7 +105,7 @@ func TestRepository_Update_MultipleFields(t *testing.T) {
|
||||
gender := "male"
|
||||
activity := "moderate"
|
||||
goal := "maintain"
|
||||
u, err := repo.Update(ctx, created.ID, UpdateProfileRequest{
|
||||
u, updateError := repo.Update(ctx, created.ID, user.UpdateProfileRequest{
|
||||
HeightCM: &height,
|
||||
WeightKG: &weight,
|
||||
DateOfBirth: &dob,
|
||||
@@ -112,8 +113,8 @@ func TestRepository_Update_MultipleFields(t *testing.T) {
|
||||
Activity: &activity,
|
||||
Goal: &goal,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
if updateError != nil {
|
||||
t.Fatalf("unexpected error: %v", updateError)
|
||||
}
|
||||
if u.HeightCM == nil || *u.HeightCM != 175 {
|
||||
t.Errorf("expected 175, got %v", u.HeightCM)
|
||||
@@ -125,36 +126,36 @@ func TestRepository_Update_MultipleFields(t *testing.T) {
|
||||
|
||||
func TestRepository_Update_Preferences(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
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, err := repo.Update(ctx, created.ID, UpdateProfileRequest{Preferences: &prefs})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
u, updateError := repo.Update(ctx, created.ID, user.UpdateProfileRequest{Preferences: &prefs})
|
||||
if updateError != nil {
|
||||
t.Fatalf("unexpected error: %v", updateError)
|
||||
}
|
||||
var p map[string]interface{}
|
||||
json.Unmarshal(u.Preferences, &p)
|
||||
if p["cuisines"] == nil {
|
||||
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 := NewRepository(pool)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-token", "token@example.com", "Token User", "")
|
||||
err := repo.SetRefreshToken(ctx, created.ID, "refresh-token-123", time.Now().Add(24*time.Hour))
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error setting token: %v", err)
|
||||
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, err := repo.FindByRefreshToken(ctx, "refresh-token-123")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error finding by token: %v", err)
|
||||
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)
|
||||
@@ -163,45 +164,45 @@ func TestRepository_SetAndFindRefreshToken(t *testing.T) {
|
||||
|
||||
func TestRepository_FindByRefreshToken_Expired(t *testing.T) {
|
||||
pool := testutil.SetupTestDB(t)
|
||||
repo := NewRepository(pool)
|
||||
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))
|
||||
|
||||
_, err := repo.FindByRefreshToken(ctx, "expired-token")
|
||||
if err == nil {
|
||||
_, 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 := NewRepository(pool)
|
||||
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))
|
||||
err := repo.ClearRefreshToken(ctx, created.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
clearError := repo.ClearRefreshToken(ctx, created.ID)
|
||||
if clearError != nil {
|
||||
t.Fatalf("unexpected error: %v", clearError)
|
||||
}
|
||||
|
||||
_, err = repo.FindByRefreshToken(ctx, "token-to-clear")
|
||||
if err == nil {
|
||||
_, 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 := NewRepository(pool)
|
||||
repo := user.NewRepository(pool)
|
||||
ctx := context.Background()
|
||||
|
||||
created, _ := repo.UpsertByFirebaseUID(ctx, "fb-noop", "noop@example.com", "Noop User", "")
|
||||
u, err := repo.Update(ctx, created.ID, UpdateProfileRequest{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
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)
|
||||
@@ -1,4 +1,4 @@
|
||||
package user
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -6,39 +6,41 @@ import (
|
||||
"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-package mock to avoid import cycles.
|
||||
// mockUserRepo is an in-test mock implementing user.UserRepository.
|
||||
type mockUserRepo struct {
|
||||
getByIDFn func(ctx context.Context, id string) (*User, error)
|
||||
updateFn func(ctx context.Context, id string, req UpdateProfileRequest) (*User, error)
|
||||
updateInTxFn func(ctx context.Context, id string, profileReq UpdateProfileRequest, caloriesReq *UpdateProfileRequest) (*User, error)
|
||||
upsertByFirebaseUIDFn func(ctx context.Context, uid, email, name, avatarURL string) (*User, error)
|
||||
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, 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, 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, error) {
|
||||
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 UpdateProfileRequest) (*User, error) {
|
||||
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 UpdateProfileRequest, caloriesReq *UpdateProfileRequest) (*User, error) {
|
||||
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, error) {
|
||||
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 {
|
||||
@@ -47,15 +49,15 @@ func (m *mockUserRepo) ClearRefreshToken(ctx context.Context, id string) error {
|
||||
|
||||
func TestGetProfile_Success(t *testing.T) {
|
||||
repo := &mockUserRepo{
|
||||
getByIDFn: func(ctx context.Context, id string) (*User, error) {
|
||||
return &User{ID: id, Email: "test@example.com", Plan: "free"}, nil
|
||||
getByIDFn: func(ctx context.Context, id string) (*user.User, error) {
|
||||
return &user.User{ID: id, Email: "test@example.com", Plan: "free"}, nil
|
||||
},
|
||||
}
|
||||
svc := NewService(repo)
|
||||
svc := user.NewService(repo)
|
||||
|
||||
u, err := svc.GetProfile(context.Background(), "user-1")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
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)
|
||||
@@ -64,29 +66,29 @@ func TestGetProfile_Success(t *testing.T) {
|
||||
|
||||
func TestGetProfile_NotFound(t *testing.T) {
|
||||
repo := &mockUserRepo{
|
||||
getByIDFn: func(ctx context.Context, id string) (*User, error) {
|
||||
getByIDFn: func(ctx context.Context, id string) (*user.User, error) {
|
||||
return nil, fmt.Errorf("not found")
|
||||
},
|
||||
}
|
||||
svc := NewService(repo)
|
||||
svc := user.NewService(repo)
|
||||
|
||||
_, err := svc.GetProfile(context.Background(), "nonexistent")
|
||||
if err == nil {
|
||||
_, 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 UpdateProfileRequest) (*User, error) {
|
||||
return &User{ID: id, Name: *req.Name, Plan: "free"}, nil
|
||||
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 := NewService(repo)
|
||||
svc := user.NewService(repo)
|
||||
|
||||
u, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Name: ptrStr("New Name")})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
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)
|
||||
@@ -95,7 +97,7 @@ func TestUpdateProfile_NameOnly(t *testing.T) {
|
||||
|
||||
func TestUpdateProfile_WithCaloriesRecalculation(t *testing.T) {
|
||||
dob := time.Now().AddDate(-30, 0, 0).Format("2006-01-02")
|
||||
profileReq := UpdateProfileRequest{
|
||||
profileReq := user.UpdateProfileRequest{
|
||||
HeightCM: ptrInt(180),
|
||||
WeightKG: ptrFloat(80),
|
||||
DateOfBirth: &dob,
|
||||
@@ -103,7 +105,7 @@ func TestUpdateProfile_WithCaloriesRecalculation(t *testing.T) {
|
||||
Activity: ptrStr("moderate"),
|
||||
Goal: ptrStr("maintain"),
|
||||
}
|
||||
finalUser := &User{
|
||||
finalUser := &user.User{
|
||||
ID: "user-1",
|
||||
HeightCM: ptrInt(180),
|
||||
WeightKG: ptrFloat(80),
|
||||
@@ -117,10 +119,10 @@ func TestUpdateProfile_WithCaloriesRecalculation(t *testing.T) {
|
||||
|
||||
repo := &mockUserRepo{
|
||||
// Service calls GetByID first to merge current values for calorie calculation
|
||||
getByIDFn: func(ctx context.Context, id string) (*User, error) {
|
||||
return &User{ID: id, Plan: "free"}, nil
|
||||
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 UpdateProfileRequest, cReq *UpdateProfileRequest) (*User, error) {
|
||||
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 {
|
||||
@@ -129,11 +131,11 @@ func TestUpdateProfile_WithCaloriesRecalculation(t *testing.T) {
|
||||
return finalUser, nil
|
||||
},
|
||||
}
|
||||
svc := NewService(repo)
|
||||
svc := user.NewService(repo)
|
||||
|
||||
u, err := svc.UpdateProfile(context.Background(), "user-1", profileReq)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
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")
|
||||
@@ -144,94 +146,94 @@ func TestUpdateProfile_WithCaloriesRecalculation(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUpdateProfile_InvalidHeight_TooLow(t *testing.T) {
|
||||
svc := NewService(&mockUserRepo{})
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{HeightCM: ptrInt(50)})
|
||||
if err == nil {
|
||||
_, 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 := NewService(&mockUserRepo{})
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{HeightCM: ptrInt(300)})
|
||||
if err == nil {
|
||||
_, 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 := NewService(&mockUserRepo{})
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{WeightKG: ptrFloat(10)})
|
||||
if err == nil {
|
||||
_, 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 := NewService(&mockUserRepo{})
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{WeightKG: ptrFloat(400)})
|
||||
if err == nil {
|
||||
_, 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 := NewService(&mockUserRepo{})
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
dob := time.Now().AddDate(-5, 0, 0).Format("2006-01-02")
|
||||
|
||||
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{DateOfBirth: &dob})
|
||||
if err == nil {
|
||||
_, 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 := NewService(&mockUserRepo{})
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
dob := time.Now().AddDate(-150, 0, 0).Format("2006-01-02")
|
||||
|
||||
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{DateOfBirth: &dob})
|
||||
if err == nil {
|
||||
_, 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 := NewService(&mockUserRepo{})
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
dob := "not-a-date"
|
||||
|
||||
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{DateOfBirth: &dob})
|
||||
if err == nil {
|
||||
_, 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 := NewService(&mockUserRepo{})
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Gender: ptrStr("other")})
|
||||
if err == nil {
|
||||
_, 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 := NewService(&mockUserRepo{})
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Activity: ptrStr("extreme")})
|
||||
if err == nil {
|
||||
_, 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 := NewService(&mockUserRepo{})
|
||||
svc := user.NewService(&mockUserRepo{})
|
||||
|
||||
_, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Goal: ptrStr("bulk")})
|
||||
if err == nil {
|
||||
_, validationError := svc.UpdateProfile(context.Background(), "user-1", user.UpdateProfileRequest{Goal: ptrStr("bulk")})
|
||||
if validationError == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
}
|
||||
@@ -239,8 +241,8 @@ func TestUpdateProfile_InvalidGoal(t *testing.T) {
|
||||
func TestUpdateProfile_Preferences(t *testing.T) {
|
||||
prefs := json.RawMessage(`{"cuisines":["russian"]}`)
|
||||
repo := &mockUserRepo{
|
||||
updateFn: func(ctx context.Context, id string, req UpdateProfileRequest) (*User, error) {
|
||||
return &User{
|
||||
updateFn: func(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error) {
|
||||
return &user.User{
|
||||
ID: id,
|
||||
Plan: "free",
|
||||
Preferences: *req.Preferences,
|
||||
@@ -248,11 +250,11 @@ func TestUpdateProfile_Preferences(t *testing.T) {
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
svc := NewService(repo)
|
||||
svc := user.NewService(repo)
|
||||
|
||||
u, err := svc.UpdateProfile(context.Background(), "user-1", UpdateProfileRequest{Preferences: &prefs})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
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