tercul-backend/internal/application/services/analytics_service_test.go
google-labs-jules[bot] 52101fbeda Refactor: Expose Analytics Service via GraphQL
This commit refactors the analytics service to align with the new DDD architecture and exposes it through the GraphQL API.

Key changes:
- A new `AnalyticsService` has been created in `internal/application/services` to encapsulate analytics-related business logic.
- The GraphQL resolver has been updated to use the new `AnalyticsService`, providing a clean and maintainable API.
- The old analytics service and its related files have been removed, reducing code duplication and confusion.
- The `bookmark`, `like`, and `work` services have been refactored to remove their dependencies on the old analytics repository.
- Unit tests have been added for the new `AnalyticsService`, and existing tests have been updated to reflect the refactoring.
2025-10-03 04:10:16 +00:00

105 lines
2.9 KiB
Go

package services
import (
"context"
"testing"
"tercul/internal/domain"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
// Mock Repositories
type MockWorkRepository struct {
mock.Mock
domain.WorkRepository
}
func (m *MockWorkRepository) Count(ctx context.Context) (int64, error) {
args := m.Called(ctx)
return args.Get(0).(int64), args.Error(1)
}
// Implement other methods of the WorkRepository interface if needed for other tests
type MockTranslationRepository struct {
mock.Mock
domain.TranslationRepository
}
func (m *MockTranslationRepository) Count(ctx context.Context) (int64, error) {
args := m.Called(ctx)
return args.Get(0).(int64), args.Error(1)
}
// Implement other methods of the TranslationRepository interface if needed for other tests
type MockAuthorRepository struct {
mock.Mock
domain.AuthorRepository
}
func (m *MockAuthorRepository) Count(ctx context.Context) (int64, error) {
args := m.Called(ctx)
return args.Get(0).(int64), args.Error(1)
}
// Implement other methods of the AuthorRepository interface if needed for other tests
type MockUserRepository struct {
mock.Mock
domain.UserRepository
}
func (m *MockUserRepository) Count(ctx context.Context) (int64, error) {
args := m.Called(ctx)
return args.Get(0).(int64), args.Error(1)
}
// Implement other methods of the UserRepository interface if needed for other tests
type MockLikeRepository struct {
mock.Mock
domain.LikeRepository
}
func (m *MockLikeRepository) Count(ctx context.Context) (int64, error) {
args := m.Called(ctx)
return args.Get(0).(int64), args.Error(1)
}
// Implement other methods of the LikeRepository interface if needed for other tests
func TestAnalyticsService_GetAnalytics(t *testing.T) {
ctx := context.Background()
mockWorkRepo := new(MockWorkRepository)
mockTranslationRepo := new(MockTranslationRepository)
mockAuthorRepo := new(MockAuthorRepository)
mockUserRepo := new(MockUserRepository)
mockLikeRepo := new(MockLikeRepository)
mockWorkRepo.On("Count", ctx).Return(int64(10), nil)
mockTranslationRepo.On("Count", ctx).Return(int64(20), nil)
mockAuthorRepo.On("Count", ctx).Return(int64(5), nil)
mockUserRepo.On("Count", ctx).Return(int64(100), nil)
mockLikeRepo.On("Count", ctx).Return(int64(50), nil)
service := NewAnalyticsService(mockWorkRepo, mockTranslationRepo, mockAuthorRepo, mockUserRepo, mockLikeRepo)
analytics, err := service.GetAnalytics(ctx)
assert.NoError(t, err)
assert.NotNil(t, analytics)
assert.Equal(t, int64(10), analytics.TotalWorks)
assert.Equal(t, int64(20), analytics.TotalTranslations)
assert.Equal(t, int64(5), analytics.TotalAuthors)
assert.Equal(t, int64(100), analytics.TotalUsers)
assert.Equal(t, int64(50), analytics.TotalLikes)
mockWorkRepo.AssertExpectations(t)
mockTranslationRepo.AssertExpectations(t)
mockAuthorRepo.AssertExpectations(t)
mockUserRepo.AssertExpectations(t)
mockLikeRepo.AssertExpectations(t)
}