Skip to content

Commit

Permalink
Fix not terminal unit without subunits
Browse files Browse the repository at this point in the history
  • Loading branch information
obieler committed Dec 4, 2023
1 parent 94a0976 commit ff2321a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 3 deletions.
2 changes: 0 additions & 2 deletions src/services/apimd.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ function getPosition (accred, gender, lang) {
} else if (!position) {
return null;
} else if (lang === 'en' && position.labelen) {
// lang EN
return position.labelen;
} else {
// lang FR
return gender === 'M'
? (position.labelfr ? position.labelfr : position.labelxx)
: (position.labelxx ? position.labelxx : position.labelfr);
Expand Down
6 changes: 5 additions & 1 deletion src/services/unit.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ async function getUnit (acro, lang, isInternal) {
unitFullDetails.people = unitPersons;
}
} else {
unitFullDetails.subunits = await getSubunits(dict.id_unite, lang);
const subunits = await getSubunits(dict.id_unite, lang);
console.log(subunits);
if (subunits.length > 0) {
unitFullDetails.subunits = subunits;
}
}
if (dict.faxes) {
unitFullDetails.faxes = dict.faxes.split(',').map((fax) => {
Expand Down
20 changes: 20 additions & 0 deletions tests/resources/cadidb/getUnit-tv-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"sigle": "TV-2",
"id_unite": 11200,
"libelle": "Série TV 2",
"libelle_en": "TV Series 2",
"hierarchie": "EPFL SO TV-2",
"resp_sciper": "670006",
"resp_nom": "Kryze",
"resp_nom_usuel": null,
"resp_prenom": "Bo-Katan",
"resp_prenom_usuel": null,
"url": "",
"faxes": "",
"adresse": " $ $ $ $ $ ",
"cmpl_type": "F",
"ghost": null,
"has_accreds": null
}
]
17 changes: 17 additions & 0 deletions tests/resources/cadidb/getUnitPath-tv-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"sigle": "SO",
"libelle": "",
"libelle_en": "Spin-off"
},
{
"sigle": "EPFL",
"libelle": "Ecole polytechnique fédérale de Lausanne",
"libelle_en": "Ecole polytechnique fédérale de Lausanne"
},
{
"sigle": "TV-2",
"libelle": "Série TV 2",
"libelle_en": "TV Series 2"
}
]
29 changes: 29 additions & 0 deletions tests/resources/unit/unit-tv-2-fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"code": 11200,
"acronym": "TV-2",
"name": "Série TV 2",
"unitPath": "EPFL SO TV-2",
"path": [
{
"acronym": "EPFL",
"name": "Ecole polytechnique fédérale de Lausanne"
},
{
"acronym": "SO",
"name": "Spin-off"
},
{
"acronym": "TV-2",
"name": "Série TV 2"
}
],
"terminal": null,
"ghost": null,
"head": {
"sciper": "670006",
"name": "Kryze",
"firstname": "Bo-Katan",
"email": "",
"profile": "670006"
}
}
26 changes: 26 additions & 0 deletions tests/unit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,32 @@ describe('Test API Unit ("/api/unit")', () => {
expect(JSON.parse(response.text)).toStrictEqual(jsonResult);
});

test('It should find TV-2 unit (where no subunits)', async () => {
const mockConnection = {
query: jest.fn().mockImplementation((query, values, referrer) => {
let jsonData;
switch (referrer) {
case 'getUnit':
jsonData = require('./resources/cadidb/getUnit-tv-2.json');
break;
case 'getUnitPath':
jsonData = require('./resources/cadidb/getUnitPath-tv-2.json');
break;
case 'getSubunits':
jsonData = [];
}
return Promise.resolve([jsonData]);
}),
release: jest.fn()
};
mysql.createPool().getConnection.mockResolvedValue(mockConnection);

const jsonResult = require('./resources/unit/unit-tv-2-fr.json');
const response = await request(app).get('/api/unit?acro=tv-2&hl=fr');
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.text)).toStrictEqual(jsonResult);
});

test('It should return an error with a status code 400', async () => {
const mockConnection = {
query: jest.fn().mockRejectedValue(),
Expand Down

0 comments on commit ff2321a

Please sign in to comment.