mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
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.
51 lines
1.4 KiB
Go
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))
|
|
}
|