Skip to content

Commit

Permalink
Merge pull request #1009 from Giveth/f_988_estimate_matching
Browse files Browse the repository at this point in the history
estimate matching
  • Loading branch information
CarlosQ96 authored May 31, 2023
2 parents de0cddb + e217d5d commit fe1772a
Show file tree
Hide file tree
Showing 5 changed files with 476 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"test:campaignRepository": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/repositories/campaignRepository.test.ts",
"test:socialProfileRepository": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/repositories/socialProfileRepository.test.ts",
"test:projectVerificationRepository": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/repositories/projectVerificationRepository.test.ts",
"test:qfRoundRepository": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/repositories/qfRoundRepository.test.ts",
"test:project": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/entities/project.test.ts",
"test:notifyDonationsWithSegment": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/services/cronJobs/notifyDonationsWithSegment.test.ts",
"test:checkProjectVerificationStatus": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/services/cronJobs/checkProjectVerificationStatus.test.ts",
Expand Down
235 changes: 235 additions & 0 deletions src/repositories/qfRoundRepository.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
import {
createDonationData,
createProjectData,
generateRandomEtheriumAddress,
saveDonationDirectlyToDb,
saveProjectDirectlyToDb,
saveUserDirectlyToDb,
} from '../../test/testUtils';
import { QfRound } from '../entities/qfRound';
import { expect } from 'chai';
import {
getProjectDonationsSqrtRootSum,
getQfRoundTotalProjectsDonationsSum,
} from './qfRoundRepository';
import { Project } from '../entities/project';

describe(
'getProjectDonationsSqrtRootSum test cases',
getProjectDonationsSqrRootSumTests,
);
describe(
'getQfRoundTotalProjectsDonationsSum test cases',
getQfRoundTotalProjectsDonationsSumTestCases,
);

function getProjectDonationsSqrRootSumTests() {
let qfRound: QfRound;
let project: Project;

beforeEach(async () => {
await QfRound.update({}, { isActive: false });
qfRound = QfRound.create({
isActive: true,
name: 'test',
allocatedFund: 100,
beginDate: new Date(),
endDate: new Date(),
});
await qfRound.save();
project = await saveProjectDirectlyToDb(createProjectData());
project.qfRounds = [qfRound];
await project.save();
});

afterEach(async () => {
qfRound.isActive = false;
await qfRound.save();
});

it('should return 0 when no donations', async () => {
const { sqrtRootSum, count } = await getProjectDonationsSqrtRootSum(
project.id,
qfRound.id,
);
expect(sqrtRootSum).to.equal(0);
expect(count).to.equal(0);
});

it('should return correct value on single donation', async () => {
const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const donation = await saveDonationDirectlyToDb(
{ ...createDonationData(), valueUsd: 100, qfRoundId: qfRound.id },
user.id,
project.id,
);

const { sqrtRootSum, count } = await getProjectDonationsSqrtRootSum(
project.id,
qfRound.id,
);
expect(sqrtRootSum).to.equal(10);
expect(count).to.equal(1);
});

it('should return correct value on multiple donations', async () => {
const valuesUsd = [4, 25, 100, 1024];
await Promise.all(
valuesUsd.map(async (valueUsd, index) => {
const user = await saveUserDirectlyToDb(
generateRandomEtheriumAddress(),
);
return saveDonationDirectlyToDb(
{ ...createDonationData(), valueUsd, qfRoundId: qfRound.id },
user.id,
project.id,
);
}),
);

const { sqrtRootSum, count } = await getProjectDonationsSqrtRootSum(
project.id,
qfRound.id,
);
// sqrtRootSum = sqrt(4) + sqrt(25) + sqrt(100) + sqrt(1024) = 2 + 5 + 10 + 32 = 49
const expectedSum = 49;

expect(sqrtRootSum).to.equal(expectedSum);
expect(count).to.equal(4);
});

it('should return correct value on multiple donations with same user', async () => {
const usersDonations: number[][] = [
[1, 3], // 4
[2, 23], // 25
[3, 97], // 100
];

await Promise.all(
usersDonations.map(async valuesUsd => {
const user = await saveUserDirectlyToDb(
generateRandomEtheriumAddress(),
);
return Promise.all(
valuesUsd.map(valueUsd => {
return saveDonationDirectlyToDb(
{ ...createDonationData(), valueUsd, qfRoundId: qfRound.id },
user.id,
project.id,
);
}),
);
}),
);

const { sqrtRootSum, count } = await getProjectDonationsSqrtRootSum(
project.id,
qfRound.id,
);
// sqrtRootSum = sqrt(4) + sqrt(25) + sqrt(100) = 2 + 5 + 10 = 17
const expectedSum = 17;

expect(sqrtRootSum).to.equal(expectedSum);
expect(count).to.equal(3);
});
}

function getQfRoundTotalProjectsDonationsSumTestCases() {
let qfRound: QfRound;
let firstProject: Project;
let secondProject: Project;
beforeEach(async () => {
await QfRound.update({}, { isActive: false });
qfRound = QfRound.create({
isActive: true,
name: 'test',
allocatedFund: 100,
beginDate: new Date(),
endDate: new Date(),
});
await qfRound.save();
firstProject = await saveProjectDirectlyToDb(createProjectData());
secondProject = await saveProjectDirectlyToDb(createProjectData());

firstProject.qfRounds = [qfRound];
secondProject.qfRounds = [qfRound];

await firstProject.save();
await secondProject.save();
});

afterEach(async () => {
qfRound.isActive = false;
await qfRound.save();
});

it('should return 0 when no donations', async () => {
const { sum, contributorsCount } =
await getQfRoundTotalProjectsDonationsSum(qfRound.id);
expect(sum).to.equal(0);
expect(contributorsCount).to.equal(0);
});

it('should return correct value for single project', async () => {
const usersDonations: number[][] = [
[1, 3], // 4
[2, 23], // 25
[3, 97], // 100
];

await Promise.all(
usersDonations.map(async valuesUsd => {
const user = await saveUserDirectlyToDb(
generateRandomEtheriumAddress(),
);
return Promise.all(
valuesUsd.map(valueUsd => {
return saveDonationDirectlyToDb(
{ ...createDonationData(), valueUsd, qfRoundId: qfRound.id },
user.id,
firstProject.id,
);
}),
);
}),
);

const { sum, contributorsCount } =
await getQfRoundTotalProjectsDonationsSum(qfRound.id);
expect(sum).to.equal(289);
expect(contributorsCount).to.equal(3);
});

it('should return correct value for multiple projects', async () => {
const user1 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const user2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const user3 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const usersDonations: [number, number, number[]][] = [
[user1.id, firstProject.id, [1, 3]], // 4
[user1.id, secondProject.id, [4, 4 * 3]], // 16
[user2.id, firstProject.id, [2, 23]], // 25
[user2.id, secondProject.id, [4 * 2, 4 * 23]], // 25 * 4
[user3.id, firstProject.id, [3, 97]], // 100
[user3.id, secondProject.id, [3 * 4, 97 * 4]], // 100 * 4
];

await Promise.all(
usersDonations.map(([userId, projectId, valuesUsd]) => {
return Promise.all(
valuesUsd.map(valueUsd => {
return saveDonationDirectlyToDb(
{ ...createDonationData(), valueUsd, qfRoundId: qfRound.id },
userId,
projectId,
);
}),
);
}),
);

const { sum, contributorsCount } =
await getQfRoundTotalProjectsDonationsSum(qfRound.id);
expect(sum).to.equal(289 * 5);
expect(contributorsCount).to.equal(3 * 2);
});
}
Loading

0 comments on commit fe1772a

Please sign in to comment.