mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 02:51:34 +00:00
This commit introduces a significant refactoring to improve the application's quality, test coverage, and production readiness, focusing on core localization and business logic features. Key changes include: - Consolidated the `CreateTranslation` and `UpdateTranslation` commands into a single, more robust `CreateOrUpdateTranslation` command. This uses a database-level `Upsert` for atomicity. - Centralized authorization for translatable entities into a new `CanEditEntity` check within the application service layer. - Fixed a critical bug in the `MergeWork` command that caused a UNIQUE constraint violation when merging works with conflicting translations. The logic now intelligently handles language conflicts. - Implemented decrementing for "like" counts in the analytics service when a like is deleted, ensuring accurate statistics. - Stabilized the test suite by switching to a file-based database for integration tests, fixing test data isolation issues, and adding a unique index to the `Translation` model to enforce data integrity. - Refactored manual mocks to use the `testify/mock` library for better consistency and maintainability.
176 lines
5.5 KiB
Go
176 lines
5.5 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"gorm.io/gorm"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
import "github.com/stretchr/testify/mock"
|
|
|
|
// MockTranslationRepository is an in-memory implementation of TranslationRepository
|
|
type MockTranslationRepository struct {
|
|
mock.Mock
|
|
}
|
|
|
|
func NewMockTranslationRepository() *MockTranslationRepository {
|
|
return new(MockTranslationRepository)
|
|
}
|
|
|
|
var _ domain.TranslationRepository = (*MockTranslationRepository)(nil)
|
|
|
|
// BaseRepository methods with context support
|
|
func (m *MockTranslationRepository) Create(ctx context.Context, t *domain.Translation) error {
|
|
args := m.Called(ctx, t)
|
|
return args.Error(0)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) GetByID(ctx context.Context, id uint) (*domain.Translation, error) {
|
|
args := m.Called(ctx, id)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).(*domain.Translation), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) Update(ctx context.Context, t *domain.Translation) error {
|
|
args := m.Called(ctx, t)
|
|
return args.Error(0)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) Delete(ctx context.Context, id uint) error {
|
|
args := m.Called(ctx, id)
|
|
return args.Error(0)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) List(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.Translation], error) {
|
|
args := m.Called(ctx, page, pageSize)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).(*domain.PaginatedResult[domain.Translation]), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) ListAll(ctx context.Context) ([]domain.Translation, error) {
|
|
args := m.Called(ctx)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).([]domain.Translation), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) Count(ctx context.Context) (int64, error) {
|
|
args := m.Called(ctx)
|
|
return args.Get(0).(int64), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) FindWithPreload(ctx context.Context, preloads []string, id uint) (*domain.Translation, error) {
|
|
args := m.Called(ctx, preloads, id)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).(*domain.Translation), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]domain.Translation, error) {
|
|
args := m.Called(ctx, batchSize, offset)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).([]domain.Translation), args.Error(1)
|
|
}
|
|
|
|
// New BaseRepository methods
|
|
func (m *MockTranslationRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Translation) error {
|
|
args := m.Called(ctx, tx, entity)
|
|
return args.Error(0)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Translation, error) {
|
|
args := m.Called(ctx, id, options)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).(*domain.Translation), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) UpdateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Translation) error {
|
|
args := m.Called(ctx, tx, entity)
|
|
return args.Error(0)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) DeleteInTx(ctx context.Context, tx *gorm.DB, id uint) error {
|
|
args := m.Called(ctx, tx, id)
|
|
return args.Error(0)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]domain.Translation, error) {
|
|
args := m.Called(ctx, options)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).([]domain.Translation), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) CountWithOptions(ctx context.Context, options *domain.QueryOptions) (int64, error) {
|
|
args := m.Called(ctx, options)
|
|
return args.Get(0).(int64), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) Exists(ctx context.Context, id uint) (bool, error) {
|
|
args := m.Called(ctx, id)
|
|
return args.Bool(0), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) BeginTx(ctx context.Context) (*gorm.DB, error) {
|
|
args := m.Called(ctx)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).(*gorm.DB), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error {
|
|
// This is tricky to mock. For now, just execute the function.
|
|
// A real test might want to mock the transaction itself.
|
|
return fn(nil)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) Upsert(ctx context.Context, t *domain.Translation) error {
|
|
args := m.Called(ctx, t)
|
|
return args.Error(0)
|
|
}
|
|
|
|
// TranslationRepository specific methods
|
|
func (m *MockTranslationRepository) ListByWorkID(ctx context.Context, workID uint) ([]domain.Translation, error) {
|
|
args := m.Called(ctx, workID)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).([]domain.Translation), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) ListByEntity(ctx context.Context, entityType string, entityID uint) ([]domain.Translation, error) {
|
|
args := m.Called(ctx, entityType, entityID)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).([]domain.Translation), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) ListByTranslatorID(ctx context.Context, translatorID uint) ([]domain.Translation, error) {
|
|
args := m.Called(ctx, translatorID)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).([]domain.Translation), args.Error(1)
|
|
}
|
|
|
|
func (m *MockTranslationRepository) ListByStatus(ctx context.Context, status domain.TranslationStatus) ([]domain.Translation, error) {
|
|
args := m.Called(ctx, status)
|
|
if args.Get(0) == nil {
|
|
return nil, args.Error(1)
|
|
}
|
|
return args.Get(0).([]domain.Translation), args.Error(1)
|
|
}
|