mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
This commit completes the Domain-Driven Design (DDD) refactoring, bringing the codebase into a stable, compilable, and fully tested state.
Key changes include:
- Refactored the `localization` service to use a Commands/Queries pattern, aligning it with the new architecture.
- Implemented the missing `GetAuthorBiography` query in the `localization` service to simplify resolver logic.
- Corrected GORM entity definitions for polymorphic relationships, changing `[]Translation` to `[]*Translation` to enable proper preloading of translations.
- Standardized the `TranslatableType` value to use the database table name (e.g., "works") instead of the model name ("Work") to ensure consistent data creation and retrieval.
- Updated GraphQL resolvers to exclusively use application services instead of direct repository access, fixing numerous build errors.
- Repaired all failing unit and integration tests by updating mock objects and correcting test data setup to reflect the architectural changes.
These changes resolve all outstanding build errors and test failures, leaving the application in a healthy and maintainable state.
91 lines
2.9 KiB
Go
91 lines
2.9 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
graph "tercul/internal/adapters/graphql"
|
|
"tercul/internal/app"
|
|
"tercul/internal/app/localization"
|
|
"tercul/internal/app/work"
|
|
"tercul/internal/domain"
|
|
domain_localization "tercul/internal/domain/localization"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
// SimpleTestSuite provides a minimal test environment with just the essentials
|
|
type SimpleTestSuite struct {
|
|
suite.Suite
|
|
WorkRepo *UnifiedMockWorkRepository
|
|
WorkService *work.Service
|
|
MockSearchClient *MockSearchClient
|
|
}
|
|
|
|
// MockSearchClient is a mock implementation of the search.SearchClient interface.
|
|
type MockSearchClient struct{}
|
|
|
|
// IndexWork is the mock implementation of the IndexWork method.
|
|
func (m *MockSearchClient) IndexWork(ctx context.Context, work *domain.Work, pipeline string) error {
|
|
return nil
|
|
}
|
|
|
|
// SetupSuite sets up the test suite
|
|
func (s *SimpleTestSuite) SetupSuite() {
|
|
s.WorkRepo = NewUnifiedMockWorkRepository()
|
|
s.MockSearchClient = &MockSearchClient{}
|
|
s.WorkService = work.NewService(s.WorkRepo, s.MockSearchClient)
|
|
}
|
|
|
|
// SetupTest resets test data for each test
|
|
func (s *SimpleTestSuite) SetupTest() {
|
|
s.WorkRepo.Reset()
|
|
}
|
|
|
|
// MockLocalizationRepository is a mock implementation of the localization repository.
|
|
type MockLocalizationRepository struct{}
|
|
|
|
func (m *MockLocalizationRepository) GetTranslation(ctx context.Context, key string, language string) (string, error) {
|
|
return "Test translation", nil
|
|
}
|
|
|
|
func (m *MockLocalizationRepository) GetTranslations(ctx context.Context, keys []string, language string) (map[string]string, error) {
|
|
results := make(map[string]string)
|
|
for _, key := range keys {
|
|
results[key] = "Test translation for " + key
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
// GetAuthorBiography is a mock implementation of the GetAuthorBiography method.
|
|
func (m *MockLocalizationRepository) GetAuthorBiography(ctx context.Context, authorID uint, language string) (string, error) {
|
|
return "This is a mock biography.", nil
|
|
}
|
|
|
|
// GetResolver returns a minimal GraphQL resolver for testing
|
|
func (s *SimpleTestSuite) GetResolver() *graph.Resolver {
|
|
var mockLocalizationRepo domain_localization.LocalizationRepository = &MockLocalizationRepository{}
|
|
localizationService := localization.NewService(mockLocalizationRepo)
|
|
|
|
return &graph.Resolver{
|
|
App: &app.Application{
|
|
Work: s.WorkService,
|
|
Localization: localizationService,
|
|
},
|
|
}
|
|
}
|
|
|
|
// CreateTestWork creates a test work with optional content
|
|
func (s *SimpleTestSuite) CreateTestWork(title, language string, content string) *domain.Work {
|
|
work := &domain.Work{
|
|
Title: title,
|
|
TranslatableModel: domain.TranslatableModel{Language: language},
|
|
}
|
|
|
|
// Add work to the mock repository
|
|
createdWork, err := s.WorkService.Commands.CreateWork(context.Background(), work)
|
|
s.Require().NoError(err)
|
|
|
|
// If content is provided, we'll need to handle it differently
|
|
// since the mock repository doesn't support translations yet
|
|
// For now, just return the work
|
|
return createdWork
|
|
} |