Skip to content

Commit

Permalink
fix: cron Object.values (#3312)
Browse files Browse the repository at this point in the history
Co-authored-by: Maxime Dréau <maxime@dreau.fr>
  • Loading branch information
moroine and totakoko authored Oct 25, 2023
1 parent 7309711 commit 1ab32ab
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 1,601 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ jobs:
tests:
name: "Tests"
runs-on: ubuntu-latest
services:
mongodb:
image: mongo:6.0.2-focal
ports:
- "27017:27017"
steps:
- name: Checkout project
uses: actions/checkout@v4
Expand Down
10 changes: 0 additions & 10 deletions jest-mongodb-config.js

This file was deleted.

5 changes: 4 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ const config = async () => {
"^@/(.*)$": "<rootDir>/server/src/$1",
"^@tests/(.*)$": "<rootDir>/server/tests/$1",
},
preset: "@shelf/jest-mongodb",
preset: "ts-jest",
setupFiles: ["<rootDir>/server/tests/jest/setupFiles.ts"],
globalTeardown: "<rootDir>/server/tests/jest/globalTeardown.ts",
setupFilesAfterEnv: ["<rootDir>/server/tests/jest/setupFileAfterEnv.ts"],
testMatch: ["<rootDir>/server/**/*(*.)@(spec|test).[tj]s?(x)"],
transform: {
Expand All @@ -48,6 +49,7 @@ const config = async () => {
{
tsconfig: "<rootDir>/server/tsconfig.json",
useESM: true,
isolatedModules: true,
},
],
},
Expand All @@ -65,6 +67,7 @@ const config = async () => {
{
tsconfig: "<rootDir>/shared/tsconfig.json",
useESM: true,
isolatedModules: true,
},
],
},
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"release:interactive": "./.bin/mna-tdb release:interactive",
"postinstall": "husky install",
"talisman:add-exception": "yarn node-talisman --githook pre-commit -i",
"test": "cross-env NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-vm-modules jest",
"test": "cross-env NODE_NO_WARNINGS=1 NODE_OPTIONS=--experimental-vm-modules jest --workerThreads",
"test:ci": "yarn test --ci -w 2",
"test:watch": "yarn test --watch",
"typecheck": "yarn foreach:parallel run typecheck",
Expand All @@ -58,7 +58,6 @@
"@commitlint/config-conventional": "^17.7.0",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/exec": "^6.0.3",
"@shelf/jest-mongodb": "^4.1.7",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"cross-env": "^7.0.3",
Expand All @@ -72,8 +71,8 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-unused-imports": "^3.0.0",
"jest": "^29.6.4",
"jest-environment-jsdom": "^29.6.4",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"lint-staged": "^14.0.1",
"next": "^13.4.19",
"node-talisman": "^1.29.10",
Expand Down
2 changes: 1 addition & 1 deletion server/.env.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## APPLICATION & ENV VARIABLES
MNA_TDB_ENV=local
MNA_TDB_PUBLIC_URL=test
MNA_TDB_MONGODB_URI=mongodb://[user]:[password]@mongodb:27017/[db]?authSource=admin&retryWrites=true&w=majority
MNA_TDB_MONGODB_URI=mongodb://127.0.0.1:27017/TDB-test-{{JEST_WORKER_ID}}?retryWrites=true&w=1&j=false
MNA_TDB_BODY_PARSER_LIMIT=100kb

## LOG VARIABLES
Expand Down
8 changes: 4 additions & 4 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,23 @@
},
"devDependencies": {
"@faker-js/faker": "7.6.0",
"@jest/globals": "^29.6.4",
"@jest/globals": "^29.7.0",
"@types/boom": "7.3.2",
"@types/bunyan": "1.8.8",
"@types/express": "4.17.17",
"@types/jest": "^29.5.1",
"@types/jest": "^29.5.6",
"@types/node": "^18.16.1",
"@types/sinon": "^10.0.14",
"axiosist": "0.10.0",
"cross-env": "7.0.3",
"dotenv": "16.0.1",
"eslint-plugin-node": "11.1.0",
"jest": "^29.5.0",
"jest": "^29.7.0",
"jest-date-mock": "^1.0.8",
"json-schema-traverse": "1.0.0",
"nock": "13.2.4",
"sentry-testkit": "^5.0.5",
"ts-jest": "^29.1.0",
"ts-jest": "^29.1.1",
"tsup": "^7.2.0",
"typescript": "^5.0.4"
}
Expand Down
43 changes: 26 additions & 17 deletions server/src/common/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,24 @@ import logger from "@/common/logger";

import { zodToMongoSchema } from "./utils/mongoSchemaBuilder";

let mongodbClient: MongoClient;
let mongodbClient: MongoClient | null = null;

