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.
146 lines
4.1 KiB
Go
146 lines
4.1 KiB
Go
package sql_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/testutil"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type WorkRepositoryTestSuite struct {
|
|
testutil.IntegrationTestSuite
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) SetupSuite() {
|
|
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) TestCreateWork() {
|
|
s.Run("should create a new work with a copyright", func() {
|
|
// Arrange
|
|
copyright := &domain.Copyright{
|
|
Name: "Test Copyright",
|
|
Identificator: "TC-123",
|
|
}
|
|
s.Require().NoError(s.DB.Create(copyright).Error)
|
|
|
|
work := &domain.Work{
|
|
Title: "New Test Work",
|
|
TranslatableModel: domain.TranslatableModel{
|
|
Language: "en",
|
|
},
|
|
Copyrights: []*domain.Copyright{copyright},
|
|
}
|
|
|
|
// Act
|
|
err := s.WorkRepo.Create(context.Background(), work)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.NotZero(work.ID)
|
|
|
|
// Verify that the work was actually created in the database
|
|
var foundWork domain.Work
|
|
err = s.DB.Preload("Copyrights").First(&foundWork, work.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Equal("New Test Work", foundWork.Title)
|
|
s.Equal("en", foundWork.Language)
|
|
s.Require().Len(foundWork.Copyrights, 1)
|
|
s.Equal("Test Copyright", foundWork.Copyrights[0].Name)
|
|
})
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) TestGetWorkByID() {
|
|
s.Run("should return a work by ID with copyrights", func() {
|
|
// Arrange
|
|
copyright := &domain.Copyright{
|
|
Name: "Test Copyright",
|
|
Identificator: "TC-123",
|
|
}
|
|
s.Require().NoError(s.DB.Create(copyright).Error)
|
|
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
s.Require().NoError(s.DB.Model(work).Association("Copyrights").Append(copyright))
|
|
|
|
// Act
|
|
foundWork, err := s.WorkRepo.GetByID(context.Background(), work.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.Require().NotNil(foundWork)
|
|
s.Equal(work.ID, foundWork.ID)
|
|
s.Equal("Test Work", foundWork.Title)
|
|
})
|
|
|
|
s.Run("should return error if work not found", func() {
|
|
// Act
|
|
foundWork, err := s.WorkRepo.GetByID(context.Background(), 999)
|
|
|
|
// Assert
|
|
s.Require().Error(err)
|
|
s.Nil(foundWork)
|
|
})
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) TestUpdateWork() {
|
|
s.Run("should update an existing work and its copyrights", func() {
|
|
// Arrange
|
|
copyright1 := &domain.Copyright{Name: "C1", Identificator: "C1"}
|
|
copyright2 := &domain.Copyright{Name: "C2", Identificator: "C2"}
|
|
s.Require().NoError(s.DB.Create(©right1).Error)
|
|
s.Require().NoError(s.DB.Create(©right2).Error)
|
|
|
|
work := s.CreateTestWork("Original Title", "en", "Original content")
|
|
s.Require().NoError(s.DB.Model(work).Association("Copyrights").Append(copyright1))
|
|
|
|
work.Title = "Updated Title"
|
|
s.Require().NoError(s.DB.Model(work).Association("Copyrights").Replace(copyright2))
|
|
|
|
// Act
|
|
err := s.WorkRepo.Update(context.Background(), work)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
|
|
// Verify that the work was actually updated in the database
|
|
var foundWork domain.Work
|
|
err = s.DB.Preload("Copyrights").First(&foundWork, work.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Equal("Updated Title", foundWork.Title)
|
|
s.Require().Len(foundWork.Copyrights, 1)
|
|
s.Equal("C2", foundWork.Copyrights[0].Name)
|
|
})
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) TestDeleteWork() {
|
|
s.Run("should delete an existing work and its associations", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("To Be Deleted", "en", "Content")
|
|
copyright := &domain.Copyright{Name: "C1", Identificator: "C1"}
|
|
s.Require().NoError(s.DB.Create(copyright).Error)
|
|
s.Require().NoError(s.DB.Model(work).Association("Copyrights").Append(copyright))
|
|
|
|
// Act
|
|
err := s.WorkRepo.Delete(context.Background(), work.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
|
|
// Verify that the work was actually deleted from the database
|
|
var foundWork domain.Work
|
|
err = s.DB.First(&foundWork, work.ID).Error
|
|
s.Require().Error(err)
|
|
|
|
// Verify that the association in the join table is also deleted
|
|
var count int64
|
|
s.DB.Table("work_copyrights").Where("work_id = ?", work.ID).Count(&count)
|
|
s.Zero(count)
|
|
})
|
|
}
|
|
|
|
func TestWorkRepository(t *testing.T) {
|
|
suite.Run(t, new(WorkRepositoryTestSuite))
|
|
}
|