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.
124 lines
4.2 KiB
Go
124 lines
4.2 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"tercul/internal/domain"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// MockWorkRepository is a mock implementation of the WorkRepository interface.
|
|
type MockWorkRepository struct {
|
|
mock.Mock
|
|
Works []*domain.Work
|
|
}
|
|
|
|
// NewMockWorkRepository creates a new MockWorkRepository.
|
|
func NewMockWorkRepository() *MockWorkRepository {
|
|
return &MockWorkRepository{Works: []*domain.Work{}}
|
|
}
|
|
|
|
// Create adds a new work to the mock repository.
|
|
func (m *MockWorkRepository) Create(ctx context.Context, work *domain.Work) error {
|
|
work.ID = uint(len(m.Works) + 1)
|
|
m.Works = append(m.Works, work)
|
|
return nil
|
|
}
|
|
|
|
// GetByID retrieves a work by its ID from the mock repository.
|
|
func (m *MockWorkRepository) GetByID(ctx context.Context, id uint) (*domain.Work, error) {
|
|
for _, w := range m.Works {
|
|
if w.ID == id {
|
|
return w, nil
|
|
}
|
|
}
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
// Exists uses the mock's Called method.
|
|
func (m *MockWorkRepository) Exists(ctx context.Context, id uint) (bool, error) {
|
|
args := m.Called(ctx, id)
|
|
return args.Bool(0), args.Error(1)
|
|
}
|
|
|
|
// The rest of the WorkRepository and BaseRepository methods can be stubbed out.
|
|
func (m *MockWorkRepository) FindByTitle(ctx context.Context, title string) ([]domain.Work, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) FindByAuthor(ctx context.Context, authorID uint) ([]domain.Work, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) FindByCategory(ctx context.Context, categoryID uint) ([]domain.Work, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) FindByLanguage(ctx context.Context, language string, page, pageSize int) (*domain.PaginatedResult[domain.Work], error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) GetWithTranslations(ctx context.Context, id uint) (*domain.Work, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
func (m *MockWorkRepository) ListWithTranslations(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.Work], error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Work) error {
|
|
return m.Create(ctx, entity)
|
|
}
|
|
func (m *MockWorkRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Work, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
func (m *MockWorkRepository) Update(ctx context.Context, entity *domain.Work) error {
|
|
for i, w := range m.Works {
|
|
if w.ID == entity.ID {
|
|
m.Works[i] = entity
|
|
return nil
|
|
}
|
|
}
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
func (m *MockWorkRepository) UpdateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Work) error {
|
|
return m.Update(ctx, entity)
|
|
}
|
|
func (m *MockWorkRepository) Delete(ctx context.Context, id uint) error {
|
|
for i, w := range m.Works {
|
|
if w.ID == id {
|
|
m.Works = append(m.Works[:i], m.Works[i+1:]...)
|
|
return nil
|
|
}
|
|
}
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
func (m *MockWorkRepository) DeleteInTx(ctx context.Context, tx *gorm.DB, id uint) error {
|
|
return m.Delete(ctx, id)
|
|
}
|
|
func (m *MockWorkRepository) List(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.Work], error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]domain.Work, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) ListAll(ctx context.Context) ([]domain.Work, error) {
|
|
var works []domain.Work
|
|
for _, w := range m.Works {
|
|
works = append(works, *w)
|
|
}
|
|
return works, nil
|
|
}
|
|
func (m *MockWorkRepository) Count(ctx context.Context) (int64, error) {
|
|
return int64(len(m.Works)), nil
|
|
}
|
|
func (m *MockWorkRepository) CountWithOptions(ctx context.Context, options *domain.QueryOptions) (int64, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) FindWithPreload(ctx context.Context, preloads []string, id uint) (*domain.Work, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
func (m *MockWorkRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]domain.Work, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) BeginTx(ctx context.Context) (*gorm.DB, error) {
|
|
return nil, nil
|
|
}
|
|
func (m *MockWorkRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error {
|
|
return fn(nil)
|
|
} |