tercul-backend/internal/app/work/queries_test.go
google-labs-jules[bot] 0a27c84771 This commit introduces a series of significant improvements to bring the codebase closer to a production-ready state.
Key changes include:

- **Architectural Refactoring (CQRS/DTOs):** Refactored the `work` and `translation` application services to use Data Transfer Objects (DTOs) for query responses. This separates the domain layer from the API layer, improving maintainability and performance.

- **Implemented Core Business Logic:** Implemented the `AnalyzeWork` command, which was previously a stub. This command now performs linguistic analysis on works and translations by calling the analytics service.

- **Dependency Injection Improvements:**
    - Refactored the configuration loading in `internal/platform/config/config.go` to use a local `viper` instance, removing the reliance on a global singleton.
    - Injected the `analytics.Service` into the `work.Service` to support the `AnalyzeWork` command.

- **Comprehensive Documentation:**
    - Created a new root `README.md` with a project overview, setup instructions, and architectural principles.
    - Added detailed `README.md` files to key packages (`api`, `analytics`, `auth`, `work`, `db`) to document their purpose and usage.

- **Improved Test Coverage:**
    - Added new unit tests for the refactored `work` and `translation` query handlers.
    - Added a new test suite for the `translation` queries, which were previously untested.
    - Added tests for the new `AnalyzeWork` command.
    - Fixed numerous compilation errors in the test suites caused by the refactoring.
2025-10-08 17:25:02 +00:00

158 lines
4.6 KiB
Go

package work
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"tercul/internal/domain"
"testing"
)
type WorkQueriesSuite struct {
suite.Suite
repo *mockWorkRepository
queries *WorkQueries
}
func (s *WorkQueriesSuite) SetupTest() {
s.repo = &mockWorkRepository{}
s.queries = NewWorkQueries(s.repo)
}
func TestWorkQueriesSuite(t *testing.T) {
suite.Run(t, new(WorkQueriesSuite))
}
func (s *WorkQueriesSuite) TestGetWorkByID_Success() {
work := &domain.Work{Title: "Test Work"}
work.ID = 1
s.repo.getByIDFunc = func(ctx context.Context, id uint) (*domain.Work, error) {
return work, nil
}
w, err := s.queries.GetWorkByID(context.Background(), 1)
assert.NoError(s.T(), err)
expectedDTO := &WorkDTO{
ID: work.ID,
Title: work.Title,
Language: work.Language,
}
assert.Equal(s.T(), expectedDTO, w)
}
func (s *WorkQueriesSuite) TestGetWorkByID_ZeroID() {
w, err := s.queries.GetWorkByID(context.Background(), 0)
assert.Error(s.T(), err)
assert.Nil(s.T(), w)
}
func (s *WorkQueriesSuite) TestListWorks_Success() {
domainWorks := &domain.PaginatedResult[domain.Work]{
Items: []domain.Work{
{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 1}, Language: "en"}, Title: "Work 1"},
{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 2}, Language: "fr"}, Title: "Work 2"},
},
TotalCount: 2,
Page: 1,
PageSize: 10,
TotalPages: 1,
}
s.repo.listFunc = func(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.Work], error) {
return domainWorks, nil
}
paginatedDTOs, err := s.queries.ListWorks(context.Background(), 1, 10)
assert.NoError(s.T(), err)
expectedDTOs := &domain.PaginatedResult[WorkDTO]{
Items: []WorkDTO{
{ID: 1, Title: "Work 1", Language: "en"},
{ID: 2, Title: "Work 2", Language: "fr"},
},
TotalCount: 2,
Page: 1,
PageSize: 10,
TotalPages: 1,
}
assert.Equal(s.T(), expectedDTOs, paginatedDTOs)
}
func (s *WorkQueriesSuite) TestGetWorkWithTranslations_Success() {
work := &domain.Work{Title: "Test Work"}
work.ID = 1
s.repo.getWithTranslationsFunc = func(ctx context.Context, id uint) (*domain.Work, error) {
return work, nil
}
w, err := s.queries.GetWorkWithTranslations(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), work, w)
}
func (s *WorkQueriesSuite) TestGetWorkWithTranslations_ZeroID() {
w, err := s.queries.GetWorkWithTranslations(context.Background(), 0)
assert.Error(s.T(), err)
assert.Nil(s.T(), w)
}
func (s *WorkQueriesSuite) TestFindWorksByTitle_Success() {
works := []domain.Work{{Title: "Test Work"}}
s.repo.findByTitleFunc = func(ctx context.Context, title string) ([]domain.Work, error) {
return works, nil
}
w, err := s.queries.FindWorksByTitle(context.Background(), "Test")
assert.NoError(s.T(), err)
assert.Equal(s.T(), works, w)
}
func (s *WorkQueriesSuite) TestFindWorksByTitle_Empty() {
w, err := s.queries.FindWorksByTitle(context.Background(), "")
assert.Error(s.T(), err)
assert.Nil(s.T(), w)
}
func (s *WorkQueriesSuite) TestFindWorksByAuthor_Success() {
works := []domain.Work{{Title: "Test Work"}}
s.repo.findByAuthorFunc = func(ctx context.Context, authorID uint) ([]domain.Work, error) {
return works, nil
}
w, err := s.queries.FindWorksByAuthor(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), works, w)
}
func (s *WorkQueriesSuite) TestFindWorksByAuthor_ZeroID() {
w, err := s.queries.FindWorksByAuthor(context.Background(), 0)
assert.Error(s.T(), err)
assert.Nil(s.T(), w)
}
func (s *WorkQueriesSuite) TestFindWorksByCategory_Success() {
works := []domain.Work{{Title: "Test Work"}}
s.repo.findByCategoryFunc = func(ctx context.Context, categoryID uint) ([]domain.Work, error) {
return works, nil
}
w, err := s.queries.FindWorksByCategory(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), works, w)
}
func (s *WorkQueriesSuite) TestFindWorksByCategory_ZeroID() {
w, err := s.queries.FindWorksByCategory(context.Background(), 0)
assert.Error(s.T(), err)
assert.Nil(s.T(), w)
}
func (s *WorkQueriesSuite) TestFindWorksByLanguage_Success() {
works := &domain.PaginatedResult[domain.Work]{}
s.repo.findByLanguageFunc = func(ctx context.Context, language string, page, pageSize int) (*domain.PaginatedResult[domain.Work], error) {
return works, nil
}
w, err := s.queries.FindWorksByLanguage(context.Background(), "en", 1, 10)
assert.NoError(s.T(), err)
assert.Equal(s.T(), works, w)
}
func (s *WorkQueriesSuite) TestFindWorksByLanguage_Empty() {
w, err := s.queries.FindWorksByLanguage(context.Background(), "", 1, 10)
assert.Error(s.T(), err)
assert.Nil(s.T(), w)
}