Skip to content

Commit

Permalink
fix(apps/whale-api): api error when getting all data using OCG listin…
Browse files Browse the repository at this point in the history
…g endpoint but response is empty (#2016)

<!--  Thanks for sending a pull request! -->

#### What this PR does / why we need it:
- As titled, ~to allow `nextProvider` of `ApiPagedResponse.of` to be
able to return `undefined`~ to enforce data and limit to be `> 0` in
`ApiPagedResponse.of`
- This is to fix the error on whale side currently when downstream is
requesting all results using `listProposals()` and `listProposalVotes()`
but response is empty. This is because on the upstream (`ain`) is using
`size: 0` to return all results.
- To add the previously skipped test cases

#### Which issue(s) does this PR fixes?:
<!--
(Optional) Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes #

#### Additional comments?:
  • Loading branch information
kyleleow authored Feb 3, 2023
1 parent 8b7e028 commit d353e96
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/whale-api/src/module.api/_core/api.paged.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class ApiPagedResponse<T> extends ApiRawResponse {
* @param {(item: T) => string} nextProvider to get next token when (limit === data array slice)
*/
static of<T> (data: T[], limit: number, nextProvider: (item: T) => string): ApiPagedResponse<T> {
if (data.length === limit) {
if (data.length === limit && data.length > 0 && limit > 0) {
const next = nextProvider(data[limit - 1])
return this.next(data, next)
}
Expand Down
9 changes: 7 additions & 2 deletions apps/whale-api/src/module.api/governance.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ describe('governance - listProposals and getProposal', () => {
size: 0
})
expect(result.data.length).toStrictEqual(2)
const emptyResult = await controller.listProposals(ListProposalsStatus.REJECTED, undefined, undefined, undefined, {
size: 0
})
expect(emptyResult.data.length).toStrictEqual(0)
})

it('should listProposals with all record when all flag is true', async () => {
Expand All @@ -174,8 +178,7 @@ describe('governance - listProposals and getProposal', () => {
expect(resultPage2.data.length).toStrictEqual(1)
})

// TODO: remove skip when blockchain fixes issue where start is ignored when non-all status is not passed
it.skip('should listProposals with type and pagination', async () => {
it('should listProposals with type and pagination', async () => {
const resultPage1 = await controller.listProposals(undefined, ListProposalsType.CFP, undefined, undefined, {
size: 1
})
Expand Down Expand Up @@ -359,6 +362,8 @@ describe('governance - listProposalVotes', () => {
it('should listProposalVotes with all records when all flag is true', async () => {
const result = await controller.listProposalVotes(cfpProposalId, undefined, undefined, true)
expect(result.data.length).toStrictEqual(3)
const emptyResult = await controller.listProposalVotes(vocProposalId, undefined, undefined, true)
expect(emptyResult.data.length).toStrictEqual(0)
})

it('should listProposalVotes with all masternodes', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ describe('Governance', () => {
expect(proposals.length).toStrictEqual(2)
})

// TODO: remove `skip` after blockchain fixes bug where `start` is not considered when non-all status is not passed
it.skip('should listGovProposals with start, including_start and limit', async () => {
it('should listGovProposals with start, including_start and limit', async () => {
const allProposals = await testing.rpc.governance.listGovProposals()
const proposalsPage1 = await testing.rpc.governance.listGovProposals({
pagination: {
Expand All @@ -270,8 +269,8 @@ describe('Governance', () => {
limit: 2
}
})
expect(proposalsPage1.length).toStrictEqual(allProposals.slice(0, 2))
expect(proposalsPage2.length).toStrictEqual(allProposals.slice(2, 4))
expect(proposalsPage1).toStrictEqual(allProposals.slice(0, 2))
expect(proposalsPage2).toStrictEqual(allProposals.slice(2, 4))
})

it('should listGovProposals with status, start, including_start and limit', async () => {
Expand Down Expand Up @@ -320,10 +319,9 @@ describe('Governance', () => {
expect(proposalsPage2).toStrictEqual(allVotingProposals.slice(-1))
})

// TODO: remove `skip` after blockchain fixes bug where `start` is not considered when cycle is passed
it.skip('should listGovProposals with status, type, cycle, start, including_start and limit', async () => {
it('should listGovProposals with status, type, cycle, start, including_start and limit', async () => {
const allVotingProposals = await testing.rpc.governance.listGovProposals().then(
proposals => proposals.filter(proposal => proposal.status === ProposalStatus.VOTING)
proposals => proposals.filter(proposal => proposal.status === ProposalStatus.VOTING && proposal.type === ProposalType.VOTE_OF_CONFIDENCE)
)
const proposalsPage1 = await testing.rpc.governance.listGovProposals({
status: ListProposalsStatus.VOTING,
Expand All @@ -340,7 +338,7 @@ describe('Governance', () => {
type: ListProposalsType.VOC,
cycle: 2,
pagination: {
start: proposalsPage1[1].proposalId,
start: allVotingProposals[2].proposalId,
including_start: true,
limit: 2
}
Expand Down

0 comments on commit d353e96

Please sign in to comment.