Skip to content

Commit

Permalink
add tests to the user sync worker and cronjob
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosQ96 committed Sep 30, 2024
1 parent c4043ae commit 38f52bd
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 19 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
"test:qfRoundHistoryRepository": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/repositories/qfRoundHistoryRepository.test.ts",
"test:qfRoundService": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/services/qfRoundService.test.ts",
"test:project": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/entities/project.test.ts",
"test:syncUsersModelScore": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/services/cronJobs/syncUsersModelScore.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",
"test:statusReasonResolver": "NODE_ENV=test mocha ./test/pre-test-scripts.ts ./src/resolvers/statusReasonResolver.test.ts",
Expand Down
56 changes: 56 additions & 0 deletions src/repositories/qfRoundRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getProjectDonationsSqrtRootSum,
getQfRoundTotalSqrtRootSumSquared,
getQfRoundStats,
findUsersWithoutMBDScoreInActiveAround,
} from './qfRoundRepository';
import { Project } from '../entities/project';
import { refreshProjectEstimatedMatchingView } from '../services/projectViewsService';
Expand All @@ -26,6 +27,11 @@ describe(
'getProjectDonationsSqrtRootSum test cases',
getProjectDonationsSqrRootSumTests,
);

describe(
'findUsersWithoutMBDScoreInActiveAround test cases',
findUsersWithoutMBDScoreInActiveAroundTestCases,
);
describe(
'getQfRoundTotalProjectsDonationsSum test cases',
getQfRoundTotalProjectsDonationsSumTestCases,
Expand All @@ -41,6 +47,56 @@ describe(
describe('findQfRoundById test cases', findQfRoundByIdTestCases);
describe('findQfRoundBySlug test cases', findQfRoundBySlugTestCases);

function findUsersWithoutMBDScoreInActiveAroundTestCases() {
it('should find users without score that donated in the round', async () => {
await QfRound.update({}, { isActive: false });
const qfRound = QfRound.create({
isActive: true,
name: 'test',
allocatedFund: 100,
minimumPassportScore: 8,
slug: new Date().getTime().toString(),
beginDate: new Date(),
endDate: moment().add(10, 'days').toDate(),
});
await qfRound.save();
const project = await saveProjectDirectlyToDb(createProjectData());
project.qfRounds = [qfRound];
await project.save();

const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const user2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
await saveDonationDirectlyToDb(
{
...createDonationData(),
segmentNotified: false,
qfRoundId: qfRound.id,
status: 'verified',
},
user.id,
project.id,
);

await saveDonationDirectlyToDb(
{
...createDonationData(),
segmentNotified: false,
qfRoundId: qfRound.id,
status: 'verified',
},
user2.id,
project.id,
);

const userIds = await findUsersWithoutMBDScoreInActiveAround();
assert.equal(userIds.length, 2);
assert.isTrue(userIds.includes(user.id) && userIds.includes(user2.id));

qfRound.isActive = false;
await qfRound.save();
});
}

function getProjectDonationsSqrRootSumTests() {
let qfRound: QfRound;
let project: Project;
Expand Down
2 changes: 1 addition & 1 deletion src/repositories/qfRoundRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export const findUsersWithoutMBDScoreInActiveAround = async (): Promise<
[activeQfRoundId],
);

return usersMissingMDBScore;
return usersMissingMDBScore.map(user => user.userId);
};

export const findQfRoundById = async (id: number): Promise<QfRound | null> => {
Expand Down
85 changes: 85 additions & 0 deletions src/services/cronJobs/syncUsersModelScore.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { assert, expect } from 'chai';

Check failure on line 1 in src/services/cronJobs/syncUsersModelScore.test.ts

View workflow job for this annotation

GitHub Actions / test

'expect' is defined but never used

Check failure on line 1 in src/services/cronJobs/syncUsersModelScore.test.ts

View workflow job for this annotation

GitHub Actions / test

'expect' is defined but never used
import moment from 'moment';
import {
createDonationData,
createProjectData,
generateRandomEtheriumAddress,
saveDonationDirectlyToDb,
saveProjectDirectlyToDb,
saveUserDirectlyToDb,
} from '../../../test/testUtils';
import { QfRound } from '../../entities/qfRound';
import { updateUsersWithoutMBDScoreInRound } from './syncUsersModelScore';
import { UserQfRoundModelScore } from '../../entities/userQfRoundModelScore';

describe(
'updateUsersWithoutMBDScoreInRound() test cases',
updateUsersWithoutMBDScoreInRoundTestCases,
);

function updateUsersWithoutMBDScoreInRoundTestCases() {
// for tests it return 1, useful to test cronjob logic and worker
it('should save the score for users that donated in the round', async () => {
await QfRound.update({}, { isActive: false });
const qfRound = QfRound.create({
isActive: true,
name: 'test',
allocatedFund: 100,
minimumPassportScore: 8,
slug: new Date().getTime().toString(),
beginDate: new Date(),
endDate: moment().add(10, 'days').toDate(),
});
await qfRound.save();
const project = await saveProjectDirectlyToDb(createProjectData());
project.qfRounds = [qfRound];
await project.save();

const user = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
const user2 = await saveUserDirectlyToDb(generateRandomEtheriumAddress());
await saveDonationDirectlyToDb(
{
...createDonationData(),
segmentNotified: false,
qfRoundId: qfRound.id,
status: 'verified',
},
user.id,
project.id,
);

await saveDonationDirectlyToDb(
{
...createDonationData(),
segmentNotified: false,
qfRoundId: qfRound.id,
status: 'verified',
},
user2.id,
project.id,
);

await updateUsersWithoutMBDScoreInRound();

const user1ModelScore = await UserQfRoundModelScore.createQueryBuilder(
'score',
)
.where('score."userId" = :userId', { userId: user.id })
.andWhere('score."qfRoundId" = :qfRoundId', { qfRoundId: qfRound.id })
.getOne();

const user2ModelScore = await UserQfRoundModelScore.createQueryBuilder(
'score',
)
.where('score."userId" = :userId', { userId: user2.id })
.andWhere('score."qfRoundId" = :qfRoundId', { qfRoundId: qfRound.id })
.getOne();

// base values for mocks
assert.equal(user1ModelScore?.score, 1);
assert.equal(user2ModelScore?.score, 1);

qfRound.isActive = false;
await qfRound.save();
});
}
28 changes: 12 additions & 16 deletions src/services/cronJobs/syncUsersModelScore.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { schedule } from 'node-cron';
import { ModuleThread, Pool, spawn, Worker } from 'threads';
import { spawn, Worker, Thread } from 'threads';
import config from '../../config';
import { logger } from '../../utils/logger';
import {
findActiveQfRound,
findUsersWithoutMBDScoreInActiveAround,
} from '../../repositories/qfRoundRepository';
import { UserMDBScoreSyncWorker } from '../../workers/usersMBDScoreSyncWorker';
import { findUserById } from '../../repositories/userRepository';
import { UserQfRoundModelScore } from '../../entities/userQfRoundModelScore';
import { UserMBDScoreSyncWorker } from '../../workers/userMBDScoreSyncWorker';

Check failure on line 11 in src/services/cronJobs/syncUsersModelScore.ts

View workflow job for this annotation

GitHub Actions / test

'UserMBDScoreSyncWorker' is defined but never used

Check failure on line 11 in src/services/cronJobs/syncUsersModelScore.ts

View workflow job for this annotation

GitHub Actions / test

'UserMBDScoreSyncWorker' is defined but never used

const cronJobTime =
(config.get('MAKE_UNREVIEWED_PROJECT_LISTED_CRONJOB_EXPRESSION') as string) ||
'0 0 * * *';
'0 0 * * * *';

const qfRoundUsersMissedMBDScore = Number(
process.env.QF_ROUND_USERS_MISSED_SCORE || 0,
Expand All @@ -38,13 +38,9 @@ export const runCheckPendingUserModelScoreCronjob = () => {
};

export const updateUsersWithoutMBDScoreInRound = async () => {
const usersMDBScoreSyncThreadPool: Pool<
ModuleThread<UserMDBScoreSyncWorker>
> = Pool(
() => spawn(new Worker('../workers/usersMBDScoreSyncWoker')),
workerOptions,
const worker = await spawn(
new Worker('../../workers/userMBDScoreSyncWorker'),
);

const userIds = await findUsersWithoutMBDScoreInActiveAround();
const activeQfRoundId =
(await findActiveQfRound())?.id || qfRoundUsersMissedMBDScore;
Expand All @@ -54,12 +50,12 @@ export const updateUsersWithoutMBDScoreInRound = async () => {

for (const userId of userIds) {
try {
const userWallet = await findUserById(userId);
const userScore = await usersMDBScoreSyncThreadPool.queue(worker =>
worker.syncUserScore({
userWallet,
}),
);
const user = await findUserById(userId);
if (!user) continue;

const userScore = await worker.syncUserScore({
userWallet: user?.walletAddress,
});
if (userScore) {
const userScoreInRound = UserQfRoundModelScore.create({
userId,
Expand All @@ -73,5 +69,5 @@ export const updateUsersWithoutMBDScoreInRound = async () => {
logger.info(`User with Id ${userId} did not sync MBD score this batch`);
}
}
await usersMDBScoreSyncThreadPool.terminate();
await Thread.terminate(worker);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { getGitcoinAdapter } from '../adapters/adaptersFactory';

type UsersMBDScoreSyncWorkerFunctions = 'syncUserScore';

export type UserMDBScoreSyncWorker =
export type UserMBDScoreSyncWorker =
WorkerModule<UsersMBDScoreSyncWorkerFunctions>;

const worker: UserMDBScoreSyncWorker = {
const worker: UserMBDScoreSyncWorker = {
async syncUserScore(args: { userWallet: string }) {
return await getGitcoinAdapter().getUserAnalysisScore(args.userWallet);
},
Expand Down

0 comments on commit 38f52bd

Please sign in to comment.