mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
This commit addresses a broken build state caused by a mid-stream architectural refactoring. The changes align the existing code with the new Domain-Driven Design (DDD-lite) structure outlined in `refactor.md`. Key changes include: - Defined missing domain interfaces for `Auth`, `Localization`, and `Search`. - Refactored application services to use a `Commands` and `Queries` pattern. - Updated GraphQL resolvers to call application services instead of accessing repositories directly. - Fixed dependency injection in `cmd/api/main.go` by removing the non-existent `ApplicationBuilder` and manually instantiating services. - Corrected numerous test files (`integration`, `unit`, and `repository` tests) to reflect the new architecture, including fixing mock objects and test suite setups. - Added missing database migrations for test schemas to resolve "no such table" errors. This effort successfully gets the application to a compilable state and passes a significant portion of the test suite, laying the groundwork for further development and fixing the remaining test failures.
124 lines
3.7 KiB
Go
124 lines
3.7 KiB
Go
package work
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/suite"
|
|
"tercul/internal/domain"
|
|
"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 := &domain.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 := &domain.Work{TranslatableModel: domain.TranslatableModel{Language: "en"}}
|
|
_, err := s.commands.CreateWork(context.Background(), work)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestCreateWork_EmptyLanguage() {
|
|
work := &domain.Work{Title: "Test Work"}
|
|
_, err := s.commands.CreateWork(context.Background(), work)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *WorkCommandsSuite) TestCreateWork_RepoError() {
|
|
work := &domain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
|
|
s.repo.createFunc = func(ctx context.Context, w *domain.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 := &domain.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 := &domain.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 := &domain.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 := &domain.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 := &domain.Work{Title: "Test Work", TranslatableModel: domain.TranslatableModel{Language: "en"}}
|
|
work.ID = 1
|
|
s.repo.updateFunc = func(ctx context.Context, w *domain.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)
|
|
} |