mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 02:51:34 +00:00
This commit marks the completion of a major refactoring effort to stabilize the codebase, improve its structure, and prepare it for production. The key changes include: - **Domain Layer Consolidation:** The `Work` entity and its related types, along with all other domain entities and repository interfaces, have been consolidated into the main `internal/domain` package. This eliminates import cycles and provides a single, coherent source of truth for the domain model. - **Data Access Layer Refactoring:** The repository implementations in `internal/data/sql` have been updated to align with the new domain layer. The `BaseRepositoryImpl` has been corrected to use pointer receivers, and all concrete repositories now correctly embed it, ensuring consistent and correct behavior. - **Application Layer Stabilization:** All application services in `internal/app` have been updated to use the new domain types and repository interfaces. Dependency injection has been corrected throughout the application, ensuring that all services are initialized with the correct dependencies. - **GraphQL Adapter Fixes:** The GraphQL resolver implementation in `internal/adapters/graphql` has been updated to correctly handle the new domain types and service methods. The auto-generated GraphQL code has been regenerated to ensure it is in sync with the schema and runtime. - **Test Suite Overhaul:** All test suites have been fixed to correctly implement their respective interfaces and use the updated domain model. Mock repositories and test suites have been corrected to properly embed the `testify` base types, resolving numerous build and linter errors. - **Dependency Management:** The Go modules have been tidied, and the module cache has been cleaned to ensure a consistent and correct dependency graph. - **Code Quality and Verification:** The entire codebase now passes all builds, tests, and linter checks, ensuring a high level of quality and stability. This comprehensive effort has resulted in a more robust, maintainable, and production-ready application.
151 lines
4.4 KiB
Go
151 lines
4.4 KiB
Go
package sql_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"tercul/internal/data/sql"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/platform/config"
|
|
"tercul/internal/testutil"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type WorkRepositoryTestSuite struct {
|
|
testutil.IntegrationTestSuite
|
|
WorkRepo domain.WorkRepository
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) SetupSuite() {
|
|
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
|
|
cfg, err := config.LoadConfig()
|
|
s.Require().NoError(err)
|
|
s.WorkRepo = sql.NewWorkRepository(s.DB, cfg)
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) TestCreateWork() {
|
|
s.Run("should create a new work with a copyright", func() {
|
|
// Arrange
|
|
copyright := &domain.Copyright{
|
|
Name: "Test Copyright",
|
|
Identificator: "TC-123",
|
|
}
|
|
s.Require().NoError(s.DB.Create(copyright).Error)
|
|
|
|
workModel := &domain.Work{
|
|
Title: "New Test Work",
|
|
TranslatableModel: domain.TranslatableModel{
|
|
Language: "en",
|
|
},
|
|
Copyrights: []*domain.Copyright{copyright},
|
|
}
|
|
|
|
// Act
|
|
err := s.WorkRepo.Create(context.Background(), workModel)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.NotZero(workModel.ID)
|
|
|
|
// Verify that the work was actually created in the database
|
|
var foundWork domain.Work
|
|
err = s.DB.Preload("Copyrights").First(&foundWork, workModel.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Equal("New Test Work", foundWork.Title)
|
|
s.Equal("en", foundWork.Language)
|
|
s.Require().Len(foundWork.Copyrights, 1)
|
|
s.Equal("Test Copyright", foundWork.Copyrights[0].Name)
|
|
})
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) TestGetWorkByID() {
|
|
s.Run("should return a work by ID with copyrights", func() {
|
|
// Arrange
|
|
copyright := &domain.Copyright{
|
|
Name: "Test Copyright",
|
|
Identificator: "TC-123",
|
|
}
|
|
s.Require().NoError(s.DB.Create(copyright).Error)
|
|
|
|
workModel := s.CreateTestWork("Test Work", "en", "Test content")
|
|
s.Require().NoError(s.DB.Model(workModel).Association("Copyrights").Append(copyright))
|
|
|
|
// Act
|
|
foundWork, err := s.WorkRepo.GetByID(context.Background(), workModel.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.Require().NotNil(foundWork)
|
|
s.Equal(workModel.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 and its copyrights", func() {
|
|
// Arrange
|
|
copyright1 := &domain.Copyright{Name: "C1", Identificator: "C1"}
|
|
copyright2 := &domain.Copyright{Name: "C2", Identificator: "C2"}
|
|
s.Require().NoError(s.DB.Create(©right1).Error)
|
|
s.Require().NoError(s.DB.Create(©right2).Error)
|
|
|
|
workModel := s.CreateTestWork("Original Title", "en", "Original content")
|
|
s.Require().NoError(s.DB.Model(workModel).Association("Copyrights").Append(copyright1))
|
|
|
|
workModel.Title = "Updated Title"
|
|
s.Require().NoError(s.DB.Model(workModel).Association("Copyrights").Replace(copyright2))
|
|
|
|
// Act
|
|
err := s.WorkRepo.Update(context.Background(), workModel)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
|
|
// Verify that the work was actually updated in the database
|
|
var foundWork domain.Work
|
|
err = s.DB.Preload("Copyrights").First(&foundWork, workModel.ID).Error
|
|
s.Require().NoError(err)
|
|
s.Equal("Updated Title", foundWork.Title)
|
|
s.Require().Len(foundWork.Copyrights, 1)
|
|
s.Equal("C2", foundWork.Copyrights[0].Name)
|
|
})
|
|
}
|
|
|
|
func (s *WorkRepositoryTestSuite) TestDeleteWork() {
|
|
s.Run("should delete an existing work and its associations", func() {
|
|
// Arrange
|
|
workModel := s.CreateTestWork("To Be Deleted", "en", "Content")
|
|
copyright := &domain.Copyright{Name: "C1", Identificator: "C1"}
|
|
s.Require().NoError(s.DB.Create(copyright).Error)
|
|
s.Require().NoError(s.DB.Model(workModel).Association("Copyrights").Append(copyright))
|
|
|
|
// Act
|
|
err := s.WorkRepo.Delete(context.Background(), workModel.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, workModel.ID).Error
|
|
s.Require().Error(err)
|
|
|
|
// Verify that the association in the join table is also deleted
|
|
var count int64
|
|
s.DB.Table("work_copyrights").Where("work_id = ?", workModel.ID).Count(&count)
|
|
s.Zero(count)
|
|
})
|
|
}
|
|
|
|
func TestWorkRepository(t *testing.T) {
|
|
suite.Run(t, new(WorkRepositoryTestSuite))
|
|
} |