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
3.8 KiB
Go
125 lines
3.8 KiB
Go
package work
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/suite"
|
|
"tercul/internal/domain"
|
|
workdomain "tercul/internal/domain/work"
|
|
"testing"
|
|
)
|
|
|
|
type WorkCommandsSuite struct {
|
|
suite.Suite
|
|
repo *mockWorkRepository
|
|
searchClient *mockSearchClient
|
|
commands *WorkCommands
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) SetupTest() {
|
|
s.repo = &mockWorkRepository{}
|
|
s.searchClient = &mockSearchClient{}
|
|
s.commands = NewWorkCommands(s.repo, s.searchClient)
|
|
}
|
|
|
|
func TestWorkCommandsSuite(t *testing.T) {
|
|
suite.Run(t, new(WorkCommandsSuite))
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestCreateWork_Success() {
|
|
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
|
|
_, err := s.commands.CreateWork(context.Background(), work)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestCreateWork_Nil() {
|
|
_, err := s.commands.CreateWork(context.Background(), nil)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestCreateWork_EmptyTitle() {
|
|
work := &workdomain.Work{TranslatableModel: domain.TranslatableModel{Language: "en"}}
|
|
_, err := s.commands.CreateWork(context.Background(), work)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestCreateWork_EmptyLanguage() {
|
|
work := &workdomain.Work{Title: "Test Work"}
|
|
_, err := s.commands.CreateWork(context.Background(), work)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestCreateWork_RepoError() {
|
|
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
|
|
s.repo.createFunc = func(ctx context.Context, w *workdomain.Work) error {
|
|
return errors.New("db error")
|
|
}
|
|
_, err := s.commands.CreateWork(context.Background(), work)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestUpdateWork_Success() {
|
|
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
|
|
work.ID = 1
|
|
err := s.commands.UpdateWork(context.Background(), work)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestUpdateWork_Nil() {
|
|
err := s.commands.UpdateWork(context.Background(), nil)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestUpdateWork_ZeroID() {
|
|
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
|
|
err := s.commands.UpdateWork(context.Background(), work)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestUpdateWork_EmptyTitle() {
|
|
work := &workdomain.Work{TranslatableModel: domain.TranslatableModel{Language: "en"}}
|
|
work.ID = 1
|
|
err := s.commands.UpdateWork(context.Background(), work)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestUpdateWork_EmptyLanguage() {
|
|
work := &workdomain.Work{Title: "Test Work"}
|
|
work.ID = 1
|
|
err := s.commands.UpdateWork(context.Background(), work)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestUpdateWork_RepoError() {
|
|
work := &workdomain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
|
|
work.ID = 1
|
|
s.repo.updateFunc = func(ctx context.Context, w *workdomain.Work) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.UpdateWork(context.Background(), work)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestDeleteWork_Success() {
|
|
err := s.commands.DeleteWork(context.Background(), 1)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestDeleteWork_ZeroID() {
|
|
err := s.commands.DeleteWork(context.Background(), 0)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestDeleteWork_RepoError() {
|
|
s.repo.deleteFunc = func(ctx context.Context, id uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.DeleteWork(context.Background(), 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestAnalyzeWork_Success() {
|
|
err := s.commands.AnalyzeWork(context.Background(), 1)
|
|
assert.NoError(s.T(), err)
|
|
} |