mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
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.
350 lines
12 KiB
Go
350 lines
12 KiB
Go
package copyright
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/suite"
|
|
"tercul/internal/domain"
|
|
"testing"
|
|
)
|
|
|
|
type CopyrightCommandsSuite struct {
|
|
suite.Suite
|
|
repo *mockCopyrightRepository
|
|
commands *CopyrightCommands
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) SetupTest() {
|
|
s.repo = &mockCopyrightRepository{}
|
|
s.commands = NewCopyrightCommands(s.repo)
|
|
}
|
|
|
|
func TestCopyrightCommandsSuite(t *testing.T) {
|
|
suite.Run(t, new(CopyrightCommandsSuite))
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestCreateCopyright_Success() {
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.repo.createFunc = func(ctx context.Context, c *domain.Copyright) error {
|
|
assert.Equal(s.T(), copyright.Name, c.Name)
|
|
return nil
|
|
}
|
|
err := s.commands.CreateCopyright(context.Background(), copyright)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestCreateCopyright_Nil() {
|
|
err := s.commands.CreateCopyright(context.Background(), nil)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestCreateCopyright_EmptyName() {
|
|
copyright := &domain.Copyright{Identificator: "TC-123"}
|
|
err := s.commands.CreateCopyright(context.Background(), copyright)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestCreateCopyright_EmptyIdentificator() {
|
|
copyright := &domain.Copyright{Name: "Test Copyright"}
|
|
err := s.commands.CreateCopyright(context.Background(), copyright)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestCreateCopyright_RepoError() {
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.repo.createFunc = func(ctx context.Context, c *domain.Copyright) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.CreateCopyright(context.Background(), copyright)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestUpdateCopyright_Success() {
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
copyright.ID = 1
|
|
s.repo.updateFunc = func(ctx context.Context, c *domain.Copyright) error {
|
|
assert.Equal(s.T(), copyright.Name, c.Name)
|
|
return nil
|
|
}
|
|
err := s.commands.UpdateCopyright(context.Background(), copyright)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestUpdateCopyright_Nil() {
|
|
err := s.commands.UpdateCopyright(context.Background(), nil)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestUpdateCopyright_ZeroID() {
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
err := s.commands.UpdateCopyright(context.Background(), copyright)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestUpdateCopyright_EmptyName() {
|
|
copyright := &domain.Copyright{Identificator: "TC-123"}
|
|
copyright.ID = 1
|
|
err := s.commands.UpdateCopyright(context.Background(), copyright)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestUpdateCopyright_EmptyIdentificator() {
|
|
copyright := &domain.Copyright{Name: "Test Copyright"}
|
|
copyright.ID = 1
|
|
err := s.commands.UpdateCopyright(context.Background(), copyright)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestUpdateCopyright_RepoError() {
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
copyright.ID = 1
|
|
s.repo.updateFunc = func(ctx context.Context, c *domain.Copyright) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.UpdateCopyright(context.Background(), copyright)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestDeleteCopyright_Success() {
|
|
s.repo.deleteFunc = func(ctx context.Context, id uint) error {
|
|
assert.Equal(s.T(), uint(1), id)
|
|
return nil
|
|
}
|
|
err := s.commands.DeleteCopyright(context.Background(), 1)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestDeleteCopyright_ZeroID() {
|
|
err := s.commands.DeleteCopyright(context.Background(), 0)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestDeleteCopyright_RepoError() {
|
|
s.repo.deleteFunc = func(ctx context.Context, id uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.DeleteCopyright(context.Background(), 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToWork_Success() {
|
|
err := s.commands.AddCopyrightToWork(context.Background(), 1, 2)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToWork_ZeroID() {
|
|
err := s.commands.AddCopyrightToWork(context.Background(), 0, 2)
|
|
assert.Error(s.T(), err)
|
|
err = s.commands.AddCopyrightToWork(context.Background(), 1, 0)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromWork_Success() {
|
|
err := s.commands.RemoveCopyrightFromWork(context.Background(), 1, 2)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToWork_RepoError() {
|
|
s.repo.addCopyrightToWorkFunc = func(ctx context.Context, workID uint, copyrightID uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.AddCopyrightToWork(context.Background(), 1, 2)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromWork_RepoError() {
|
|
s.repo.removeCopyrightFromWorkFunc = func(ctx context.Context, workID uint, copyrightID uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.RemoveCopyrightFromWork(context.Background(), 1, 2)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromWork_ZeroID() {
|
|
err := s.commands.RemoveCopyrightFromWork(context.Background(), 0, 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToAuthor_ZeroID() {
|
|
err := s.commands.AddCopyrightToAuthor(context.Background(), 0, 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromAuthor_ZeroID() {
|
|
err := s.commands.RemoveCopyrightFromAuthor(context.Background(), 0, 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToBook_ZeroID() {
|
|
err := s.commands.AddCopyrightToBook(context.Background(), 0, 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromBook_ZeroID() {
|
|
err := s.commands.RemoveCopyrightFromBook(context.Background(), 0, 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToPublisher_ZeroID() {
|
|
err := s.commands.AddCopyrightToPublisher(context.Background(), 0, 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromPublisher_ZeroID() {
|
|
err := s.commands.RemoveCopyrightFromPublisher(context.Background(), 0, 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToSource_ZeroID() {
|
|
err := s.commands.AddCopyrightToSource(context.Background(), 0, 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromSource_ZeroID() {
|
|
err := s.commands.RemoveCopyrightFromSource(context.Background(), 0, 1)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToAuthor_RepoError() {
|
|
s.repo.addCopyrightToAuthorFunc = func(ctx context.Context, authorID uint, copyrightID uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.AddCopyrightToAuthor(context.Background(), 1, 2)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromAuthor_RepoError() {
|
|
s.repo.removeCopyrightFromAuthorFunc = func(ctx context.Context, authorID uint, copyrightID uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.RemoveCopyrightFromAuthor(context.Background(), 1, 2)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToBook_RepoError() {
|
|
s.repo.addCopyrightToBookFunc = func(ctx context.Context, bookID uint, copyrightID uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.AddCopyrightToBook(context.Background(), 1, 2)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromBook_RepoError() {
|
|
s.repo.removeCopyrightFromBookFunc = func(ctx context.Context, bookID uint, copyrightID uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.RemoveCopyrightFromBook(context.Background(), 1, 2)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToPublisher_RepoError() {
|
|
s.repo.addCopyrightToPublisherFunc = func(ctx context.Context, publisherID uint, copyrightID uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.AddCopyrightToPublisher(context.Background(), 1, 2)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromPublisher_RepoError() {
|
|
s.repo.removeCopyrightFromPublisherFunc = func(ctx context.Context, publisherID uint, copyrightID uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.RemoveCopyrightFromPublisher(context.Background(), 1, 2)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToSource_RepoError() {
|
|
s.repo.addCopyrightToSourceFunc = func(ctx context.Context, sourceID uint, copyrightID uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.AddCopyrightToSource(context.Background(), 1, 2)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromSource_RepoError() {
|
|
s.repo.removeCopyrightFromSourceFunc = func(ctx context.Context, sourceID uint, copyrightID uint) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.RemoveCopyrightFromSource(context.Background(), 1, 2)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToAuthor_Success() {
|
|
err := s.commands.AddCopyrightToAuthor(context.Background(), 1, 2)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromAuthor_Success() {
|
|
err := s.commands.RemoveCopyrightFromAuthor(context.Background(), 1, 2)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToBook_Success() {
|
|
err := s.commands.AddCopyrightToBook(context.Background(), 1, 2)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromBook_Success() {
|
|
err := s.commands.RemoveCopyrightFromBook(context.Background(), 1, 2)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToPublisher_Success() {
|
|
err := s.commands.AddCopyrightToPublisher(context.Background(), 1, 2)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromPublisher_Success() {
|
|
err := s.commands.RemoveCopyrightFromPublisher(context.Background(), 1, 2)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddCopyrightToSource_Success() {
|
|
err := s.commands.AddCopyrightToSource(context.Background(), 1, 2)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestRemoveCopyrightFromSource_Success() {
|
|
err := s.commands.RemoveCopyrightFromSource(context.Background(), 1, 2)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddTranslation_Success() {
|
|
translation := &domain.CopyrightTranslation{CopyrightID: 1, LanguageCode: "en", Message: "Test"}
|
|
err := s.commands.AddTranslation(context.Background(), translation)
|
|
assert.NoError(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddTranslation_Nil() {
|
|
err := s.commands.AddTranslation(context.Background(), nil)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddTranslation_ZeroCopyrightID() {
|
|
translation := &domain.CopyrightTranslation{LanguageCode: "en", Message: "Test"}
|
|
err := s.commands.AddTranslation(context.Background(), translation)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddTranslation_RepoError() {
|
|
translation := &domain.CopyrightTranslation{CopyrightID: 1, LanguageCode: "en", Message: "Test"}
|
|
s.repo.addTranslationFunc = func(ctx context.Context, t *domain.CopyrightTranslation) error {
|
|
return errors.New("db error")
|
|
}
|
|
err := s.commands.AddTranslation(context.Background(), translation)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddTranslation_EmptyLanguageCode() {
|
|
translation := &domain.CopyrightTranslation{CopyrightID: 1, Message: "Test"}
|
|
err := s.commands.AddTranslation(context.Background(), translation)
|
|
assert.Error(s.T(), err)
|
|
}
|
|
|
|
func (s *CopyrightCommandsSuite) TestAddTranslation_EmptyMessage() {
|
|
translation := &domain.CopyrightTranslation{CopyrightID: 1, LanguageCode: "en"}
|
|
err := s.commands.AddTranslation(context.Background(), translation)
|
|
assert.Error(s.T(), err)
|
|
}
|