-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1009 from Giveth/f_988_estimate_matching
estimate matching
- Loading branch information
Showing
5 changed files
with
476 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} |
Oops, something went wrong.