tercul-backend/internal/app/copyright/queries_test.go
google-labs-jules[bot] fa90dd79da feat: Complete large-scale refactor and prepare for production
This commit marks the completion of a major refactoring effort to stabilize the codebase, improve its structure, and prepare it for production.

The key changes include:

- **Domain Layer Consolidation:** The `Work` entity and its related types, along with all other domain entities and repository interfaces, have been consolidated into the main `internal/domain` package. This eliminates import cycles and provides a single, coherent source of truth for the domain model.

- **Data Access Layer Refactoring:** The repository implementations in `internal/data/sql` have been updated to align with the new domain layer. The `BaseRepositoryImpl` has been corrected to use pointer receivers, and all concrete repositories now correctly embed it, ensuring consistent and correct behavior.

- **Application Layer Stabilization:** All application services in `internal/app` have been updated to use the new domain types and repository interfaces. Dependency injection has been corrected throughout the application, ensuring that all services are initialized with the correct dependencies.

- **GraphQL Adapter Fixes:** The GraphQL resolver implementation in `internal/adapters/graphql` has been updated to correctly handle the new domain types and service methods. The auto-generated GraphQL code has been regenerated to ensure it is in sync with the schema and runtime.

- **Test Suite Overhaul:** All test suites have been fixed to correctly implement their respective interfaces and use the updated domain model. Mock repositories and test suites have been corrected to properly embed the `testify` base types, resolving numerous build and linter errors.

- **Dependency Management:** The Go modules have been tidied, and the module cache has been cleaned to ensure a consistent and correct dependency graph.

- **Code Quality and Verification:** The entire codebase now passes all builds, tests, and linter checks, ensuring a high level of quality and stability.

This comprehensive effort has resulted in a more robust, maintainable, and production-ready application.
2025-10-07 11:09:37 +00:00

232 lines
8.5 KiB
Go

