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

Commit

Permalink
add basic irv tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jparklev committed May 10, 2020
1 parent 733e226 commit e0a5350
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 3 deletions.
34 changes: 33 additions & 1 deletion packages/dai-plugin-governance/test/GovPollingService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {
dummyAllPollsData,
dummyOption,
dummyWeight,
dummyNumUnique
dummyNumUnique,
dummyBallotNoMajority,
dummyBallotNoMajorityExpect,
dummyBallotWithMajority,
dummyBallotWithMajorityExpect
} from './fixtures';
import { MKR } from '../src/utils/constants';

Expand Down Expand Up @@ -140,3 +144,31 @@ test('getPercentageMkrVoted', async () => {
expect(mockFn).toBeCalled();
expect(percentage).toBe(40);
});

test('ranked choice tallying algorithm with majority', async () => {
govQueryApiService.getMkrSupportRankedChoice = jest.fn(
() => dummyBallotWithMajority
);
govPollingService._getPoll = jest.fn(() => ({
endDate: 123
}));
const tally = await govPollingService.getTallyRankedChoiceIrv();

expect(JSON.stringify(tally)).toBe(
JSON.stringify(dummyBallotWithMajorityExpect)
);
});

test('ranked choice tallying algorithm with no majority', async () => {
govQueryApiService.getMkrSupportRankedChoice = jest.fn(
() => dummyBallotNoMajority
);
govPollingService._getPoll = jest.fn(() => ({
endDate: 123
}));
const tally = await govPollingService.getTallyRankedChoiceIrv();

expect(JSON.stringify(tally)).toBe(
JSON.stringify(dummyBallotNoMajorityExpect)
);
});
93 changes: 93 additions & 0 deletions packages/dai-plugin-governance/test/fixtures.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MKR } from '../src/utils/constants';
import { createBallot } from './helpers/index';

export const dummyMkrSupportData = [
{
Expand Down Expand Up @@ -80,3 +81,95 @@ export const parsedDummyEsmData = [
time: new Date('2019-11-13T15:03:28+00:00')
}
];

// ---

export const dummyBallotWithMajority = [
{
optionIdRaw: '769',
mkrSupport: '60.025000000000000000',
ballot: createBallot([1, 3]) // [1st choice, 2nd choice, ...]
},
{
optionIdRaw: '259',
mkrSupport: '200.598801867883985831',
ballot: createBallot([3, 1])
},
{
optionIdRaw: '770',
mkrSupport: '64.068823529411764706',
ballot: createBallot([2, 3])
}
];

export const dummyBallotWithMajorityExpect = {
rounds: 1,
winner: '3',
totalMkrParticipation: '324.692625397295750537',
options: {
'1': {
firstChoice: '60.025',
transfer: '0',
winner: false,
eliminated: false
},
'2': {
firstChoice: '64.068823529411764706',
transfer: '0',
winner: false,
eliminated: false
},
'3': {
firstChoice: '200.598801867883985831',
transfer: '0',
winner: true,
eliminated: false
}
}
};

// ---

export const dummyBallotNoMajority = [
{
optionIdRaw: '769',
mkrSupport: '60.025000000000000000',
ballot: createBallot([1, 3]) // [1st choice, 2nd choice, ...]
},
{
optionIdRaw: '259',
mkrSupport: '102.598801867883985831',
ballot: createBallot([3, 1])
},
{
optionIdRaw: '770',
mkrSupport: '64.068823529411764706',
ballot: createBallot([2, 3])
}
];

export const dummyBallotNoMajorityExpect = {
rounds: 2,
winner: '3',
totalMkrParticipation: '226.692625397295750537',
options: {
'1': {
firstChoice: '60.025',
transfer: '0',
winner: false,
eliminated: true
},
'2': {
firstChoice: '64.068823529411764706',
transfer: '0',
winner: false,
eliminated: false
},
'3': {
firstChoice: '102.598801867883985831',
transfer: '60.025',
winner: true,
eliminated: false
}
}
};
13 changes: 11 additions & 2 deletions packages/dai-plugin-governance/test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Maker from '@makerdao/dai';
import govPlugin from '../../src/index';
import configPlugin from '@makerdao/dai-plugin-config';
import { createCurrency } from '@makerdao/currency';
import { paddedArray } from '../../src/utils/helpers';

const infuraProjectId = 'c3f0f26a4c1742e0949d8eedfc47be67';

Expand Down Expand Up @@ -61,7 +62,7 @@ export async function restoreSnapshotOriginal(snapId) {
}
}

export const setupMakerOld = async (network) => {
export const setupMakerOld = async network => {
const accounts = {
owner: {
type: 'privateKey',
Expand All @@ -82,7 +83,7 @@ export const setupMakerOld = async (network) => {
};

let url;
if(network==='ganache') url = 'http://localhost:2000';
if (network === 'ganache') url = 'http://localhost:2000';
const preset = network === 'ganache' ? 'http' : network;
const maker = await Maker.create(preset, {
plugins: [[govPlugin, { network }]],
Expand Down Expand Up @@ -211,3 +212,11 @@ export const setUpAllowance = async (maker, address) => {
};

export const addressRegex = /^0x[a-fA-F0-9]{40}$/;

export const createBallot = preferenceList => {
const reversedPreferenceList = preferenceList.reverse();
return paddedArray(
32 - reversedPreferenceList.length,
reversedPreferenceList
);
};

0 comments on commit e0a5350

Please sign in to comment.