Skip to content

Commit

Permalink
fix: same total count when offset and size is applied (#122)
Browse files Browse the repository at this point in the history
* fix: same total count when offset ans size is applied

* Fix: test

* fix: commenting test cases

* fix: test case

* fix: test cases

* fix: comments

---------

Co-authored-by: utsav14nov <[email protected]>
  • Loading branch information
utsav14nov and utsav14nov authored Feb 15, 2024
1 parent 4cdfc56 commit 607e725
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
10 changes: 9 additions & 1 deletion internal/store/postgres/appeal_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,13 @@ func (r *AppealRepository) Find(ctx context.Context, filters *domain.ListAppeals

func (r *AppealRepository) GetAppealsTotalCount(ctx context.Context, filter *domain.ListAppealsFilter) (int64, error) {
db := r.db.WithContext(ctx)
db = applyAppealFilter(db, filter)

appealFilters := *filter
appealFilters.Size = 0
appealFilters.Offset = 0

db = applyAppealFilter(db, &appealFilters)

var count int64
err := db.Model(&model.Appeal{}).Count(&count).Error

Expand Down Expand Up @@ -179,12 +185,14 @@ func applyAppealFilter(db *gorm.DB, filters *domain.ListAppealsFilter) *gorm.DB
if filters.ResourceTypes != nil {
db = db.Where(`"resources"."type" IN ?`, filters.ResourceTypes)
}

if filters.Size > 0 {
db = db.Limit(filters.Size)
}
if filters.Offset > 0 {
db = db.Offset(filters.Offset)
}

if filters.CreatedBy != "" {
db = db.Where(`"appeals"."created_by" = ?`, filters.CreatedBy)
}
Expand Down
42 changes: 42 additions & 0 deletions internal/store/postgres/appeal_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,52 @@ func (s *AppealRepositoryTestSuite) TestGetByID() {
}

func (s *AppealRepositoryTestSuite) TestGetAppealsTotalCount() {

dummyAppeals := []*domain.Appeal{
{
ResourceID: s.dummyResource.ID,
PolicyID: s.dummyPolicy.ID,
PolicyVersion: s.dummyPolicy.Version,
AccountID: "[email protected]",
AccountType: domain.DefaultAppealAccountType,
Role: "role_test",
Status: domain.AppealStatusApproved,
Permissions: []string{"permission_test"},
CreatedBy: "[email protected]",
},
}

err := s.repository.BulkUpsert(context.Background(), dummyAppeals)
s.Require().NoError(err)

s.Run("should return 0", func() {
_, actualError := s.repository.GetAppealsTotalCount(context.Background(), &domain.ListAppealsFilter{})
s.Nil(actualError)
})

s.Run("Should always return total count on any size and any offset on success", func() {
testCases := []struct {
filters domain.ListAppealsFilter
}{
{
filters: domain.ListAppealsFilter{
Size: 1,
Offset: 0,
},
},
{
filters: domain.ListAppealsFilter{
Size: 1,
Offset: 1,
},
},
}
for _, tc := range testCases {
total, actualError := s.repository.GetAppealsTotalCount(context.Background(), &tc.filters)
s.NotNil(total)
s.Nil(actualError)
}
})
}

func (s *AppealRepositoryTestSuite) TestFind() {
Expand Down
9 changes: 8 additions & 1 deletion internal/store/postgres/grant_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ func (r *GrantRepository) List(ctx context.Context, filter domain.ListGrantsFilt

func (r *GrantRepository) GetGrantsTotalCount(ctx context.Context, filter domain.ListGrantsFilter) (int64, error) {
db := r.db.WithContext(ctx)
db = applyGrantFilter(db, filter)

grantFilters := filter
grantFilters.Size = 0
grantFilters.Offset = 0

db = applyGrantFilter(db, grantFilters)
var count int64
err := db.Model(&model.Grant{}).Count(&count).Error

Expand Down Expand Up @@ -219,12 +224,14 @@ func applyGrantFilter(db *gorm.DB, filter domain.ListGrantsFilter) *gorm.DB {
Or(`"resources"."name" LIKE ?`, fmt.Sprintf("%%%s%%", filter.Q)),
)
}

if filter.Size > 0 {
db = db.Limit(filter.Size)
}
if filter.Offset > 0 {
db = db.Offset(filter.Offset)
}

if filter.AccountIDs != nil {
db = db.Where(`"grants"."account_id" IN ?`, filter.AccountIDs)
}
Expand Down
41 changes: 41 additions & 0 deletions internal/store/postgres/grant_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,52 @@ func (s *GrantRepositoryTestSuite) TearDownSuite() {
}

func (s *GrantRepositoryTestSuite) TestGetGrantsTotalCount() {
dummyGrants := []*domain.Grant{
{
Status: domain.GrantStatusActive,
AppealID: s.dummyAppeal.ID,
AccountID: s.dummyAppeal.AccountID,
AccountType: s.dummyAppeal.AccountType,
ResourceID: s.dummyAppeal.ResourceID,
Role: s.dummyAppeal.Role,
Permissions: s.dummyAppeal.Permissions,
CreatedBy: s.dummyAppeal.CreatedBy,
IsPermanent: true,
Source: domain.GrantSourceImport,
},
}
err := s.repository.BulkInsert(context.Background(), dummyGrants)
s.Require().NoError(err)

s.Run("should return 0", func() {
_, actualError := s.repository.GetGrantsTotalCount(context.Background(), domain.ListGrantsFilter{})

s.Nil(actualError)
})

s.Run("Should always total count on any size and any offset on success", func() {
testCases := []struct {
filters domain.ListGrantsFilter
}{
{
filters: domain.ListGrantsFilter{
Size: 1,
Offset: 0,
},
},
{
filters: domain.ListGrantsFilter{
Size: 1,
Offset: 1,
},
},
}
for _, tc := range testCases {
total, actualError := s.repository.GetGrantsTotalCount(context.Background(), tc.filters)
s.NotNil(total)
s.Nil(actualError)
}
})
}
func (s *GrantRepositoryTestSuite) TestListUserRoles() {
s.Run("should return roles", func() {
Expand Down

0 comments on commit 607e725

Please sign in to comment.