Unit tests
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Joan Sanchez 2021-06-10 16:05:10 +02:00
parent 1b62233ea7
commit 3f3e5c2735
4 changed files with 186 additions and 149 deletions

View File

@ -2,78 +2,56 @@ const app = require('vn-loopback/server/server');
describe('Worker absences()', () => { describe('Worker absences()', () => {
it('should get the absence calendar for a full year contract', async() => { it('should get the absence calendar for a full year contract', async() => {
let ctx = {req: {accessToken: {userId: 106}}}; const ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106; const businessId = 106;
const started = new Date(); const now = new Date();
started.setHours(0, 0, 0, 0); const year = now.getFullYear();
started.setMonth(0);
started.setDate(1);
const monthIndex = 11; const [absences] = await app.models.Calendar.absences(ctx, businessId, year);
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
ended.setDate(0);
let result = await app.models.Calendar.absences(ctx, workerFk, started, ended); const firstType = absences[0].absenceType().name;
let calendar = result[0]; const sixthType = absences[5].absenceType().name;
let absences = result[1];
expect(calendar.totalHolidays).toEqual(27.5);
expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
expect(firstType).toMatch(/(Holidays|Leave of absence)/); expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/); expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
}); });
it('should get the absence calendar for a permanent contract', async() => { it('should get the absence calendar for a permanent contract', async() => {
let workerFk = 106; const businessId = 106;
let worker = await app.models.WorkerLabour.findById(workerFk); const ctx = {req: {accessToken: {userId: 9}}};
let endedDate = worker.ended;
await app.models.WorkerLabour.rawSql( const now = new Date();
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`, const year = now.getFullYear();
[null, worker.businessFk]
);
let ctx = {req: {accessToken: {userId: 9}}}; const tx = await app.models.Calendar.beginTransaction({});
const started = new Date(); try {
started.setHours(0, 0, 0, 0); const options = {transaction: tx};
started.setMonth(0);
started.setDate(1);
const monthIndex = 11; const worker = await app.models.WorkerLabour.findById(businessId, null, options);
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
ended.setDate(0);
let result = await app.models.Calendar.absences(ctx, workerFk, started, ended); await app.models.WorkerLabour.rawSql(
let calendar = result[0]; `UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
let absences = result[1]; [null, worker.businessFk], options);
expect(calendar.totalHolidays).toEqual(27.5); const [absences] = await app.models.Calendar.absences(ctx, businessId, year, options);
expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name; let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name; let sixthType = absences[5].absenceType().name;
expect(firstType).toMatch(/(Holidays|Leave of absence)/); expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/); expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
await tx.rollback();
// restores the contract end date } catch (e) {
await app.models.WorkerLabour.rawSql( await tx.rollback();
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`, throw e;
[endedDate, worker.businessFk] }
);
}); });
it('should give the same holidays as worked days since the holidays amount matches the amount of days in a year', async() => { it('should give the same holidays as worked days since the holidays amount matches the amount of days in a year', async() => {
const businessId = 106;
const userId = 106;
const today = new Date(); const today = new Date();
// getting how many days in a year // getting how many days in a year
@ -94,70 +72,47 @@ describe('Worker absences()', () => {
const daysInYear = Math.round((endedTime - startedTime) / dayTimestamp); const daysInYear = Math.round((endedTime - startedTime) / dayTimestamp);
// sets the holidays per year to the amount of days in the current year const tx = await app.models.Calendar.beginTransaction({});
let holidaysConfig = await app.models.WorkCenterHoliday.findOne({ try {
where: { const options = {transaction: tx};
workCenterFk: 1,
year: today.getFullYear()
}});
let originalHolidaysValue = holidaysConfig.days; // sets the holidays per year to the amount of days in the current year
const holidaysConfig = await app.models.WorkCenterHoliday.findOne({
where: {
workCenterFk: 1,
year: today.getFullYear()
}
}, options);
await holidaysConfig.updateAttribute('days', daysInYear); await holidaysConfig.updateAttribute('days', daysInYear, options);
// normal test begins // normal test begins
const userId = 106; const contract = await app.models.WorkerLabour.findById(businessId, null, options);
const contract = await app.models.WorkerLabour.findById(userId);
const contractStartDate = contract.started;
const startingContract = new Date(); const startingContract = new Date();
startingContract.setHours(0, 0, 0, 0); startingContract.setHours(0, 0, 0, 0);
startingContract.setMonth(today.getMonth()); startingContract.setMonth(today.getMonth());
startingContract.setDate(1); startingContract.setDate(1);
await app.models.WorkerLabour.rawSql( await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`, `UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`,
[startingContract, yearEnd, contract.businessFk] [startingContract, yearEnd, contract.businessFk], options
); );
let ctx = {req: {accessToken: {userId: userId}}}; const ctx = {req: {accessToken: {userId: userId}}};
let result = await app.models.Calendar.absences(ctx, userId, yearStart, yearEnd); const [absences] = await app.models.Calendar.absences(ctx, businessId, currentYear);
let calendar = result[0];
let absences = result[1];
let remainingDays = 0; const firstType = absences[0].absenceType().name;
for (let i = today.getMonth(); i < 12; i++) { const sixthType = absences[5].absenceType().name;
today.setDate(1);
today.setMonth(i + 1);
today.setDate(0);
remainingDays += today.getDate(); expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
} }
expect(calendar.totalHolidays).toEqual(remainingDays);
expect(calendar.holidaysEnjoyed).toEqual(5);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
// resets the holidays per year with originalHolidaysValue and the contract starting date
await app.models.WorkCenterHoliday.updateAll(
{
workCenterFk: 1,
year: today.getFullYear()
},
{
days: originalHolidaysValue
}
);
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_start = ? WHERE business_id = ?`,
[contractStartDate, contract.businessFk]
);
}); });
}); });

View File

@ -5,18 +5,34 @@ describe('Worker createAbsence()', () => {
const workerId = 18; const workerId = 18;
it('should return an error for a user without enough privileges', async() => { it('should return an error for a user without enough privileges', async() => {
const ctx = {req: {accessToken: {userId: 18}}}; const ctx = {
const absenceTypeId = 1; req: {accessToken: {userId: 18}},
const dated = new Date(); args: {
businessFk: 18,
absenceTypeId: 1,
dated: new Date()
}
};
let error; const tx = await app.models.Calendar.beginTransaction({});
await app.models.Worker.createAbsence(ctx, workerId, absenceTypeId, dated).catch(e => {
error = e;
}).finally(() => {
expect(error.message).toEqual(`You don't have enough privileges`);
});
expect(error).toBeDefined(); try {
const options = {transaction: tx};
let error;
await app.models.Worker.createAbsence(ctx, workerId, options).catch(e => {
error = e;
}).finally(() => {
expect(error.message).toEqual(`You don't have enough privileges`);
});
expect(error).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
it('should create a new absence', async() => { it('should create a new absence', async() => {
@ -24,7 +40,14 @@ describe('Worker createAbsence()', () => {
accessToken: {userId: 19}, accessToken: {userId: 19},
headers: {origin: 'http://localhost'} headers: {origin: 'http://localhost'}
}; };
const ctx = {req: activeCtx}; const ctx = {
req: activeCtx,
args: {
businessFk: 18,
absenceTypeId: 1,
dated: new Date()
}
};
ctx.req.__ = value => { ctx.req.__ = value => {
return value; return value;
}; };
@ -32,17 +55,23 @@ describe('Worker createAbsence()', () => {
active: activeCtx active: activeCtx
}); });
const absenceTypeId = 1; const tx = await app.models.Calendar.beginTransaction({});
const dated = new Date();
const createdAbsence = await app.models.Worker.createAbsence(ctx, workerId, absenceTypeId, dated);
const expectedBusinessId = 18; try {
const expectedAbsenceTypeId = 1; const options = {transaction: tx};
expect(createdAbsence.businessFk).toEqual(expectedBusinessId); const createdAbsence = await app.models.Worker.createAbsence(ctx, workerId, options);
expect(createdAbsence.dayOffTypeFk).toEqual(expectedAbsenceTypeId);
// Restores const expectedBusinessId = 18;
await app.models.Calendar.destroyById(createdAbsence.id); const expectedAbsenceTypeId = 1;
expect(createdAbsence.businessFk).toEqual(expectedBusinessId);
expect(createdAbsence.dayOffTypeFk).toEqual(expectedAbsenceTypeId);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
}); });

View File

@ -12,45 +12,68 @@ describe('Worker deleteAbsence()', () => {
ctx.req.__ = value => { ctx.req.__ = value => {
return value; return value;
}; };
let createdAbsence;
beforeEach(async() => { beforeEach(async() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({ spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx active: activeCtx
}); });
createdAbsence = await app.models.Calendar.create({
businessFk: businessId,
dayOffTypeFk: 1,
dated: new Date()
});
});
afterEach(async() => {
await app.models.Calendar.destroyById(createdAbsence.id);
}); });
it('should return an error for a user without enough privileges', async() => { it('should return an error for a user without enough privileges', async() => {
activeCtx.accessToken.userId = 106; activeCtx.accessToken.userId = 106;
const tx = await app.models.Calendar.beginTransaction({});
let error; try {
await app.models.Worker.deleteAbsence(ctx, 18, createdAbsence.id).catch(e => { const options = {transaction: tx};
error = e; const createdAbsence = await app.models.Calendar.create({
}).finally(() => { businessFk: businessId,
expect(error.message).toEqual(`You don't have enough privileges`); dayOffTypeFk: 1,
}); dated: new Date()
}, options);
expect(error).toBeDefined(); ctx.args = {absenceId: createdAbsence.id};
let error;
await app.models.Worker.deleteAbsence(ctx, workerId).catch(e => {
error = e;
}).finally(() => {
expect(error.message).toEqual(`You don't have enough privileges`);
});
expect(error).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
it('should create a new absence', async() => { it('should successfully delete an absence', async() => {
activeCtx.accessToken.userId = 19; activeCtx.accessToken.userId = 19;
expect(createdAbsence.businessFk).toEqual(businessId); const tx = await app.models.Calendar.beginTransaction({});
await app.models.Worker.deleteAbsence(ctx, workerId, createdAbsence.id); try {
const options = {transaction: tx};
const createdAbsence = await app.models.Calendar.create({
businessFk: businessId,
dayOffTypeFk: 1,
dated: new Date()
}, options);
const deletedAbsence = await app.models.Calendar.findById(createdAbsence.id); ctx.args = {absenceId: createdAbsence.id};
expect(deletedAbsence).toBeNull(); await app.models.Worker.deleteAbsence(ctx, workerId, options);
const deletedAbsence = await app.models.Calendar.findById(createdAbsence.id, null, options);
expect(deletedAbsence).toBeNull();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
}); });

View File

@ -0,0 +1,30 @@
const app = require('vn-loopback/server/server');
const LoopBackContext = require('loopback-context');
describe('Worker holidays()', () => {
const businessId = 106;
const workerId = 106;
const activeCtx = {
accessToken: {userId: workerId},
headers: {origin: 'http://localhost'}
};
const ctx = {req: activeCtx};
beforeEach(async() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
});
it('should get the absence calendar for a full year contract', async() => {
const now = new Date();
const year = now.getFullYear();
ctx.args = {businessFk: businessId, year: year};
const result = await app.models.Worker.holidays(ctx, workerId);
expect(result.totalHolidays).toEqual(27.5);
expect(result.holidaysEnjoyed).toEqual(5);
});
});