mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 00:31:35 +00:00
260 lines
7.1 KiB
Go
260 lines
7.1 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"gorm.io/gorm"
|
|
"tercul/internal/models"
|
|
"tercul/internal/repositories"
|
|
)
|
|
|
|
// UnifiedMockWorkRepository is a shared mock for WorkRepository tests
|
|
// Implements all required methods and uses an in-memory slice
|
|
|
|
type UnifiedMockWorkRepository struct {
|
|
Works []*models.Work
|
|
}
|
|
|
|
func NewUnifiedMockWorkRepository() *UnifiedMockWorkRepository {
|
|
return &UnifiedMockWorkRepository{Works: []*models.Work{}}
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) AddWork(work *models.Work) {
|
|
work.ID = uint(len(m.Works) + 1)
|
|
if work.Language == "" {
|
|
work.Language = "en" // default for tests, can be set by caller
|
|
}
|
|
m.Works = append(m.Works, work)
|
|
}
|
|
|
|
// BaseRepository methods with context support
|
|
func (m *UnifiedMockWorkRepository) Create(ctx context.Context, entity *models.Work) error {
|
|
m.AddWork(entity)
|
|
return nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) GetByID(ctx context.Context, id uint) (*models.Work, error) {
|
|
for _, w := range m.Works {
|
|
if w.ID == id {
|
|
return w, nil
|
|
}
|
|
}
|
|
return nil, repositories.ErrEntityNotFound
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) Update(ctx context.Context, entity *models.Work) error {
|
|
for i, w := range m.Works {
|
|
if w.ID == entity.ID {
|
|
m.Works[i] = entity
|
|
return nil
|
|
}
|
|
}
|
|
return repositories.ErrEntityNotFound
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) 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 repositories.ErrEntityNotFound
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) List(ctx context.Context, page, pageSize int) (*repositories.PaginatedResult[models.Work], error) {
|
|
var all []models.Work
|
|
for _, w := range m.Works {
|
|
if w != nil {
|
|
all = append(all, *w)
|
|
}
|
|
}
|
|
total := int64(len(all))
|
|
start := (page - 1) * pageSize
|
|
end := start + pageSize
|
|
if start > len(all) {
|
|
return &repositories.PaginatedResult[models.Work]{Items: []models.Work{}, TotalCount: total}, nil
|
|
}
|
|
if end > len(all) {
|
|
end = len(all)
|
|
}
|
|
return &repositories.PaginatedResult[models.Work]{Items: all[start:end], TotalCount: total}, nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) ListAll(ctx context.Context) ([]models.Work, error) {
|
|
var all []models.Work
|
|
for _, w := range m.Works {
|
|
if w != nil {
|
|
all = append(all, *w)
|
|
}
|
|
}
|
|
return all, nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) Count(ctx context.Context) (int64, error) {
|
|
return int64(len(m.Works)), nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) FindWithPreload(ctx context.Context, preloads []string, id uint) (*models.Work, error) {
|
|
for _, w := range m.Works {
|
|
if w.ID == id {
|
|
return w, nil
|
|
}
|
|
}
|
|
return nil, repositories.ErrEntityNotFound
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]models.Work, error) {
|
|
var result []models.Work
|
|
end := offset + batchSize
|
|
if end > len(m.Works) {
|
|
end = len(m.Works)
|
|
}
|
|
for i := offset; i < end; i++ {
|
|
if m.Works[i] != nil {
|
|
result = append(result, *m.Works[i])
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// New BaseRepository methods
|
|
func (m *UnifiedMockWorkRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *models.Work) error {
|
|
return m.Create(ctx, entity)
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) GetByIDWithOptions(ctx context.Context, id uint, options *repositories.QueryOptions) (*models.Work, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) UpdateInTx(ctx context.Context, tx *gorm.DB, entity *models.Work) error {
|
|
return m.Update(ctx, entity)
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) DeleteInTx(ctx context.Context, tx *gorm.DB, id uint) error {
|
|
return m.Delete(ctx, id)
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) ListWithOptions(ctx context.Context, options *repositories.QueryOptions) ([]models.Work, error) {
|
|
result, err := m.List(ctx, 1, 1000)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Items, nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) CountWithOptions(ctx context.Context, options *repositories.QueryOptions) (int64, error) {
|
|
return m.Count(ctx)
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) Exists(ctx context.Context, id uint) (bool, error) {
|
|
_, err := m.GetByID(ctx, id)
|
|
return err == nil, nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) BeginTx(ctx context.Context) (*gorm.DB, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error {
|
|
return fn(nil)
|
|
}
|
|
|
|
// WorkRepository specific methods
|
|
func (m *UnifiedMockWorkRepository) FindByTitle(ctx context.Context, title string) ([]models.Work, error) {
|
|
var result []models.Work
|
|
for _, w := range m.Works {
|
|
if len(title) == 0 || (len(w.Title) >= len(title) && w.Title[:len(title)] == title) {
|
|
result = append(result, *w)
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) FindByLanguage(ctx context.Context, language string, page, pageSize int) (*repositories.PaginatedResult[models.Work], error) {
|
|
var filtered []models.Work
|
|
for _, w := range m.Works {
|
|
if w.Language == language {
|
|
filtered = append(filtered, *w)
|
|
}
|
|
}
|
|
total := int64(len(filtered))
|
|
start := (page - 1) * pageSize
|
|
end := start + pageSize
|
|
if start > len(filtered) {
|
|
return &repositories.PaginatedResult[models.Work]{Items: []models.Work{}, TotalCount: total}, nil
|
|
}
|
|
if end > len(filtered) {
|
|
end = len(filtered)
|
|
}
|
|
return &repositories.PaginatedResult[models.Work]{Items: filtered[start:end], TotalCount: total}, nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) FindByAuthor(ctx context.Context, authorID uint) ([]models.Work, error) {
|
|
result := make([]models.Work, len(m.Works))
|
|
for i, w := range m.Works {
|
|
if w != nil {
|
|
result[i] = *w
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) FindByCategory(ctx context.Context, categoryID uint) ([]models.Work, error) {
|
|
result := make([]models.Work, len(m.Works))
|
|
for i, w := range m.Works {
|
|
if w != nil {
|
|
result[i] = *w
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) GetWithTranslations(ctx context.Context, id uint) (*models.Work, error) {
|
|
for _, w := range m.Works {
|
|
if w.ID == id {
|
|
return w, nil
|
|
}
|
|
}
|
|
return nil, repositories.ErrEntityNotFound
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) ListWithTranslations(ctx context.Context, page, pageSize int) (*repositories.PaginatedResult[models.Work], error) {
|
|
var all []models.Work
|
|
for _, w := range m.Works {
|
|
if w != nil {
|
|
all = append(all, *w)
|
|
}
|
|
}
|
|
total := int64(len(all))
|
|
start := (page - 1) * pageSize
|
|
end := start + pageSize
|
|
if start > len(all) {
|
|
return &repositories.PaginatedResult[models.Work]{Items: []models.Work{}, TotalCount: total}, nil
|
|
}
|
|
if end > len(all) {
|
|
end = len(all)
|
|
}
|
|
return &repositories.PaginatedResult[models.Work]{Items: all[start:end], TotalCount: total}, nil
|
|
}
|
|
|
|
func (m *UnifiedMockWorkRepository) Reset() {
|
|
m.Works = []*models.Work{}
|
|
}
|
|
|
|
// Add helper to get GraphQL-style Work with Name mapped from Title
|
|
func (m *UnifiedMockWorkRepository) GetGraphQLWorkByID(id uint) map[string]interface{} {
|
|
for _, w := range m.Works {
|
|
if w.ID == id {
|
|
return map[string]interface{}{
|
|
"id": w.ID,
|
|
"name": w.Title,
|
|
"language": w.Language,
|
|
"content": "",
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Add other interface methods as needed for your tests
|