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 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}
|
||||
}
|
||||
|
||||
|
||||
36
backend/internal/domain/product/mocks/repository.go
Normal file
36
backend/internal/domain/product/mocks/repository.go
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user