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:
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user