mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit introduces a comprehensive enhancement of the application's analytics features, addressing performance, data modeling, and feature set. The key changes include: - **Performance Improvement:** The analytics repository now uses a database "UPSERT" operation to increment counters, reducing two separate database calls (read and write) into a single, more efficient operation. - **New Metrics:** The `WorkStats` and `TranslationStats` models have been enriched with new, calculated metrics: - `ReadingTime`: An estimation of the time required to read the work or translation. - `Complexity`: A score representing the linguistic complexity of the text. - `Sentiment`: A score indicating the emotional tone of the text. - **Service Refactoring:** The analytics service has been refactored to support the new metrics. It now includes methods to calculate and update these scores, leveraging the existing linguistics package for text analysis. - **GraphQL API Expansion:** The new analytics fields (`readingTime`, `complexity`, `sentiment`) have been exposed through the GraphQL API by updating the `WorkStats` and `TranslationStats` types in the schema. - **Validation and Testing:** - GraphQL input validation has been centralized and improved by moving from ad-hoc checks to a consistent validation pattern in the GraphQL layer. - The test suite has been significantly improved with the addition of new tests for the analytics service and the data access layer, ensuring the correctness and robustness of the new features. This includes fixing several bugs that were discovered during the development process.
238 lines
7.2 KiB
Go
238 lines
7.2 KiB
Go
package analytics_test
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
"tercul/internal/app/analytics"
|
|
"tercul/internal/data/sql"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/jobs/linguistics"
|
|
"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)
|
|
analysisRepo := linguistics.NewGORMAnalysisRepository(s.DB)
|
|
translationRepo := sql.NewTranslationRepository(s.DB)
|
|
sentimentProvider, _ := linguistics.NewGoVADERSentimentProvider()
|
|
s.service = analytics.NewService(analyticsRepo, analysisRepo, translationRepo, sentimentProvider)
|
|
}
|
|
|
|
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 (s *AnalyticsServiceTestSuite) TestUpdateWorkReadingTime() {
|
|
s.Run("should update the reading time for a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
s.DB.Create(&domain.ReadabilityScore{WorkID: work.ID})
|
|
s.DB.Create(&domain.LanguageAnalysis{WorkID: work.ID, Analysis: domain.JSONB{}})
|
|
textMetadata := &domain.TextMetadata{
|
|
WorkID: work.ID,
|
|
WordCount: 1000,
|
|
}
|
|
s.DB.Create(textMetadata)
|
|
|
|
// Act
|
|
err := s.service.UpdateWorkReadingTime(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateWorkStats(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(5, stats.ReadingTime) // 1000 words / 200 wpm = 5 minutes
|
|
})
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) TestUpdateTranslationReadingTime() {
|
|
s.Run("should update the reading time for a translation", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
translation := s.CreateTestTranslation(work.ID, "es", strings.Repeat("Contenido de prueba con quinientas palabras. ", 100))
|
|
|
|
// Act
|
|
err := s.service.UpdateTranslationReadingTime(context.Background(), translation.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateTranslationStats(context.Background(), translation.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(3, stats.ReadingTime) // 500 words / 200 wpm = 2.5 -> 3 minutes
|
|
})
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) TestUpdateWorkComplexity() {
|
|
s.Run("should update the complexity for a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
s.DB.Create(&domain.TextMetadata{WorkID: work.ID})
|
|
s.DB.Create(&domain.LanguageAnalysis{WorkID: work.ID, Analysis: domain.JSONB{}})
|
|
readabilityScore := &domain.ReadabilityScore{
|
|
WorkID: work.ID,
|
|
Score: 12.34,
|
|
}
|
|
s.DB.Create(readabilityScore)
|
|
|
|
// Act
|
|
err := s.service.UpdateWorkComplexity(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateWorkStats(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(12.34, stats.Complexity)
|
|
})
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) TestUpdateWorkSentiment() {
|
|
s.Run("should update the sentiment for a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
s.DB.Create(&domain.TextMetadata{WorkID: work.ID})
|
|
s.DB.Create(&domain.ReadabilityScore{WorkID: work.ID})
|
|
languageAnalysis := &domain.LanguageAnalysis{
|
|
WorkID: work.ID,
|
|
Analysis: domain.JSONB{
|
|
"sentiment": 0.5678,
|
|
},
|
|
}
|
|
s.DB.Create(languageAnalysis)
|
|
|
|
// Act
|
|
err := s.service.UpdateWorkSentiment(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateWorkStats(context.Background(), work.ID)
|
|
s.Require().NoError(err)
|
|
s.Equal(0.5678, stats.Sentiment)
|
|
})
|
|
}
|
|
|
|
func (s *AnalyticsServiceTestSuite) TestUpdateTranslationSentiment() {
|
|
s.Run("should update the sentiment for a translation", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
translation := s.CreateTestTranslation(work.ID, "en", "This is a wonderfully positive and uplifting sentence.")
|
|
|
|
// Act
|
|
err := s.service.UpdateTranslationSentiment(context.Background(), translation.ID)
|
|
s.Require().NoError(err)
|
|
|
|
// Assert
|
|
stats, err := s.service.GetOrCreateTranslationStats(context.Background(), translation.ID)
|
|
s.Require().NoError(err)
|
|
s.True(stats.Sentiment > 0.5)
|
|
})
|
|
}
|
|
|
|
func TestAnalyticsService(t *testing.T) {
|
|
suite.Run(t, new(AnalyticsServiceTestSuite))
|
|
}
|