Skip to content

Commit

Permalink
feat: stock out unit test and integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoteo committed Nov 7, 2024
1 parent d956af7 commit 1c7e986
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 73 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
"SwitchCase": 1
}
]
},
"globals": {
"jest": true
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"scripts": {
"cz": "cz",
"test": "jest",
"test": "jest --testTimeout=25000 --runInBand",
"semantic-release": "semantic-release",
"prepare": "husky"
},
Expand Down
10 changes: 9 additions & 1 deletion src/add-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ async function selectFeature(configs) {
choices: remainingFeatures.map((ft) => ({
name: FEATURES[ft],
value: ft,
}))
})),
when: function (answers){
const argv = process.argv;
if (!argv[4]){
return true;
}
answers.name = argv[4];
return false;
}
}
]);

Expand Down
34 changes: 32 additions & 2 deletions src/features/stock-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,15 @@ async function getStockOutConfigs({
type: 'input',
name: 'form_name',
message: 'Enter stock out form ID',
default: 'stock_out'
default: 'stock_out',
when: function (answers){
const argv = process.argv;
if (!argv[5]){
return true;
}
answers.form_name = argv[5];
return false;
}
},
{
type: 'list',
Expand All @@ -279,12 +287,34 @@ async function getStockOutConfigs({
value: 'weekly_qty'
}
],
when: function (answers){
const argv = process.argv;
if (!argv[6]){
return true;
}
answers.formular = argv[6];
return false;
}
},
...languages.map((language) => ({
type: 'input',
name: `title.${language}`,
message: `Enter stock out form title in ${language}`,
default: 'Stock Out'
default: 'Stock Out',
when: function (answers){
const argv = process.argv;
if (!argv[7]){
return true;
}
const answer = {
title:{
'en': argv[7].split(',')[0],
'fr': argv[7].split(',')[1]
}
};
Object.assign(answers, answer);
return false;
}
}))
]);
return configs;
Expand Down
110 changes: 110 additions & 0 deletions test/mocks/mocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,114 @@
module.exports = {
stockOutMockConfigs: {
features: {
stock_out: {
form_name: 'stock_out',
formular: 'item_danger_qty',
title: {
en: 'Stock Out Title',
fr: 'Titre du Stock'
},
},
},
levels: {
1: {
contact_type: 'c62_chw',
role: 'chw',
place_type: 'c62_chw_site'
},
2: {
contact_type: 'c52_supervisor',
role: 'supervisor',
place_type: 'c50_supervision_area'
},
},
languages: ['en', 'fr'],
items: {
paracetamol: {
name: 'paracetamol',
label: {
en: 'Paracetamol',
fr: 'Paracetamole'
},
isInSet: 'Y',
set: {
label: {
en: 'Box of 8',
fr: 'Boite de 8'
},
count: '8'
},
unit: {
label: {
en: 'Tablet',
fr: 'Comprimes'
}
},
warning_total: '20',
danger_total: '15',
max_total: '15',
category: 'malaria'
}
},
categories: {
malaria: {
name: 'malaria',
label: {
fr: 'Categorie'
},
description: {
fr: 'Categorie'
}
}
},
useItemCategory: true,
defaultLanguage: 'fr',
},

stockOutScenario: {
initScenario: [
'init',
'2_levels',
'c62_chw',
'chw',
'c52_supervisor',
'supervisor',
'Y',
'stock_count',
'[{contact_type: \'c62_chw\', role: \'chw\', place_type: \'c60_chw_site\' },{contact_type: \'c52_supervisor\',role: \'supervisor\',place_type: \'c50_supervision_area\'}]',
'action',
'end_of_week',
['Stock count', 'Stock count'],
'patient_assessment_under_5',
'Y',
'now()',
'malaria',
['Category', 'Categorie'],
['Category', 'Categorie'],
'paracetamol',
['Paracetamol', 'Paracetamole'],
'Y',
['Box of 8', 'Boite de 8'],
8,
['Tablet', 'Comprimes'],
20,
15,
15,
'by_user',
0,
],
addStockOutFeatureScenario: [
'add', 'feature', 'stock_out', 'stock_out', 'item_danger_qty', ['Stock Out', 'Stock Out']
],
productsScenario: [
'paracetamol_at_hand___set',
'paracetamol_at_hand___unit',
'paracetamol_required___set',
'paracetamol_required___unit'
]

},

stockCountScenario: {
initScenario: [
'init',
Expand Down
8 changes: 8 additions & 0 deletions test/project-config/translations/messages-en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,11 @@ contact.child.mother.indicator.potable.water = Number of households with access
contact.child.mother.indicator.cash.transfert = Number of households that have benefited from cash transfers
contact.child.mother.indicator.sexual.mutilation = Number of children with sexual mutilations
contact.child.mother.indicator.malnutrition = Number of children aged 6-59 months having been screened for malnutrition in a household

cht-stock-monitoring-workflow.stock_out.tasks.stock_out = Stock out
cht-stock-monitoring-workflow.stock_out.message.stock_at_hand = Stock at hand: {{qty}}
cht-stock-monitoring-workflow.stock_out.message.stock_required = Stock required: {{qty}}
cht-stock-monitoring-workflow.stock_out.message.summary_header = Summary
cht-stock-monitoring-workflow.stock_out.message.submit_note = {{name}} has low stock of the following items
cht-stock-monitoring-workflow.stock_out.message.summary_note = Stock out
cht-stock-monitoring-workflow.items.paracetamol.label = Paracetamol
8 changes: 8 additions & 0 deletions test/project-config/translations/messages-fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,11 @@ contact.child.mother.indicator.potable.water = Nombre de ménage qui a accès à
contact.child.mother.indicator.cash.transfert = Nombre de ménage qui recoit un cash transfert
contact.child.mother.indicator.sexual.mutilation = Nombre d'enfant fille ayant subi une mutilation sexuelle
contact.child.mother.indicator.malnutrition = Nombre d\'enfant de 6-59 mois ayant ete depiste de la malnutrition dans un menage
cht-stock-monitoring-workflow.stock_out.tasks.stock_out = Stock épuisé
cht-stock-monitoring-workflow.stock_out.message.stock_at_hand = Stock actuel: {{qty}}
cht-stock-monitoring-workflow.stock_out.message.stock_required = Stock nécessaire: {{qty}}
cht-stock-monitoring-workflow.stock_out.message.summary_header = Résumé
cht-stock-monitoring-workflow.stock_out.message.submit_note = {{name}} a épuisé son stock des éléments suivants:
cht-stock-monitoring-workflow.stock_out.message.summary_note = Stock épuisé
cht-stock-monitoring-workflow.items.paracetamol.label = Paracetamole
101 changes: 33 additions & 68 deletions test/stock-count.spec.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,60 @@
const { fork } = require('child_process');
const { spawnSync } = require('child_process');
const path = require('path');
const fs = require('fs-extra');
const { once } = require('events');
const ExcelJS = require('exceljs');

const { stockCountScenario } = require('./mocks/mocks');
const {
setDirToprojectConfig,
revertBackToProjectHome
revertBackToProjectHome,
cleanUp,
readDataFromXforms
} = require('./test-utils');


describe('Stock count', () => {
const workingDir = process.cwd();
const createdAppFormFiles = ['stock_count.properties.json', 'stock_count.xlsx'];


beforeEach(() => {
setDirToprojectConfig();
});

afterEach(() => {
revertBackToProjectHome(process.cwd());
cleanUp(workingDir, createdAppFormFiles);
revertBackToProjectHome(workingDir);
});

it('Add stock count summaries test', async() => {
it('Add stock count integration test', async() => {
const processDir = process.cwd();
const childProcess = fork('../../main.js', stockCountScenario.initScenario);
await once(childProcess, 'close');

const formPath = path.join(processDir, 'forms', 'app', `stock_count.xlsx`);
const formPropertiesPath = path.join(processDir, 'forms', 'app', `stock_count.properties.json`);
// Check that stock count xform and properties files does not exist
for(const createdAppFormFile of createdAppFormFiles){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(false);
}

const childProcess = spawnSync('../../main.js', stockCountScenario.initScenario);

if (childProcess.error) {
throw childProcess.error;
}

const stockMonitoringConfig = path.join(processDir, 'stock-monitoring.config.json');

// Check that stock monitoring is initialized and stock count xform is generated
expect(fs.existsSync(stockMonitoringConfig)).toBe(true);
expect(fs.existsSync(formPropertiesPath)).toBe(true);
expect(fs.existsSync(formPath)).toBe(true);

// Check that the products are available in stock count xform
const productCategoryList = stockCountScenario.productCategoryScenario;
const productsList = stockCountScenario.productsScenario;
const workbook = new ExcelJS.Workbook();
await workbook.xlsx.readFile(formPath);
const surveyWorkSheet = workbook.getWorksheet('survey');
const nameCol = surveyWorkSheet.getColumn('B');
const cellProductCategoriesList = [];
const cellProductsList = [];
let productCatIndex =0;
let productIndex = 0;
nameCol.eachCell(function(cell){

if(cell.value === productCategoryList[productCatIndex] && productCatIndex < productCategoryList.length){
cellProductCategoriesList.push(cell.value);
productCatIndex ++;
productIndex = 0;
}

if(cell.value === productsList[productIndex] && productIndex < productsList.length){
cellProductsList.push(cell.value);
productIndex ++;
}

});

expect(productsList.length).toBe(cellProductsList.length);
expect(productsList.entries).toStrictEqual(cellProductsList.entries);

expect(productCategoryList.length).toBe(cellProductCategoriesList.length);
expect(productCategoryList.entries).toStrictEqual(cellProductCategoriesList.entries);

//Removing the stock monitoring init file and stock count file
expect(fs.unlinkSync(stockMonitoringConfig)).toBe(undefined);
expect(fs.unlinkSync(formPath)).toBe(undefined);
expect(fs.unlinkSync(formPropertiesPath)).toBe(undefined);

const translationFiles = fs.readdirSync(path.join(processDir, 'translations'));
for(const translationFile of translationFiles){

const messageFileContent = fs.readFileSync(path.join(processDir, 'translations', translationFile), {encoding: 'utf-8'});
expect(messageFileContent).not.toBe('');
const newMessageContent = messageFileContent.split('\n').map(message => {
if(!message.toString().includes('cht-stock-monitoring-workflow') && message.toString()!==''){
return `${message.toString()}\n`;
}
});

expect(newMessageContent.includes('cht-stock-monitoring-workflow')).toBe(false);
fs.truncate(path.join(processDir, 'translations', translationFile), 0, function () {});
fs.writeFile(path.join(processDir, 'translations', translationFile),newMessageContent.toString().replaceAll(',', ''));


for(const createdAppFormFile of createdAppFormFiles){
expect(fs.existsSync(path.join(processDir, 'forms', 'app', createdAppFormFile))).toBe(true);
}
});

});
// Check that the products and categories are available in stock count xform
const { productsList, productCategoryList } = await readDataFromXforms(stockCountScenario.productCategoryScenario, stockCountScenario.productsScenario, 'stock_count.xlsx');

expect(productsList.length).toBe(stockCountScenario.productsScenario.length);
expect(productsList.entries).toStrictEqual(stockCountScenario.productsScenario.entries);
expect(productCategoryList.length).toBe(stockCountScenario.productCategoryScenario.length);
expect(productCategoryList.entries).toStrictEqual(stockCountScenario.productCategoryScenario.entries);

});

});
Loading

0 comments on commit 1c7e986

Please sign in to comment.