From f9e31e883696691482841791ad58f40d48f78976 Mon Sep 17 00:00:00 2001 From: Michiel Mulders Date: Wed, 1 Apr 2020 22:16:21 +0200 Subject: [PATCH] Add tests checkAddress and getRewards (#555) --- lib/source/cosmosV0-source.js | 2 +- lib/source/cosmosV2-source.js | 1 + tests/source/cosmosv0-source.test.js | 25 +++++++ tests/source/cosmosv2-source.test.js | 98 ++++++++++++++++++++++++++++ tests/source/mock_data/delegators.js | 54 +++++++++++++++ 5 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 tests/source/cosmosv0-source.test.js create mode 100644 tests/source/cosmosv2-source.test.js create mode 100644 tests/source/mock_data/delegators.js diff --git a/lib/source/cosmosV0-source.js b/lib/source/cosmosV0-source.js index 3364033341..53f79fed3c 100644 --- a/lib/source/cosmosV0-source.js +++ b/lib/source/cosmosV0-source.js @@ -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... diff --git a/lib/source/cosmosV2-source.js b/lib/source/cosmosV2-source.js index 714429e6d0..2757dd6414 100644 --- a/lib/source/cosmosV2-source.js +++ b/lib/source/cosmosV2-source.js @@ -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 }) => diff --git a/tests/source/cosmosv0-source.test.js b/tests/source/cosmosv0-source.test.js new file mode 100644 index 0000000000..38c8b9b286 --- /dev/null +++ b/tests/source/cosmosv0-source.test.js @@ -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() + }) + }) +}) diff --git a/tests/source/cosmosv2-source.test.js b/tests/source/cosmosv2-source.test.js new file mode 100644 index 0000000000..7b6ccb2c09 --- /dev/null +++ b/tests/source/cosmosv2-source.test.js @@ -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] + } + ]) + }) + }) +}) diff --git a/tests/source/mock_data/delegators.js b/tests/source/mock_data/delegators.js new file mode 100644 index 0000000000..817d3fd9f3 --- /dev/null +++ b/tests/source/mock_data/delegators.js @@ -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 +}