tercul-backend/internal/app/work/queries_test.go
google-labs-jules[bot] bb5e18d162 refactor: Introduce application layer and dataloaders
This commit introduces a new application layer to the codebase, which decouples the GraphQL resolvers from the data layer. The resolvers now call application services, which in turn call the repositories. This change improves the separation of concerns and makes the code more testable and maintainable.

Additionally, this commit introduces dataloaders to solve the N+1 problem in the GraphQL resolvers. The dataloaders are used to batch and cache database queries, which significantly improves the performance of the API.

The following changes were made:
- Created application services for most of the domains.
- Refactored the GraphQL resolvers to use the new application services.
- Implemented dataloaders for the `Author` aggregate.
- Updated the `app.Application` struct to hold the application services instead of the repositories.
- Fixed a large number of compilation errors in the test files that arose from these changes.

There are still some compilation errors in the `internal/adapters/graphql/integration_test.go` file. These errors are due to the test files still trying to access the repositories directly from the `app.Application` struct. The remaining work is to update these tests to use the new application services.
2025-09-08 10:19:43 +00:00

137 lines
4.2 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
work.Authors = []*domain.Author{
{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 1}}, Name: "Author 1"},
}
s.repo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Work, error) {
return work, nil
}
w, err := s.queries.GetWorkByID(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), work, w)
assert.Equal(s.T(), []uint{1}, w.AuthorIDs)
}
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[domain.Work]{}
s.repo.listFunc = func(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.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 := &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)
}