tercul-backend/internal/data/sql/monetization_repository_test.go
google-labs-jules[bot] fd921ee7d2 Refactor repository tests to be more DRY and maintainable.
Introduced a new testing strategy for the data access layer to avoid redundant testing of generic repository methods. This change centralizes the testing of common functionality, making the test suite cleaner and more efficient.

- Created a comprehensive test suite for the generic `BaseRepository` using a dedicated `TestEntity`. This suite covers all common CRUD operations, including transactions and error handling, in a single location.
- Added a new, focused test suite for the previously untested `CategoryRepository`.
- Refactored the existing test suites for `AuthorRepository`, `BookRepository`, `PublisherRepository`, and `SourceRepository` to remove redundant CRUD tests, leaving only tests for repository-specific logic.
- Updated the test utilities to support the new testing strategy.

This change significantly improves the maintainability and efficiency of the test suite and provides a clear, future-proof pattern for testing all repositories.
2025-09-06 13:08:49 +00:00

51 lines
1.4 KiB
Go

package sql_test
import (
"context"
"testing"
"tercul/internal/domain"
"tercul/internal/testutil"
"github.com/stretchr/testify/suite"
)
type MonetizationRepositoryTestSuite struct {
testutil.IntegrationTestSuite
}
func (s *MonetizationRepositoryTestSuite) SetupSuite() {
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
}
func (s *MonetizationRepositoryTestSuite) SetupTest() {
s.DB.Exec("DELETE FROM work_monetizations")
s.DB.Exec("DELETE FROM monetizations")
s.DB.Exec("DELETE FROM works")
}
func (s *MonetizationRepositoryTestSuite) 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.MonetizationRepo.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 TestMonetizationRepository(t *testing.T) {
suite.Run(t, new(MonetizationRepositoryTestSuite))
}