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()', () => {
it('should get the absence calendar for a full year contract', async() => {
let ctx = {req: {accessToken: {userId: 106}}};
let workerFk = 106;
const ctx = {req: {accessToken: {userId: 106}}};
const businessId = 106;
const started = new Date();
started.setHours(0, 0, 0, 0);
started.setMonth(0);
started.setDate(1);
const now = new Date();
const year = now.getFullYear();
const monthIndex = 11;
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
ended.setDate(0);
const [absences] = await app.models.Calendar.absences(ctx, businessId, year);
let result = await app.models.Calendar.absences(ctx, workerFk, started, ended);
let calendar = result[0];
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;
const firstType = absences[0].absenceType().name;
const sixthType = absences[5].absenceType().name;
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
});
it('should get the absence calendar for a permanent contract', async() => {
let workerFk = 106;
let worker = await app.models.WorkerLabour.findById(workerFk);
let endedDate = worker.ended;
const businessId = 106;
const ctx = {req: {accessToken: {userId: 9}}};
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
[null, worker.businessFk]
);
const now = new Date();
const year = now.getFullYear();
let ctx = {req: {accessToken: {userId: 9}}};
const tx = await app.models.Calendar.beginTransaction({});
const started = new Date();
started.setHours(0, 0, 0, 0);
started.setMonth(0);
started.setDate(1);
try {
const options = {transaction: tx};
const monthIndex = 11;
const ended = new Date();
ended.setHours(0, 0, 0, 0);
ended.setMonth(monthIndex + 1);
ended.setDate(0);
const worker = await app.models.WorkerLabour.findById(businessId, null, options);
let result = await app.models.Calendar.absences(ctx, workerFk, started, ended);
let calendar = result[0];
let absences = result[1];
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
[null, worker.businessFk], options);
expect(calendar.totalHolidays).toEqual(27.5);
expect(calendar.holidaysEnjoyed).toEqual(5);
const [absences] = await app.models.Calendar.absences(ctx, businessId, year, options);
let firstType = absences[0].absenceType().name;
let sixthType = absences[5].absenceType().name;
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)/);
// restores the contract end date
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_end = ? WHERE business_id = ?`,
[endedDate, worker.businessFk]
);
expect(firstType).toMatch(/(Holidays|Leave of absence)/);
expect(sixthType).toMatch(/(Holidays|Leave of absence)/);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
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();
// getting how many days in a year
@ -94,70 +72,47 @@ describe('Worker absences()', () => {
const daysInYear = Math.round((endedTime - startedTime) / dayTimestamp);
// sets the holidays per year to the amount of days in the current year
let holidaysConfig = await app.models.WorkCenterHoliday.findOne({
where: {
workCenterFk: 1,
year: today.getFullYear()
}});
const tx = await app.models.Calendar.beginTransaction({});
try {
const options = {transaction: tx};
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
const userId = 106;
const contract = await app.models.WorkerLabour.findById(userId);
const contractStartDate = contract.started;
// normal test begins
const contract = await app.models.WorkerLabour.findById(businessId, null, options);
const startingContract = new Date();
startingContract.setHours(0, 0, 0, 0);
startingContract.setMonth(today.getMonth());
startingContract.setDate(1);
const startingContract = new Date();
startingContract.setHours(0, 0, 0, 0);
startingContract.setMonth(today.getMonth());
startingContract.setDate(1);
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`,
[startingContract, yearEnd, contract.businessFk]
);
await app.models.WorkerLabour.rawSql(
`UPDATE postgresql.business SET date_start = ?, date_end = ? WHERE business_id = ?`,
[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);
let calendar = result[0];
let absences = result[1];
const [absences] = await app.models.Calendar.absences(ctx, businessId, currentYear);
let remainingDays = 0;
for (let i = today.getMonth(); i < 12; i++) {
today.setDate(1);
today.setMonth(i + 1);
today.setDate(0);
const firstType = absences[0].absenceType().name;
const sixthType = absences[5].absenceType().name;
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;
it('should return an error for a user without enough privileges', async() => {
const ctx = {req: {accessToken: {userId: 18}}};
const absenceTypeId = 1;
const dated = new Date();
const ctx = {
req: {accessToken: {userId: 18}},
args: {
businessFk: 18,
absenceTypeId: 1,
dated: new Date()
}
};
let error;
await app.models.Worker.createAbsence(ctx, workerId, absenceTypeId, dated).catch(e => {
error = e;
}).finally(() => {
expect(error.message).toEqual(`You don't have enough privileges`);
});
const tx = await app.models.Calendar.beginTransaction({});
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() => {
@ -24,7 +40,14 @@ describe('Worker createAbsence()', () => {
accessToken: {userId: 19},
headers: {origin: 'http://localhost'}
};
const ctx = {req: activeCtx};
const ctx = {
req: activeCtx,
args: {
businessFk: 18,
absenceTypeId: 1,
dated: new Date()
}
};
ctx.req.__ = value => {
return value;
};
@ -32,17 +55,23 @@ describe('Worker createAbsence()', () => {
active: activeCtx
});
const absenceTypeId = 1;
const dated = new Date();
const createdAbsence = await app.models.Worker.createAbsence(ctx, workerId, absenceTypeId, dated);
const tx = await app.models.Calendar.beginTransaction({});
const expectedBusinessId = 18;
const expectedAbsenceTypeId = 1;
try {
const options = {transaction: tx};
expect(createdAbsence.businessFk).toEqual(expectedBusinessId);
expect(createdAbsence.dayOffTypeFk).toEqual(expectedAbsenceTypeId);
const createdAbsence = await app.models.Worker.createAbsence(ctx, workerId, options);
// Restores
await app.models.Calendar.destroyById(createdAbsence.id);
const expectedBusinessId = 18;
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 => {
return value;
};
let createdAbsence;
beforeEach(async() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
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() => {
activeCtx.accessToken.userId = 106;
const tx = await app.models.Calendar.beginTransaction({});
let error;
await app.models.Worker.deleteAbsence(ctx, 18, createdAbsence.id).catch(e => {
error = e;
}).finally(() => {
expect(error.message).toEqual(`You don't have enough privileges`);
});
try {
const options = {transaction: tx};
const createdAbsence = await app.models.Calendar.create({
businessFk: businessId,
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;
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);
});
});