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