mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
This commit refactors the GraphQL test suite to resolve persistent build failures and establish a stable, mock-based unit testing environment. The key changes include: - Consolidating all GraphQL test helper functions into a single, canonical file (`internal/adapters/graphql/graphql_test_utils_test.go`). - Removing duplicated test helper code from `integration_test.go` and other test files. - Creating a new, dedicated unit test file for the `like` and `unlike` mutations (`internal/adapters/graphql/like_resolvers_unit_test.go`) using a mock-based approach. - Introducing mock services (`MockLikeService`, `MockAnalyticsService`) and updating mock repositories (`MockLikeRepository`, `MockWorkRepository`) in the `internal/testutil` package to support `testify/mock`. - Adding a `ContextWithUserID` helper function to `internal/platform/auth/middleware.go` to facilitate testing of authenticated resolvers. These changes resolve the `redeclared in this block` and package collision errors, resulting in a clean and passing test suite. This provides a solid foundation for future Test-Driven Development.
120 lines
3.6 KiB
Go
120 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"
|
|
"tercul/internal/testutil"
|
|
|
|
"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 *testutil.MockLikeRepository
|
|
mockWorkRepo *testutil.MockWorkRepository
|
|
mockAnalyticsSvc *testutil.MockAnalyticsService
|
|
}
|
|
|
|
func (s *LikeResolversUnitSuite) SetupTest() {
|
|
// 1. Create mock repositories
|
|
s.mockLikeRepo = new(testutil.MockLikeRepository)
|
|
s.mockWorkRepo = new(testutil.MockWorkRepository)
|
|
s.mockAnalyticsSvc = new(testutil.MockAnalyticsService)
|
|
|
|
// 2. Create real services with mock repositories
|
|
likeService := like.NewService(s.mockLikeRepo)
|
|
analyticsService := analytics.NewService(s.mockAnalyticsSvc, nil, nil, nil, nil)
|
|
|
|
// 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))
|
|
} |