tercul-backend/internal/app/monetization/commands_test.go
google-labs-jules[bot] 5d6a6ef47b This commit addresses the "Stabilize non-linguistics tests and interfaces" task from TODO.md.
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.
2025-09-06 06:25:11 +00:00

216 lines
7.6 KiB
Go

package monetization_test
import (
"context"
"testing"
"tercul/internal/app/monetization"
"tercul/internal/domain"
"tercul/internal/testutil"
"github.com/stretchr/testify/suite"
)
type MonetizationCommandsTestSuite struct {
testutil.IntegrationTestSuite
commands *monetization.MonetizationCommands
}
func (s *MonetizationCommandsTestSuite) SetupSuite() {
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
s.commands = monetization.NewMonetizationCommands(s.MonetizationRepo)
}
func (s *MonetizationCommandsTestSuite) TestAddMonetizationToWork() {
s.Run("should add a monetization to a work", func() {
// Arrange
work := s.CreateTestWork("Test Work", "en", "Test content")
monetization := &domain.Monetization{Amount: 10.0}
s.Require().NoError(s.DB.Create(monetization).Error)
// Act
err := s.commands.AddMonetizationToWork(context.Background(), work.ID, monetization.ID)
// Assert
s.Require().NoError(err)
// Verify that the association was created in the database
var foundWork domain.Work
err = s.DB.Preload("Monetizations").First(&foundWork, work.ID).Error
s.Require().NoError(err)
s.Require().Len(foundWork.Monetizations, 1)
s.Equal(monetization.ID, foundWork.Monetizations[0].ID)
})
}
func (s *MonetizationCommandsTestSuite) TestAddMonetizationToAuthor() {
s.Run("should add a monetization to an author", func() {
// Arrange
author := &domain.Author{Name: "Test Author"}
s.Require().NoError(s.AuthorRepo.Create(context.Background(), author))
monetization := &domain.Monetization{Amount: 10.0}
s.Require().NoError(s.DB.Create(monetization).Error)
// Act
err := s.commands.AddMonetizationToAuthor(context.Background(), author.ID, monetization.ID)
// Assert
s.Require().NoError(err)
var foundAuthor domain.Author
err = s.DB.Preload("Monetizations").First(&foundAuthor, author.ID).Error
s.Require().NoError(err)
s.Require().Len(foundAuthor.Monetizations, 1)
s.Equal(monetization.ID, foundAuthor.Monetizations[0].ID)
})
}
func (s *MonetizationCommandsTestSuite) TestRemoveMonetizationFromAuthor() {
s.Run("should remove a monetization from an author", func() {
// Arrange
author := &domain.Author{Name: "Test Author"}
s.Require().NoError(s.AuthorRepo.Create(context.Background(), author))
monetization := &domain.Monetization{Amount: 10.0}
s.Require().NoError(s.DB.Create(monetization).Error)
s.Require().NoError(s.commands.AddMonetizationToAuthor(context.Background(), author.ID, monetization.ID))
// Act
err := s.commands.RemoveMonetizationFromAuthor(context.Background(), author.ID, monetization.ID)
// Assert
s.Require().NoError(err)
var foundAuthor domain.Author
err = s.DB.Preload("Monetizations").First(&foundAuthor, author.ID).Error
s.Require().NoError(err)
s.Require().Len(foundAuthor.Monetizations, 0)
})
}
func (s *MonetizationCommandsTestSuite) TestAddMonetizationToBook() {
s.Run("should add a monetization to a book", func() {
// Arrange
book := &domain.Book{Title: "Test Book"}
s.Require().NoError(s.BookRepo.Create(context.Background(), book))
monetization := &domain.Monetization{Amount: 10.0}
s.Require().NoError(s.DB.Create(monetization).Error)
// Act
err := s.commands.AddMonetizationToBook(context.Background(), book.ID, monetization.ID)
// Assert
s.Require().NoError(err)
var foundBook domain.Book
err = s.DB.Preload("Monetizations").First(&foundBook, book.ID).Error
s.Require().NoError(err)
s.Require().Len(foundBook.Monetizations, 1)
s.Equal(monetization.ID, foundBook.Monetizations[0].ID)
})
}
func (s *MonetizationCommandsTestSuite) TestRemoveMonetizationFromBook() {
s.Run("should remove a monetization from a book", func() {
// Arrange
book := &domain.Book{Title: "Test Book"}
s.Require().NoError(s.BookRepo.Create(context.Background(), book))
monetization := &domain.Monetization{Amount: 10.0}
s.Require().NoError(s.DB.Create(monetization).Error)
s.Require().NoError(s.commands.AddMonetizationToBook(context.Background(), book.ID, monetization.ID))
// Act
err := s.commands.RemoveMonetizationFromBook(context.Background(), book.ID, monetization.ID)
// Assert
s.Require().NoError(err)
var foundBook domain.Book
err = s.DB.Preload("Monetizations").First(&foundBook, book.ID).Error
s.Require().NoError(err)
s.Require().Len(foundBook.Monetizations, 0)
})
}
func (s *MonetizationCommandsTestSuite) TestAddMonetizationToPublisher() {
s.Run("should add a monetization to a publisher", func() {
// Arrange
publisher := &domain.Publisher{Name: "Test Publisher"}
s.Require().NoError(s.PublisherRepo.Create(context.Background(), publisher))
monetization := &domain.Monetization{Amount: 10.0}
s.Require().NoError(s.DB.Create(monetization).Error)
// Act
err := s.commands.AddMonetizationToPublisher(context.Background(), publisher.ID, monetization.ID)
// Assert
s.Require().NoError(err)
var foundPublisher domain.Publisher
err = s.DB.Preload("Monetizations").First(&foundPublisher, publisher.ID).Error
s.Require().NoError(err)
s.Require().Len(foundPublisher.Monetizations, 1)
s.Equal(monetization.ID, foundPublisher.Monetizations[0].ID)
})
}
func (s *MonetizationCommandsTestSuite) TestRemoveMonetizationFromPublisher() {
s.Run("should remove a monetization from a publisher", func() {
// Arrange
publisher := &domain.Publisher{Name: "Test Publisher"}
s.Require().NoError(s.PublisherRepo.Create(context.Background(), publisher))
monetization := &domain.Monetization{Amount: 10.0}
s.Require().NoError(s.DB.Create(monetization).Error)
s.Require().NoError(s.commands.AddMonetizationToPublisher(context.Background(), publisher.ID, monetization.ID))
// Act
err := s.commands.RemoveMonetizationFromPublisher(context.Background(), publisher.ID, monetization.ID)
// Assert
s.Require().NoError(err)
var foundPublisher domain.Publisher
err = s.DB.Preload("Monetizations").First(&foundPublisher, publisher.ID).Error
s.Require().NoError(err)
s.Require().Len(foundPublisher.Monetizations, 0)
})
}
func (s *MonetizationCommandsTestSuite) TestAddMonetizationToSource() {
s.Run("should add a monetization to a source", func() {
// Arrange
source := &domain.Source{Name: "Test Source"}
s.Require().NoError(s.SourceRepo.Create(context.Background(), source))
monetization := &domain.Monetization{Amount: 10.0}
s.Require().NoError(s.DB.Create(monetization).Error)
// Act
err := s.commands.AddMonetizationToSource(context.Background(), source.ID, monetization.ID)
// Assert
s.Require().NoError(err)
var foundSource domain.Source
err = s.DB.Preload("Monetizations").First(&foundSource, source.ID).Error
s.Require().NoError(err)
s.Require().Len(foundSource.Monetizations, 1)
s.Equal(monetization.ID, foundSource.Monetizations[0].ID)
})
}
func (s *MonetizationCommandsTestSuite) TestRemoveMonetizationFromSource() {
s.Run("should remove a monetization from a source", func() {
// Arrange
source := &domain.Source{Name: "Test Source"}
s.Require().NoError(s.SourceRepo.Create(context.Background(), source))
monetization := &domain.Monetization{Amount: 10.0}
s.Require().NoError(s.DB.Create(monetization).Error)
s.Require().NoError(s.commands.AddMonetizationToSource(context.Background(), source.ID, monetization.ID))
// Act
err := s.commands.RemoveMonetizationFromSource(context.Background(), source.ID, monetization.ID)
// Assert
s.Require().NoError(err)
var foundSource domain.Source
err = s.DB.Preload("Monetizations").First(&foundSource, source.ID).Error
s.Require().NoError(err)
s.Require().Len(foundSource.Monetizations, 0)
})
}
func TestMonetizationCommands(t *testing.T) {
suite.Run(t, new(MonetizationCommandsTestSuite))
}