package copyright
import (
"context"
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"tercul/internal/domain"
"testing"
)
type CopyrightQueriesSuite struct {
suite.Suite
repo *mockCopyrightRepository
workRepo *mockWorkRepository
authorRepo *mockAuthorRepository
bookRepo *mockBookRepository
publisherRepo *mockPublisherRepository
sourceRepo *mockSourceRepository
queries *CopyrightQueries
}
func (s *CopyrightQueriesSuite) SetupTest() {
s.repo = &mockCopyrightRepository{}
s.workRepo = &mockWorkRepository{}
s.authorRepo = &mockAuthorRepository{}
s.bookRepo = &mockBookRepository{}
s.publisherRepo = &mockPublisherRepository{}
s.sourceRepo = &mockSourceRepository{}
s.queries = NewCopyrightQueries(s.repo, s.workRepo, s.authorRepo, s.bookRepo, s.publisherRepo, s.sourceRepo)
}
func TestCopyrightQueriesSuite(t *testing.T) {
suite.Run(t, new(CopyrightQueriesSuite))
}
func (s *CopyrightQueriesSuite) TestGetCopyrightByID_Success() {
copyright := &domain.Copyright{Name: "Test Copyright"}
copyright.ID = 1
s.repo.getByIDFunc = func(ctx context.Context, id uint) (*domain.Copyright, error) {
return copyright, nil
}
c, err := s.queries.GetCopyrightByID(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), copyright, c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightByID_ZeroID() {
c, err := s.queries.GetCopyrightByID(context.Background(), 0)
assert.Error(s.T(), err)
assert.Nil(s.T(), c)
}
func (s *CopyrightQueriesSuite) TestListCopyrights_Success() {
copyrights := []domain.Copyright{{Name: "Test Copyright"}}
s.repo.listAllFunc = func(ctx context.Context) ([]domain.Copyright, error) {
return copyrights, nil
}
c, err := s.queries.ListCopyrights(context.Background())
assert.NoError(s.T(), err)
assert.Equal(s.T(), copyrights, c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightsForAuthor_RepoError() {
s.authorRepo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Author, error) {
return nil, errors.New("db error")
}
c, err := s.queries.GetCopyrightsForAuthor(context.Background(), 1)
assert.Error(s.T(), err)
assert.Nil(s.T(), c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightsForBook_RepoError() {
s.bookRepo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Book, error) {
return nil, errors.New("db error")
}
c, err := s.queries.GetCopyrightsForBook(context.Background(), 1)
assert.Error(s.T(), err)
assert.Nil(s.T(), c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightsForPublisher_RepoError() {
s.publisherRepo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Publisher, error) {
return nil, errors.New("db error")
}
c, err := s.queries.GetCopyrightsForPublisher(context.Background(), 1)
assert.Error(s.T(), err)
assert.Nil(s.T(), c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightsForSource_RepoError() {
s.sourceRepo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Source, error) {
return nil, errors.New("db error")
}
c, err := s.queries.GetCopyrightsForSource(context.Background(), 1)
assert.Error(s.T(), err)
assert.Nil(s.T(), c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightsForWork_Success() {
copyrights := []*domain.Copyright{{Name: "Test Copyright"}}
s.workRepo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Work, error) {
return &domain.Work{Copyrights: copyrights}, nil
}
c, err := s.queries.GetCopyrightsForWork(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), copyrights, c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightsForWork_RepoError() {
s.workRepo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Work, error) {
return nil, errors.New("db error")
}
c, err := s.queries.GetCopyrightsForWork(context.Background(), 1)
assert.Error(s.T(), err)
assert.Nil(s.T(), c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightsForAuthor_Success() {
copyrights := []*domain.Copyright{{Name: "Test Copyright"}}
s.authorRepo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Author, error) {
return &domain.Author{Copyrights: copyrights}, nil
}
c, err := s.queries.GetCopyrightsForAuthor(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), copyrights, c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightsForBook_Success() {
copyrights := []*domain.Copyright{{Name: "Test Copyright"}}
s.bookRepo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Book, error) {
return &domain.Book{Copyrights: copyrights}, nil
}
c, err := s.queries.GetCopyrightsForBook(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), copyrights, c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightsForPublisher_Success() {
copyrights := []*domain.Copyright{{Name: "Test Copyright"}}
s.publisherRepo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Publisher, error) {
return &domain.Publisher{Copyrights: copyrights}, nil
}
c, err := s.queries.GetCopyrightsForPublisher(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), copyrights, c)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightsForSource_Success() {
copyrights := []*domain.Copyright{{Name: "Test Copyright"}}
s.sourceRepo.getByIDWithOptionsFunc = func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Source, error) {
return &domain.Source{Copyrights: copyrights}, nil
}
c, err := s.queries.GetCopyrightsForSource(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), copyrights, c)
}
func (s *CopyrightQueriesSuite) TestGetTranslations_Success() {
translations := []domain.CopyrightTranslation{{Message: "Test"}}
s.repo.getTranslationsFunc = func(ctx context.Context, copyrightID uint) ([]domain.CopyrightTranslation, error) {
return translations, nil
}
t, err := s.queries.GetTranslations(context.Background(), 1)
assert.NoError(s.T(), err)
assert.Equal(s.T(), translations, t)
}
func (s *CopyrightQueriesSuite) TestGetTranslations_ZeroID() {
t, err := s.queries.GetTranslations(context.Background(), 0)
assert.Error(s.T(), err)
assert.Nil(s.T(), t)
}
func (s *CopyrightQueriesSuite) TestGetCopyrightByID_RepoError() {
s.repo.getByIDFunc = func(ctx context.Context, id uint) (*domain.Copyright, error) {
return nil, errors.New("db error")
}
c, err := s.queries.GetCopyrightByID(context.Background(), 1)
assert.Error(s.T(), err)
assert.Nil(s.T(), c)
}
func (s *CopyrightQueriesSuite) TestListCopyrights_RepoError() {
s.repo.listAllFunc = func(ctx context.Context) ([]domain.Copyright, error) {
return nil, errors.New("db error")
}
c, err := s.queries.ListCopyrights(context.Background())
assert.Error(s.T(), err)
assert.Nil(s.T(), c)
}
func (s *CopyrightQueriesSuite) TestGetTranslations_RepoError() {
s.repo.getTranslationsFunc = func(ctx context.Context, copyrightID uint) ([]domain.CopyrightTranslation, error) {
return nil, errors.New("db error")
}
t, err := s.queries.GetTranslations(context.Background(), 1)
assert.Error(s.T(), err)
assert.Nil(s.T(), t)
}
func (s *CopyrightQueriesSuite) TestGetTranslationByLanguage_RepoError() {
s.repo.getTranslationByLanguageFunc = func(ctx context.Context, copyrightID uint, languageCode string) (*domain.CopyrightTranslation, error) {
return nil, errors.New("db error")
}
t, err := s.queries.GetTranslationByLanguage(context.Background(), 1, "en")
assert.Error(s.T(), err)
assert.Nil(s.T(), t)
}
func (s *CopyrightQueriesSuite) TestGetTranslationByLanguage_Success() {
translation := &domain.CopyrightTranslation{Message: "Test"}
s.repo.getTranslationByLanguageFunc = func(ctx context.Context, copyrightID uint, languageCode string) (*domain.CopyrightTranslation, error) {
return translation, nil
}
t, err := s.queries.GetTranslationByLanguage(context.Background(), 1, "en")
assert.NoError(s.T(), err)
assert.Equal(s.T(), translation, t)
}
func (s *CopyrightQueriesSuite) TestGetTranslationByLanguage_ZeroID() {
t, err := s.queries.GetTranslationByLanguage(context.Background(), 0, "en")
assert.Error(s.T(), err)
assert.Nil(s.T(), t)
}
func (s *CopyrightQueriesSuite) TestGetTranslationByLanguage_EmptyLang() {
t, err := s.queries.GetTranslationByLanguage(context.Background(), 1, "")
assert.Error(s.T(), err)
assert.Nil(s.T(), t)
}