From e0f7b917d75d59eded4b2879cab7bf326d053f74 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Wed, 12 Feb 2020 10:39:46 +0100 Subject: [PATCH] db test ticket_recalcComponent --- db/dump/fixtures.sql | 22 +++---- db/tests/vn/ticket_recalcComponents.spec.js | 62 +++++++++++++++++++ .../ticket/specs/priceDifference.spec.js | 4 +- 3 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 db/tests/vn/ticket_recalcComponents.spec.js diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index 7b7651a9d..0b9c21780 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -753,7 +753,7 @@ INSERT INTO `vn`.`sale`(`id`, `itemFk`, `ticketFk`, `concept`, `quantity`, `pric (7, 2, 11, 'Melee weapon combat fist 15cm', 15, 7.44, 0, 0, 0, CURDATE()), (8, 4, 11, 'Melee weapon heavy shield 1x0.5m', 10, 1.79, 0, 0, 0, CURDATE()), (9, 1, 16, 'Ranged weapon longbow 2m', 1, 103.49, 0, 0, 0, CURDATE()), - (10, 2, 16, 'Melee weapon combat fist 15cm', 10, 7.08, 0, 0, 0, CURDATE()), + (10, 2, 16, 'Melee weapon combat fist 15cm', 10, 7.09, 0, 0, 0, CURDATE()), (11, 1, 16, 'Ranged weapon longbow 2m', 1, 103.49, 0, 0, 0, CURDATE()), (12, 4, 16, 'Melee weapon heavy shield 1x0.5m', 20, 1.71, 0, 0, 0, CURDATE()), (13, 2, 8, 'Melee weapon combat fist 15cm', 10, 7.08, 0, 0, 0, CURDATE()), @@ -814,25 +814,25 @@ INSERT INTO `vn`.`saleComponent`(`saleFk`, `componentFk`, `value`) (8, 28, 1.25), (8, 29, 0.42), (8, 39, 0.017), + (9, 15, 3.0949), (9, 21, 0.001), - (9, 28, 50), - (9, 29, 49.4), + (9, 28, 53), + (9, 29, 46.4), (9, 39, 0.994), - (10, 15, 0.0111), - (10, 21, -0.001), - (10, 28, 5), - (10, 29, 2), + (10, 15, 0.0199), + (10, 28, 7), + (10, 29, 0), (10, 39, 0.07), (11, 15, 3.0949), (11, 21, 0.001), - (11, 28, 50), - (11, 29, 49.4), + (11, 28, 53), + (11, 29, 46.4), (11, 39, 0.994), (12, 15, 0.0199), (12, 21, 0.003), - (12, 28, 1.25), - (12, 29, 0.42), + (12, 28, 2.25), + (12, 29, -0.58), (12, 39, 0.017), (13, 15, 0.114), (13, 28, 5), diff --git a/db/tests/vn/ticket_recalcComponents.spec.js b/db/tests/vn/ticket_recalcComponents.spec.js new file mode 100644 index 000000000..e140db34c --- /dev/null +++ b/db/tests/vn/ticket_recalcComponents.spec.js @@ -0,0 +1,62 @@ +const app = require('vn-loopback/server/server'); +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + +describe('ticket_recalcComponents()', () => { + it('should recalculate the components in a ticket and check it', async() => { + let stmts = []; + let stmt; + const ticketId = 11; + + stmts.push('START TRANSACTION'); + + let sales = await app.models.Sale.find({where: {ticketFk: ticketId}}); + + stmt = new ParameterizedSQL('UPDATE vn.sale SET price=100 WHERE id IN(?,?)', [ + sales[0].id, + sales[1].id + ]); + stmts.push(stmt); + + stmt = new ParameterizedSQL('SELECT * FROM vn.sale WHERE ticketFk = ?', [ + ticketId + ]); + stmts.push(stmt); + + let modifiedSales = stmts.push(stmt) - 1; + + stmt = new ParameterizedSQL('CALL vn.ticket_recalcComponents(?)', [ + ticketId, + ]); + stmts.push(stmt); + + stmt = new ParameterizedSQL('SELECT * FROM vn.sale WHERE ticketFk = ?', [ + ticketId + ]); + stmts.push(stmt); + + let expectedSales = stmts.push(stmt) - 1; + + stmts.push('ROLLBACK'); + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await app.models.Ticket.rawStmt(sql); + + // original data + const firstPrice = sales[0].price; + const secondPrice = sales[1].price; + + // alteratons for test purposes + const modifiedFirstPrice = result[modifiedSales][0].price; + const modifiedSecondPrice = result[modifiedSales][1].price; + + // expected data after recalc + const expectedSalesFirstPrice = result[expectedSales][0].price; + const expectedSalesSecondPrice = result[expectedSales][1].price; + + expect(firstPrice).not.toEqual(modifiedFirstPrice); + expect(secondPrice).not.toEqual(modifiedSecondPrice); + + expect(firstPrice).toEqual(expectedSalesFirstPrice); + expect(secondPrice).toEqual(expectedSalesSecondPrice); + }); +}); diff --git a/modules/ticket/back/methods/ticket/specs/priceDifference.spec.js b/modules/ticket/back/methods/ticket/specs/priceDifference.spec.js index 61060c9a2..bf61524c7 100644 --- a/modules/ticket/back/methods/ticket/specs/priceDifference.spec.js +++ b/modules/ticket/back/methods/ticket/specs/priceDifference.spec.js @@ -17,8 +17,8 @@ describe('sale priceDifference()', () => { let result = await app.models.Ticket.priceDifference(httpCtx, ticketId, landed, addressId, agencyModeId, zoneId, warehouseId); - expect(result.totalUnitPrice).toEqual(215.77); - expect(result.totalNewPrice).toEqual(215.77); + expect(result.totalUnitPrice).toEqual(215.78); + expect(result.totalNewPrice).toEqual(215.78); expect(result.totalDifference).toEqual(0); });