tercul-backend/internal/app/work/mock_analytics_service_test.go
google-labs-jules[bot] 952a62c139 test: Increase test coverage for work package to over 80%
This commit increases the test coverage of the `internal/app/work` package from 73.1% to over 80% by adding new tests and fixing a bug discovered during testing.

The following changes were made:
- Added tests for the `ListByCollectionID` query in `queries_test.go`.
- Added a unit test for the `NewService` constructor in `service_test.go`.
- Added tests for authorization, unauthorized access, and other edge cases in the `UpdateWork`, `DeleteWork`, and `MergeWork` commands in `commands_test.go`.
- Fixed a bug in the `mergeWorkStats` function where it was not correctly creating stats for a target work that had no prior stats. This was discovered and fixed as part of writing the new tests.
- Updated the `analytics.Service` interface and its mock implementation to support the bug fix.
2025-10-08 20:45:49 +00:00

104 lines
4.1 KiB
Go

package work
import (
"context"
"tercul/internal/domain"
)
type mockAnalyticsService struct {
updateWorkReadingTimeFunc func(ctx context.Context, workID uint) error
updateWorkComplexityFunc func(ctx context.Context, workID uint) error
updateWorkSentimentFunc func(ctx context.Context, workID uint) error
updateTranslationReadingTimeFunc func(ctx context.Context, translationID uint) error
updateTranslationSentimentFunc func(ctx context.Context, translationID uint) error
getOrCreateWorkStatsFunc func(ctx context.Context, workID uint) (*domain.WorkStats, error)
updateWorkStatsFunc func(ctx context.Context, workID uint, stats domain.WorkStats) error
}
func (m *mockAnalyticsService) UpdateWorkStats(ctx context.Context, workID uint, stats domain.WorkStats) error {
if m.updateWorkStatsFunc != nil {
return m.updateWorkStatsFunc(ctx, workID, stats)
}
return nil
}
func (m *mockAnalyticsService) UpdateWorkReadingTime(ctx context.Context, workID uint) error {
if m.updateWorkReadingTimeFunc != nil {
return m.updateWorkReadingTimeFunc(ctx, workID)
}
return nil
}
func (m *mockAnalyticsService) UpdateWorkComplexity(ctx context.Context, workID uint) error {
if m.updateWorkComplexityFunc != nil {
return m.updateWorkComplexityFunc(ctx, workID)
}
return nil
}
func (m *mockAnalyticsService) UpdateWorkSentiment(ctx context.Context, workID uint) error {
if m.updateWorkSentimentFunc != nil {
return m.updateWorkSentimentFunc(ctx, workID)
}
return nil
}
func (m *mockAnalyticsService) UpdateTranslationReadingTime(ctx context.Context, translationID uint) error {
if m.updateTranslationReadingTimeFunc != nil {
return m.updateTranslationReadingTimeFunc(ctx, translationID)
}
return nil
}
func (m *mockAnalyticsService) UpdateTranslationSentiment(ctx context.Context, translationID uint) error {
if m.updateTranslationSentimentFunc != nil {
return m.updateTranslationSentimentFunc(ctx, translationID)
}
return nil
}
// Implement other methods of the analytics.Service interface to satisfy the compiler
func (m *mockAnalyticsService) IncrementWorkViews(ctx context.Context, workID uint) error { return nil }
func (m *mockAnalyticsService) IncrementWorkLikes(ctx context.Context, workID uint) error { return nil }
func (m *mockAnalyticsService) IncrementWorkComments(ctx context.Context, workID uint) error {
return nil
}
func (m *mockAnalyticsService) IncrementWorkBookmarks(ctx context.Context, workID uint) error {
return nil
}
func (m *mockAnalyticsService) IncrementWorkShares(ctx context.Context, workID uint) error { return nil }
func (m *mockAnalyticsService) IncrementWorkTranslationCount(ctx context.Context, workID uint) error {
return nil
}
func (m *mockAnalyticsService) IncrementTranslationViews(ctx context.Context, translationID uint) error {
return nil
}
func (m *mockAnalyticsService) IncrementTranslationLikes(ctx context.Context, translationID uint) error {
return nil
}
func (m *mockAnalyticsService) DecrementWorkLikes(ctx context.Context, workID uint) error { return nil }
func (m *mockAnalyticsService) DecrementTranslationLikes(ctx context.Context, translationID uint) error {
return nil
}
func (m *mockAnalyticsService) IncrementTranslationComments(ctx context.Context, translationID uint) error {
return nil
}
func (m *mockAnalyticsService) IncrementTranslationShares(ctx context.Context, translationID uint) error {
return nil
}
func (m *mockAnalyticsService) GetOrCreateWorkStats(ctx context.Context, workID uint) (*domain.WorkStats, error) {
if m.getOrCreateWorkStatsFunc != nil {
return m.getOrCreateWorkStatsFunc(ctx, workID)
}
return nil, nil
}
func (m *mockAnalyticsService) GetOrCreateTranslationStats(ctx context.Context, translationID uint) (*domain.TranslationStats, error) {
return nil, nil
}
func (m *mockAnalyticsService) UpdateUserEngagement(ctx context.Context, userID uint, eventType string) error {
return nil
}
func (m *mockAnalyticsService) UpdateTrending(ctx context.Context) error { return nil }
func (m *mockAnalyticsService) GetTrendingWorks(ctx context.Context, timePeriod string, limit int) ([]*domain.Work, error) {
return nil, nil
}