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:
dbastrikin
2026-03-15 22:54:09 +02:00
parent 7c338c35f3
commit bfaca1a2c1
21 changed files with 1452 additions and 28 deletions

View File

@@ -1,6 +1,7 @@
package diary
import (
"context"
"encoding/json"
"log/slog"
"net/http"
@@ -9,13 +10,20 @@ import (
"github.com/go-chi/chi/v5"
)
// DiaryRepository is the data layer interface used by Handler.
type DiaryRepository interface {
ListByDate(ctx context.Context, userID, date string) ([]*Entry, error)
Create(ctx context.Context, userID string, req CreateRequest) (*Entry, error)
Delete(ctx context.Context, id, userID string) error
}
// Handler handles diary endpoints.
type Handler struct {
repo *Repository
repo DiaryRepository
}
// NewHandler creates a new Handler.
func NewHandler(repo *Repository) *Handler {
func NewHandler(repo DiaryRepository) *Handler {
return &Handler{repo: repo}
}

View File

@@ -0,0 +1,26 @@
package mocks
import (
"context"
"github.com/food-ai/backend/internal/domain/diary"
)
// MockDiaryRepository is a test double implementing diary.DiaryRepository.
type MockDiaryRepository struct {
ListByDateFn func(ctx context.Context, userID, date string) ([]*diary.Entry, error)
CreateFn func(ctx context.Context, userID string, req diary.CreateRequest) (*diary.Entry, error)
DeleteFn func(ctx context.Context, id, userID string) error
}
func (m *MockDiaryRepository) ListByDate(ctx context.Context, userID, date string) ([]*diary.Entry, error) {
return m.ListByDateFn(ctx, userID, date)
}
func (m *MockDiaryRepository) Create(ctx context.Context, userID string, req diary.CreateRequest) (*diary.Entry, error) {
return m.CreateFn(ctx, userID, req)
}
func (m *MockDiaryRepository) Delete(ctx context.Context, id, userID string) error {
return m.DeleteFn(ctx, id, userID)
}

View File

@@ -1,19 +1,25 @@
package ingredient
import (
"context"
"encoding/json"
"log/slog"
"net/http"
"strconv"
)
// IngredientSearcher is the data layer interface used by Handler.
type IngredientSearcher interface {
Search(ctx context.Context, query string, limit int) ([]*IngredientMapping, error)
}
// Handler handles ingredient HTTP requests.
type Handler struct {
repo *Repository
repo IngredientSearcher
}
// NewHandler creates a new Handler.
func NewHandler(repo *Repository) *Handler {
func NewHandler(repo IngredientSearcher) *Handler {
return &Handler{repo: repo}
}

View File

@@ -84,7 +84,7 @@ func (h *Handler) GetMenu(w http.ResponseWriter, r *http.Request) {
return
}
weekStart, err := resolveWeekStart(r.URL.Query().Get("week"))
weekStart, err := ResolveWeekStart(r.URL.Query().Get("week"))
if err != nil {
writeError(w, http.StatusBadRequest, "invalid week parameter, expected YYYY-WNN")
return
@@ -120,7 +120,7 @@ func (h *Handler) GenerateMenu(w http.ResponseWriter, r *http.Request) {
}
_ = json.NewDecoder(r.Body).Decode(&body)
weekStart, err := resolveWeekStart(body.Week)
weekStart, err := ResolveWeekStart(body.Week)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid week parameter")
return
@@ -302,7 +302,7 @@ func (h *Handler) GenerateShoppingList(w http.ResponseWriter, r *http.Request) {
}
_ = json.NewDecoder(r.Body).Decode(&body)
weekStart, err := resolveWeekStart(body.Week)
weekStart, err := ResolveWeekStart(body.Week)
if err != nil {
writeError(w, http.StatusBadRequest, "invalid week parameter")
return
@@ -342,7 +342,7 @@ func (h *Handler) GetShoppingList(w http.ResponseWriter, r *http.Request) {
return
}
weekStart, err := resolveWeekStart(r.URL.Query().Get("week"))
weekStart, err := ResolveWeekStart(r.URL.Query().Get("week"))
if err != nil {
writeError(w, http.StatusBadRequest, "invalid week parameter")
return
@@ -392,7 +392,7 @@ func (h *Handler) ToggleShoppingItem(w http.ResponseWriter, r *http.Request) {
return
}
weekStart, err := resolveWeekStart(r.URL.Query().Get("week"))
weekStart, err := ResolveWeekStart(r.URL.Query().Get("week"))
if err != nil {
writeError(w, http.StatusBadRequest, "invalid week parameter")
return
@@ -477,7 +477,7 @@ func recipeToCreateRequest(r ai.Recipe) dish.CreateRequest {
cr := dish.CreateRequest{
Name: r.Title,
Description: r.Description,
CuisineSlug: mapCuisineSlug(r.Cuisine),
CuisineSlug: MapCuisineSlug(r.Cuisine),
ImageURL: r.ImageURL,
Difficulty: r.Difficulty,
PrepTimeMin: r.PrepTimeMin,
@@ -507,9 +507,9 @@ func recipeToCreateRequest(r ai.Recipe) dish.CreateRequest {
return cr
}
// 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",
@@ -538,8 +538,8 @@ func mapCuisineSlug(cuisine string) string {
return "other"
}
// resolveWeekStart parses "YYYY-WNN" or returns current week's Monday.
func resolveWeekStart(week string) (string, error) {
// ResolveWeekStart parses "YYYY-WNN" or returns current week's Monday.
func ResolveWeekStart(week string) (string, error) {
if week == "" {
return currentWeekStart(), nil
}

View File

@@ -1,6 +1,7 @@
package product
import (
"context"
"encoding/json"
"errors"
"log/slog"
@@ -10,13 +11,22 @@ import (
"github.com/go-chi/chi/v5"
)
// ProductRepository is the data layer interface used by Handler.
type ProductRepository interface {
List(ctx context.Context, userID string) ([]*Product, error)
Create(ctx context.Context, userID string, req CreateRequest) (*Product, error)
BatchCreate(ctx context.Context, userID string, items []CreateRequest) ([]*Product, error)
Update(ctx context.Context, id, userID string, req UpdateRequest) (*Product, error)
Delete(ctx context.Context, id, userID string) error
}
// Handler handles /products HTTP requests.
type Handler struct {
repo *Repository
repo ProductRepository
}
// NewHandler creates a new Handler.
func NewHandler(repo *Repository) *Handler {
func NewHandler(repo ProductRepository) *Handler {
return &Handler{repo: repo}
}

View File

@@ -0,0 +1,36 @@
package mocks
import (
"context"
"github.com/food-ai/backend/internal/domain/product"
)
// MockProductRepository is a test double implementing product.ProductRepository.
type MockProductRepository struct {
ListFn func(ctx context.Context, userID string) ([]*product.Product, error)
CreateFn func(ctx context.Context, userID string, req product.CreateRequest) (*product.Product, error)
BatchCreateFn func(ctx context.Context, userID string, items []product.CreateRequest) ([]*product.Product, error)
UpdateFn func(ctx context.Context, id, userID string, req product.UpdateRequest) (*product.Product, error)
DeleteFn func(ctx context.Context, id, userID string) error
}
func (m *MockProductRepository) List(ctx context.Context, userID string) ([]*product.Product, error) {
return m.ListFn(ctx, userID)
}
func (m *MockProductRepository) Create(ctx context.Context, userID string, req product.CreateRequest) (*product.Product, error) {
return m.CreateFn(ctx, userID, req)
}
func (m *MockProductRepository) BatchCreate(ctx context.Context, userID string, items []product.CreateRequest) ([]*product.Product, error) {
return m.BatchCreateFn(ctx, userID, items)
}
func (m *MockProductRepository) Update(ctx context.Context, id, userID string, req product.UpdateRequest) (*product.Product, error) {
return m.UpdateFn(ctx, id, userID, req)
}
func (m *MockProductRepository) Delete(ctx context.Context, id, userID string) error {
return m.DeleteFn(ctx, id, userID)
}

View File

@@ -134,7 +134,7 @@ func (h *Handler) RecognizeProducts(w http.ResponseWriter, r *http.Request) {
}
wg.Wait()
merged := mergeAndDeduplicate(allItems)
merged := MergeAndDeduplicate(allItems)
enriched := h.enrichItems(r.Context(), merged)
writeJSON(w, http.StatusOK, map[string]any{"items": enriched})
}
@@ -258,9 +258,9 @@ func (h *Handler) saveClassification(ctx context.Context, c *ai.IngredientClassi
return saved
}
// mergeAndDeduplicate combines results from multiple images.
// MergeAndDeduplicate combines results from multiple images.
// Items sharing the same name (case-insensitive) have their quantities summed.
func mergeAndDeduplicate(batches [][]ai.RecognizedItem) []ai.RecognizedItem {
func MergeAndDeduplicate(batches [][]ai.RecognizedItem) []ai.RecognizedItem {
seen := make(map[string]*ai.RecognizedItem)
var order []string

View File

@@ -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}
}

View 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)
}

View File

@@ -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",