const ensureInitialization = () => {
if (!mongodbClient) {
const ensureInitialization = (client: MongoClient | null): MongoClient => {
if (!client) {
throw new Error("Database connection does not exist. Please call connectToMongodb before.");
}
return client;
};

/**
* @param {string} uri
* @returns client
*/
export const connectToMongodb = async (uri) => {
if (mongodbClient) {
return mongodbClient;
}

logger.info("Connecting to MongoDB...");
const client = new MongoClient(uri);

Expand All @@ -28,36 +33,41 @@ export const connectToMongodb = async (uri) => {
return client;
};

export const getMongodbClient = () => mongodbClient;
export const getMongodbClient = () => ensureInitialization(mongodbClient);

export const closeMongodbConnection = () => {
ensureInitialization();
return mongodbClient.close();
export const closeMongodbConnection = async () => {
if (!mongodbClient) {
return;
}
try {
await mongodbClient.close();
} finally {
mongodbClient = null;
}
};

export const getDatabase = () => {
ensureInitialization();
return mongodbClient.db();
return ensureInitialization(mongodbClient).db();
};

export const getCollectionList = () => {
return mongodbClient.db().listCollections().toArray();
return ensureInitialization(mongodbClient).db().listCollections().toArray();
};

export const getDbCollection = <TSchema extends Document>(name) => {
ensureInitialization();
return mongodbClient.db().collection<TSchema>(name);
return ensureInitialization(mongodbClient).db().collection<TSchema>(name);
};

export const getDbCollectionSchema = async (name) => {
ensureInitialization();
const collectionInfo: CollectionInfo | null = await mongodbClient.db().listCollections({ name }).next();
const collectionInfo: CollectionInfo | null = await ensureInitialization(mongodbClient)
.db()
.listCollections({ name })
.next();
return collectionInfo?.options?.validator;
};

export const getDbCollectionIndexes = async (name) => {
ensureInitialization();
return await mongodbClient.db().collection(name).indexes();
return await ensureInitialization(mongodbClient).db().collection(name).indexes();
};

/**
Expand Down Expand Up @@ -123,6 +133,5 @@ export const clearAllCollections = async () => {
*/
export async function clearCollection(name) {
logger.warn(`Suppression des données de la collection ${name}...`);
ensureInitialization();
await getDatabase().collection(name).deleteMany({});
}
7 changes: 3 additions & 4 deletions server/src/jobs/crons_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,19 @@ export async function cronsInit() {

logger.info(`Crons - initialise crons in DB`);

const crons = Object.values(CRONS);
let schedulerRequired = false;

await getDbCollection("jobs").deleteMany({
name: { $nin: crons.map((c) => c.name) },
name: { $nin: CRONS.map((c) => c.name) },
type: "cron",
});
await getDbCollection("jobs").deleteMany({
name: { $nin: crons.map((c) => c.name) },
name: { $nin: CRONS.map((c) => c.name) },
status: { $in: ["pending", "will_start"] },
type: "cron_task",
});

for (const cron of crons) {
for (const cron of CRONS) {
const cronJob = await findJob({
name: cron.name,
type: "cron",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ const sampleOrganisme: Organisme = {
* @param nbDuplicates
*/
const insertDuplicateEffectifs = async (sampleEffectif, nbDuplicates = 2) => {
const insertedIdList: ObjectId[] = [];
const insertedIdList: Promise<ObjectId>[] = [];
for (let index = 0; index < nbDuplicates; index++) {
const { insertedId } = await effectifsDb().insertOne({
...sampleEffectif,
id_erp_apprenant: `ID_ERP_${index}`,
annee_scolaire: "2023-2024",
});
insertedIdList.push(insertedId);
insertedIdList.push(
effectifsDb()
.insertOne({
...sampleEffectif,
id_erp_apprenant: `ID_ERP_${index}`,
annee_scolaire: "2023-2024",
})
.then(({ insertedId }) => insertedId)
);
}

return insertedIdList;
return Promise.all(insertedIdList);
};

const sanitizeString = (string) => string.replace(/\s/g, "").toLowerCase();
Expand Down
48 changes: 0 additions & 48 deletions server/tests/integration/db/indexes/formations.indexes.test.ts

This file was deleted.

37 changes: 0 additions & 37 deletions server/tests/integration/db/indexes/users.indexes.test.ts

This file was deleted.

29 changes: 29 additions & 0 deletions server/tests/jest/globalTeardown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import dotenv from "dotenv";
import { MongoClient } from "mongodb";

export default async () => {
dotenv.config({ path: "./server/.env.test" });

const workerId = `${process.env.JEST_WORKER_ID ?? ""}`;
const client = new MongoClient(process.env.MNA_TDB_MONGODB_URI?.replace("{{JEST_WORKER_ID}}", workerId) ?? "");
try {
if (process.env.CI) {
return;
}
await client.connect();
const dbs = await client.db().admin().listDatabases();
await Promise.all(
dbs.databases.map((db) => {
if (db.name.startsWith(`TDB-test-${workerId}`)) {
return client.db(db.name).dropDatabase();
}

return;
})
);
} catch (e) {
console.error(e);
} finally {
await client.close();
}
};
7 changes: 3 additions & 4 deletions server/tests/jest/setupMongo.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { modelDescriptors } from "@/common/model/collections";
import { createIndexes } from "@/common/model/indexes";
import { clearAllCollections, configureDbSchemaValidation } from "@/common/mongodb";
import { startAndConnectMongodb, stopMongodb } from "@tests/utils/mongoUtils";

export const useMongo = () => {
beforeAll(async () => {
// connect to mongodb and create indexes before running tests
await startAndConnectMongodb();
await createIndexes();
}, 10_000);
await configureDbSchemaValidation(modelDescriptors);
}, 30_000);
afterAll(async () => {
await stopMongodb();
});
beforeEach(async () => {
await Promise.all([clearAllCollections(), configureDbSchemaValidation(modelDescriptors)]);
await clearAllCollections();
});
};
Loading

0 comments on commit 1ab32ab

Please sign in to comment.