From 2339d347f71da37678d5aa20f94a7700236167c2 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Wed, 22 Jan 2020 08:26:54 +0100 Subject: [PATCH] Added back unit tests --- .../back/methods/travel/createThermograph.js | 10 +++- .../travel/specs/createThermograph.spec.js | 50 +++++++++++++++++ .../travel/specs/deleteThermograph.spec.js | 56 +++++++++++++++++++ 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 modules/travel/back/methods/travel/specs/createThermograph.spec.js create mode 100644 modules/travel/back/methods/travel/specs/deleteThermograph.spec.js diff --git a/modules/travel/back/methods/travel/createThermograph.js b/modules/travel/back/methods/travel/createThermograph.js index 56a27ddb3..cbf0678d1 100644 --- a/modules/travel/back/methods/travel/createThermograph.js +++ b/modules/travel/back/methods/travel/createThermograph.js @@ -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 diff --git a/modules/travel/back/methods/travel/specs/createThermograph.spec.js b/modules/travel/back/methods/travel/specs/createThermograph.spec.js new file mode 100644 index 000000000..b85dcca04 --- /dev/null +++ b/modules/travel/back/methods/travel/specs/createThermograph.spec.js @@ -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); + }); +}); diff --git a/modules/travel/back/methods/travel/specs/deleteThermograph.spec.js b/modules/travel/back/methods/travel/specs/deleteThermograph.spec.js new file mode 100644 index 000000000..843fa046c --- /dev/null +++ b/modules/travel/back/methods/travel/specs/deleteThermograph.spec.js @@ -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(); + }); +});