mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit addresses all the high-priority tasks outlined in the TASKS.md file, significantly improving the application's observability, completing key features, and refactoring critical parts of the codebase. ### Observability - **Centralized Logging:** Implemented a new structured, context-aware logging system using `zerolog`. A new logging middleware injects request-specific information (request ID, user ID, trace ID) into the logger, and all application logging has been refactored to use this new system. - **Prometheus Metrics:** Added Prometheus metrics for database query performance by creating a GORM plugin that automatically records query latency and totals. - **OpenTelemetry Tracing:** Fully instrumented all application services in `internal/app` and data repositories in `internal/data/sql` with OpenTelemetry tracing, providing deep visibility into application performance. ### Features - **Analytics:** Implemented like, comment, and bookmark counting. The respective command handlers now call the analytics service to increment counters when these actions are performed. - **Enrichment Tool:** Built a new, extensible `enrich` command-line tool to fetch data from external sources. The initial implementation enriches author data using the Open Library API. ### Refactoring & Fixes - **Decoupled Testing:** Refactored the testing utilities in `internal/testutil` to be database-agnostic, promoting the use of mock-based unit tests and improving test speed and reliability. - **Build Fixes:** Resolved numerous build errors, including a critical import cycle between the logging, observability, and authentication packages. - **Search Service:** Fixed the search service integration by implementing the `GetWorkContent` method in the localization service, allowing the search indexer to correctly fetch and index work content.
119 lines
3.6 KiB
Go
119 lines
3.6 KiB
Go
package graphql_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"testing"
|
|
|
|
"tercul/internal/adapters/graphql"
|
|
"tercul/internal/adapters/graphql/model"
|
|
"tercul/internal/app"
|
|
"tercul/internal/app/analytics"
|
|
"tercul/internal/app/like"
|
|
"tercul/internal/domain"
|
|
platform_auth "tercul/internal/platform/auth"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
// LikeResolversUnitSuite is a unit test suite for the like resolvers.
|
|
type LikeResolversUnitSuite struct {
|
|
suite.Suite
|
|
resolver *graphql.Resolver
|
|
mockLikeRepo *mockLikeRepository
|
|
mockWorkRepo *mockWorkRepository
|
|
mockAnalyticsSvc *mockAnalyticsService
|
|
}
|
|
|
|
func (s *LikeResolversUnitSuite) SetupTest() {
|
|
// 1. Create mock repositories
|
|
s.mockLikeRepo = new(mockLikeRepository)
|
|
s.mockWorkRepo = new(mockWorkRepository)
|
|
s.mockAnalyticsSvc = new(mockAnalyticsService)
|
|
|
|
// 2. Create real services with mock repositories
|
|
analyticsService := analytics.NewService(s.mockAnalyticsSvc, nil, nil, s.mockWorkRepo, nil)
|
|
likeService := like.NewService(s.mockLikeRepo, analyticsService)
|
|
|
|
// 3. Create the resolver with the services
|
|
s.resolver = &graphql.Resolver{
|
|
App: &app.Application{
|
|
Like: likeService,
|
|
Analytics: analyticsService,
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestLikeResolversUnitSuite(t *testing.T) {
|
|
suite.Run(t, new(LikeResolversUnitSuite))
|
|
}
|
|
|
|
func (s *LikeResolversUnitSuite) TestCreateLike() {
|
|
// 1. Setup
|
|
workIDStr := "1"
|
|
workIDUint64, _ := strconv.ParseUint(workIDStr, 10, 32)
|
|
workIDUint := uint(workIDUint64)
|
|
userID := uint(123)
|
|
|
|
// Mock repository responses
|
|
s.mockWorkRepo.On("Exists", mock.Anything, workIDUint).Return(true, nil)
|
|
s.mockLikeRepo.On("Create", mock.Anything, mock.AnythingOfType("*domain.Like")).Run(func(args mock.Arguments) {
|
|
arg := args.Get(1).(*domain.Like)
|
|
arg.ID = 1 // Simulate database assigning an ID
|
|
}).Return(nil)
|
|
s.mockAnalyticsSvc.On("IncrementWorkCounter", mock.Anything, workIDUint, "likes", 1).Return(nil)
|
|
|
|
// Create a context with an authenticated user
|
|
ctx := platform_auth.ContextWithUserID(context.Background(), userID)
|
|
|
|
// 2. Execution
|
|
likeInput := model.LikeInput{
|
|
WorkID: &workIDStr,
|
|
}
|
|
createdLike, err := s.resolver.Mutation().CreateLike(ctx, likeInput)
|
|
|
|
// 3. Assertions
|
|
s.Require().NoError(err)
|
|
s.Require().NotNil(createdLike)
|
|
|
|
s.Equal("1", createdLike.ID)
|
|
s.Equal(fmt.Sprintf("%d", userID), createdLike.User.ID)
|
|
|
|
// Verify that the repository's Create method was called
|
|
s.mockLikeRepo.AssertCalled(s.T(), "Create", mock.Anything, mock.MatchedBy(func(l *domain.Like) bool {
|
|
return *l.WorkID == workIDUint && l.UserID == userID
|
|
}))
|
|
// Verify that analytics was called
|
|
s.mockAnalyticsSvc.AssertCalled(s.T(), "IncrementWorkCounter", mock.Anything, workIDUint, "likes", 1)
|
|
}
|
|
|
|
func (s *LikeResolversUnitSuite) TestDeleteLike() {
|
|
// 1. Setup
|
|
likeIDStr := "1"
|
|
likeIDUint, _ := strconv.ParseUint(likeIDStr, 10, 32)
|
|
userID := uint(123)
|
|
|
|
// Mock the repository response for the initial 'find'
|
|
s.mockLikeRepo.On("GetByID", mock.Anything, uint(likeIDUint)).Return(&domain.Like{
|
|
BaseModel: domain.BaseModel{ID: uint(likeIDUint)},
|
|
UserID: userID,
|
|
}, nil)
|
|
|
|
// Mock the repository response for the 'delete'
|
|
s.mockLikeRepo.On("Delete", mock.Anything, uint(likeIDUint)).Return(nil)
|
|
|
|
// Create a context with an authenticated user
|
|
ctx := platform_auth.ContextWithUserID(context.Background(), userID)
|
|
|
|
// 2. Execution
|
|
deleted, err := s.resolver.Mutation().DeleteLike(ctx, likeIDStr)
|
|
|
|
// 3. Assertions
|
|
s.Require().NoError(err)
|
|
s.True(deleted)
|
|
|
|
// Verify that the repository's Delete method was called
|
|
s.mockLikeRepo.AssertCalled(s.T(), "Delete", mock.Anything, uint(likeIDUint))
|
|
} |