tercul-backend/internal/data/sql/user_repository_test.go
Damir Mukimov d50722dad5
Some checks failed
Test / Integration Tests (push) Successful in 4s
Build / Build Binary (push) Failing after 2m9s
Docker Build / Build Docker Image (push) Failing after 2m32s
Test / Unit Tests (push) Failing after 3m12s
Lint / Go Lint (push) Failing after 1m0s
Refactor ID handling to use UUIDs across the application
- Updated database models and repositories to replace uint IDs with UUIDs.
- Modified test fixtures to generate and use UUIDs for authors, translations, users, and works.
- Adjusted mock implementations to align with the new UUID structure.
- Ensured all relevant functions and methods are updated to handle UUIDs correctly.
- Added necessary imports for UUID handling in various files.
2025-12-27 00:33:34 +01:00

115 lines
3.1 KiB
Go

package sql_test
import (
"context"
"tercul/internal/data/sql"
"tercul/internal/domain"
"tercul/internal/platform/config"
"tercul/internal/testutil"
"testing"
"github.com/stretchr/testify/suite"
)
type UserRepositoryTestSuite struct {
testutil.IntegrationTestSuite
UserRepo domain.UserRepository
}
func (s *UserRepositoryTestSuite) SetupSuite() {
s.IntegrationTestSuite.SetupSuite(testutil.DefaultTestConfig())
cfg, err := config.LoadConfig()
s.Require().NoError(err)
s.UserRepo = sql.NewUserRepository(s.DB, cfg)
}
func (s *UserRepositoryTestSuite) SetupTest() {
s.IntegrationTestSuite.SetupTest()
s.DB.Exec("DELETE FROM users")
}
func TestUserRepository(t *testing.T) {
suite.Run(t, new(UserRepositoryTestSuite))
}
func (s *UserRepositoryTestSuite) createUser(username, email string, role domain.UserRole) *domain.User {
user := &domain.User{
Username: username,
Email: email,
Role: role,
}
err := s.UserRepo.Create(context.Background(), user)
s.Require().NoError(err)
return user
}
func (s *UserRepositoryTestSuite) TestFindByUsername() {
s.Run("should find a user by username", func() {
// Arrange
expectedUser := s.createUser("testuser", "test@test.com", domain.UserRoleReader)
// Act
foundUser, err := s.UserRepo.FindByUsername(context.Background(), "testuser")
// Assert
s.Require().NoError(err)
s.Require().NotNil(foundUser)
s.Equal(expectedUser.ID, foundUser.ID)
s.Equal("testuser", foundUser.Username)
})
s.Run("should return error if user not found", func() {
_, err := s.UserRepo.FindByUsername(context.Background(), "nonexistent")
s.Require().Error(err)
s.ErrorIs(err, domain.ErrEntityNotFound)
})
}
func (s *UserRepositoryTestSuite) TestFindByEmail() {
s.Run("should find a user by email", func() {
// Arrange
expectedUser := s.createUser("testuser", "test@test.com", domain.UserRoleReader)
// Act
foundUser, err := s.UserRepo.FindByEmail(context.Background(), "test@test.com")
// Assert
s.Require().NoError(err)
s.Require().NotNil(foundUser)
s.Equal(expectedUser.ID, foundUser.ID)
s.Equal("test@test.com", foundUser.Email)
})
s.Run("should return error if user not found", func() {
_, err := s.UserRepo.FindByEmail(context.Background(), "nonexistent@test.com")
s.Require().Error(err)
s.ErrorIs(err, domain.ErrEntityNotFound)
})
}
func (s *UserRepositoryTestSuite) TestListByRole() {
s.Run("should return all users for a given role", func() {
// Arrange
s.createUser("reader1", "reader1@test.com", domain.UserRoleReader)
s.createUser("reader2", "reader2@test.com", domain.UserRoleReader)
s.createUser("admin1", "admin1@test.com", domain.UserRoleAdmin)
// Act
readers, err := s.UserRepo.ListByRole(context.Background(), domain.UserRoleReader)
s.Require().NoError(err)
admins, err := s.UserRepo.ListByRole(context.Background(), domain.UserRoleAdmin)
s.Require().NoError(err)
// Assert
s.Len(readers, 2)
s.Len(admins, 1)
})
s.Run("should return empty slice if no users for role", func() {
users, err := s.UserRepo.ListByRole(context.Background(), domain.UserRoleContributor)
s.Require().NoError(err)
s.Len(users, 0)
})
}