mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 02:51:34 +00:00
- Fixes a potential panic in the background job queue by changing `log.Fatalf` to `log.Printf`, allowing for more graceful error handling.
- Implements all `panic("not implemented")` methods in the mock repositories for `Like`, `Work`, and `User`, enabling more robust testing.
- Consolidates duplicated `WorkAnalytics` and `TranslationAnalytics` structs into a central `internal/domain/analytics` package to reduce code duplication and improve maintainability.
- Corrects build errors that arose during testing, including an unused import and an incorrect struct field name in a mock repository.
167 lines
5.0 KiB
Go
167 lines
5.0 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)
|
|
}
|
|
users := m.Users[start:end]
|
|
var resultUsers []domain.User
|
|
for _, u := range users {
|
|
resultUsers = append(resultUsers, *u)
|
|
}
|
|
return &domain.PaginatedResult[domain.User]{
|
|
Items: resultUsers,
|
|
TotalCount: int64(len(m.Users)),
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
}, nil
|
|
}
|
|
func (m *MockUserRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]domain.User, error) {
|
|
// This is a mock implementation, so we'll just return all users for now.
|
|
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, so we'll just return the total count for now.
|
|
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)
|
|
}
|
|
users := m.Users[start:end]
|
|
var resultUsers []domain.User
|
|
for _, u := range users {
|
|
resultUsers = append(resultUsers, *u)
|
|
}
|
|
return resultUsers, 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)
|
|
} |