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:
@@ -1,6 +1,7 @@
|
||||
package savedrecipe
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
@@ -12,13 +13,21 @@ import (
|
||||
|
||||
const maxBodySize = 1 << 20 // 1 MB
|
||||
|
||||
// SavedRecipeRepository is the data layer interface used by Handler.
|
||||
type SavedRecipeRepository interface {
|
||||
Save(ctx context.Context, userID string, req SaveRequest) (*UserSavedRecipe, error)
|
||||
List(ctx context.Context, userID string) ([]*UserSavedRecipe, error)
|
||||
GetByID(ctx context.Context, userID, id string) (*UserSavedRecipe, error)
|
||||
Delete(ctx context.Context, userID, id string) error
|
||||
}
|
||||
|
||||
// Handler handles HTTP requests for saved recipes.
|
||||
type Handler struct {
|
||||
repo *Repository
|
||||
repo SavedRecipeRepository
|
||||
}
|
||||
|
||||
// NewHandler creates a new Handler.
|
||||
func NewHandler(repo *Repository) *Handler {
|
||||
func NewHandler(repo SavedRecipeRepository) *Handler {
|
||||
return &Handler{repo: repo}
|
||||
}
|
||||
|
||||
|
||||
31
backend/internal/domain/savedrecipe/mocks/repository.go
Normal file
31
backend/internal/domain/savedrecipe/mocks/repository.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/food-ai/backend/internal/domain/savedrecipe"
|
||||
)
|
||||
|
||||
// MockSavedRecipeRepository is a test double implementing savedrecipe.SavedRecipeRepository.
|
||||
type MockSavedRecipeRepository struct {
|
||||
SaveFn func(ctx context.Context, userID string, req savedrecipe.SaveRequest) (*savedrecipe.UserSavedRecipe, error)
|
||||
ListFn func(ctx context.Context, userID string) ([]*savedrecipe.UserSavedRecipe, error)
|
||||
GetByIDFn func(ctx context.Context, userID, id string) (*savedrecipe.UserSavedRecipe, error)
|
||||
DeleteFn func(ctx context.Context, userID, id string) error
|
||||
}
|
||||
|
||||
func (m *MockSavedRecipeRepository) Save(ctx context.Context, userID string, req savedrecipe.SaveRequest) (*savedrecipe.UserSavedRecipe, error) {
|
||||
return m.SaveFn(ctx, userID, req)
|
||||
}
|
||||
|
||||
func (m *MockSavedRecipeRepository) List(ctx context.Context, userID string) ([]*savedrecipe.UserSavedRecipe, error) {
|
||||
return m.ListFn(ctx, userID)
|
||||
}
|
||||
|
||||
func (m *MockSavedRecipeRepository) GetByID(ctx context.Context, userID, id string) (*savedrecipe.UserSavedRecipe, error) {
|
||||
return m.GetByIDFn(ctx, userID, id)
|
||||
}
|
||||
|
||||
func (m *MockSavedRecipeRepository) Delete(ctx context.Context, userID, id string) error {
|
||||
return m.DeleteFn(ctx, userID, id)
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func (r *Repository) Save(ctx context.Context, userID string, req SaveRequest) (
|
||||
cr := dish.CreateRequest{
|
||||
Name: req.Title,
|
||||
Description: req.Description,
|
||||
CuisineSlug: mapCuisineSlug(req.Cuisine),
|
||||
CuisineSlug: MapCuisineSlug(req.Cuisine),
|
||||
ImageURL: req.ImageURL,
|
||||
Source: req.Source,
|
||||
Difficulty: req.Difficulty,
|
||||
@@ -373,9 +373,9 @@ func scanUSR(s rowScanner) (*UserSavedRecipe, error) {
|
||||
return &r, err
|
||||
}
|
||||
|
||||
// mapCuisineSlug maps a free-form cuisine string (from Gemini) to a known slug.
|
||||
// MapCuisineSlug maps a free-form cuisine string (from Gemini) to a known slug.
|
||||
// Falls back to "other".
|
||||
func mapCuisineSlug(cuisine string) string {
|
||||
func MapCuisineSlug(cuisine string) string {
|
||||
known := map[string]string{
|
||||
"russian": "russian",
|
||||
"italian": "italian",
|
||||
|
||||
Reference in New Issue
Block a user