tercul-backend/internal/testutil/mock_like_repository.go
google-labs-jules[bot] 80cfe71e59 refactor: Refactor GraphQL tests to use mock-based unit tests
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.
2025-10-03 09:21:41 +00:00

152 lines
4.6 KiB
Go

package testutil
import (
"context"
"tercul/internal/domain"
"github.com/stretchr/testify/mock"
"gorm.io/gorm"
)
// MockLikeRepository is a mock implementation of the LikeRepository interface.
type MockLikeRepository struct {
mock.Mock
Likes []*domain.Like // Keep for other potential tests, but new mocks will use testify
}
// NewMockLikeRepository creates a new MockLikeRepository.
func NewMockLikeRepository() *MockLikeRepository {
return &MockLikeRepository{Likes: []*domain.Like{}}
}
// Create uses the mock's Called method.
func (m *MockLikeRepository) Create(ctx context.Context, like *domain.Like) error {
args := m.Called(ctx, like)
return args.Error(0)
}
// GetByID retrieves a like by its ID from the mock repository.
func (m *MockLikeRepository) GetByID(ctx context.Context, id uint) (*domain.Like, error) {
args := m.Called(ctx, id)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*domain.Like), args.Error(1)
}
// ListByUserID retrieves likes by their user ID from the mock repository.
func (m *MockLikeRepository) ListByUserID(ctx context.Context, userID uint) ([]domain.Like, error) {
var likes []domain.Like
for _, l := range m.Likes {
if l.UserID == userID {
likes = append(likes, *l)
}
}
return likes, nil
}
// ListByWorkID retrieves likes by their work ID from the mock repository.
func (m *MockLikeRepository) ListByWorkID(ctx context.Context, workID uint) ([]domain.Like, error) {
var likes []domain.Like
for _, l := range m.Likes {
if l.WorkID != nil && *l.WorkID == workID {
likes = append(likes, *l)
}
}
return likes, nil
}
// ListByTranslationID retrieves likes by their translation ID from the mock repository.
func (m *MockLikeRepository) ListByTranslationID(ctx context.Context, translationID uint) ([]domain.Like, error) {
var likes []domain.Like
for _, l := range m.Likes {
if l.TranslationID != nil && *l.TranslationID == translationID {
likes = append(likes, *l)
}
}
return likes, nil
}
// ListByCommentID retrieves likes by their comment ID from the mock repository.
func (m *MockLikeRepository) ListByCommentID(ctx context.Context, commentID uint) ([]domain.Like, error) {
var likes []domain.Like
for _, l := range m.Likes {
if l.CommentID != nil && *l.CommentID == commentID {
likes = append(likes, *l)
}
}
return likes, nil
}
// The rest of the BaseRepository methods can be stubbed out or implemented as needed.
func (m *MockLikeRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Like) error {
return m.Create(ctx, entity)
}
func (m *MockLikeRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Like, error) {
return m.GetByID(ctx, id)
}
func (m *MockLikeRepository) Update(ctx context.Context, entity *domain.Like) error {
args := m.Called(ctx, entity)
return args.Error(0)
}
func (m *MockLikeRepository) UpdateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Like) error {
return m.Update(ctx, entity)
}
func (m *MockLikeRepository) Delete(ctx context.Context, id uint) error {
args := m.Called(ctx, id)
return args.Error(0)
}
func (m *MockLikeRepository) DeleteInTx(ctx context.Context, tx *gorm.DB, id uint) error {
return m.Delete(ctx, id)
}
func (m *MockLikeRepository) List(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.Like], error) {
panic("not implemented")
}
func (m *MockLikeRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]domain.Like, error) {
panic("not implemented")
}
func (m *MockLikeRepository) ListAll(ctx context.Context) ([]domain.Like, error) {
var likes []domain.Like
for _, l := range m.Likes {
likes = append(likes, *l)
}
return likes, nil
}
func (m *MockLikeRepository) Count(ctx context.Context) (int64, error) {
return int64(len(m.Likes)), nil
}
func (m *MockLikeRepository) CountWithOptions(ctx context.Context, options *domain.QueryOptions) (int64, error) {
panic("not implemented")
}
func (m *MockLikeRepository) FindWithPreload(ctx context.Context, preloads []string, id uint) (*domain.Like, error) {
return m.GetByID(ctx, id)
}
func (m *MockLikeRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]domain.Like, error) {
panic("not implemented")
}
func (m *MockLikeRepository) Exists(ctx context.Context, id uint) (bool, error) {
args := m.Called(ctx, id)
return args.Bool(0), args.Error(1)
}
func (m *MockLikeRepository) BeginTx(ctx context.Context) (*gorm.DB, error) {
return nil, nil
}
func (m *MockLikeRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error {
return fn(nil)
}