tercul-backend/internal/data/sql/book_repository_test.go
google-labs-jules[bot] fd921ee7d2 Refactor repository tests to be more DRY and maintainable.
Introduced a new testing strategy for the data access layer to avoid redundant testing of generic repository methods. This change centralizes the testing of common functionality, making the test suite cleaner and more efficient.

- Created a comprehensive test suite for the generic `BaseRepository` using a dedicated `TestEntity`. This suite covers all common CRUD operations, including transactions and error handling, in a single location.
- Added a new, focused test suite for the previously untested `CategoryRepository`.
- Refactored the existing test suites for `AuthorRepository`, `BookRepository`, `PublisherRepository`, and `SourceRepository` to remove redundant CRUD tests, leaving only tests for repository-specific logic.
- Updated the test utilities to support the new testing strategy.

This change significantly improves the maintainability and efficiency of the test suite and provides a clear, future-proof pattern for testing all repositories.
2025-09-06 13:08:49 +00:00

66 lines
1.4 KiB
Go

package sql_test
import (
"context"
"testing"
"tercul/internal/domain"
"tercul/internal/testutil"
"github.com/stretchr/testify/suite"
)
type BookRepositoryTestSuite struct {
testutil.IntegrationTestSuite
}
func (s *BookRepositoryTestSuite) SetupSuite() {
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
}
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))
}