tercul-backend/internal/testutil/mock_user_repository.go
google-labs-jules[bot] fa90dd79da feat: Complete large-scale refactor and prepare for production
This commit marks the completion of a major refactoring effort to stabilize the codebase, improve its structure, and prepare it for production.

The key changes include:

- **Domain Layer Consolidation:** The `Work` entity and its related types, along with all other domain entities and repository interfaces, have been consolidated into the main `internal/domain` package. This eliminates import cycles and provides a single, coherent source of truth for the domain model.

- **Data Access Layer Refactoring:** The repository implementations in `internal/data/sql` have been updated to align with the new domain layer. The `BaseRepositoryImpl` has been corrected to use pointer receivers, and all concrete repositories now correctly embed it, ensuring consistent and correct behavior.

- **Application Layer Stabilization:** All application services in `internal/app` have been updated to use the new domain types and repository interfaces. Dependency injection has been corrected throughout the application, ensuring that all services are initialized with the correct dependencies.

- **GraphQL Adapter Fixes:** The GraphQL resolver implementation in `internal/adapters/graphql` has been updated to correctly handle the new domain types and service methods. The auto-generated GraphQL code has been regenerated to ensure it is in sync with the schema and runtime.

- **Test Suite Overhaul:** All test suites have been fixed to correctly implement their respective interfaces and use the updated domain model. Mock repositories and test suites have been corrected to properly embed the `testify` base types, resolving numerous build and linter errors.

- **Dependency Management:** The Go modules have been tidied, and the module cache has been cleaned to ensure a consistent and correct dependency graph.

- **Code Quality and Verification:** The entire codebase now passes all builds, tests, and linter checks, ensuring a high level of quality and stability.

This comprehensive effort has resulted in a more robust, maintainable, and production-ready application.
2025-10-07 11:09:37 +00:00

183 lines
5.1 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) {
start := (page - 1) * pageSize
end := start + pageSize
if start > len(m.Users) {
start = len(m.Users)
}
if end > len(m.Users) {
end = len(m.Users)
}
paginatedUsers := m.Users[start:end]
var users []domain.User
for _, u := range paginatedUsers {
users = append(users, *u)
}
totalCount := int64(len(m.Users))
totalPages := int(totalCount) / pageSize
if int(totalCount)%pageSize != 0 {
totalPages++
}
return &domain.PaginatedResult[domain.User]{
Items: users,
TotalCount: totalCount,
Page: page,
PageSize: pageSize,
TotalPages: totalPages,
HasNext: page < totalPages,
HasPrev: page > 1,
}, nil
}
func (m *MockUserRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]domain.User, error) {
// This is a mock implementation and doesn't handle options.
return m.ListAll(ctx)
}
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) {
// This is a mock implementation and doesn't handle options.
return m.Count(ctx)
}
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) {
start := offset
end := start + batchSize
if start > len(m.Users) {
return []domain.User{}, nil
}
if end > len(m.Users) {
end = len(m.Users)
}
var users []domain.User
for _, u := range m.Users[start:end] {
users = append(users, *u)
}
return users, nil
}
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)
}