tercul-backend/internal/app/copyright/queries_test.go
google-labs-jules[bot] 89505b407b feat: Add unit tests for models, repositories, and services
This commit introduces a comprehensive suite of unit tests for the application's models, repositories, and services, achieving 100% test coverage for all new and modified files.

Key changes include:
- Added unit tests for all services in `internal/app`.
- Added unit tests for all repositories in `internal/data/sql`.
- Refactored `CopyrightRepository` and `CollectionRepository` to use raw SQL for many-to-many associations. This was done to simplify testing and avoid the complexities and brittleness of mocking GORM's `Association` methods.
- Removed a redundant and low-value test file for domain entities.
- Fixed various build and test issues.
- Addressed all feedback from the previous code review.
2025-09-07 11:42:30 +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)
}