mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit isolates the `Work` aggregate into its own package at `internal/domain/work`, following the first step of the refactoring plan in `refactor.md`. - The `Work` struct, related types, and the `WorkRepository` interface have been moved to the new package. - A circular dependency between `domain` and `work` was resolved by moving the `AnalyticsRepository` to the `app` layer. - All references to the moved types have been updated across the entire codebase to fix compilation errors. - Test files, including mocks and integration tests, have been updated to reflect the new structure.
125 lines
4.2 KiB
Go
125 lines
4.2 KiB
Go
package testutil
|
|
|
|
import (
|
|
"context"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/domain/work"
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// MockWorkRepository is a mock implementation of the WorkRepository interface.
|
|
type MockWorkRepository struct {
|
|
mock.Mock
|
|
Works []*work.Work
|
|
}
|
|
|
|
// NewMockWorkRepository creates a new MockWorkRepository.
|
|
func NewMockWorkRepository() *MockWorkRepository {
|
|
return &MockWorkRepository{Works: []*work.Work{}}
|
|
}
|
|
|
|
// Create adds a new work to the mock repository.
|
|
func (m *MockWorkRepository) Create(ctx context.Context, work *work.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) (*work.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) ([]work.Work, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) FindByAuthor(ctx context.Context, authorID uint) ([]work.Work, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) FindByCategory(ctx context.Context, categoryID uint) ([]work.Work, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) FindByLanguage(ctx context.Context, language string, page, pageSize int) (*domain.PaginatedResult[work.Work], error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) GetWithTranslations(ctx context.Context, id uint) (*work.Work, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
func (m *MockWorkRepository) ListWithTranslations(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[work.Work], error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *work.Work) error {
|
|
return m.Create(ctx, entity)
|
|
}
|
|
func (m *MockWorkRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*work.Work, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
func (m *MockWorkRepository) Update(ctx context.Context, entity *work.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 *work.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[work.Work], error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]work.Work, error) {
|
|
panic("not implemented")
|
|
}
|
|
func (m *MockWorkRepository) ListAll(ctx context.Context) ([]work.Work, error) {
|
|
var works []work.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) (*work.Work, error) {
|
|
return m.GetByID(ctx, id)
|
|
}
|
|
func (m *MockWorkRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]work.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)
|
|
} |