tercul-backend/internal/app/work/queries_test.go
google-labs-jules[bot] 06e6e2be85 refactor(domain): Isolate Work aggregate
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.
2025-10-03 16:15:09 +00:00

133 lines
4.0 KiB
Go

package work
import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"tercul/internal/domain"
workdomain "tercul/internal/domain/work"
"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 := &workdomain.Work{Title: "Test Work"}
work.ID = 1
s.repo.getByIDFunc = func(ctx context.Context, id uint) (*workdomain.Work, error) {
return work, nil
}
w, err := s.queries.GetWorkByID(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), work, 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() {
works := &domain.PaginatedResult[workdomain.Work]{}
s.repo.listFunc = func(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[workdomain.Work], error) {
return works, nil
}
w, err := s.queries.ListWorks(context.Background(), 1, 10)
assert.NoError(s.T(), err)
assert.Equal(s.T(), works, w)
}
func (s *WorkQueriesSuite) TestGetWorkWithTranslations_Success() {
work := &workdomain.Work{Title: "Test Work"}
work.ID = 1
s.repo.getWithTranslationsFunc = func(ctx context.Context, id uint) (*workdomain.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 := []workdomain.Work{{Title: "Test Work"}}
s.repo.findByTitleFunc = func(ctx context.Context, title string) ([]workdomain.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 := []workdomain.Work{{Title: "Test Work"}}
s.repo.findByAuthorFunc = func(ctx context.Context, authorID uint) ([]workdomain.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 := []workdomain.Work{{Title: "Test Work"}}
s.repo.findByCategoryFunc = func(ctx context.Context, categoryID uint) ([]workdomain.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[workdomain.Work]{}
s.repo.findByLanguageFunc = func(ctx context.Context, language string, page, pageSize int) (*domain.PaginatedResult[workdomain.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)
}