package services_test import ( "context" "testing" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/suite" "tercul/models" "tercul/repositories" "tercul/services" "gorm.io/gorm" ) // MockWorkRepository is a mock implementation of the WorkRepository interface type MockWorkRepository struct { mock.Mock } func (m *MockWorkRepository) Create(ctx context.Context, entity *models.Work) error { args := m.Called(ctx, entity) return args.Error(0) } func (m *MockWorkRepository) CreateInTx(ctx context.Context, _ *gorm.DB, _ *models.Work) error { return nil } func (m *MockWorkRepository) GetByID(ctx context.Context, id uint) (*models.Work, error) { args := m.Called(ctx, id) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).(*models.Work), args.Error(1) } func (m *MockWorkRepository) GetByIDWithOptions(ctx context.Context, _ uint, _ *repositories.QueryOptions) (*models.Work, error) { return nil, nil } func (m *MockWorkRepository) Update(ctx context.Context, entity *models.Work) error { args := m.Called(ctx, entity) return args.Error(0) } func (m *MockWorkRepository) UpdateInTx(ctx context.Context, _ *gorm.DB, _ *models.Work) error { return nil } func (m *MockWorkRepository) Delete(ctx context.Context, id uint) error { args := m.Called(ctx, id) return args.Error(0) } func (m *MockWorkRepository) DeleteInTx(ctx context.Context, _ *gorm.DB, _ uint) error { return nil } func (m *MockWorkRepository) List(ctx context.Context, page, pageSize int) (*repositories.PaginatedResult[models.Work], error) { args := m.Called(ctx, page, pageSize) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).(*repositories.PaginatedResult[models.Work]), args.Error(1) } func (m *MockWorkRepository) ListWithOptions(ctx context.Context, _ *repositories.QueryOptions) ([]models.Work, error) { return nil, nil } func (m *MockWorkRepository) ListAll(ctx context.Context) ([]models.Work, error) { args := m.Called(ctx) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).([]models.Work), args.Error(1) } func (m *MockWorkRepository) GetAllForSync(ctx context.Context, batchSize, offset int) ([]models.Work, error) { args := m.Called(ctx, batchSize, offset) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).([]models.Work), args.Error(1) } func (m *MockWorkRepository) Count(ctx context.Context) (int64, error) { args := m.Called(ctx) return args.Get(0).(int64), args.Error(1) } func (m *MockWorkRepository) CountWithOptions(ctx context.Context, _ *repositories.QueryOptions) (int64, error) { return 0, nil } func (m *MockWorkRepository) FindWithPreload(ctx context.Context, preloads []string, id uint) (*models.Work, error) { args := m.Called(ctx, preloads, id) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).(*models.Work), args.Error(1) } func (m *MockWorkRepository) Exists(ctx context.Context, _ uint) (bool, error) { return false, nil } func (m *MockWorkRepository) BeginTx(ctx context.Context) (*gorm.DB, error) { return nil, nil } func (m *MockWorkRepository) WithTx(ctx context.Context, fn func(tx *gorm.DB) error) error { return nil } func (m *MockWorkRepository) FindByTitle(ctx context.Context, title string) ([]models.Work, error) { args := m.Called(ctx, title) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).([]models.Work), args.Error(1) } func (m *MockWorkRepository) FindByAuthor(ctx context.Context, authorID uint) ([]models.Work, error) { args := m.Called(ctx, authorID) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).([]models.Work), args.Error(1) } func (m *MockWorkRepository) FindByCategory(ctx context.Context, categoryID uint) ([]models.Work, error) { args := m.Called(ctx, categoryID) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).([]models.Work), args.Error(1) } func (m *MockWorkRepository) FindByLanguage(ctx context.Context, language string, page, pageSize int) (*repositories.PaginatedResult[models.Work], error) { args := m.Called(ctx, language, page, pageSize) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).(*repositories.PaginatedResult[models.Work]), args.Error(1) } func (m *MockWorkRepository) GetWithTranslations(ctx context.Context, id uint) (*models.Work, error) { args := m.Called(ctx, id) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).(*models.Work), args.Error(1) } func (m *MockWorkRepository) ListWithTranslations(ctx context.Context, page, pageSize int) (*repositories.PaginatedResult[models.Work], error) { args := m.Called(ctx, page, pageSize) if args.Get(0) == nil { return nil, args.Error(1) } return args.Get(0).(*repositories.PaginatedResult[models.Work]), args.Error(1) } // MockAnalyzer is a mock implementation of the Analyzer interface type MockAnalyzer struct { mock.Mock } func (m *MockAnalyzer) AnalyzeWork(ctx context.Context, workID uint) error { args := m.Called(ctx, workID) return args.Error(0) } // WorkServiceSuite is a test suite for the WorkService type WorkServiceSuite struct { suite.Suite mockRepo *MockWorkRepository mockAnalyzer *MockAnalyzer service services.WorkService } // SetupTest sets up each test func (s *WorkServiceSuite) SetupTest() { s.mockRepo = new(MockWorkRepository) s.mockAnalyzer = new(MockAnalyzer) s.service = services.NewWorkService(s.mockRepo, s.mockAnalyzer) } // TestCreateWork tests the CreateWork method func (s *WorkServiceSuite) TestCreateWork() { // Setup work := &models.Work{ Title: "Test Work", Description: "Test description", Status: "published", } work.Language = "en" // Mock repository s.mockRepo.On("Create", mock.Anything, work).Return(nil) // Execute err := s.service.CreateWork(context.Background(), work) // Assert s.Require().NoError(err) // Verify mocks s.mockRepo.AssertCalled(s.T(), "Create", mock.Anything, work) } // TestCreateWorkWithEmptyTitle tests the CreateWork method with an empty title func (s *WorkServiceSuite) TestCreateWorkWithEmptyTitle() { // Setup work := &models.Work{ Description: "Test description", Status: "published", } work.Language = "en" // Execute err := s.service.CreateWork(context.Background(), work) // Assert s.Require().Error(err) s.Contains(err.Error(), "title cannot be empty") // Verify mocks s.mockRepo.AssertNotCalled(s.T(), "Create") } // TestCreateWorkWithEmptyLanguage tests the CreateWork method with an empty language func (s *WorkServiceSuite) TestCreateWorkWithEmptyLanguage() { // Setup work := &models.Work{ Title: "Test Work", Description: "Test description", Status: "published", } // Language is empty // Execute err := s.service.CreateWork(context.Background(), work) // Assert s.Require().Error(err) s.Contains(err.Error(), "language cannot be empty") // Verify mocks s.mockRepo.AssertNotCalled(s.T(), "Create") } // TestGetWorkByID tests the GetWorkByID method func (s *WorkServiceSuite) TestGetWorkByID() { // Setup id := uint(1) expectedWork := &models.Work{ Title: "Test Work", Description: "Test description", Status: "published", } expectedWork.ID = id expectedWork.Language = "en" // Mock repository s.mockRepo.On("GetByID", mock.Anything, id).Return(expectedWork, nil) // Execute result, err := s.service.GetWorkByID(context.Background(), id) // Assert s.Require().NoError(err) s.Require().NotNil(result) s.Equal(expectedWork.ID, result.ID) s.Equal(expectedWork.Title, result.Title) s.Equal(expectedWork.Language, result.Language) // Verify mocks s.mockRepo.AssertCalled(s.T(), "GetByID", mock.Anything, id) } // TestGetWorkByIDWithInvalidID tests the GetWorkByID method with an invalid ID func (s *WorkServiceSuite) TestGetWorkByIDWithInvalidID() { // Setup id := uint(0) // Invalid ID // Execute result, err := s.service.GetWorkByID(context.Background(), id) // Assert s.Require().Error(err) s.Nil(result) s.Contains(err.Error(), "invalid work ID") // Verify mocks s.mockRepo.AssertNotCalled(s.T(), "GetByID") } // TestUpdateWork tests the UpdateWork method func (s *WorkServiceSuite) TestUpdateWork() { // Setup work := &models.Work{ Title: "Test Work", Description: "Test description", Status: "published", } work.ID = 1 work.Language = "en" // Mock repository s.mockRepo.On("Update", mock.Anything, work).Return(nil) // Execute err := s.service.UpdateWork(context.Background(), work) // Assert s.Require().NoError(err) // Verify mocks s.mockRepo.AssertCalled(s.T(), "Update", mock.Anything, work) } // TestUpdateWorkWithInvalidID tests the UpdateWork method with an invalid ID func (s *WorkServiceSuite) TestUpdateWorkWithInvalidID() { // Setup work := &models.Work{ Title: "Test Work", Description: "Test description", Status: "published", } work.ID = 0 // Invalid ID work.Language = "en" // Execute err := s.service.UpdateWork(context.Background(), work) // Assert s.Require().Error(err) s.Contains(err.Error(), "work ID cannot be zero") // Verify mocks s.mockRepo.AssertNotCalled(s.T(), "Update") } // TestDeleteWork tests the DeleteWork method func (s *WorkServiceSuite) TestDeleteWork() { // Setup id := uint(1) // Mock repository s.mockRepo.On("Delete", mock.Anything, id).Return(nil) // Execute err := s.service.DeleteWork(context.Background(), id) // Assert s.Require().NoError(err) // Verify mocks s.mockRepo.AssertCalled(s.T(), "Delete", mock.Anything, id) } // TestDeleteWorkWithInvalidID tests the DeleteWork method with an invalid ID func (s *WorkServiceSuite) TestDeleteWorkWithInvalidID() { // Setup id := uint(0) // Invalid ID // Execute err := s.service.DeleteWork(context.Background(), id) // Assert s.Require().Error(err) s.Contains(err.Error(), "invalid work ID") // Verify mocks s.mockRepo.AssertNotCalled(s.T(), "Delete") } // TestAnalyzeWork tests the AnalyzeWork method func (s *WorkServiceSuite) TestAnalyzeWork() { // Setup id := uint(1) ctx := context.Background() // Mock analyzer s.mockAnalyzer.On("AnalyzeWork", ctx, id).Return(nil) // Execute err := s.service.AnalyzeWork(ctx, id) // Assert s.Require().NoError(err) // Verify mocks s.mockAnalyzer.AssertCalled(s.T(), "AnalyzeWork", ctx, id) } // TestAnalyzeWorkWithInvalidID tests the AnalyzeWork method with an invalid ID func (s *WorkServiceSuite) TestAnalyzeWorkWithInvalidID() { // Setup id := uint(0) // Invalid ID ctx := context.Background() // Execute err := s.service.AnalyzeWork(ctx, id) // Assert s.Require().Error(err) s.Contains(err.Error(), "invalid work ID") // Verify mocks s.mockAnalyzer.AssertNotCalled(s.T(), "AnalyzeWork") } // TestWorkServiceSuite runs the test suite func TestWorkServiceSuite(t *testing.T) { suite.Run(t, new(WorkServiceSuite)) }