From 0f7ed5bbfa9c3e0ccff4397d90d85852fb74753a Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Tue, 3 Mar 2020 15:14:28 +0100 Subject: [PATCH 1/2] get worked hours unit test --- .../back/methods/worker/mySubordinates.js | 1 - .../methods/worker/specs/getWorkedHours.spec.js | 17 +++++++++++++++++ .../worker/back/methods/worker/timeControl.js | 1 - 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 modules/worker/back/methods/worker/specs/getWorkedHours.spec.js diff --git a/modules/worker/back/methods/worker/mySubordinates.js b/modules/worker/back/methods/worker/mySubordinates.js index bf1cd1be8..cf45d3a9d 100644 --- a/modules/worker/back/methods/worker/mySubordinates.js +++ b/modules/worker/back/methods/worker/mySubordinates.js @@ -35,7 +35,6 @@ module.exports = Self => { let sql = ParameterizedSQL.join(stmts, ';'); let result = await conn.executeStmt(sql); - return result[1]; }; }; diff --git a/modules/worker/back/methods/worker/specs/getWorkedHours.spec.js b/modules/worker/back/methods/worker/specs/getWorkedHours.spec.js new file mode 100644 index 000000000..148b8467e --- /dev/null +++ b/modules/worker/back/methods/worker/specs/getWorkedHours.spec.js @@ -0,0 +1,17 @@ +const app = require('vn-loopback/server/server'); + +describe('Worker getWorkedHours()', () => { + it(`should return the expected hours and the worked hours of a given date`, async() => { + const workerID = 106; + let started = new Date(); + started.setHours(0, 0, 0, 0); + + let ended = new Date(); + ended.setHours(0, 0, 0, 0); + + const [result] = await app.models.Worker.getWorkedHours(workerID, started, ended); + + expect(result.expectedHours).toEqual(28800); // 8:00 hours seconds + expect(result.workedHours).toEqual(29400); // 8:10 hours in seconds + }); +}); diff --git a/modules/worker/back/methods/worker/timeControl.js b/modules/worker/back/methods/worker/timeControl.js index bc88197fe..9479ff0a1 100644 --- a/modules/worker/back/methods/worker/timeControl.js +++ b/modules/worker/back/methods/worker/timeControl.js @@ -44,7 +44,6 @@ module.exports = Self => { let sql = ParameterizedSQL.join(stmts, ';'); let result = await conn.executeStmt(sql); - return result[0]; }; }; From 0179461cb3ffbaf58a4f607d6e0b2cdebcee2a03 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez Ruiz Date: Wed, 4 Mar 2020 15:12:36 +0100 Subject: [PATCH 2/2] timeBusiness and timeControl db tests --- db/dump/fixtures.sql | 4 +- db/tests/vn/ticketCreateWithUser.spec.js | 1 - .../vn/timeBusiness_calculateByUser.spec.js | 40 +++++++ .../vn/timeControl_calculateByUser.spec.js | 107 ++++++++++++++++++ 4 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 db/tests/vn/timeBusiness_calculateByUser.spec.js create mode 100644 db/tests/vn/timeControl_calculateByUser.spec.js diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 9d9b84da4..696bfdbfd 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -1677,12 +1677,12 @@ INSERT INTO `vn`.`zoneIncluded` (`zoneFk`, `geoFk`, `isIncluded`) INSERT INTO `vn`.`zoneEvent`(`zoneFk`, `type`, `dated`) VALUES - (1, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=2, 2, 9 ) - DAYOFWEEK(CURDATE())) DAY)), + (1, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=2, 2, 9) - DAYOFWEEK(CURDATE())) DAY)), (1, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=3, 3, 10) - DAYOFWEEK(CURDATE())) DAY)), (1, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=4, 4, 11) - DAYOFWEEK(CURDATE())) DAY)), (1, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=5, 5, 12) - DAYOFWEEK(CURDATE())) DAY)), (1, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=6, 6, 13) - DAYOFWEEK(CURDATE())) DAY)), - (2, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=2, 2, 9 ) - DAYOFWEEK(CURDATE())) DAY)), + (2, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=2, 2, 9) - DAYOFWEEK(CURDATE())) DAY)), (2, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=3, 3, 10) - DAYOFWEEK(CURDATE())) DAY)), (2, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=4, 4, 11) - DAYOFWEEK(CURDATE())) DAY)), (2, 'day', DATE_ADD(CURDATE(), INTERVAL (IF(DAYOFWEEK(CURDATE())<=5, 5, 12) - DAYOFWEEK(CURDATE())) DAY)), diff --git a/db/tests/vn/ticketCreateWithUser.spec.js b/db/tests/vn/ticketCreateWithUser.spec.js index 51ebfe000..6728eb2da 100644 --- a/db/tests/vn/ticketCreateWithUser.spec.js +++ b/db/tests/vn/ticketCreateWithUser.spec.js @@ -43,7 +43,6 @@ describe('ticket ticketCreateWithUser()', () => { let ticketResult = result[ticketResultIndex][0]; - expect(ticketResult.id).toBeGreaterThan(21); expect(ticketResult.clientFk).toEqual(params.clientFk); expect(ticketResult.warehouseFk).toEqual(params.warehouseFk); diff --git a/db/tests/vn/timeBusiness_calculateByUser.spec.js b/db/tests/vn/timeBusiness_calculateByUser.spec.js new file mode 100644 index 000000000..0b09447ef --- /dev/null +++ b/db/tests/vn/timeBusiness_calculateByUser.spec.js @@ -0,0 +1,40 @@ +const app = require('vn-loopback/server/server'); +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + +describe('timeBusiness_calculateByUser()', () => { + it('should return the expected hours for today', async() => { + let start = new Date(); + start.setHours(0, 0, 0, 0); + let end = new Date(); + end.setHours(0, 0, 0, 0); + + let stmts = []; + let stmt; + + stmts.push('START TRANSACTION'); + + let params = { + workerID: 106, + start: start, + end: end + }; + + stmt = new ParameterizedSQL('CALL vn.timeBusiness_calculateByUser(?, ?, ?)', [ + params.workerID, + params.start, + params.end + ]); + stmts.push(stmt); + + let tableIndex = stmts.push('SELECT * FROM tmp.timeBusinessCalculate') - 1; + + stmts.push('ROLLBACK'); + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await app.models.Ticket.rawStmt(sql); + + let [timeBusinessCalculateTable] = result[tableIndex]; + + expect(timeBusinessCalculateTable.timeBusinessSeconds).toEqual(28800); + }); +}); diff --git a/db/tests/vn/timeControl_calculateByUser.spec.js b/db/tests/vn/timeControl_calculateByUser.spec.js new file mode 100644 index 000000000..ac442ab7b --- /dev/null +++ b/db/tests/vn/timeControl_calculateByUser.spec.js @@ -0,0 +1,107 @@ +const app = require('vn-loopback/server/server'); +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + +describe('timeControl_calculateByUser()', () => { + it(`should return today's worked hours`, async() => { + let start = new Date(); + start.setHours(0, 0, 0, 0); + start.setDate(start.getDate() - 1); + + let end = new Date(); + end.setHours(0, 0, 0, 0); + end.setDate(end.getDate() + 1); + + let stmts = []; + let stmt; + + stmts.push('START TRANSACTION'); + + stmts.push(` + DROP TEMPORARY TABLE IF EXISTS + tmp.timeControlCalculate, + tmp.timeBusinessCalculate + `); + + let params = { + workerID: 106, + start: start, + end: end + }; + + stmt = new ParameterizedSQL('CALL vn.timeControl_calculateByUser(?, ?, ?)', [ + params.workerID, + params.start, + params.end + ]); + stmts.push(stmt); + + let tableIndex = stmts.push('SELECT * FROM tmp.timeControlCalculate') - 1; + + stmts.push('ROLLBACK'); + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await app.models.Ticket.rawStmt(sql); + + let [timeControlCalculateTable] = result[tableIndex]; + + expect(timeControlCalculateTable.timeWorkSeconds).toEqual(29400); + }); + + it(`should return the worked hours between last sunday and monday`, async() => { + let lastSunday = new Date(); + let daysSinceSunday = lastSunday.getDay(); + if (daysSinceSunday === 0) // this means today is sunday but you need the previous sunday :) + daysSinceSunday = 7; + lastSunday.setHours(23, 0, 0, 0); + lastSunday.setDate(lastSunday.getDate() - daysSinceSunday); + + let monday = new Date(); + let daysSinceMonday = daysSinceSunday - 1; // aiming for monday (today could be monday) + monday.setHours(7, 0, 0, 0); + monday.setDate(monday.getDate() - daysSinceMonday); + + let stmts = []; + let stmt; + + stmts.push('START TRANSACTION'); + + stmts.push(` + DROP TEMPORARY TABLE IF EXISTS + tmp.timeControlCalculate, + tmp.timeBusinessCalculate + `); + + const workerID = 107; + + stmt = new ParameterizedSQL(` + INSERT INTO vn.workerTimeControl(userFk, timed, manual, direction) + VALUES + (?, ?, 1, 'in'), + (?, ?, 1, 'out') + `, [ + workerID, + lastSunday, + workerID, + monday + ]); + stmts.push(stmt); + + stmt = new ParameterizedSQL('CALL vn.timeControl_calculateByUser(?, ?, ?)', [ + workerID, + lastSunday, + monday + ]); + stmts.push(stmt); + + let tableIndex = stmts.push('SELECT * FROM tmp.timeControlCalculate') - 1; + + stmts.push('ROLLBACK'); + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await app.models.Ticket.rawStmt(sql); + + let [timeControlCalculateTable] = result[tableIndex]; + + expect(timeControlCalculateTable.timeWorkSeconds).toEqual(30000); + }); +});