mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
The main changes are: - Fixed GORM migration issues related to polymorphic many-to-many relationships by using the `gorm:"-"` tag on the `Copyrights`, `Monetizations`, and `Claimables` fields in the domain entities. This prevents GORM from trying to automatically manage these complex relationships, which was causing the migrations to fail. The relationships will need to be managed manually through the repositories. - Added a new test file `internal/data/sql/work_repository_test.go` with tests for the `WorkRepository`. This includes tests for the `Create`, `GetByID`, `Update`, and `Delete` methods. - The tests for the `internal/data/sql` package are now passing. I was stuck for a while on the GORM polymorphic many-to-many relationship issue. I tried several approaches to configure the GORM tags correctly, but none of them worked as expected. The `gorm:"-"` solution is a workaround that allows the project to move forward, but a more robust solution for these relationships might be needed in the future.
112 lines
2.6 KiB
Go
112 lines
2.6 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", func() {
|
|
// Arrange
|
|
work := &domain.Work{
|
|
Title: "New Test Work",
|
|
TranslatableModel: domain.TranslatableModel{
|
|
Language: "en",
|
|
},
|
|
}
|
|
|
|
// 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.First(&foundWork, work.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Equal("New Test Work", foundWork.Title)
|
|
s.Equal("en", foundWork.Language)
|
|
})
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) TestGetWorkByID() {
|
|
s.Run("should return a work by ID", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Test Work", "en", "Test content")
|
|
|
|
// 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", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("Original Title", "en", "Original content")
|
|
work.Title = "Updated Title"
|
|
|
|
// 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.First(&foundWork, work.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Equal("Updated Title", foundWork.Title)
|
|
})
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) TestDeleteWork() {
|
|
s.Run("should delete an existing work", func() {
|
|
// Arrange
|
|
work := s.CreateTestWork("To Be Deleted", "en", "Content")
|
|
|
|
// 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)
|
|
})
|
|
}
|
|
|
|
func TestWorkRepository(t *testing.T) {
|
|
suite.Run(t, new(WorkRepositoryTestSuite))
|
|
}
|