test: expand test coverage across diary, product, savedrecipe, ingredient, menu, recognition
- Fix locale_test: add TestMain to pre-populate Supported map so zh/es tests pass - Export pure functions for testability: ResolveWeekStart, MapCuisineSlug (menu + savedrecipe), MergeAndDeduplicate - Introduce repository interfaces (DiaryRepository, ProductRepository, SavedRecipeRepository, IngredientSearcher) in each handler; NewHandler now accepts interfaces — concrete *Repository still satisfies them - Add mock files: diary/mocks, product/mocks, savedrecipe/mocks - Add handler unit tests (no DB) for diary (8), product (8), savedrecipe (8), ingredient (5) - Add pure-function unit tests: menu/ResolveWeekStart (6), savedrecipe/MapCuisineSlug (5), recognition/MergeAndDeduplicate (6) - Add repository integration tests (//go:build integration): diary (4), product (6) - Extend recipe integration tests: GetByID_Found, GetByID_WithTranslation Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
186
backend/tests/savedrecipe/handler_test.go
Normal file
186
backend/tests/savedrecipe/handler_test.go
Normal file
@@ -0,0 +1,186 @@
|
||||
package savedrecipe_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/food-ai/backend/internal/domain/savedrecipe"
|
||||
savedrecipemocks "github.com/food-ai/backend/internal/domain/savedrecipe/mocks"
|
||||
"github.com/food-ai/backend/internal/infra/middleware"
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type alwaysAuthValidator struct{ userID string }
|
||||
|
||||
func (v *alwaysAuthValidator) ValidateAccessToken(_ string) (*middleware.TokenClaims, error) {
|
||||
return &middleware.TokenClaims{UserID: v.userID}, nil
|
||||
}
|
||||
|
||||
func buildRouter(handler *savedrecipe.Handler, userID string) *chi.Mux {
|
||||
router := chi.NewRouter()
|
||||
router.Use(middleware.Auth(&alwaysAuthValidator{userID: userID}))
|
||||
router.Post("/saved-recipes", handler.Save)
|
||||
router.Get("/saved-recipes", handler.List)
|
||||
router.Get("/saved-recipes/{id}", handler.GetByID)
|
||||
router.Delete("/saved-recipes/{id}", handler.Delete)
|
||||
return router
|
||||
}
|
||||
|
||||
func authorizedRequest(method, target string, body []byte) *http.Request {
|
||||
request := httptest.NewRequest(method, target, bytes.NewReader(body))
|
||||
request.Header.Set("Authorization", "Bearer test-token")
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
return request
|
||||
}
|
||||
|
||||
func makeUserSavedRecipe(id string) *savedrecipe.UserSavedRecipe {
|
||||
return &savedrecipe.UserSavedRecipe{
|
||||
ID: id,
|
||||
UserID: "user-1",
|
||||
RecipeID: "recipe-1",
|
||||
SavedAt: time.Now(),
|
||||
DishName: "Pasta Carbonara",
|
||||
Ingredients: []savedrecipe.RecipeIngredient{},
|
||||
Steps: []savedrecipe.RecipeStep{},
|
||||
Tags: []string{},
|
||||
}
|
||||
}
|
||||
|
||||
func TestSave_MissingTitleAndRecipeID(t *testing.T) {
|
||||
handler := savedrecipe.NewHandler(&savedrecipemocks.MockSavedRecipeRepository{})
|
||||
router := buildRouter(handler, "user-1")
|
||||
|
||||
body, _ := json.Marshal(map[string]string{"cuisine": "italian"})
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, authorizedRequest(http.MethodPost, "/saved-recipes", body))
|
||||
|
||||
if recorder.Code != http.StatusBadRequest {
|
||||
t.Errorf("expected 400, got %d", recorder.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSave_WithTitle(t *testing.T) {
|
||||
mockRepo := &savedrecipemocks.MockSavedRecipeRepository{
|
||||
SaveFn: func(ctx context.Context, userID string, req savedrecipe.SaveRequest) (*savedrecipe.UserSavedRecipe, error) {
|
||||
return makeUserSavedRecipe("saved-1"), nil
|
||||
},
|
||||
}
|
||||
handler := savedrecipe.NewHandler(mockRepo)
|
||||
router := buildRouter(handler, "user-1")
|
||||
|
||||
body, _ := json.Marshal(savedrecipe.SaveRequest{Title: "My Recipe"})
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, authorizedRequest(http.MethodPost, "/saved-recipes", body))
|
||||
|
||||
if recorder.Code != http.StatusCreated {
|
||||
t.Errorf("expected 201, got %d", recorder.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSave_WithRecipeID(t *testing.T) {
|
||||
mockRepo := &savedrecipemocks.MockSavedRecipeRepository{
|
||||
SaveFn: func(ctx context.Context, userID string, req savedrecipe.SaveRequest) (*savedrecipe.UserSavedRecipe, error) {
|
||||
return makeUserSavedRecipe("saved-2"), nil
|
||||
},
|
||||
}
|
||||
handler := savedrecipe.NewHandler(mockRepo)
|
||||
router := buildRouter(handler, "user-1")
|
||||
|
||||
body, _ := json.Marshal(savedrecipe.SaveRequest{RecipeID: "recipe-42"})
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, authorizedRequest(http.MethodPost, "/saved-recipes", body))
|
||||
|
||||
if recorder.Code != http.StatusCreated {
|
||||
t.Errorf("expected 201, got %d", recorder.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestList_Success(t *testing.T) {
|
||||
mockRepo := &savedrecipemocks.MockSavedRecipeRepository{
|
||||
ListFn: func(ctx context.Context, userID string) ([]*savedrecipe.UserSavedRecipe, error) {
|
||||
return []*savedrecipe.UserSavedRecipe{makeUserSavedRecipe("saved-1")}, nil
|
||||
},
|
||||
}
|
||||
handler := savedrecipe.NewHandler(mockRepo)
|
||||
router := buildRouter(handler, "user-1")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, authorizedRequest(http.MethodGet, "/saved-recipes", nil))
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d", recorder.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetByID_NotFound(t *testing.T) {
|
||||
mockRepo := &savedrecipemocks.MockSavedRecipeRepository{
|
||||
GetByIDFn: func(ctx context.Context, userID, id string) (*savedrecipe.UserSavedRecipe, error) {
|
||||
return nil, nil
|
||||
},
|
||||
}
|
||||
handler := savedrecipe.NewHandler(mockRepo)
|
||||
router := buildRouter(handler, "user-1")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, authorizedRequest(http.MethodGet, "/saved-recipes/nonexistent", nil))
|
||||
|
||||
if recorder.Code != http.StatusNotFound {
|
||||
t.Errorf("expected 404, got %d", recorder.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetByID_Success(t *testing.T) {
|
||||
mockRepo := &savedrecipemocks.MockSavedRecipeRepository{
|
||||
GetByIDFn: func(ctx context.Context, userID, id string) (*savedrecipe.UserSavedRecipe, error) {
|
||||
return makeUserSavedRecipe(id), nil
|
||||
},
|
||||
}
|
||||
handler := savedrecipe.NewHandler(mockRepo)
|
||||
router := buildRouter(handler, "user-1")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, authorizedRequest(http.MethodGet, "/saved-recipes/saved-1", nil))
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Errorf("expected 200, got %d", recorder.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelete_NotFound(t *testing.T) {
|
||||
mockRepo := &savedrecipemocks.MockSavedRecipeRepository{
|
||||
DeleteFn: func(ctx context.Context, userID, id string) error {
|
||||
return savedrecipe.ErrNotFound
|
||||
},
|
||||
}
|
||||
handler := savedrecipe.NewHandler(mockRepo)
|
||||
router := buildRouter(handler, "user-1")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, authorizedRequest(http.MethodDelete, "/saved-recipes/nonexistent", nil))
|
||||
|
||||
if recorder.Code != http.StatusNotFound {
|
||||
t.Errorf("expected 404, got %d", recorder.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelete_Success(t *testing.T) {
|
||||
mockRepo := &savedrecipemocks.MockSavedRecipeRepository{
|
||||
DeleteFn: func(ctx context.Context, userID, id string) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
handler := savedrecipe.NewHandler(mockRepo)
|
||||
router := buildRouter(handler, "user-1")
|
||||
|
||||
recorder := httptest.NewRecorder()
|
||||
router.ServeHTTP(recorder, authorizedRequest(http.MethodDelete, "/saved-recipes/saved-1", nil))
|
||||
|
||||
if recorder.Code != http.StatusNoContent {
|
||||
t.Errorf("expected 204, got %d", recorder.Code)
|
||||
}
|
||||
}
|
||||
71
backend/tests/savedrecipe/map_cuisine_slug_test.go
Normal file
71
backend/tests/savedrecipe/map_cuisine_slug_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package savedrecipe_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/food-ai/backend/internal/domain/savedrecipe"
|
||||
)
|
||||
|
||||
func TestMapCuisineSlug_KnownCuisines(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
want string
|
||||
}{
|
||||
{"italian", "italian"},
|
||||
{"french", "french"},
|
||||
{"russian", "russian"},
|
||||
{"chinese", "chinese"},
|
||||
{"japanese", "japanese"},
|
||||
{"korean", "korean"},
|
||||
{"mexican", "mexican"},
|
||||
{"mediterranean", "mediterranean"},
|
||||
{"indian", "indian"},
|
||||
{"thai", "thai"},
|
||||
{"american", "american"},
|
||||
{"georgian", "georgian"},
|
||||
{"spanish", "spanish"},
|
||||
{"german", "german"},
|
||||
{"middle_eastern", "middle_eastern"},
|
||||
{"turkish", "turkish"},
|
||||
{"greek", "greek"},
|
||||
{"vietnamese", "vietnamese"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
result := savedrecipe.MapCuisineSlug(tc.input)
|
||||
if result != tc.want {
|
||||
t.Errorf("MapCuisineSlug(%q) = %q, want %q", tc.input, result, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapCuisineSlug_GenericFallsToOther(t *testing.T) {
|
||||
for _, input := range []string{"asian", "european"} {
|
||||
result := savedrecipe.MapCuisineSlug(input)
|
||||
if result != "other" {
|
||||
t.Errorf("MapCuisineSlug(%q) = %q, want %q", input, result, "other")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapCuisineSlug_Unknown(t *testing.T) {
|
||||
result := savedrecipe.MapCuisineSlug("peruvian")
|
||||
if result != "other" {
|
||||
t.Errorf("MapCuisineSlug(peruvian) = %q, want %q", result, "other")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapCuisineSlug_Empty(t *testing.T) {
|
||||
result := savedrecipe.MapCuisineSlug("")
|
||||
if result != "other" {
|
||||
t.Errorf("MapCuisineSlug() = %q, want %q", result, "other")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapCuisineSlug_CaseSensitive(t *testing.T) {
|
||||
// The function does NOT lowercase — "ITALIAN" is unknown and falls back to "other".
|
||||
result := savedrecipe.MapCuisineSlug("ITALIAN")
|
||||
if result != "other" {
|
||||
t.Errorf("MapCuisineSlug(ITALIAN) = %q, want %q", result, "other")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user