mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01: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.
102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
package sql_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/testutil"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type PublisherRepositoryTestSuite struct {
|
|
testutil.IntegrationTestSuite
|
|
}
|
|
|
|
func (s *PublisherRepositoryTestSuite) SetupSuite() {
|
|
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
|
|
}
|
|
|
|
func (s *PublisherRepositoryTestSuite) TestCreatePublisher() {
|
|
s.Run("should create a new publisher", func() {
|
|
// Arrange
|
|
publisher := &domain.Publisher{
|
|
Name: "New Test Publisher",
|
|
TranslatableModel: domain.TranslatableModel{
|
|
Language: "en",
|
|
},
|
|
}
|
|
|
|
// Act
|
|
err := s.PublisherRepo.Create(context.Background(), publisher)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.NotZero(publisher.ID)
|
|
|
|
// Verify that the publisher was actually created in the database
|
|
var foundPublisher domain.Publisher
|
|
err = s.DB.First(&foundPublisher, publisher.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Equal("New Test Publisher", foundPublisher.Name)
|
|
s.Equal("en", foundPublisher.Language)
|
|
})
|
|
}
|
|
|
|
func (s *PublisherRepositoryTestSuite) TestGetPublisherByID() {
|
|
s.Run("should return a publisher by ID", func() {
|
|
// Arrange
|
|
publisher := &domain.Publisher{Name: "Test Publisher"}
|
|
s.Require().NoError(s.PublisherRepo.Create(context.Background(), publisher))
|
|
|
|
// Act
|
|
foundPublisher, err := s.PublisherRepo.GetByID(context.Background(), publisher.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.Require().NotNil(foundPublisher)
|
|
s.Equal(publisher.ID, foundPublisher.ID)
|
|
s.Equal("Test Publisher", foundPublisher.Name)
|
|
})
|
|
}
|
|
|
|
func (s *PublisherRepositoryTestSuite) TestUpdatePublisher() {
|
|
s.Run("should update an existing publisher", func() {
|
|
// Arrange
|
|
publisher := &domain.Publisher{Name: "Original Name"}
|
|
s.Require().NoError(s.PublisherRepo.Create(context.Background(), publisher))
|
|
publisher.Name = "Updated Name"
|
|
|
|
// Act
|
|
err := s.PublisherRepo.Update(context.Background(), publisher)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
var foundPublisher domain.Publisher
|
|
err = s.DB.First(&foundPublisher, publisher.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Equal("Updated Name", foundPublisher.Name)
|
|
})
|
|
}
|
|
|
|
func (s *PublisherRepositoryTestSuite) TestDeletePublisher() {
|
|
s.Run("should delete an existing publisher", func() {
|
|
// Arrange
|
|
publisher := &domain.Publisher{Name: "To Be Deleted"}
|
|
s.Require().NoError(s.PublisherRepo.Create(context.Background(), publisher))
|
|
|
|
// Act
|
|
err := s.PublisherRepo.Delete(context.Background(), publisher.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
var foundPublisher domain.Publisher
|
|
err = s.DB.First(&foundPublisher, publisher.ID).Error
|
|
s.Require().Error(err)
|
|
})
|
|
}
|
|
|
|
func TestPublisherRepository(t *testing.T) {
|
|
suite.Run(t, new(PublisherRepositoryTestSuite))
|
|
}
|