Skip to content

Commit

Permalink
Add tests checkAddress and getRewards (#555)
Browse files Browse the repository at this point in the history
  • Loading branch information
michielmulders authored Apr 1, 2020
1 parent 87eb1f0 commit f9e31e8
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/source/cosmosV0-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CosmosV0API extends RESTDataSource {
if ((await this.cache.getTotalSize()) > 100000) {
await this.cache.flush()
}
// cleareing memoizedResults
// clearing memoizedResults
this.memoizedResults.clear()
try {
return await this.get(url, null, { cacheOptions: { ttl: 1 } }) // normally setting cacheOptions should be enought, but...
Expand Down
1 change: 1 addition & 0 deletions lib/source/cosmosV2-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class CosmosV2API extends CosmosV0API {
const result = await this.query(
`distribution/delegators/${delegatorAddress}/rewards`
)

return (result.rewards || [])
.filter(({ reward }) => reward && reward.length > 0)
.map(({ reward, validator_address }) =>
Expand Down
25 changes: 25 additions & 0 deletions tests/source/cosmosv0-source.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const CosmosV0API = require('../../lib/source/cosmosV0-source')
const networks = require('../../data/networks')

describe('Cosmos V0 API', function() {
describe('checkAddress()', function() {
let api, cosmosNetworkConfig

beforeEach(() => {
cosmosNetworkConfig = networks.find(
network => network.id === 'cosmos-hub-testnet'
)
api = new CosmosV0API(cosmosNetworkConfig, {})
})

it('When a valid prefix is passed, it should not throw an error', () => {
expect(() =>
api.checkAddress(`${cosmosNetworkConfig.bech32_prefix}12345ABCDE`)
).not.toThrow()
})

it('When an invalid prefix is passed, it should throw an error', () => {
expect(() => api.checkAddress('12345ABCDE')).toThrow()
})
})
})
98 changes: 98 additions & 0 deletions tests/source/cosmosv2-source.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const CosmosV2API = require('../../lib/source/cosmosV2-source')
const networks = require('../../data/networks')
const {
delegatorAddress,
mockValidatorsDictionary,
delegatorRewards
} = require('./mock_data/delegators')

let mockDelegatorRewards = { ...delegatorRewards }
jest.mock('apollo-datasource-rest', () => {
class MockRESTDataSource {
constructor() {
this.memoizedResults = {
clear: jest.fn()
}
}

get() {
return mockDelegatorRewards
}
}

return {
RESTDataSource: MockRESTDataSource,
HTTPCache: class MockHTTPCache {}
}
})

describe('Cosmos V2 API', function() {
describe('getRewards()', function() {
let api, cosmosNetworkConfig

beforeEach(() => {
cosmosNetworkConfig = networks.find(
network => network.id === 'cosmos-hub-testnet'
)
api = new CosmosV2API(cosmosNetworkConfig, {})
mockDelegatorRewards = JSON.parse(JSON.stringify(delegatorRewards))
})

it('When an existing delegator address is passed, it should return the calculated rewards', async () => {
//Act
const result = await api.getRewards(
delegatorAddress,
mockValidatorsDictionary
)

//Assert
expect(result[0]).toHaveProperty('amount')
expect(result[0]).toHaveProperty('denom')
expect(result[0].validator).toEqual(
mockValidatorsDictionary[delegatorAddress]
)
})

it('When an existing delegator address has no rewards, it should return no rewards', async () => {
//Arrange
mockDelegatorRewards.result.rewards = []

//Act & Assert
await expect(
api.getRewards(delegatorAddress, mockValidatorsDictionary)
).resolves.toEqual([])
})

it('When an existing delegator address is passed with a reward 49000000 (umuon), it should return amount 49 (muon)', async () => {
//Arrange
mockDelegatorRewards.result.rewards[0].reward[0].amount = 49000000

//Act & Assert
await expect(
api.getRewards(delegatorAddress, mockValidatorsDictionary)
).resolves.toEqual([
{
amount: '49.000000',
denom: 'MUON',
validator: mockValidatorsDictionary[delegatorAddress]
}
])
})

it('When an existing delegator address is passed with a reward < 1 (umuon), it should return amount = 0 (muon)', async () => {
//Arrange
mockDelegatorRewards.result.rewards[0].reward[0].amount = 0.05

//Act & Assert
await expect(
api.getRewards(delegatorAddress, mockValidatorsDictionary)
).resolves.toEqual([
{
amount: '0.000000',
denom: 'MUON',
validator: mockValidatorsDictionary[delegatorAddress]
}
])
})
})
})
54 changes: 54 additions & 0 deletions tests/source/mock_data/delegators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const delegatorAddress = 'cosmosvaloper1y3v65vhz5f93k8uk7vnz5v7yr7ks5gdcumgcqr'

const mockValidatorsDictionary = {
[delegatorAddress]: {
networkId: 'cosmos-hub-testnet',
operatorAddress: delegatorAddress,
consensusPubkey:
'cosmosvalconspub1zcjduepqndtvyf7ggaamy2qzafjm9fnn3us4fllv89wtl40nt586q5g90rnsaxtvlz',
jailed: false,
details: '',
website: 'https://fissionlabs.io',
identity: '',
name: 'Fission Labs',
votingPower: '0.084015352',
startHeight: '0',
uptimePercentage: 1,
tokens: '60002.000010',
commissionUpdateTime: '2019-12-20T17:00:00Z',
commission: '0.500000000000000000',
maxCommission: '1.000000000000000000',
maxChangeCommission: '1.000000000000000000',
status: 'ACTIVE',
statusDetailed: 'active',
delegatorShares: '60002000010.000000000000000000'
}
}

const delegatorRewards = {
result: {
rewards: [
{
validator_address: delegatorAddress,
reward: [
{
denom: 'umuon',
amount: '0.107080042987452090'
}
]
}
],
total: [
{
denom: 'umuon',
amount: '0.107080042987452090'
}
]
}
}

module.exports = {
delegatorAddress,
mockValidatorsDictionary,
delegatorRewards
}

0 comments on commit f9e31e8

Please sign in to comment.