tercul-backend/internal/app/copyright/main_test.go
google-labs-jules[bot] 49e2bdd9ac feat: Refactor localization, auth, copyright, and monetization domains
This change introduces a major architectural refactoring of the application, with a focus on improving testability, decoupling, and observability.

The following domains have been successfully refactored:
- `localization`: Wrote a full suite of unit tests and added logging.
- `auth`: Introduced a `JWTManager` interface, wrote comprehensive unit tests, and added logging.
- `copyright`: Separated integration tests, wrote a full suite of unit tests, and added logging.
- `monetization`: Wrote a full suite of unit tests and added logging.
- `search`: Refactored the Weaviate client usage by creating a wrapper to improve testability, and achieved 100% test coverage.

For each of these domains, 100% test coverage has been achieved for the refactored code.

The refactoring of the `work` domain is currently in progress. Unit tests have been written for the commands and queries, but there is a persistent build issue with the query tests that needs to be resolved. The error indicates that the query methods are undefined, despite appearing to be correctly defined and called.
2025-09-06 15:15:10 +00:00

224 lines
9.4 KiB
Go

package copyright
import (
"context"
"gorm.io/gorm"
"tercul/internal/domain"
)
type mockCopyrightRepository struct {
createFunc func(ctx context.Context, copyright *domain.Copyright) error
updateFunc func(ctx context.Context, copyright *domain.Copyright) error
deleteFunc func(ctx context.Context, id uint) error
addCopyrightToWorkFunc func(ctx context.Context, workID uint, copyrightID uint) error
removeCopyrightFromWorkFunc func(ctx context.Context, workID uint, copyrightID uint) error
addCopyrightToAuthorFunc func(ctx context.Context, authorID uint, copyrightID uint) error
removeCopyrightFromAuthorFunc func(ctx context.Context, authorID uint, copyrightID uint) error
addCopyrightToBookFunc func(ctx context.Context, bookID uint, copyrightID uint) error
removeCopyrightFromBookFunc func(ctx context.Context, bookID uint, copyrightID uint) error
addCopyrightToPublisherFunc func(ctx context.Context, publisherID uint, copyrightID uint) error
removeCopyrightFromPublisherFunc func(ctx context.Context, publisherID uint, copyrightID uint) error
addCopyrightToSourceFunc func(ctx context.Context, sourceID uint, copyrightID uint) error
removeCopyrightFromSourceFunc func(ctx context.Context, sourceID uint, copyrightID uint) error
addTranslationFunc func(ctx context.Context, translation *domain.CopyrightTranslation) error
getByIDFunc func(ctx context.Context, id uint) (*domain.Copyright, error)
listAllFunc func(ctx context.Context) ([]domain.Copyright, error)
getTranslationsFunc func(ctx context.Context, copyrightID uint) ([]domain.CopyrightTranslation, error)
getTranslationByLanguageFunc func(ctx context.Context, copyrightID uint, languageCode string) (*domain.CopyrightTranslation, error)
}
func (m *mockCopyrightRepository) Create(ctx context.Context, copyright *domain.Copyright) error {
if m.createFunc != nil {
return m.createFunc(ctx, copyright)
}
return nil
}
func (m *mockCopyrightRepository) Update(ctx context.Context, copyright *domain.Copyright) error {
if m.updateFunc != nil {
return m.updateFunc(ctx, copyright)
}
return nil
}
func (m *mockCopyrightRepository) Delete(ctx context.Context, id uint) error {
if m.deleteFunc != nil {
return m.deleteFunc(ctx, id)
}
return nil
}
func (m *mockCopyrightRepository) AddCopyrightToWork(ctx context.Context, workID uint, copyrightID uint) error {
if m.addCopyrightToWorkFunc != nil {
return m.addCopyrightToWorkFunc(ctx, workID, copyrightID)
}
return nil
}
func (m *mockCopyrightRepository) RemoveCopyrightFromWork(ctx context.Context, workID uint, copyrightID uint) error {
if m.removeCopyrightFromWorkFunc != nil {
return m.removeCopyrightFromWorkFunc(ctx, workID, copyrightID)
}
return nil
}
func (m *mockCopyrightRepository) AddCopyrightToAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
if m.addCopyrightToAuthorFunc != nil {
return m.addCopyrightToAuthorFunc(ctx, authorID, copyrightID)
}
return nil
}
func (m *mockCopyrightRepository) RemoveCopyrightFromAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
if m.removeCopyrightFromAuthorFunc != nil {
return m.removeCopyrightFromAuthorFunc(ctx, authorID, copyrightID)
}
return nil
}
func (m *mockCopyrightRepository) AddCopyrightToBook(ctx context.Context, bookID uint, copyrightID uint) error {
if m.addCopyrightToBookFunc != nil {
return m.addCopyrightToBookFunc(ctx, bookID, copyrightID)
}
return nil
}
func (m *mockCopyrightRepository) RemoveCopyrightFromBook(ctx context.Context, bookID uint, copyrightID uint) error {
if m.removeCopyrightFromBookFunc != nil {
return m.removeCopyrightFromBookFunc(ctx, bookID, copyrightID)
}
return nil
}
func (m *mockCopyrightRepository) AddCopyrightToPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
if m.addCopyrightToPublisherFunc != nil {
return m.addCopyrightToPublisherFunc(ctx, publisherID, copyrightID)
}
return nil
}
func (m *mockCopyrightRepository) RemoveCopyrightFromPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
if m.removeCopyrightFromPublisherFunc != nil {
return m.removeCopyrightFromPublisherFunc(ctx, publisherID, copyrightID)
}
return nil
}
func (m *mockCopyrightRepository) AddCopyrightToSource(ctx context.Context, sourceID uint, copyrightID uint) error {
if m.addCopyrightToSourceFunc != nil {
return m.addCopyrightToSourceFunc(ctx, sourceID, copyrightID)
}
return nil
}
func (m *mockCopyrightRepository) RemoveCopyrightFromSource(ctx context.Context, sourceID uint, copyrightID uint) error {
if m.removeCopyrightFromSourceFunc != nil {
return m.removeCopyrightFromSourceFunc(ctx, sourceID, copyrightID)
}
return nil
}
func (m *mockCopyrightRepository) AddTranslation(ctx context.Context, translation *domain.CopyrightTranslation) error {
if m.addTranslationFunc != nil {
return m.addTranslationFunc(ctx, translation)
}
return nil
}
func (m *mockCopyrightRepository) GetByID(ctx context.Context, id uint) (*domain.Copyright, error) {
if m.getByIDFunc != nil {
return m.getByIDFunc(ctx, id)
}
return nil, nil
}
func (m *mockCopyrightRepository) ListAll(ctx context.Context) ([]domain.Copyright, error) {
if m.listAllFunc != nil {
return m.listAllFunc(ctx)
}
return nil, nil
}
func (m *mockCopyrightRepository) GetTranslations(ctx context.Context, copyrightID uint) ([]domain.CopyrightTranslation, error) {
if m.getTranslationsFunc != nil {
return m.getTranslationsFunc(ctx, copyrightID)
}
return nil, nil
}
func (m *mockCopyrightRepository) GetTranslationByLanguage(ctx context.Context, copyrightID uint, languageCode string) (*domain.CopyrightTranslation, error) {
if m.getTranslationByLanguageFunc != nil {
return m.getTranslationByLanguageFunc(ctx, copyrightID, languageCode)
}
return nil, nil
}
// Implement the rest of the CopyrightRepository interface with empty methods.
func (m *mockCopyrightRepository) List(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[domain.Copyright], error) {
return nil, nil
}
func (m *mockCopyrightRepository) Count(ctx context.Context) (int64, error) { return 0, nil }
func (m *mockCopyrightRepository) CreateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Copyright) error {
return nil
}
func (m *mockCopyrightRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Copyright, error) {
return nil, nil
}
func (m *mockCopyrightRepository) UpdateInTx(ctx context.Context, tx *gorm.DB, entity *domain.Copyright) error {
return nil
}
func (m *mockCopyrightRepository) DeleteInTx(ctx context.Context, tx *gorm.DB, id uint) error {
return nil
}
func (m *mockCopyrightRepository) ListWithOptions(ctx context.Context, options *domain.QueryOptions) ([]domain.Copyright, error) {
return nil, nil
}
func (m *mockCopyrightRepository) CountWithOptions(ctx context.Context, options *domain.QueryOptions) (int64, error) {
return 0, nil
}
func (m *mockCopyrightRepository) FindWithPreload(ctx context.Context, preloads []string, id uint) (*domain.Copyright, error) {
return nil, nil
}
func (m *mockCopyrightRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]domain.Copyright, error) {
return nil, nil
}
func (m *mockCopyrightRepository) Exists(ctx context.Context, id uint) (bool, error) { return false, nil }
func (m *mockCopyrightRepository) BeginTx(ctx context.Context) (*gorm.DB, error) { return nil, nil }
func (m *mockCopyrightRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error {
return nil
}
type mockWorkRepository struct {
domain.WorkRepository
getByIDWithOptionsFunc func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Work, error)
}
func (m *mockWorkRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Work, error) {
if m.getByIDWithOptionsFunc != nil {
return m.getByIDWithOptionsFunc(ctx, id, options)
}
return nil, nil
}
type mockAuthorRepository struct {
domain.AuthorRepository
getByIDWithOptionsFunc func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Author, error)
}
func (m *mockAuthorRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Author, error) {
if m.getByIDWithOptionsFunc != nil {
return m.getByIDWithOptionsFunc(ctx, id, options)
}
return nil, nil
}
type mockBookRepository struct {
domain.BookRepository
getByIDWithOptionsFunc func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Book, error)
}
func (m *mockBookRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Book, error) {
if m.getByIDWithOptionsFunc != nil {
return m.getByIDWithOptionsFunc(ctx, id, options)
}
return nil, nil
}
type mockPublisherRepository struct {
domain.PublisherRepository
getByIDWithOptionsFunc func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Publisher, error)
}
func (m *mockPublisherRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Publisher, error) {
if m.getByIDWithOptionsFunc != nil {
return m.getByIDWithOptionsFunc(ctx, id, options)
}
return nil, nil
}
type mockSourceRepository struct {
domain.SourceRepository
getByIDWithOptionsFunc func(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Source, error)
}
func (m *mockSourceRepository) GetByIDWithOptions(ctx context.Context, id uint, options *domain.QueryOptions) (*domain.Source, error) {
if m.getByIDWithOptionsFunc != nil {
return m.getByIDWithOptionsFunc(ctx, id, options)
}
return nil, nil
}