mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit significantly increases the test coverage across the application and fixes several underlying bugs that were discovered while writing the new tests. The key changes include: - **New Tests:** Added extensive integration and unit tests for GraphQL resolvers, application services, and data repositories, substantially increasing the test coverage for packages like `graphql`, `user`, `translation`, and `analytics`. - **Authorization Bug Fixes:** - Fixed a critical bug where a user creating a `Work` was not correctly associated as its author, causing subsequent permission failures. - Corrected the authorization logic in `authz.Service` to properly check for entity ownership by non-admin users. - **Test Refactoring:** - Refactored numerous test suites to use `testify/mock` instead of manual mocks, improving test clarity and maintainability. - Isolated integration tests by creating a fresh admin user and token for each test run, eliminating test pollution. - Centralized domain errors into `internal/domain/errors.go` and updated repositories to use them, making error handling more consistent. - **Code Quality Improvements:** - Replaced manual mock implementations with `testify/mock` for better consistency. - Cleaned up redundant and outdated test files. These changes stabilize the test suite, improve the overall quality of the codebase, and move the project closer to the goal of 80% test coverage.
129 lines
4.6 KiB
Go
129 lines
4.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 LikeRepositoryTestSuite struct {
|
|
testutil.IntegrationTestSuite
|
|
LikeRepo domain.LikeRepository
|
|
UserRepo domain.UserRepository
|
|
WorkRepo domain.WorkRepository
|
|
}
|
|
|
|
func (s *LikeRepositoryTestSuite) SetupSuite() {
|
|
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
|
|
cfg, err := config.LoadConfig()
|
|
s.Require().NoError(err)
|
|
s.LikeRepo = sql.NewLikeRepository(s.DB, cfg)
|
|
s.UserRepo = sql.NewUserRepository(s.DB, cfg)
|
|
s.WorkRepo = sql.NewWorkRepository(s.DB, cfg)
|
|
}
|
|
|
|
func (s *LikeRepositoryTestSuite) SetupTest() {
|
|
s.IntegrationTestSuite.SetupTest()
|
|
s.DB.Exec("DELETE FROM likes")
|
|
s.DB.Exec("DELETE FROM works")
|
|
}
|
|
|
|
func TestLikeRepository(t *testing.T) {
|
|
suite.Run(t, new(LikeRepositoryTestSuite))
|
|
}
|
|
|
|
func (s *LikeRepositoryTestSuite) createUser(username string) *domain.User {
|
|
user := &domain.User{Username: username, Email: username + "@test.com"}
|
|
err := s.UserRepo.Create(context.Background(), user)
|
|
s.Require().NoError(err)
|
|
return user
|
|
}
|
|
|
|
func (s *LikeRepositoryTestSuite) TestListByUserID() {
|
|
s.Run("should return all likes for a given user", func() {
|
|
// Arrange
|
|
user1 := s.createUser("user1")
|
|
user2 := s.createUser("user2")
|
|
work := s.CreateTestWork(s.AdminCtx, "Test Work", "en", "Test content")
|
|
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, WorkID: &work.ID}))
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, WorkID: &work.ID}))
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user2.ID, WorkID: &work.ID}))
|
|
|
|
// Act
|
|
likes, err := s.LikeRepo.ListByUserID(context.Background(), user1.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.Len(likes, 2)
|
|
})
|
|
}
|
|
|
|
func (s *LikeRepositoryTestSuite) TestListByWorkID() {
|
|
s.Run("should return all likes for a given work", func() {
|
|
// Arrange
|
|
user1 := s.createUser("user1")
|
|
work1 := s.CreateTestWork(s.AdminCtx, "Test Work 1", "en", "Test content")
|
|
work2 := s.CreateTestWork(s.AdminCtx, "Test Work 2", "en", "Test content")
|
|
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, WorkID: &work1.ID}))
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, WorkID: &work1.ID}))
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, WorkID: &work2.ID}))
|
|
|
|
// Act
|
|
likes, err := s.LikeRepo.ListByWorkID(context.Background(), work1.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.Len(likes, 2)
|
|
})
|
|
}
|
|
|
|
func (s *LikeRepositoryTestSuite) TestListByTranslationID() {
|
|
s.Run("should return all likes for a given translation", func() {
|
|
// Arrange
|
|
user1 := s.createUser("user1")
|
|
work := s.CreateTestWork(s.AdminCtx, "Test Work", "en", "Test content")
|
|
translation1 := s.CreateTestTranslation(work.ID, "es", "Contenido de prueba")
|
|
translation2 := s.CreateTestTranslation(work.ID, "fr", "Contenu de test")
|
|
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, TranslationID: &translation1.ID}))
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, TranslationID: &translation1.ID}))
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, TranslationID: &translation2.ID}))
|
|
|
|
// Act
|
|
likes, err := s.LikeRepo.ListByTranslationID(context.Background(), translation1.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.Len(likes, 2)
|
|
})
|
|
}
|
|
|
|
func (s *LikeRepositoryTestSuite) TestListByCommentID() {
|
|
s.Run("should return all likes for a given comment", func() {
|
|
// Arrange
|
|
user1 := s.createUser("user1")
|
|
work := s.CreateTestWork(s.AdminCtx, "Test Work", "en", "Test content")
|
|
comment1 := &domain.Comment{UserID: user1.ID, WorkID: &work.ID, Text: "Comment 1"}
|
|
comment2 := &domain.Comment{UserID: user1.ID, WorkID: &work.ID, Text: "Comment 2"}
|
|
s.Require().NoError(s.DB.Create(comment1).Error)
|
|
s.Require().NoError(s.DB.Create(comment2).Error)
|
|
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, CommentID: &comment1.ID}))
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, CommentID: &comment1.ID}))
|
|
s.Require().NoError(s.LikeRepo.Create(context.Background(), &domain.Like{UserID: user1.ID, CommentID: &comment2.ID}))
|
|
|
|
// Act
|
|
likes, err := s.LikeRepo.ListByCommentID(context.Background(), comment1.ID)
|
|
|
|
// Assert
|
|
s.Require().NoError(err)
|
|
s.Len(likes, 2)
|
|
})
|
|
} |