Added back unit tests
gitea/salix/1982-travel_add_thermograph There was a failure building this commit Details

This commit is contained in:
Joan Sanchez 2020-01-22 08:26:54 +01:00
parent 77f6333aba
commit 2339d347f7
3 changed files with 114 additions and 2 deletions

View File

@ -1,3 +1,5 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('createThermograph', {
description: 'Upload and attach a document',
@ -52,8 +54,6 @@ module.exports = Self => {
try {
const options = {transaction: tx};
const uploadedFiles = await models.Dms.uploadFile(ctx, options);
const firstDms = uploadedFiles[0];
const travelThermograph = await models.TravelThermograph.findOne({
where: {
@ -62,6 +62,12 @@ module.exports = Self => {
}
}, options);
if (!travelThermograph)
throw new UserError('No valid travel thermograph found');
const uploadedFiles = await models.Dms.uploadFile(ctx, options);
const firstDms = uploadedFiles[0];
await travelThermograph.updateAttributes({
dmsFk: firstDms.id,
travelFk: id

View File

@ -0,0 +1,50 @@
const app = require('vn-loopback/server/server');
describe('Travel createThermograph()', () => {
const models = app.models;
const travelId = 3;
const currentUserId = 102;
const thermographId = '138350-0';
const ctx = {req: {accessToken: {userId: currentUserId}}, args: {dmsTypeId: 1}};
let travelThermographBefore;
afterAll(async done => {
await app.models.TravelThermograph.rawSql(`
UPDATE travelThermograph
SET travelFk = NULL, dmsFk = NULL
WHERE id = ?`, [travelThermographBefore.id]);
done();
});
it(`should set the travelFk and dmsFk properties to the travel thermograph`, async() => {
spyOn(app.models.Dms, 'uploadFile').and.returnValue([{id: 5}]);
travelThermographBefore = await models.TravelThermograph.findOne({
where: {
thermographFk: thermographId,
travelFk: null
}
});
await models.Travel.createThermograph(ctx, travelId, thermographId);
const travelThermographAfter = await models.TravelThermograph.findOne({
where: {
thermographFk: thermographId,
travelFk: travelId
}
});
expect(app.models.Dms.uploadFile).toHaveBeenCalledWith(ctx, jasmine.any(Object));
expect(travelThermographBefore).toBeDefined();
expect(travelThermographBefore.thermographFk).toEqual(thermographId);
expect(travelThermographBefore.travelFk).toBeNull();
expect(travelThermographAfter).toBeDefined();
expect(travelThermographAfter.thermographFk).toEqual(thermographId);
expect(travelThermographAfter.travelFk).toEqual(travelId);
expect(travelThermographAfter.dmsFk).toEqual(5);
});
});

View File

@ -0,0 +1,56 @@
const app = require('vn-loopback/server/server');
describe('Travel deleteThermograph()', () => {
const models = app.models;
const travelId = 1;
const currentUserId = 102;
const thermographId = 'TZ1905012010';
const travelThermographId = 4;
const dmsId = 5;
const ctx = {req: {accessToken: {userId: currentUserId}}};
let travelThermographBefore;
afterAll(async done => {
await app.models.TravelThermograph.rawSql(`
UPDATE travelThermograph
SET travelFk = ?, dmsFk = ?
WHERE id = ?`, [
travelThermographBefore.travelFk,
travelThermographBefore.dmsFk,
travelThermographBefore.id
]);
done();
});
it(`should set the travelFk and dmsFk properties to null for travel thermograph removal`, async() => {
spyOn(app.models.Dms, 'removeFile').and.returnValue([{id: 5}]);
travelThermographBefore = await models.TravelThermograph.findOne({
where: {
thermographFk: thermographId,
travelFk: travelId
}
});
await models.Travel.deleteThermograph(ctx, travelThermographId);
const travelThermographAfter = await models.TravelThermograph.findOne({
where: {
thermographFk: thermographId,
travelFk: null
}
});
expect(app.models.Dms.removeFile).toHaveBeenCalledWith(ctx, dmsId);
expect(travelThermographBefore).toBeDefined();
expect(travelThermographBefore.thermographFk).toEqual(thermographId);
expect(travelThermographBefore.travelFk).toEqual(travelId);
expect(travelThermographBefore.dmsFk).toEqual(5);
expect(travelThermographAfter).toBeDefined();
expect(travelThermographAfter.thermographFk).toEqual(thermographId);
expect(travelThermographAfter.travelFk).toBeNull();
});
});