mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
The main changes are: - Refactored the `Copyright` and `Monetization` relationships to use explicit join tables for each owning model, as per the "Option A" strategy. This fixes the GORM migration issues related to polymorphic many-to-many relationships. - Created new join table structs (e.g., `WorkCopyright`, `AuthorCopyright`, `WorkMonetization`, etc.). - Updated the domain models to use standard `gorm:"many2many"` tags with the new join tables. - Refactored the `CopyrightRepository` and `MonetizationRepository` to use the new association-based logic. - Updated the application services (`CopyrightCommands`, `CopyrightQueries`, `MonetizationCommands`, `MonetizationQueries`) to use the new repository methods. - Consolidated all repository interfaces into a single `internal/domain/interfaces.go` file for better code organization. - Added extensive integration tests for the new repository and application layer logic for `Copyrights` and `Monetizations`. - Fixed the deletion logic for `WorkRepository` to correctly handle cascading deletes with SQLite. - Updated the `TODO.md` file to mark the "Stabilize non-linguistics tests and interfaces" task as complete.
238 lines
8.6 KiB
Go
238 lines
8.6 KiB
Go
package copyright_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"tercul/internal/app/copyright"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/testutil"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type CopyrightCommandsTestSuite struct {
|
|
testutil.IntegrationTestSuite
|
|
commands *copyright.CopyrightCommands
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) SetupSuite() {
|
|
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
|
|
s.commands = copyright.NewCopyrightCommands(s.CopyrightRepo)
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) TestAddCopyrightToWork() {
|
|
s.Run("should add a copyright to a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.Require().NoError(s.CopyrightRepo.Create(context.Background(), copyright))
|
|
|
|
// Act
|
|
err := s.commands.AddCopyrightToWork(context.Background(), work.ID, copyright.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
|
|
// Verify that the association was created in the database
|
|
var foundWork domain.Work
|
|
err = s.DB.Preload("Copyrights").First(&foundWork, work.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Require().Len(foundWork.Copyrights, 1)
|
|
s.Equal(copyright.ID, foundWork.Copyrights[0].ID)
|
|
})
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) TestRemoveCopyrightFromWork() {
|
|
s.Run("should remove a copyright from a work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.Require().NoError(s.CopyrightRepo.Create(context.Background(), copyright))
|
|
s.Require().NoError(s.commands.AddCopyrightToWork(context.Background(), work.ID, copyright.ID))
|
|
|
|
// Act
|
|
err := s.commands.RemoveCopyrightFromWork(context.Background(), work.ID, copyright.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
|
|
// Verify that the association was removed from the database
|
|
var foundWork domain.Work
|
|
err = s.DB.Preload("Copyrights").First(&foundWork, work.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Require().Len(foundWork.Copyrights, 0)
|
|
})
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) TestAddCopyrightToAuthor() {
|
|
s.Run("should add a copyright to an author", func() {
|
|
// Arrange
|
|
author := &domain.Author{Name: "Test Author"}
|
|
s.Require().NoError(s.AuthorRepo.Create(context.Background(), author))
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.Require().NoError(s.CopyrightRepo.Create(context.Background(), copyright))
|
|
|
|
// Act
|
|
err := s.commands.AddCopyrightToAuthor(context.Background(), author.ID, copyright.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
var foundAuthor domain.Author
|
|
err = s.DB.Preload("Copyrights").First(&foundAuthor, author.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Require().Len(foundAuthor.Copyrights, 1)
|
|
s.Equal(copyright.ID, foundAuthor.Copyrights[0].ID)
|
|
})
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) TestRemoveCopyrightFromAuthor() {
|
|
s.Run("should remove a copyright from an author", func() {
|
|
// Arrange
|
|
author := &domain.Author{Name: "Test Author"}
|
|
s.Require().NoError(s.AuthorRepo.Create(context.Background(), author))
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.Require().NoError(s.CopyrightRepo.Create(context.Background(), copyright))
|
|
s.Require().NoError(s.commands.AddCopyrightToAuthor(context.Background(), author.ID, copyright.ID))
|
|
|
|
// Act
|
|
err := s.commands.RemoveCopyrightFromAuthor(context.Background(), author.ID, copyright.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
var foundAuthor domain.Author
|
|
err = s.DB.Preload("Copyrights").First(&foundAuthor, author.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Require().Len(foundAuthor.Copyrights, 0)
|
|
})
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) TestAddCopyrightToBook() {
|
|
s.Run("should add a copyright to a book", func() {
|
|
// Arrange
|
|
book := &domain.Book{Title: "Test Book"}
|
|
s.Require().NoError(s.BookRepo.Create(context.Background(), book))
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.Require().NoError(s.CopyrightRepo.Create(context.Background(), copyright))
|
|
|
|
// Act
|
|
err := s.commands.AddCopyrightToBook(context.Background(), book.ID, copyright.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
var foundBook domain.Book
|
|
err = s.DB.Preload("Copyrights").First(&foundBook, book.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Require().Len(foundBook.Copyrights, 1)
|
|
s.Equal(copyright.ID, foundBook.Copyrights[0].ID)
|
|
})
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) TestRemoveCopyrightFromBook() {
|
|
s.Run("should remove a copyright from a book", func() {
|
|
// Arrange
|
|
book := &domain.Book{Title: "Test Book"}
|
|
s.Require().NoError(s.BookRepo.Create(context.Background(), book))
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.Require().NoError(s.CopyrightRepo.Create(context.Background(), copyright))
|
|
s.Require().NoError(s.commands.AddCopyrightToBook(context.Background(), book.ID, copyright.ID))
|
|
|
|
// Act
|
|
err := s.commands.RemoveCopyrightFromBook(context.Background(), book.ID, copyright.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
var foundBook domain.Book
|
|
err = s.DB.Preload("Copyrights").First(&foundBook, book.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Require().Len(foundBook.Copyrights, 0)
|
|
})
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) TestAddCopyrightToPublisher() {
|
|
s.Run("should add a copyright to a publisher", func() {
|
|
// Arrange
|
|
publisher := &domain.Publisher{Name: "Test Publisher"}
|
|
s.Require().NoError(s.PublisherRepo.Create(context.Background(), publisher))
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.Require().NoError(s.CopyrightRepo.Create(context.Background(), copyright))
|
|
|
|
// Act
|
|
err := s.commands.AddCopyrightToPublisher(context.Background(), publisher.ID, copyright.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
var foundPublisher domain.Publisher
|
|
err = s.DB.Preload("Copyrights").First(&foundPublisher, publisher.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Require().Len(foundPublisher.Copyrights, 1)
|
|
s.Equal(copyright.ID, foundPublisher.Copyrights[0].ID)
|
|
})
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) TestRemoveCopyrightFromPublisher() {
|
|
s.Run("should remove a copyright from a publisher", func() {
|
|
// Arrange
|
|
publisher := &domain.Publisher{Name: "Test Publisher"}
|
|
s.Require().NoError(s.PublisherRepo.Create(context.Background(), publisher))
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.Require().NoError(s.CopyrightRepo.Create(context.Background(), copyright))
|
|
s.Require().NoError(s.commands.AddCopyrightToPublisher(context.Background(), publisher.ID, copyright.ID))
|
|
|
|
// Act
|
|
err := s.commands.RemoveCopyrightFromPublisher(context.Background(), publisher.ID, copyright.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
var foundPublisher domain.Publisher
|
|
err = s.DB.Preload("Copyrights").First(&foundPublisher, publisher.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Require().Len(foundPublisher.Copyrights, 0)
|
|
})
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) TestAddCopyrightToSource() {
|
|
s.Run("should add a copyright to a source", func() {
|
|
// Arrange
|
|
source := &domain.Source{Name: "Test Source"}
|
|
s.Require().NoError(s.SourceRepo.Create(context.Background(), source))
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.Require().NoError(s.CopyrightRepo.Create(context.Background(), copyright))
|
|
|
|
// Act
|
|
err := s.commands.AddCopyrightToSource(context.Background(), source.ID, copyright.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
var foundSource domain.Source
|
|
err = s.DB.Preload("Copyrights").First(&foundSource, source.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Require().Len(foundSource.Copyrights, 1)
|
|
s.Equal(copyright.ID, foundSource.Copyrights[0].ID)
|
|
})
|
|
}
|
|
|
|
func (s *CopyrightCommandsTestSuite) TestRemoveCopyrightFromSource() {
|
|
s.Run("should remove a copyright from a source", func() {
|
|
// Arrange
|
|
source := &domain.Source{Name: "Test Source"}
|
|
s.Require().NoError(s.SourceRepo.Create(context.Background(), source))
|
|
copyright := &domain.Copyright{Name: "Test Copyright", Identificator: "TC-123"}
|
|
s.Require().NoError(s.CopyrightRepo.Create(context.Background(), copyright))
|
|
s.Require().NoError(s.commands.AddCopyrightToSource(context.Background(), source.ID, copyright.ID))
|
|
|
|
// Act
|
|
err := s.commands.RemoveCopyrightFromSource(context.Background(), source.ID, copyright.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
var foundSource domain.Source
|
|
err = s.DB.Preload("Copyrights").First(&foundSource, source.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Require().Len(foundSource.Copyrights, 0)
|
|
})
|
|
}
|
|
|
|
func TestCopyrightCommands(t *testing.T) {
|
|
suite.Run(t, new(CopyrightCommandsTestSuite))
|
|
}
|