Skip to content

Commit

Permalink
Update creating pagination info
Browse files Browse the repository at this point in the history
* Add test.
* Validate input arguments.
  • Loading branch information
qubicmio committed Jan 24, 2025
1 parent d472a1d commit 3c7dce3
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
29 changes: 24 additions & 5 deletions rpc/v2_endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"github.com/cockroachdb/pebble"
"github.com/qubic/go-archiver/validator/tick"
"log"
"slices"

"github.com/pkg/errors"
Expand Down Expand Up @@ -370,15 +371,33 @@ func (s *Server) GetIdentityTransfersInTickRangeV2(ctx context.Context, req *pro
totalTransactions = append(totalTransactions, transfers)
}

pagination, err := getPaginationInformation(totalCount, pageNumber+1, int(pageSize))
if err != nil {
log.Printf("Error creating pagination info: %s", err.Error())
return nil, status.Error(codes.Internal, "creating pagination info")
}

return &protobuff.GetIdentityTransfersInTickRangeResponseV2{
Pagination: getPaginationInformation(totalCount, pageNumber+1, int(pageSize)),
Pagination: pagination,
Transactions: totalTransactions,
}, nil

}

// ATTENTION: first page has pageNumber == 1 as API starts with index 1
func getPaginationInformation(totalRecords, pageNumber, pageSize int) *protobuff.Pagination {
func getPaginationInformation(totalRecords, pageNumber, pageSize int) (*protobuff.Pagination, error) {

if pageNumber < 1 {
return nil, errors.Errorf("invalid page number [%d]", pageNumber)
}

if pageSize < 1 {
return nil, errors.Errorf("invalid page size [%d]", pageSize)
}

if totalRecords < 0 {
return nil, errors.Errorf("invalid number of total records [%d]", totalRecords)
}

totalPages := totalRecords / pageSize // rounds down
if totalRecords%pageSize != 0 {
Expand All @@ -402,10 +421,10 @@ func getPaginationInformation(totalRecords, pageNumber, pageSize int) *protobuff
CurrentPage: int32(min(totalRecords, pageNumber)), // 0 if there are no records
TotalPages: int32(totalPages), // 0 if there are no records
PageSize: int32(pageSize),
NextPage: int32(nextPage), // -1 if there is none
PreviousPage: int32(previousPage), // -1 if there is none
NextPage: int32(nextPage), // -1 if there is none
PreviousPage: int32(min(totalPages, previousPage)), // -1 if there is none, do not exceed total pages
}
return &pagination
return &pagination, nil
}

func (s *Server) GetEmptyTickListV2(ctx context.Context, req *protobuff.GetEmptyTickListRequestV2) (*protobuff.GetEmptyTickListResponseV2, error) {
Expand Down
67 changes: 67 additions & 0 deletions rpc/v2_endpoints_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package rpc

import (
"github.com/qubic/go-archiver/protobuff"
"github.com/stretchr/testify/assert"
"testing"
)

func Test_V2Endpoints_createPaginationInfo(t *testing.T) {

pagination, err := getPaginationInformation(0, 1, 100)
assert.NoError(t, err)
verify(t, pagination,
0, 0, 100, 0, -1, -1)

pagination, err = getPaginationInformation(1, 1, 100)
assert.NoError(t, err)
verify(t, pagination,
1, 1, 100, 1, -1, -1)

pagination, err = getPaginationInformation(12345, 1, 100)
assert.NoError(t, err)
verify(t, pagination,
12345, 1, 100, 124, -1, 2)

pagination, err = getPaginationInformation(12345, 10, 100)
assert.NoError(t, err)
verify(t, pagination,
12345, 10, 100, 124, 9, 11)

pagination, err = getPaginationInformation(12345, 124, 100)
assert.NoError(t, err)
verify(t, pagination,
12345, 124, 100, 124, 123, -1)

pagination, err = getPaginationInformation(12345, 125, 100)
assert.NoError(t, err)
verify(t, pagination,
12345, 125, 100, 124, 124, -1)

pagination, err = getPaginationInformation(12345, 999, 100)
assert.NoError(t, err)
verify(t, pagination,
12345, 999, 100, 124, 124, -1)

}

func Test_V2Endpoints_createPaginationInfo_givenInvalidArguments_thenError(t *testing.T) {
_, err := getPaginationInformation(12345, 0, 100)
assert.Error(t, err)

_, err = getPaginationInformation(-1, 1, 100)
assert.Error(t, err)

_, err = getPaginationInformation(12345, 1, 0)
assert.Error(t, err)
}

func verify(t *testing.T, pagination *protobuff.Pagination, totalRecords, pageNumber, pageSize, totalPages, previousPage, nextPage int) {
assert.NotNil(t, pagination)
assert.Equal(t, int32(totalRecords), pagination.TotalRecords, "unexpected number of total records")
assert.Equal(t, int32(pageNumber), pagination.CurrentPage, "unexpected current page number")
assert.Equal(t, int32(pageSize), pagination.PageSize, "unexpected page size")
assert.Equal(t, int32(totalPages), pagination.TotalPages, "unexpected number of total pages")
assert.Equal(t, int32(previousPage), pagination.PreviousPage, "unexpected previous page number")
assert.Equal(t, int32(nextPage), pagination.NextPage, "unexpected next page number")
}

0 comments on commit 3c7dce3

Please sign in to comment.