mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 00:31:35 +00:00
This commit introduces analytics features to the application. It includes: - Extended domain models for storing analytics data. - An analytics repository and service for managing the data. - Integration with GraphQL mutations to update analytics counts. - New GraphQL queries to expose analytics data. - Unit and integration tests for the new features.
127 lines
3.5 KiB
Go
127 lines
3.5 KiB
Go
package analytics_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"tercul/internal/app/analytics"
|
|
"tercul/internal/data/sql"
|
|
"tercul/internal/testutil"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type AnalyticsServiceTestSuite struct {
|
|
testutil.IntegrationTestSuite
|
|
service analytics.Service
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) SetupSuite() {
|
|
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
|
|
analyticsRepo := sql.NewAnalyticsRepository(s.DB)
|
|
s.service = analytics.NewService(analyticsRepo)
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) SetupTest() {
|
|
s.IntegrationTestSuite.SetupTest()
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) TestIncrementWorkViews() {
|
|
s.Run("should increment the view count for a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
|
|
// Act
|
|
err := s.service.IncrementWorkViews(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateWorkStats(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(int64(1), stats.Views)
|
|
})
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) TestIncrementWorkLikes() {
|
|
s.Run("should increment the like count for a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
|
|
// Act
|
|
err := s.service.IncrementWorkLikes(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateWorkStats(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(int64(1), stats.Likes)
|
|
})
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) TestIncrementWorkComments() {
|
|
s.Run("should increment the comment count for a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
|
|
// Act
|
|
err := s.service.IncrementWorkComments(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateWorkStats(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(int64(1), stats.Comments)
|
|
})
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) TestIncrementWorkBookmarks() {
|
|
s.Run("should increment the bookmark count for a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
|
|
// Act
|
|
err := s.service.IncrementWorkBookmarks(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateWorkStats(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(int64(1), stats.Bookmarks)
|
|
})
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) TestIncrementWorkShares() {
|
|
s.Run("should increment the share count for a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
|
|
// Act
|
|
err := s.service.IncrementWorkShares(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateWorkStats(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(int64(1), stats.Shares)
|
|
})
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) TestIncrementWorkTranslationCount() {
|
|
s.Run("should increment the translation count for a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
|
|
// Act
|
|
err := s.service.IncrementWorkTranslationCount(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateWorkStats(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(int64(1), stats.TranslationCount)
|
|
})
|
|
}
|
|
|
|
func TestAnalyticsService(t *testing.T) {
|
|
suite.Run(t, new(AnalyticsServiceTestSuite))
|
|
}
|