tercul-backend/internal/app/work/main_test.go
google-labs-jules[bot] 9fd2331eb4 feat: Implement production-ready API patterns
This commit introduces a comprehensive set of foundational improvements to make the API more robust, secure, and observable.

The following features have been implemented:

- **Observability Stack:** A new `internal/observability` package has been added, providing structured logging with `zerolog`, Prometheus metrics, and OpenTelemetry tracing. This stack is fully integrated into the application's request pipeline.

- **Centralized Authorization:** A new `internal/app/authz` service has been created to centralize authorization logic. This service is now used by the `user`, `work`, and `comment` services to protect all Create, Update, and Delete operations.

- **Standardized Input Validation:** The previous ad-hoc validation has been replaced with a more robust, struct-tag-based system using the `go-playground/validator` library. This has been applied to all GraphQL input models.

- **Structured Error Handling:** A new set of custom error types has been introduced in the `internal/domain` package. A custom `gqlgen` error presenter has been implemented to map these domain errors to structured GraphQL error responses with specific error codes.

- **`updateUser` Endpoint:** The `updateUser` mutation has been fully implemented as a proof of concept for the new patterns, including support for partial updates and comprehensive authorization checks.

- **Test Refactoring:** The test suite has been significantly improved by decoupling mock repositories from the shared `testutil` package, resolving circular dependency issues and making the tests more maintainable.
2025-10-04 18:16:08 +00:00

101 lines
3.6 KiB
Go

package work
import (
"context"
"tercul/internal/domain"
"tercul/internal/domain/work"
)
type mockWorkRepository struct {
work.WorkRepository
createFunc func(ctx context.Context, work *work.Work) error
updateFunc func(ctx context.Context, work *work.Work) error
deleteFunc func(ctx context.Context, id uint) error
getByIDFunc func(ctx context.Context, id uint) (*work.Work, error)
listFunc func(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[work.Work], error)
getWithTranslationsFunc func(ctx context.Context, id uint) (*work.Work, error)
findByTitleFunc func(ctx context.Context, title string) ([]work.Work, error)
findByAuthorFunc func(ctx context.Context, authorID uint) ([]work.Work, error)
findByCategoryFunc func(ctx context.Context, categoryID uint) ([]work.Work, error)
findByLanguageFunc func(ctx context.Context, language string, page, pageSize int) (*domain.PaginatedResult[work.Work], error)
isAuthorFunc func(ctx context.Context, workID uint, authorID uint) (bool, error)
}
func (m *mockWorkRepository) IsAuthor(ctx context.Context, workID uint, authorID uint) (bool, error) {
if m.isAuthorFunc != nil {
return m.isAuthorFunc(ctx, workID, authorID)
}
return false, nil
}
func (m *mockWorkRepository) Create(ctx context.Context, work *work.Work) error {
if m.createFunc != nil {
return m.createFunc(ctx, work)
}
return nil
}
func (m *mockWorkRepository) Update(ctx context.Context, work *work.Work) error {
if m.updateFunc != nil {
return m.updateFunc(ctx, work)
}
return nil
}
func (m *mockWorkRepository) Delete(ctx context.Context, id uint) error {
if m.deleteFunc != nil {
return m.deleteFunc(ctx, id)
}
return nil
}
func (m *mockWorkRepository) GetByID(ctx context.Context, id uint) (*work.Work, error) {
if m.getByIDFunc != nil {
return m.getByIDFunc(ctx, id)
}
return &work.Work{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: id}}}, nil
}
func (m *mockWorkRepository) List(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[work.Work], error) {
if m.listFunc != nil {
return m.listFunc(ctx, page, pageSize)
}
return nil, nil
}
func (m *mockWorkRepository) GetWithTranslations(ctx context.Context, id uint) (*work.Work, error) {
if m.getWithTranslationsFunc != nil {
return m.getWithTranslationsFunc(ctx, id)
}
return nil, nil
}
func (m *mockWorkRepository) FindByTitle(ctx context.Context, title string) ([]work.Work, error) {
if m.findByTitleFunc != nil {
return m.findByTitleFunc(ctx, title)
}
return nil, nil
}
func (m *mockWorkRepository) FindByAuthor(ctx context.Context, authorID uint) ([]work.Work, error) {
if m.findByAuthorFunc != nil {
return m.findByAuthorFunc(ctx, authorID)
}
return nil, nil
}
func (m *mockWorkRepository) FindByCategory(ctx context.Context, categoryID uint) ([]work.Work, error) {
if m.findByCategoryFunc != nil {
return m.findByCategoryFunc(ctx, categoryID)
}
return nil, nil
}
func (m *mockWorkRepository) FindByLanguage(ctx context.Context, language string, page, pageSize int) (*domain.PaginatedResult[work.Work], error) {
if m.findByLanguageFunc != nil {
return m.findByLanguageFunc(ctx, language, page, pageSize)
}
return nil, nil
}
type mockSearchClient struct {
indexWorkFunc func(ctx context.Context, work *work.Work, pipeline string) error
}
func (m *mockSearchClient) IndexWork(ctx context.Context, work *work.Work, pipeline string) error {
if m.indexWorkFunc != nil {
return m.indexWorkFunc(ctx, work, pipeline)
}
return nil
}