Skip to content

Commit

Permalink
test: ajout des tests pour les familles de métiers
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ST0R committed Dec 9, 2024
1 parent 614db8a commit 22be793
Show file tree
Hide file tree
Showing 8 changed files with 900 additions and 9 deletions.
10 changes: 9 additions & 1 deletion server/src/jobs/bcn/importBCNFamilleMetier.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flattenArray, oleoduc, transformData, writeData } from "oleoduc";
import { filterData, flattenArray, oleoduc, transformData, writeData } from "oleoduc";
import { omitNil } from "#src/common/utils/objectUtils.js";
import { getLoggerWithContext } from "#src/common/logger.js";
import BCNRepository from "#src/common/repositories/bcn.js";
Expand All @@ -22,12 +22,20 @@ export async function importBCNFamilleMetier(options = {}) {
const bcnMef = await BCNMefRepository.first({
mef: data["MEF"],
});
if (!bcnMef) {
return null;
}

const bcn = await BCNRepository.first({
code_certification: bcnMef.mef_stat_11,
});
if (!bcn) {
return null;
}

return { data, bcn };
}),
filterData((d) => d),
transformData(async (data) => {
const formationWithContinuum = await BCNRepository.cfdsParentAndChildren(data.bcn.code_certification);
return formationWithContinuum.map((f) => {
Expand Down
45 changes: 37 additions & 8 deletions server/src/jobs/stats/importAnneesNonTerminales.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Readable } from "stream";
import { flattenArray, oleoduc, transformData, writeData } from "oleoduc";
import streamToArray from "stream-to-array";
import { upsert } from "#src/common/db/mongodb.js";
import { getLoggerWithContext } from "#src/common/logger.js";
import { omitNil } from "#src/common/utils/objectUtils.js";
Expand All @@ -10,6 +11,7 @@ import { certificationsStats, regionalesStats, formationsStats } from "#src/comm
import CertificationStatsRepository from "#src/common/repositories/certificationStats.js";
import RegionaleStatsRepository from "#src/common/repositories/regionaleStats.js";
import FormationStatsRepository from "#src/common/repositories/formationStats.js";
import BCNMefRepository from "#src/common/repositories/bcnMef.js";

const logger = getLoggerWithContext("import");

Expand Down Expand Up @@ -37,6 +39,28 @@ const statCollections = {
},
};

async function getCertificationForYear(year, { code_certification, code_formation_diplome }) {
const previousMef = code_certification.substr(0, 3) + year + code_certification.substr(3 + 1);
const certification = await getCertificationInfo(previousMef);

if (certification) {
return certification;
}

const certificationsFromMef = await streamToArray(
await BCNMefRepository.find({
formation_diplome: code_formation_diplome,
annee_dispositif: `${year}`,
})
);

if (certificationsFromMef.length === 1) {
return await getCertificationInfo(certificationsFromMef[0].mef_stat_11);
}

return null;
}

export async function importAnneesNonTerminales(options = {}) {
const jobStats = { created: 0, updated: 0, failed: 0 };
const statsType = options.stats || ["certifications", "formations", "regionales"];
Expand All @@ -61,19 +85,25 @@ export async function importAnneesNonTerminales(options = {}) {
millesime: millesime,
"donnee_source.type": { $exists: true },
}),
transformData((stats) => {
transformData(async (stats) => {
const previousYearStats = [];
for (let year = stats.code_certification[3] - 1; year > 0; year--) {
const previousMef = stats.code_certification.substr(0, 3) + year + stats.code_certification.substr(3 + 1);
previousYearStats.push({
stats,
code_certification: previousMef,
});
const previousCertification = await getCertificationForYear(
year,
pick(stats, ["code_formation_diplome", "code_certification"])
);
if (previousCertification) {
previousYearStats.push({
stats,
certification: previousCertification,
code_certification: previousCertification.code_certification,
});
}
}
return previousYearStats;
}),
flattenArray(),
writeData(async ({ code_certification, stats }) => {
writeData(async ({ code_certification, certification, stats }) => {
const query = {
millesime: stats.millesime,
code_certification: code_certification,
Expand All @@ -84,7 +114,6 @@ export async function importAnneesNonTerminales(options = {}) {
};

try {
const certification = await getCertificationInfo(code_certification);
if (!certification) {
logger.error(`Certification ${code_certification} pour ${stats.code_formation_diplome} inconnue.`);
jobStats.failed++;
Expand Down
36 changes: 36 additions & 0 deletions server/tests/http/certificationsRoutes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,42 @@ describe("certificationsRoutes", () => {
});
});

it("Vérifie qu'on peut obtenir une année non terminale", async () => {
const { httpClient } = await startServer();
await insertCertificationsStats(
{
code_certification: "12345678910",
code_formation_diplome: "12345678",
filiere: "pro",
certificationsTerminales: [{ code_certification: "32220000000" }],
},
false
);

const response = await httpClient.get(`/api/inserjeunes/certifications/12345678910`);

assert.strictEqual(response.status, 200);
assert.deepStrictEqual(response.data, {
millesime: "2020",
code_certification: "12345678910",
code_certification_type: "mef11",
code_formation_diplome: "12345678",
libelle: "LIBELLE",
filiere: "pro",
diplome: { code: "4", libelle: "BAC" },
certificationsTerminales: [{ code_certification: "32220000000" }],
donnee_source: {
code_certification: "12345678910",
type: "self",
},
formation_fermee: false,
_meta: {
titre: "Certification 12345678910",
details: "Données InserJeunes pour la certification 12345678910 (BAC filière pro) pour le millésime 2020",
},
});
});

it("Ne retourne pas de stats par défaut si il n'y a pas de données pour le millésime le plus récent", async () => {
const { httpClient } = await startServer();
await insertCertificationsStats({
Expand Down
48 changes: 48 additions & 0 deletions server/tests/http/formationsRoutes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,54 @@ describe("formationsRoutes", () => {
});
});

it("Vérifie qu'on peut obtenir une année non terminale", async () => {
const { httpClient } = await startServer();
await insertFormationsStats(
{
uai: "0751234J",
code_certification: "12345678910",
code_formation_diplome: "12345678",
filiere: "pro",
certificationsTerminales: [{ code_certification: "32220000000" }],
},
false
);

const response = await httpClient.get(`/api/inserjeunes/formations/0751234J-12345678910`);

assert.strictEqual(response.status, 200);
assert.deepStrictEqual(response.data, {
millesime: "2018_2019",
code_certification: "12345678910",
code_certification_type: "mef11",
code_formation_diplome: "12345678",
libelle: "LIBELLE",
libelle_etablissement: "Lycée",
filiere: "pro",
diplome: { code: "4", libelle: "BAC" },
certificationsTerminales: [{ code_certification: "32220000000" }],
donnee_source: {
code_certification: "12345678910",
type: "self",
},
academie: {
code: "01",
nom: "Paris",
},
region: {
code: "11",
nom: "Île-de-France",
},
uai: "0751234J",
formation_fermee: false,
_meta: {
titre: "Certification 12345678910, établissement 0751234J",
details:
"Données InserJeunes pour la certification 12345678910 (BAC filière pro) dispensée par l'établissement 0751234J, pour le millésime 2018_2019",
},
});
});

it("Ne retourne pas de stats par défaut si il n'y a pas de données pour le millésime le plus récent", async () => {
const { httpClient } = await startServer();
await insertCFD({ code_certification: "12345678" });
Expand Down
42 changes: 42 additions & 0 deletions server/tests/http/regionalesRoutes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,48 @@ describe("regionalesRoutes", () => {
});
});

it("Vérifie qu'on peut obtenir une année non terminale", async () => {
const { httpClient } = await startServer();
await insertRegionalesStats(
{
region: { code: "11", nom: "Île-de-France" },
code_certification: "12345678910",
code_formation_diplome: "12345678",
filiere: "pro",
certificationsTerminales: [{ code_certification: "32220000000" }],
},
false
);

const response = await httpClient.get(`/api/inserjeunes/regionales/11/certifications/12345678910`);

assert.strictEqual(response.status, 200);
assert.deepStrictEqual(response.data, {
millesime: "2018_2019",
code_certification: "12345678910",
code_certification_type: "mef11",
code_formation_diplome: "12345678",
libelle: "LIBELLE",
filiere: "pro",
diplome: { code: "4", libelle: "BAC" },
certificationsTerminales: [{ code_certification: "32220000000" }],
donnee_source: {
code_certification: "12345678910",
type: "self",
},
region: {
code: "11",
nom: "Île-de-France",
},
formation_fermee: false,
_meta: {
titre: "Certification 12345678910",
details:
"Données InserJeunes pour la certification 12345678910 (BAC filière pro) pour le millésime 2018_2019 et la région Île-de-France",
},
});
});

it("Ne retourne pas de stats par défaut si il n'y a pas de données pour le millésime le plus récent", async () => {
const { httpClient } = await startServer();
await insertCFD({ code_certification: "12345678" });
Expand Down
112 changes: 112 additions & 0 deletions server/tests/jobs/bcn/importBCNFamilleMetier-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import assert from "assert";
import MockDate from "mockdate";
import { omit } from "lodash-es";
import { insertBCNMEF, insertMEF } from "#tests/utils/fakeData.js";
import { importBCNFamilleMetier } from "#src/jobs/bcn/importBCNFamilleMetier.js";
import BCNRepository from "#src/common/repositories/bcn.js";

describe("importBCNFamilleMetier", () => {
before(() => {
MockDate.set("2023-01-01");
});

after(() => {
MockDate.reset();
});

describe("Vérifie que l'on ajoute les familles de métiers", () => {
it("Pour une seconde commune", async () => {
await Promise.all([
insertBCNMEF({
mef_stat_11: "23810031211",
formation_diplome: "40031211",
mef: "2473121131",
}),
insertMEF({
code_certification: "23810031211",
code_formation_diplome: "40031211",
libelle_long: "2NDPRO MET. RELATION CLIENT 2NDE COMMUNE",
}),
]);

const result = await importBCNFamilleMetier();

assert.deepEqual(result, {
failed: 0,
total: 1,
updated: 1,
});

const found = await BCNRepository.first({});
assert.deepStrictEqual(omit(found, ["_id"]), {
type: "mef",
code_certification: "23810031211",
code_formation_diplome: "40031211",
date_fermeture: new Date("2022-08-30T22:00:00.000Z"),
date_ouverture: new Date(),
diplome: { code: "4", libelle: "BAC" },
libelle: "BAC PRO",
libelle_long: "2NDPRO MET. RELATION CLIENT 2NDE COMMUNE",
ancien_diplome: [],
nouveau_diplome: [],
familleMetier: {
code: "003",
isAnneeCommune: true,
libelle: "Relation client",
},
_meta: {
created_on: new Date("2023-01-01T00:00:00.000Z"),
updated_on: new Date("2023-01-01T00:00:00.000Z"),
date_import: new Date("2023-01-01T00:00:00.000Z"),
},
});
});

it("Pour une année d'option", async () => {
await Promise.all([
insertBCNMEF({
mef_stat_11: "23830031212",
formation_diplome: "40031211",
mef: "2473121131",
}),
insertMEF({
code_certification: "23830031212",
code_formation_diplome: "40031211",
libelle_long: "TERMINALE",
}),
]);

const result = await importBCNFamilleMetier();

assert.deepEqual(result, {
failed: 0,
total: 1,
updated: 1,
});

const found = await BCNRepository.first({});
assert.deepStrictEqual(omit(found, ["_id"]), {
type: "mef",
code_certification: "23830031212",
code_formation_diplome: "40031211",
date_fermeture: new Date("2022-08-30T22:00:00.000Z"),
date_ouverture: new Date(),
diplome: { code: "4", libelle: "BAC" },
libelle: "BAC PRO",
libelle_long: "TERMINALE",
ancien_diplome: [],
nouveau_diplome: [],
familleMetier: {
code: "003",
isAnneeCommune: true,
libelle: "Relation client",
},
_meta: {
created_on: new Date("2023-01-01T00:00:00.000Z"),
updated_on: new Date("2023-01-01T00:00:00.000Z"),
date_import: new Date("2023-01-01T00:00:00.000Z"),
},
});
});
});
});
Loading

0 comments on commit 22be793

Please sign in to comment.