Skip to content

Commit

Permalink
test(generators): aumenta a cobertura dos testes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelo-lourenco committed Oct 10, 2024
1 parent f184d29 commit f800552
Show file tree
Hide file tree
Showing 16 changed files with 236 additions and 233 deletions.
11 changes: 6 additions & 5 deletions src/generators/cartao-credito.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,13 @@ const creditCard = {
const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
const getRandomMonth = () => getRandomInt(1, 12);
const getRandomDay = (month) => {
if ([1, 3, 5, 7, 8, 10, 12].includes(month)) {
return getRandomInt(1, 31);
} if ([4, 6, 9, 11].includes(month)) {
return getRandomInt(1, 30);
let maxDay = 28;
if (['01', '03', '05', '07', '08', '10', '12'].includes(month)) {
maxDay = 31;
} else if (['04', '06', '09', '11'].includes(month)) {
maxDay = 30;
}
return getRandomInt(1, 28);
return getRandomInt(1, maxDay);
};

const month = String(getRandomMonth()).padStart(2, '0');
Expand Down
54 changes: 30 additions & 24 deletions src/generators/cartao-nacional-saude.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
/**
* Calcula o dígito verificador do CNS.
* @param {string} cnsBase - A base do CNS (sem o dígito verificador).
* @returns {string} O dígito verificador do CNS.
*/
export function calcDV(cnsBase) {
let sum = 0;
for (let i = 0; i < cnsBase.length; i++) {
sum += Number(cnsBase.charAt(i)) * (15 - i);
}

let mod = sum % 11;
let dv = 11 - mod;
if (dv === 10) {
sum = 2;
for (let i = 0; i < cnsBase.length; i++) {
sum += Number(cnsBase.charAt(i)) * (15 - i);
}
mod = sum % 11;
dv = 11 - mod;
return '001' + dv; // Adiciona "001" se dv = 10
} else if (dv === 11) {
return '0000';
} else {
return '000' + dv; // Adiciona dv normal
}
}

/**
* Gera um número de Cartão Nacional de Saúde (CNS) aleatório.
* @param {boolean} mask - Se `true`, retorna o CNS com máscara (XXX XXXX XXXX XXXX).
* @param {boolean} mask - Se true, retorna o CNS com máscara (XXX XXXX XXXX XXXX).
* @param {string} type - O tipo de CNS a ser gerado ('beneficiario' para beneficiários do SUS, 'profissional' para profissionais de saúde).
* @returns {string} O número de CNS gerado.
* @example
Expand Down Expand Up @@ -37,30 +65,8 @@ export function cns(mask, type) {

// Constrói a base do CNS
let cnsGen = `${firstDigit}${n2}${n3}`; // Base: 1 dígito inicial + 10 dígitos

// Cálculo do dígito verificador
let sum = 0;
for (let i = 0; i < cnsGen.length; i++) {
sum += Number(cnsGen.charAt(i)) * (15 - i);
}

let mod = sum % 11;
let dv = 11 - mod;

if (dv === 10) {
sum = 2;
for (let i = 0; i < cnsGen.length; i++) {
sum += Number(cnsGen.charAt(i)) * (15 - i);
}
mod = sum % 11;
dv = 11 - mod;
cnsGen += `001${dv}`; // Adiciona "001" se dv = 10
} else if (dv === 11) {
dv = 0;
cnsGen += `000${dv}`;
} else {
cnsGen += `000${dv}`; // Adiciona dv normal
}
cnsGen += calcDV(cnsGen);

if (mask) {
return `${cnsGen.substr(0, 3)} ${cnsGen.substr(3, 4)} ${cnsGen.substr(7, 4)} ${cnsGen.substr(11, 4)}`;
Expand Down
2 changes: 1 addition & 1 deletion src/generators/cep.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const getRandom = (arr) => arr[Math.floor(Math.random() * arr.length)];
*/
export function cep(mask = false, state) {
const uf = state || stateRand;
const fullAddress = uf ? dbCep.filter((enderecos) => enderecos.uf === uf) : dbCep;
const fullAddress = dbCep.filter((enderecos) => enderecos.uf === uf);
const sortAddress = getRandom(fullAddress);
return mask ? sortAddress.cep : sortAddress.cep.replace(/\D/g, '');
}
16 changes: 8 additions & 8 deletions src/generators/cnh.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ function calcDV(cnhNum) {
}

dv1 %= 11;
dv1 = dv1 > 9 ? 0 : dv1;
dv1 = adjustDV(dv1);

dv2 %= 11;

if (dv2 > 9) {
dv2 = dv2 - 2 < 0 ? dv2 + 9 : dv2 - 2;
}

dv2 = dv2 > 9 ? 0 : dv2;
dv2 = (dv2 % 11) > 9 ? (dv2 >= 2 ? dv2 - 2 : dv2 + 9) : dv2;
dv2 = adjustDV(dv2);

return `${dv1}${dv2}`;
}

function adjustDV(dv) {
return dv > 9 ? 0 : dv;
}

/**
* Gera um número de CNH aleatório.
*
Expand All @@ -45,3 +44,4 @@ export function cnh() {
const cnhDv = calcDV(cnhNum);
return `${cnhNum}${cnhDv}`;
}
cnh()
2 changes: 1 addition & 1 deletion src/generators/cnpj.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @param {number} dv - O valor a ser calculado.
* @returns {number} O dígito verificador calculado.
*/
function calcDV(dv) {
export function calcDV(dv) {
const dvGen = 11 - (dv % 11);
return (dvGen >= 10) ? 0 : dvGen;
}
Expand Down
17 changes: 10 additions & 7 deletions src/generators/cpf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function numStates(state) {
9: ['PR', 'SC'],
};

return Object.keys(stateGroups).find((key) => stateGroups[key].includes(state)) || null;
return Object.keys(stateGroups).find((key) => stateGroups[key].includes(state));
}

/**
Expand All @@ -37,6 +37,13 @@ function mod(dividend, divider) {
return Math.round(dividend - (Math.floor(dividend / divider) * divider));
}

function adjustDV(dv) {
if (dv >= 10) { dv = 0; }
return dv
}



/**
* Gera um número de CPF aleatório.
* @param {boolean} mask - Se `true`, o CPF será retornado com a máscara de formatação (xxx.xxx.xxx-xx). Se `false`, o CPF será retornado apenas com os dígitos.
Expand All @@ -59,15 +66,11 @@ export function cpf(mask, state) {

let d1 = n9 * 2 + n8 * 3 + n7 * 4 + n6 * 5 + n5 * 6 + n4 * 7 + n3 * 8 + n2 * 9 + n1 * 10;
d1 = 11 - (mod(d1, 11));
if (d1 >= 10) {
d1 = 0;
}
d1 = adjustDV(d1)

let d2 = d1 * 2 + n9 * 3 + n8 * 4 + n7 * 5 + n6 * 6 + n5 * 7 + n4 * 8 + n3 * 9 + n2 * 10 + n1 * 11;
d2 = 11 - (mod(d2, 11));
if (d2 >= 10) {
d2 = 0;
}
d2 = adjustDV(d2)

let cpfGen = `${n1}${n2}${n3}.${n4}${n5}${n6}.${n7}${n8}${n9}-${d1}${d2}`;
return mask ? cpfGen : cpfGen.replace(/\D/g, '');
Expand Down
2 changes: 1 addition & 1 deletion src/generators/endereco.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const getRandom = (arr) => arr[Math.floor(Math.random() * arr.length)];
*/
export function endereco(mask = false, state) {
const uf = state || stateRand;
const fullAddress = uf ? dbCep.filter((enderecos) => enderecos.uf === uf) : dbCep;
const fullAddress = dbCep.filter((enderecos) => enderecos.uf === uf);
const sortAddress = getRandom(fullAddress);
const address = {};
address.cep = mask ? sortAddress.cep : sortAddress.cep.replace(/\D/g, '');
Expand Down
Loading

0 comments on commit f800552

Please sign in to comment.