mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit addresses several high-priority tasks from the TASKS.md file, including: - **Fix Background Job Panic:** Replaced `log.Fatalf` with `log.Printf` in the `asynq` server to prevent crashes. - **Refactor API Server Setup:** Consolidated the GraphQL Playground and Prometheus metrics endpoints into the main API server. - **Implement `DeleteUser` Mutation:** Implemented the `DeleteUser` resolver. - **Implement `CreateContribution` Mutation:** Implemented the `CreateContribution` resolver and its required application service. Additionally, this commit includes a major refactoring of the configuration management system to fix a broken build. The global `config.Cfg` variable has been removed and replaced with a dependency injection approach, where the configuration object is passed to all components that require it. This change has been applied across the entire codebase, including the test suite, to ensure a stable and testable application.
71 lines
1.6 KiB
Go
71 lines
1.6 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 BookRepositoryTestSuite struct {
|
|
testutil.IntegrationTestSuite
|
|
BookRepo domain.BookRepository
|
|
}
|
|
|
|
func (s *BookRepositoryTestSuite) SetupSuite() {
|
|
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
|
|
cfg, err := config.LoadConfig()
|
|
s.Require().NoError(err)
|
|
s.BookRepo = sql.NewBookRepository(s.DB, cfg)
|
|
}
|
|
|
|
func (s *BookRepositoryTestSuite) SetupTest() {
|
|
s.DB.Exec("DELETE FROM books")
|
|
}
|
|
|
|
func (s *BookRepositoryTestSuite) createBook(title, isbn string) *domain.Book {
|
|
book := &domain.Book{
|
|
Title: title,
|
|
ISBN: isbn,
|
|
TranslatableModel: domain.TranslatableModel{
|
|
Language: "en",
|
|
},
|
|
}
|
|
err := s.BookRepo.Create(context.Background(), book)
|
|
s.Require().NoError(err)
|
|
return book
|
|
}
|
|
|
|
func (s *BookRepositoryTestSuite) TestFindByISBN() {
|
|
s.Run("should return a book by ISBN", func() {
|
|
// Arrange
|
|
s.createBook("Test Book", "1234567890")
|
|
|
|
// Act
|
|
foundBook, err := s.BookRepo.FindByISBN(context.Background(), "1234567890")
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.Require().NotNil(foundBook)
|
|
s.Equal("Test Book", foundBook.Title)
|
|
})
|
|
|
|
s.Run("should return error if ISBN not found", func() {
|
|
// Arrange
|
|
s.createBook("Another Book", "1111111111")
|
|
|
|
// Act
|
|
_, err := s.BookRepo.FindByISBN(context.Background(), "9999999999")
|
|
|
|
// Assert
|
|
s.Require().Error(err)
|
|
})
|
|
}
|
|
|
|
func TestBookRepository(t *testing.T) {
|
|
suite.Run(t, new(BookRepositoryTestSuite))
|
|
} |