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)) }