Skip to content
This repository has been archived by the owner on Jan 17, 2022. It is now read-only.

Commit

Permalink
fix: include accumulator at stop index (#17)
Browse files Browse the repository at this point in the history
* fix: include accumulator at stop index

* test: add tests for getAccumulatorArray

Co-authored-by: William <[email protected]>
  • Loading branch information
weichweich and wischli authored Mar 17, 2020
1 parent 04f2099 commit a5da253
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/blockchain/Blockchain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,36 @@ describe('chain mocks', () => {
)
expect(latestAccumulator.valueOf()).toBe(newAccumulator.valueOf())
})
it('Should getAccumulatorArray', async () => {
api.query.portablegabi.accumulatorList.mockResolvedValue(
(stringToHex('currAccumulator') as unknown) as Promise<Codec>
)
const currAccumulators = await BlockchainMock.getAccumulatorArray(
dummyAddress,
1,
4
)
expect(Array.isArray(currAccumulators)).toBeTruthy()
expect(currAccumulators.length).toEqual(4)
})
})
describe('Negative tests', () => {
it('Should throw error for swapped indices when requesting accumulator array', async () => {
api.query.portablegabi.accumulatorList.mockResolvedValue(
(stringToHex('currAccumulator') as unknown) as Promise<Codec>
)
expect(
BlockchainMock.getAccumulatorArray(dummyAddress, 4, 1)
).rejects.toThrowError()
})
it('Should throw error when accumulator array index out is of range', async () => {
api.query.portablegabi.accumulatorList.mockResolvedValue(
(stringToHex('currAccumulator') as unknown) as Promise<Codec>
)
expect(
BlockchainMock.getAccumulatorArray(dummyAddress, 999999999)
).rejects.toThrowError()
})
it('Should throw for empty accumulatorList (maxIndex === -1)', async () => {
const accCount = 0
const requestedIndex = 1
Expand Down
2 changes: 1 addition & 1 deletion src/blockchain/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export default class Blockchain implements IBlockchainApi {
}
const endIndex = _endIndex || (await this.getMaxIndex(address))
// create [[address, startIndex], ..., [address, endIndex]] for multi query
const multiQuery = new Array(endIndex - startIndex)
const multiQuery = new Array(endIndex - startIndex + 1)
.fill(startIndex)
.map((x, i) => [address, x + i])

Expand Down

0 comments on commit a5da253

Please sign in to comment.