//go:build integration package monetization_test import ( "context" "tercul/internal/app/monetization" "tercul/internal/domain" "tercul/internal/testutil" "testing" "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(s.AdminCtx, "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)) }