tercul-backend/internal/data/sql/source_repository_test.go
google-labs-jules[bot] 5d6a6ef47b This commit addresses the "Stabilize non-linguistics tests and interfaces" task from TODO.md.
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.
2025-09-06 06:25:11 +00:00

102 lines
2.5 KiB
Go

package sql_test
import (
"context"
"testing"
"tercul/internal/domain"
"tercul/internal/testutil"
"github.com/stretchr/testify/suite"
)
type SourceRepositoryTestSuite struct {
testutil.IntegrationTestSuite
}
func (s *SourceRepositoryTestSuite) SetupSuite() {
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
}
func (s *SourceRepositoryTestSuite) TestCreateSource() {
s.Run("should create a new source", func() {
// Arrange
source := &domain.Source{
Name: "New Test Source",
TranslatableModel: domain.TranslatableModel{
Language: "en",
},
}
// Act
err := s.SourceRepo.Create(context.Background(), source)
// Assert
s.Require().NoError(err)
s.NotZero(source.ID)
// Verify that the source was actually created in the database
var foundSource domain.Source
err = s.DB.First(&foundSource, source.ID).Error
s.Require().NoError(err)
s.Equal("New Test Source", foundSource.Name)
s.Equal("en", foundSource.Language)
})
}
func (s *SourceRepositoryTestSuite) TestGetSourceByID() {
s.Run("should return a source by ID", func() {
// Arrange
source := &domain.Source{Name: "Test Source"}
s.Require().NoError(s.SourceRepo.Create(context.Background(), source))
// Act
foundSource, err := s.SourceRepo.GetByID(context.Background(), source.ID)
// Assert
s.Require().NoError(err)
s.Require().NotNil(foundSource)
s.Equal(source.ID, foundSource.ID)
s.Equal("Test Source", foundSource.Name)
})
}
func (s *SourceRepositoryTestSuite) TestUpdateSource() {
s.Run("should update an existing source", func() {
// Arrange
source := &domain.Source{Name: "Original Name"}
s.Require().NoError(s.SourceRepo.Create(context.Background(), source))
source.Name = "Updated Name"
// Act
err := s.SourceRepo.Update(context.Background(), source)
// Assert
s.Require().NoError(err)
var foundSource domain.Source
err = s.DB.First(&foundSource, source.ID).Error
s.Require().NoError(err)
s.Equal("Updated Name", foundSource.Name)
})
}
func (s *SourceRepositoryTestSuite) TestDeleteSource() {
s.Run("should delete an existing source", func() {
// Arrange
source := &domain.Source{Name: "To Be Deleted"}
s.Require().NoError(s.SourceRepo.Create(context.Background(), source))
// Act
err := s.SourceRepo.Delete(context.Background(), source.ID)
// Assert
s.Require().NoError(err)
var foundSource domain.Source
err = s.DB.First(&foundSource, source.ID).Error
s.Require().Error(err)
})
}
func TestSourceRepository(t *testing.T) {
suite.Run(t, new(SourceRepositoryTestSuite))
}