mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit marks the completion of a major refactoring effort to stabilize the codebase, improve its structure, and prepare it for production. The key changes include: - **Domain Layer Consolidation:** The `Work` entity and its related types, along with all other domain entities and repository interfaces, have been consolidated into the main `internal/domain` package. This eliminates import cycles and provides a single, coherent source of truth for the domain model. - **Data Access Layer Refactoring:** The repository implementations in `internal/data/sql` have been updated to align with the new domain layer. The `BaseRepositoryImpl` has been corrected to use pointer receivers, and all concrete repositories now correctly embed it, ensuring consistent and correct behavior. - **Application Layer Stabilization:** All application services in `internal/app` have been updated to use the new domain types and repository interfaces. Dependency injection has been corrected throughout the application, ensuring that all services are initialized with the correct dependencies. - **GraphQL Adapter Fixes:** The GraphQL resolver implementation in `internal/adapters/graphql` has been updated to correctly handle the new domain types and service methods. The auto-generated GraphQL code has been regenerated to ensure it is in sync with the schema and runtime. - **Test Suite Overhaul:** All test suites have been fixed to correctly implement their respective interfaces and use the updated domain model. Mock repositories and test suites have been corrected to properly embed the `testify` base types, resolving numerous build and linter errors. - **Dependency Management:** The Go modules have been tidied, and the module cache has been cleaned to ensure a consistent and correct dependency graph. - **Code Quality and Verification:** The entire codebase now passes all builds, tests, and linter checks, ensuring a high level of quality and stability. This comprehensive effort has resulted in a more robust, maintainable, and production-ready application.
184 lines
5.9 KiB
Go
184 lines
5.9 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) ListByWorkIDPaginated(ctx context.Context, workID uint, language *string, page, pageSize int) (*domain.PaginatedResult[domain.Translation], error) {
|
|
args := m.Called(ctx, workID, language, 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) 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)
|
|
}
|