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.
134 lines
4.2 KiB
Go
134 lines
4.2 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"tercul/internal/domain"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// MockUserRepository is a mock implementation of the UserRepository interface.
|
|
type MockUserRepository struct {
|
|
Users []*domain.User
|
|
}
|
|
|
|
// NewMockUserRepository creates a new MockUserRepository.
|
|
func NewMockUserRepository() *MockUserRepository {
|
|
return &MockUserRepository{Users: []*domain.User{}}
|
|
}
|
|
|
|
// Create adds a new user to the mock repository.
|
|
func (m *MockUserRepository) Create(ctx context.Context, user *domain.User) error {
|
|
user.ID = uint(len(m.Users) + 1)
|
|
m.Users = append(m.Users, user)
|
|
return nil
|
|
}
|
|
|
|
// GetByID retrieves a user by their ID from the mock repository.
|
|
func (m *MockUserRepository) GetByID(ctx context.Context, id uint) (*domain.User, error) {
|
|
for _, u := range m.Users {
|
|
if u.ID == id {
|
|
return u, nil
|
|
}
|
|
}
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
// FindByUsername retrieves a user by their username from the mock repository.
|
|
func (m *MockUserRepository) FindByUsername(ctx context.Context, username string) (*domain.User, error) {
|
|
for _, u := range m.Users {
|
|
if strings.EqualFold(u.Username, username) {
|
|
return u, nil
|
|
}
|
|
}
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
// FindByEmail retrieves a user by their email from the mock repository.
|
|
func (m *MockUserRepository) FindByEmail(ctx context.Context, email string) (*domain.User, error) {
|
|
for _, u := range m.Users {
|
|
if strings.EqualFold(u.Email, email) {
|
|
return u, nil
|
|
}
|
|
}
|
|
return nil, gorm.ErrRecordNotFound
|
|
}
|
|
|
|
// ListByRole retrieves users by their role from the mock repository.
|
|
func (m *MockUserRepository) ListByRole(ctx context.Context, role domain.UserRole) ([]domain.User, error) {
|
|
var users []domain.User
|
|
for _, u := range m.Users {
|
|
if u.Role == role {
|
|
users = append(users, *u)
|
|
}
|
|
}
|
|
return users, nil
|
|
}
|
|
|
|
// The rest of the BaseRepository methods can be stubbed out or implemented as needed.
|
|
func (m *MockUserRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *domain.User) error {
|
|
return m.Create(ctx, entity)
|
|
}
|
|
func (m *MockUserRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.User, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
func (m *MockUserRepository) Update(ctx context.Context, entity *domain.User) error {
|
|
for i, u := range m.Users {
|
|
if u.ID == entity.ID {
|
|
m.Users[i] = entity
|
|
return nil
|
|
}
|
|
}
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
func (m *MockUserRepository) UpdateInTx(ctx context.Context, tx *gorm.DB, entity *domain.User) error {
|
|
return m.Update(ctx, entity)
|
|
}
|
|
func (m *MockUserRepository) Delete(ctx context.Context, id uint) error {
|
|
for i, u := range m.Users {
|
|
if u.ID == id {
|
|
m.Users = append(m.Users[:i], m.Users[i+1:]...)
|
|
return nil
|
|
}
|
|
}
|
|
return gorm.ErrRecordNotFound
|
|
}
|
|
func (m *MockUserRepository) DeleteInTx(ctx context.Context, tx *gorm.DB, id uint) error {
|
|
return m.Delete(ctx, id)
|
|
}
|
|
func (m *MockUserRepository) List(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.User], error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockUserRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]domain.User, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockUserRepository) ListAll(ctx context.Context) ([]domain.User, error) {
|
|
var users []domain.User
|
|
for _, u := range m.Users {
|
|
users = append(users, *u)
|
|
}
|
|
return users, nil
|
|
}
|
|
func (m *MockUserRepository) Count(ctx context.Context) (int64, error) {
|
|
return int64(len(m.Users)), nil
|
|
}
|
|
func (m *MockUserRepository) CountWithOptions(ctx context.Context, options *domain.QueryOptions) (int64, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockUserRepository) FindWithPreload(ctx context.Context, preloads []string, id uint) (*domain.User, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
func (m *MockUserRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]domain.User, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockUserRepository) Exists(ctx context.Context, id uint) (bool, error) {
|
|
_, err := m.GetByID(ctx, id)
|
|
return err == nil, nil
|
|
}
|
|
func (m *MockUserRepository) BeginTx(ctx context.Context) (*gorm.DB, error) {
|
|
return nil, nil
|
|
}
|
|
func (m *MockUserRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error {
|
|
return fn(nil)
|
|
} |