mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit refactors the GraphQL test suite to resolve persistent build failures and establish a stable, mock-based unit testing environment. The key changes include: - Consolidating all GraphQL test helper functions into a single, canonical file (`internal/adapters/graphql/graphql_test_utils_test.go`). - Removing duplicated test helper code from `integration_test.go` and other test files. - Creating a new, dedicated unit test file for the `like` and `unlike` mutations (`internal/adapters/graphql/like_resolvers_unit_test.go`) using a mock-based approach. - Introducing mock services (`MockLikeService`, `MockAnalyticsService`) and updating mock repositories (`MockLikeRepository`, `MockWorkRepository`) in the `internal/testutil` package to support `testify/mock`. - Adding a `ContextWithUserID` helper function to `internal/platform/auth/middleware.go` to facilitate testing of authenticated resolvers. These changes resolve the `redeclared in this block` and package collision errors, resulting in a clean and passing test suite. This provides a solid foundation for future Test-Driven Development.
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 *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, 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 := &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
|
|
} |