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.
162 lines
4.8 KiB
Go
162 lines
4.8 KiB
Go
package work
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"tercul/internal/domain"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
type WorkQueriesSuite struct {
|
|
suite.Suite
|
|
repo *mockWorkRepository
|
|
queries *WorkQueries
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) SetupTest() {
|
|
s.repo = &mockWorkRepository{}
|
|
s.queries = NewWorkQueries(s.repo)
|
|
}
|
|
|
|
func TestWorkQueriesSuite(t *testing.T) {
|
|
suite.Run(t, new(WorkQueriesSuite))
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestGetWorkByID_Success() {
|
|
work := &domain.Work{Title: "Test Work"}
|
|
work.ID = 1
|
|
s.repo.On("GetByID", mock.Anything, uint(1)).Return(work, nil)
|
|
|
|
w, err := s.queries.GetWorkByID(context.Background(), 1)
|
|
assert.NoError(s.T(), err)
|
|
expectedDTO := &WorkDTO{
|
|
ID: work.ID,
|
|
Title: work.Title,
|
|
Language: work.Language,
|
|
}
|
|
assert.Equal(s.T(), expectedDTO, w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestGetWorkByID_ZeroID() {
|
|
w, err := s.queries.GetWorkByID(context.Background(), 0)
|
|
assert.Error(s.T(), err)
|
|
assert.Nil(s.T(), w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestListByCollectionID_Success() {
|
|
works := []domain.Work{{Title: "Test Work"}}
|
|
s.repo.On("ListByCollectionID", mock.Anything, uint(1)).Return(works, nil)
|
|
w, err := s.queries.ListByCollectionID(context.Background(), 1)
|
|
assert.NoError(s.T(), err)
|
|
assert.Equal(s.T(), works, w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestListByCollectionID_ZeroID() {
|
|
w, err := s.queries.ListByCollectionID(context.Background(), 0)
|
|
assert.Error(s.T(), err)
|
|
assert.Nil(s.T(), w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestListWorks_Success() {
|
|
domainWorks := &domain.PaginatedResult[domain.Work]{
|
|
Items: []domain.Work{
|
|
{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 1}, Language: "en"}, Title: "Work 1"},
|
|
{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: 2}, Language: "fr"}, Title: "Work 2"},
|
|
},
|
|
TotalCount: 2,
|
|
Page: 1,
|
|
PageSize: 10,
|
|
TotalPages: 1,
|
|
}
|
|
s.repo.On("List", mock.Anything, 1, 10).Return(domainWorks, nil)
|
|
|
|
paginatedDTOs, err := s.queries.ListWorks(context.Background(), 1, 10)
|
|
assert.NoError(s.T(), err)
|
|
|
|
expectedDTOs := &domain.PaginatedResult[WorkDTO]{
|
|
Items: []WorkDTO{
|
|
{ID: 1, Title: "Work 1", Language: "en"},
|
|
{ID: 2, Title: "Work 2", Language: "fr"},
|
|
},
|
|
TotalCount: 2,
|
|
Page: 1,
|
|
PageSize: 10,
|
|
TotalPages: 1,
|
|
}
|
|
assert.Equal(s.T(), expectedDTOs, paginatedDTOs)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestGetWorkWithTranslations_Success() {
|
|
work := &domain.Work{Title: "Test Work"}
|
|
work.ID = 1
|
|
s.repo.On("GetWithTranslations", mock.Anything, uint(1)).Return(work, nil)
|
|
w, err := s.queries.GetWorkWithTranslations(context.Background(), 1)
|
|
assert.NoError(s.T(), err)
|
|
assert.Equal(s.T(), work, w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestGetWorkWithTranslations_ZeroID() {
|
|
w, err := s.queries.GetWorkWithTranslations(context.Background(), 0)
|
|
assert.Error(s.T(), err)
|
|
assert.Nil(s.T(), w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestFindWorksByTitle_Success() {
|
|
works := []domain.Work{{Title: "Test Work"}}
|
|
s.repo.On("FindByTitle", mock.Anything, "Test").Return(works, nil)
|
|
w, err := s.queries.FindWorksByTitle(context.Background(), "Test")
|
|
assert.NoError(s.T(), err)
|
|
assert.Equal(s.T(), works, w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestFindWorksByTitle_Empty() {
|
|
w, err := s.queries.FindWorksByTitle(context.Background(), "")
|
|
assert.Error(s.T(), err)
|
|
assert.Nil(s.T(), w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestFindWorksByAuthor_Success() {
|
|
works := []domain.Work{{Title: "Test Work"}}
|
|
s.repo.On("FindByAuthor", mock.Anything, uint(1)).Return(works, nil)
|
|
w, err := s.queries.FindWorksByAuthor(context.Background(), 1)
|
|
assert.NoError(s.T(), err)
|
|
assert.Equal(s.T(), works, w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestFindWorksByAuthor_ZeroID() {
|
|
w, err := s.queries.FindWorksByAuthor(context.Background(), 0)
|
|
assert.Error(s.T(), err)
|
|
assert.Nil(s.T(), w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestFindWorksByCategory_Success() {
|
|
works := []domain.Work{{Title: "Test Work"}}
|
|
s.repo.On("FindByCategory", mock.Anything, uint(1)).Return(works, nil)
|
|
w, err := s.queries.FindWorksByCategory(context.Background(), 1)
|
|
assert.NoError(s.T(), err)
|
|
assert.Equal(s.T(), works, w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestFindWorksByCategory_ZeroID() {
|
|
w, err := s.queries.FindWorksByCategory(context.Background(), 0)
|
|
assert.Error(s.T(), err)
|
|
assert.Nil(s.T(), w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestFindWorksByLanguage_Success() {
|
|
works := &domain.PaginatedResult[domain.Work]{}
|
|
s.repo.On("FindByLanguage", mock.Anything, "en", 1, 10).Return(works, nil)
|
|
w, err := s.queries.FindWorksByLanguage(context.Background(), "en", 1, 10)
|
|
assert.NoError(s.T(), err)
|
|
assert.Equal(s.T(), works, w)
|
|
}
|
|
|
|
func (s *WorkQueriesSuite) TestFindWorksByLanguage_Empty() {
|
|
w, err := s.queries.FindWorksByLanguage(context.Background(), "", 1, 10)
|
|
assert.Error(s.T(), err)
|
|
assert.Nil(s.T(), w)
|
|
} |