tercul-backend/internal/data/sql/author_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

121 lines
3.2 KiB
Go

package sql_test
import (
"context"
"testing"
"tercul/internal/domain"
"tercul/internal/testutil"
"github.com/stretchr/testify/suite"
)
type AuthorRepositoryTestSuite struct {
testutil.IntegrationTestSuite
}
func (s *AuthorRepositoryTestSuite) SetupSuite() {
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
}
func (s *AuthorRepositoryTestSuite) TestCreateAuthor() {
s.Run("should create a new author", func() {
// Arrange
author := &domain.Author{
Name: "New Test Author",
TranslatableModel: domain.TranslatableModel{
Language: "en",
},
}
// Act
err := s.AuthorRepo.Create(context.Background(), author)
// Assert
s.Require().NoError(err)
s.NotZero(author.ID)
// Verify that the author was actually created in the database
var foundAuthor domain.Author
err = s.DB.First(&foundAuthor, author.ID).Error
s.Require().NoError(err)
s.Equal("New Test Author", foundAuthor.Name)
s.Equal("en", foundAuthor.Language)
})
}
func (s *AuthorRepositoryTestSuite) TestGetAuthorByID() {
s.Run("should return an author by ID", func() {
// Arrange
author := &domain.Author{Name: "Test Author"}
s.Require().NoError(s.AuthorRepo.Create(context.Background(), author))
// Act
foundAuthor, err := s.AuthorRepo.GetByID(context.Background(), author.ID)
// Assert
s.Require().NoError(err)
s.Require().NotNil(foundAuthor)
s.Equal(author.ID, foundAuthor.ID)
s.Equal("Test Author", foundAuthor.Name)
})
}
func (s *AuthorRepositoryTestSuite) TestUpdateAuthor() {
s.Run("should update an existing author", func() {
// Arrange
author := &domain.Author{Name: "Original Name"}
s.Require().NoError(s.AuthorRepo.Create(context.Background(), author))
author.Name = "Updated Name"
// Act
err := s.AuthorRepo.Update(context.Background(), author)
// Assert
s.Require().NoError(err)
var foundAuthor domain.Author
err = s.DB.First(&foundAuthor, author.ID).Error
s.Require().NoError(err)
s.Equal("Updated Name", foundAuthor.Name)
})
}
func (s *AuthorRepositoryTestSuite) TestDeleteAuthor() {
s.Run("should delete an existing author", func() {
// Arrange
author := &domain.Author{Name: "To Be Deleted"}
s.Require().NoError(s.AuthorRepo.Create(context.Background(), author))
// Act
err := s.AuthorRepo.Delete(context.Background(), author.ID)
// Assert
s.Require().NoError(err)
var foundAuthor domain.Author
err = s.DB.First(&foundAuthor, author.ID).Error
s.Require().Error(err)
})
}
func (s *AuthorRepositoryTestSuite) TestListByWorkID() {
s.Run("should return all authors for a given work", func() {
// Arrange
work := s.CreateTestWork("Test Work", "en", "Test content")
author1 := &domain.Author{Name: "Author 1"}
author2 := &domain.Author{Name: "Author 2"}
s.Require().NoError(s.AuthorRepo.Create(context.Background(), author1))
s.Require().NoError(s.AuthorRepo.Create(context.Background(), author2))
s.Require().NoError(s.DB.Model(&work).Association("Authors").Append([]*domain.Author{author1, author2}))
// Act
authors, err := s.AuthorRepo.ListByWorkID(context.Background(), work.ID)
// Assert
s.Require().NoError(err)
s.Len(authors, 2)
})
}
func TestAuthorRepository(t *testing.T) {
suite.Run(t, new(AuthorRepositoryTestSuite))
}