tercul-backend/internal/testutil/mock_analytics_service.go
google-labs-jules[bot] 80cfe71e59 refactor: Refactor GraphQL tests to use mock-based unit tests
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.
2025-10-03 09:21:41 +00:00

101 lines
3.2 KiB
Go

package testutil
import (
"context"
"tercul/internal/domain"
"time"
"github.com/stretchr/testify/mock"
)
// MockAnalyticsService is a mock implementation of the analytics.Service interface.
type MockAnalyticsService struct {
mock.Mock
}
func (m *MockAnalyticsService) GetTrendingWorks(ctx context.Context, timePeriod string, limit int) ([]*domain.Work, error) {
args := m.Called(ctx, timePeriod, limit)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).([]*domain.Work), args.Error(1)
}
func (m *MockAnalyticsService) UpdateTrending(ctx context.Context) error {
args := m.Called(ctx)
return args.Error(0)
}
func (m *MockAnalyticsService) IncrementWorkLikes(ctx context.Context, workID uint) {
m.Called(ctx, workID)
}
func (m *MockAnalyticsService) IncrementTranslationLikes(ctx context.Context, translationID uint) {
m.Called(ctx, translationID)
}
func (m *MockAnalyticsService) IncrementWorkComments(ctx context.Context, workID uint) {
m.Called(ctx, workID)
}
func (m *MockAnalyticsService) IncrementTranslationComments(ctx context.Context, translationID uint) {
m.Called(ctx, translationID)
}
func (m *MockAnalyticsService) IncrementWorkBookmarks(ctx context.Context, workID uint) {
m.Called(ctx, workID)
}
func (m *MockAnalyticsService) GetOrCreateWorkStats(ctx context.Context, workID uint) (*domain.WorkStats, error) {
args := m.Called(ctx, workID)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*domain.WorkStats), args.Error(1)
}
func (m *MockAnalyticsService) GetOrCreateTranslationStats(ctx context.Context, translationID uint) (*domain.TranslationStats, error) {
args := m.Called(ctx, translationID)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*domain.TranslationStats), args.Error(1)
}
func (m *MockAnalyticsService) GetOrCreateUserEngagement(ctx context.Context, userID uint, date time.Time) (*domain.UserEngagement, error) {
args := m.Called(ctx, userID, date)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*domain.UserEngagement), args.Error(1)
}
func (m *MockAnalyticsService) UpdateUserEngagement(ctx context.Context, engagement *domain.UserEngagement) error {
args := m.Called(ctx, engagement)
return args.Error(0)
}
func (m *MockAnalyticsService) IncrementWorkCounter(ctx context.Context, workID uint, counter string, value int) error {
args := m.Called(ctx, workID, counter, value)
return args.Error(0)
}
func (m *MockAnalyticsService) IncrementTranslationCounter(ctx context.Context, translationID uint, counter string, value int) error {
args := m.Called(ctx, translationID, counter, value)
return args.Error(0)
}
func (m *MockAnalyticsService) UpdateWorkStats(ctx context.Context, workID uint, stats domain.WorkStats) error {
args := m.Called(ctx, workID, stats)
return args.Error(0)
}
func (m *MockAnalyticsService) UpdateTranslationStats(ctx context.Context, translationID uint, stats domain.TranslationStats) error {
args := m.Called(ctx, translationID, stats)
return args.Error(0)
}
func (m *MockAnalyticsService) UpdateTrendingWorks(ctx context.Context, timePeriod string, trendingWorks []*domain.Trending) error {
args := m.Called(ctx, timePeriod, trendingWorks)
return args.Error(0)
}