tercul-backend/internal/testutil/simple_test_utils.go
google-labs-jules[bot] 06e6e2be85 refactor(domain): Isolate Work aggregate
This commit isolates the `Work` aggregate into its own package at `internal/domain/work`, following the first step of the refactoring plan in `refactor.md`.

- The `Work` struct, related types, and the `WorkRepository` interface have been moved to the new package.
- A circular dependency between `domain` and `work` was resolved by moving the `AnalyticsRepository` to the `app` layer.
- All references to the moved types have been updated across the entire codebase to fix compilation errors.
- Test files, including mocks and integration tests, have been updated to reflect the new structure.
2025-10-03 16:15:09 +00:00

92 lines
3.0 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"
domain_work "tercul/internal/domain/work"
"github.com/stretchr/testify/suite"
)
// SimpleTestSuite provides a minimal test environment with just the essentials
type SimpleTestSuite struct {
suite.Suite
WorkRepo *MockWorkRepository
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.Work, pipeline string) error {
return nil
}
// SetupSuite sets up the test suite
func (s *SimpleTestSuite) SetupSuite() {
s.WorkRepo = NewMockWorkRepository()
s.MockSearchClient = &MockSearchClient{}
s.WorkService = work.NewService(s.WorkRepo, s.MockSearchClient)
}
// SetupTest resets test data for each test
func (s *SimpleTestSuite) SetupTest() {
s.WorkRepo = NewMockWorkRepository()
}
// 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 {
work := &domain_work.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
}