mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
This commit addresses a broken build state caused by a mid-stream architectural refactoring. The changes align the existing code with the new Domain-Driven Design (DDD-lite) structure outlined in `refactor.md`. Key changes include: - Defined missing domain interfaces for `Auth`, `Localization`, and `Search`. - Refactored application services to use a `Commands` and `Queries` pattern. - Updated GraphQL resolvers to call application services instead of accessing repositories directly. - Fixed dependency injection in `cmd/api/main.go` by removing the non-existent `ApplicationBuilder` and manually instantiating services. - Corrected numerous test files (`integration`, `unit`, and `repository` tests) to reflect the new architecture, including fixing mock objects and test suite setups. - Added missing database migrations for test schemas to resolve "no such table" errors. This effort successfully gets the application to a compilable state and passes a significant portion of the test suite, laying the groundwork for further development and fixing the remaining test failures.
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package sql_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"tercul/internal/data/sql"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/testutil"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type AuthorRepositoryTestSuite struct {
|
|
testutil.IntegrationTestSuite
|
|
AuthorRepo domain.AuthorRepository
|
|
}
|
|
|
|
func (s *AuthorRepositoryTestSuite) SetupSuite() {
|
|
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
|
|
s.AuthorRepo = sql.NewAuthorRepository(s.DB)
|
|
}
|
|
|
|
func (s *AuthorRepositoryTestSuite) SetupTest() {
|
|
s.DB.Exec("DELETE FROM work_authors")
|
|
s.DB.Exec("DELETE FROM authors")
|
|
s.DB.Exec("DELETE FROM works")
|
|
}
|
|
|
|
func (s *AuthorRepositoryTestSuite) createAuthor(name string) *domain.Author {
|
|
author := &domain.Author{
|
|
Name: name,
|
|
TranslatableModel: domain.TranslatableModel{
|
|
Language: "en",
|
|
},
|
|
}
|
|
err := s.AuthorRepo.Create(context.Background(), author)
|
|
s.Require().NoError(err)
|
|
return author
|
|
}
|
|
|
|
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 := s.createAuthor("Author 1")
|
|
author2 := s.createAuthor("Author 2")
|
|
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)
|
|
s.ElementsMatch([]string{"Author 1", "Author 2"}, []string{authors[0].Name, authors[1].Name})
|
|
})
|
|
}
|
|
|
|
func TestAuthorRepository(t *testing.T) {
|
|
suite.Run(t, new(AuthorRepositoryTestSuite))
|
|
}
|