mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 00:31:35 +00:00
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.
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package translation
|
|
|
|
import (
|
|
"context"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/suite"
|
|
"tercul/internal/domain"
|
|
"testing"
|
|
)
|
|
|
|
type TranslationQueriesSuite struct {
|
|
suite.Suite
|
|
repo *mockTranslationRepository
|
|
queries *TranslationQueries
|
|
}
|
|
|
|
func (s *TranslationQueriesSuite) SetupTest() {
|
|
s.repo = &mockTranslationRepository{}
|
|
s.queries = NewTranslationQueries(s.repo)
|
|
}
|
|
|
|
func TestTranslationQueriesSuite(t *testing.T) {
|
|
suite.Run(t, new(TranslationQueriesSuite))
|
|
}
|
|
|
|
func (s *TranslationQueriesSuite) TestTranslation_Success() {
|
|
translation := &domain.Translation{
|
|
BaseModel: domain.BaseModel{ID: 1},
|
|
Title: "Test Translation",
|
|
Language: "es",
|
|
Content: "Test content",
|
|
TranslatableID: 1,
|
|
}
|
|
s.repo.getByIDFunc = func(ctx context.Context, id uint) (*domain.Translation, error) {
|
|
return translation, nil
|
|
}
|
|
dto, err := s.queries.Translation(context.Background(), 1)
|
|
assert.NoError(s.T(), err)
|
|
|
|
expectedDTO := &TranslationDTO{
|
|
ID: 1,
|
|
Title: "Test Translation",
|
|
Language: "es",
|
|
Content: "Test content",
|
|
TranslatableID: 1,
|
|
}
|
|
assert.Equal(s.T(), expectedDTO, dto)
|
|
}
|
|
|
|
func (s *TranslationQueriesSuite) TestListTranslations_Success() {
|
|
domainTranslations := &domain.PaginatedResult[domain.Translation]{
|
|
Items: []domain.Translation{
|
|
{BaseModel: domain.BaseModel{ID: 1}, Title: "Translation 1", Language: "es", Content: "Content 1", TranslatableID: 1},
|
|
{BaseModel: domain.BaseModel{ID: 2}, Title: "Translation 2", Language: "fr", Content: "Content 2", TranslatableID: 1},
|
|
},
|
|
TotalCount: 2,
|
|
Page: 1,
|
|
PageSize: 10,
|
|
TotalPages: 1,
|
|
}
|
|
s.repo.listByWorkIDPaginatedFunc = func(ctx context.Context, workID uint, language *string, page, pageSize int) (*domain.PaginatedResult[domain.Translation], error) {
|
|
return domainTranslations, nil
|
|
}
|
|
|
|
paginatedDTOs, err := s.queries.ListTranslations(context.Background(), 1, nil, 1, 10)
|
|
assert.NoError(s.T(), err)
|
|
|
|
expectedDTOs := &domain.PaginatedResult[TranslationDTO]{
|
|
Items: []TranslationDTO{
|
|
{ID: 1, Title: "Translation 1", Language: "es", Content: "Content 1", TranslatableID: 1},
|
|
{ID: 2, Title: "Translation 2", Language: "fr", Content: "Content 2", TranslatableID: 1},
|
|
},
|
|
TotalCount: 2,
|
|
Page: 1,
|
|
PageSize: 10,
|
|
TotalPages: 1,
|
|
}
|
|
assert.Equal(s.T(), expectedDTOs, paginatedDTOs)
|
|
} |