From 8b89c64dd2bc68299686c24ee298fd9ef65fe8e6 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Wed, 21 Nov 2018 11:50:30 +0100 Subject: [PATCH] refactor to parametrizedSql --- services/db/tests/vn/buyUltimate.spec.js | 2 +- .../tests/vn/buyUltimateFromInterval.spec.js | 6 +- services/db/tests/vn/logAddWithUser.spec.js | 2 +- .../db/tests/vn/ticketCalculateClon.spec.js | 4 +- .../vn/ticketComponentUpdateSale.spec.js | 6 +- .../db/tests/vn/ticketCreateWithUser.spec.js | 178 +++++++++++------- 6 files changed, 119 insertions(+), 79 deletions(-) diff --git a/services/db/tests/vn/buyUltimate.spec.js b/services/db/tests/vn/buyUltimate.spec.js index 7e0785baa..5a8cc4d15 100644 --- a/services/db/tests/vn/buyUltimate.spec.js +++ b/services/db/tests/vn/buyUltimate.spec.js @@ -4,7 +4,7 @@ const ParameterizedSQL = server.loopbackConnector.ParameterizedSQL; describe('buyUltimate()', () => { const today = new Date(); - it(`should create buyUltimate temporal table and update it's values`, async() => { + it(`should create buyUltimate temporal table and update it's values`, async () => { let stmts = []; let stmt; diff --git a/services/db/tests/vn/buyUltimateFromInterval.spec.js b/services/db/tests/vn/buyUltimateFromInterval.spec.js index d558c3cf6..3c41cb013 100644 --- a/services/db/tests/vn/buyUltimateFromInterval.spec.js +++ b/services/db/tests/vn/buyUltimateFromInterval.spec.js @@ -20,7 +20,7 @@ describe('buyUltimateFromInterval()', () => { future = [year, futureMonth, day].join('-'); }); - it(`should create a temporal table with it's data`, async() => { + it(`should create a temporal table with it's data`, async () => { let stmts = []; let stmt; @@ -63,7 +63,7 @@ describe('buyUltimateFromInterval()', () => { expect(buyUltimateFromIntervalTable[1].landed).toEqual(today); }); - it(`should create a temporal table with it's data in which started value is assigned to ended`, async() => { + it(`should create a temporal table with it's data in which started value is assigned to ended`, async () => { let params = { warehouseFk: 1, started: today, @@ -99,7 +99,7 @@ describe('buyUltimateFromInterval()', () => { expect(buyUltimateFromIntervalTable[1].landed).toEqual(today); }); - it(`should create a temporal table with it's data in which ended value is a date in the future`, async() => { + it(`should create a temporal table with it's data in which ended value is a date in the future`, async () => { let params = { warehouseFk: 1, started: today, diff --git a/services/db/tests/vn/logAddWithUser.spec.js b/services/db/tests/vn/logAddWithUser.spec.js index 73b05964c..ed0c12203 100644 --- a/services/db/tests/vn/logAddWithUser.spec.js +++ b/services/db/tests/vn/logAddWithUser.spec.js @@ -3,7 +3,7 @@ const server = require(`../../../loopback/server/server`); const ParameterizedSQL = server.loopbackConnector.ParameterizedSQL; describe('logAddWithUser()', () => { - it('should log any action taken by the user in a table ending in Log', async() => { + it('should log any action taken by the user in a table ending in Log', async () => { let stmts = []; let stmt; diff --git a/services/db/tests/vn/ticketCalculateClon.spec.js b/services/db/tests/vn/ticketCalculateClon.spec.js index c2e9e2b59..901ced859 100644 --- a/services/db/tests/vn/ticketCalculateClon.spec.js +++ b/services/db/tests/vn/ticketCalculateClon.spec.js @@ -4,7 +4,7 @@ const ParameterizedSQL = server.loopbackConnector.ParameterizedSQL; describe('ticket ticketCalculateClon()', () => { const today = new Date(); - it('should add the ticket to the order containing the original ticket', async() => { + it('should add the ticket to the order containing the original ticket', async () => { let stmts = []; let stmt; @@ -57,7 +57,7 @@ describe('ticket ticketCalculateClon()', () => { expect(result[angencyHourIndex][0].landed).toBeDefined(); }); - it('should add the ticket to the order containing the original ticket and generate landed value if it was null', async() => { + it('should add the ticket to the order containing the original ticket and generate landed value if it was null', async () => { let stmts = []; let stmt; diff --git a/services/db/tests/vn/ticketComponentUpdateSale.spec.js b/services/db/tests/vn/ticketComponentUpdateSale.spec.js index a0f63ee8b..6f2c06319 100644 --- a/services/db/tests/vn/ticketComponentUpdateSale.spec.js +++ b/services/db/tests/vn/ticketComponentUpdateSale.spec.js @@ -3,7 +3,7 @@ const server = require(`../../../loopback/server/server`); const ParameterizedSQL = server.loopbackConnector.ParameterizedSQL; describe('ticketComponentUpdateSale()', () => { - it(`should update the sale price when option ONE using the base components and reclaculate only the ones with isRenewable TRUE`, async() => { + it(`should update the sale price when option ONE using the base components and reclaculate only the ones with isRenewable TRUE`, async () => { let stmts = []; let stmt; @@ -96,7 +96,7 @@ describe('ticketComponentUpdateSale()', () => { expect(result[firstSalePriceIndexAfter][0].price).toEqual(5); }); - it(`should keep the sale price when option TWO using the base components and save the difference in a new component`, async() => { + it(`should keep the sale price when option TWO using the base components and save the difference in a new component`, async () => { let stmts = []; let stmt; @@ -190,7 +190,7 @@ describe('ticketComponentUpdateSale()', () => { expect(result[firstSalePriceIndexBefore][0].price).toEqual(result[firstSalePriceIndexAfter][0].price); }); - it(`should not change the sale price when option SEVEN, instead it stores 80% and 20% of the price in two components and any price difference in a third one`, async() => { + it(`should not change the sale price when option SEVEN, instead it stores 80% and 20% of the price in two components and any price difference in a third one`, async () => { let stmts = []; let stmt; diff --git a/services/db/tests/vn/ticketCreateWithUser.spec.js b/services/db/tests/vn/ticketCreateWithUser.spec.js index a3765a298..5480800c8 100644 --- a/services/db/tests/vn/ticketCreateWithUser.spec.js +++ b/services/db/tests/vn/ticketCreateWithUser.spec.js @@ -1,8 +1,15 @@ const app = require(`../../../ticket/server/server`); +const server = require(`../../../loopback/server/server`); +const ParameterizedSQL = server.loopbackConnector.ParameterizedSQL; describe('ticket ticketCreateWithUser()', () => { const today = new Date(); - it('should confirm the procedure creates the expected ticket', async() => { + it('should confirm the procedure creates the expected ticket', async () => { + let stmts = []; + let stmt; + + stmts.push('START TRANSACTION'); + let params = { clientFk: 101, shipped: today, @@ -15,13 +22,7 @@ describe('ticket ticketCreateWithUser()', () => { userId: 18 }; - let query = ` - START TRANSACTION; - CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId); - SELECT @newTicketId newTicketId; - SELECT * FROM vn.ticket WHERE id = @newTicketId; - ROLLBACK;`; - let result = await app.models.Ticket.rawSql(query, [ + stmt = new ParameterizedSQL(`CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId)`, [ params.clientFk, params.shipped, params.warehouseFk, @@ -32,22 +33,33 @@ describe('ticket ticketCreateWithUser()', () => { params.landed, params.userId ]); + stmts.push(stmt); - let ticketResult = result[2][0]; + let ticketResultIndex = stmts.push(`SELECT * FROM vn.ticket WHERE id = @newTicketId`) - 1; - expect(ticketResult.newTicketId).toBeGreaterThan(21); + stmts.push('ROLLBACK'); - let createdTicket = result[3][0]; + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await app.models.Ticket.rawStmt(sql); - expect(createdTicket.clientFk).toEqual(params.clientFk); - expect(createdTicket.warehouseFk).toEqual(params.warehouseFk); - expect(createdTicket.companyFk).toEqual(params.companyFk); - expect(createdTicket.addressFk).toEqual(params.addressFk); - expect(createdTicket.agencyModeFk).toEqual(params.agencyModeFk); - expect(createdTicket.routeFk).toEqual(params.routeFk); + let ticketResult = result[ticketResultIndex][0]; + + + expect(ticketResult.id).toBeGreaterThan(21); + expect(ticketResult.clientFk).toEqual(params.clientFk); + expect(ticketResult.warehouseFk).toEqual(params.warehouseFk); + expect(ticketResult.companyFk).toEqual(params.companyFk); + expect(ticketResult.addressFk).toEqual(params.addressFk); + expect(ticketResult.agencyModeFk).toEqual(params.agencyModeFk); + expect(ticketResult.routeFk).toEqual(params.routeFk); }); - it('should confirm the procedure creates the expected observations in the ticket', async() => { + it('should confirm the procedure creates the expected observations in the ticket', async () => { + let stmts = []; + let stmt; + + stmts.push('START TRANSACTION'); + let params = { clientFk: 101, shipped: today, @@ -60,13 +72,7 @@ describe('ticket ticketCreateWithUser()', () => { userId: 18 }; - let query = ` - START TRANSACTION; - CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId); - SELECT @newTicketId newTicketId; - SELECT * FROM vn.ticketObservation WHERE ticketFk = @newTicketId; - ROLLBACK;`; - let result = await app.models.Ticket.rawSql(query, [ + stmt = new ParameterizedSQL('CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId)', [ params.clientFk, params.shipped, params.warehouseFk, @@ -77,10 +83,19 @@ describe('ticket ticketCreateWithUser()', () => { params.landed, params.userId ]); + stmts.push(stmt); - let firstTicketObservation = result[3][0]; - let secondTicketObservation = result[3][1]; - let thirdTicketObservation = result[3][2]; + let ticketObsevationsIndex = stmts.push(`SELECT * FROM vn.ticketObservation WHERE ticketFk = @newTicketId`) - 1; + + stmts.push('ROLLBACK'); + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await app.models.Ticket.rawStmt(sql); + + + let firstTicketObservation = result[ticketObsevationsIndex][0]; + let secondTicketObservation = result[ticketObsevationsIndex][1]; + let thirdTicketObservation = result[ticketObsevationsIndex][2]; expect(firstTicketObservation.observationTypeFk).toEqual(1); expect(firstTicketObservation.description).toEqual('under the floor'); @@ -90,7 +105,12 @@ describe('ticket ticketCreateWithUser()', () => { expect(thirdTicketObservation.description).toEqual('care with the dog'); }); - it('should confirm the procedure sets companyFk as Tenerife if vProvinceName is SANTA CRUZ DE TENERIFE', async() => { + it('should confirm the procedure sets companyFk as Tenerife if vProvinceName is SANTA CRUZ DE TENERIFE', async () => { + let stmts = []; + let stmt; + + stmts.push('START TRANSACTION'); + let params = { clientFk: 101, shipped: today, @@ -105,17 +125,12 @@ describe('ticket ticketCreateWithUser()', () => { let tenerife = 1381; - let changeProvinceToTenerife = `UPDATE province p, client c SET p.name ='SANTA CRUZ DE TENERIFE' WHERE p.id = c.provinceFk AND c.id = ?;`; - - let query = ` - START TRANSACTION; - ${changeProvinceToTenerife} - CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId); - SELECT @newTicketId newTicketId; - SELECT companyFk FROM vn.ticket WHERE id = @newTicketId; - ROLLBACK;`; - let result = await app.models.Ticket.rawSql(query, [ + stmt = new ParameterizedSQL(`UPDATE province p, client c SET p.name ='SANTA CRUZ DE TENERIFE' WHERE p.id = c.provinceFk AND c.id = ?`, [ params.clientFk, + ]); + stmts.push(stmt); + + stmt = new ParameterizedSQL(`CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId)`, [ params.clientFk, params.shipped, params.warehouseFk, @@ -126,13 +141,26 @@ describe('ticket ticketCreateWithUser()', () => { params.landed, params.userId ]); + stmts.push(stmt); - let ticket = result[4][0]; + let ticketCompanyIndex = stmts.push(`SELECT companyFk FROM vn.ticket WHERE id = @newTicketId`) - 1; + + stmts.push('ROLLBACK'); + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await app.models.Ticket.rawStmt(sql); + + let ticket = result[ticketCompanyIndex][0]; expect(ticket.companyFk).toEqual(tenerife); }); - it('should confirm the procedure sets address if it received it null', async() => { + it('should confirm the procedure sets address if it received it null', async () => { + let stmts = []; + let stmt; + + stmts.push('START TRANSACTION'); + let params = { clientFk: 101, shipped: today, @@ -145,14 +173,7 @@ describe('ticket ticketCreateWithUser()', () => { userId: 18 }; - let query = ` - START TRANSACTION; - CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId); - SELECT @newTicketId newTicketId; - SELECT addressFk FROM vn.ticket WHERE id = @newTicketId; - SELECT id FROM vn.address WHERE clientFk = ? AND isDefaultAddress = 1; - ROLLBACK;`; - let result = await app.models.Ticket.rawSql(query, [ + stmt = new ParameterizedSQL(`CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId)`, [ params.clientFk, params.shipped, params.warehouseFk, @@ -161,17 +182,34 @@ describe('ticket ticketCreateWithUser()', () => { params.agencyModeFk, params.routeFk, params.landed, - params.userId, - params.clientFk + params.userId ]); + stmts.push(stmt); - let ticket = result[2][0]; - let clientDefaultAddress = result[3][0]; + let ticketAddressIndex = stmts.push(`SELECT addressFk FROM vn.ticket WHERE id = @newTicketId`) - 1; - expect(ticket.addressFk).toEqual(clientDefaultAddress.id); + stmt = new ParameterizedSQL(`SELECT id FROM vn.address WHERE clientFk = ? AND isDefaultAddress = 1`, [ + params.clientFk, + ]); + let clientDefaultAddressIndex = stmts.push(stmt) - 1; + + stmts.push('ROLLBACK'); + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await app.models.Ticket.rawStmt(sql); + + let ticketAddress = result[ticketAddressIndex][0]; + let clientDefaultAddress = result[clientDefaultAddressIndex][0]; + + expect(ticketAddress.addressFk).toEqual(clientDefaultAddress.id); }); - it('should confirm the procedure creates the state as delivered if the client has isCreatedAsServed TRUE', async() => { + it('should confirm the procedure creates the state as delivered if the client has isCreatedAsServed TRUE', async () => { + let stmts = []; + let stmt; + + stmts.push('START TRANSACTION'); + let params = { clientFk: 101, shipped: today, @@ -184,17 +222,12 @@ describe('ticket ticketCreateWithUser()', () => { userId: 18 }; - let setAsIsDelivered = `UPDATE vn.client SET isCreatedAsServed = 1 WHERE id = ?;`; - - let query = ` - START TRANSACTION; - ${setAsIsDelivered} - CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId); - SELECT @newTicketId newTicketId; - SELECT code FROM vn.ticketState WHERE ticketFk = @newTicketId; - ROLLBACK;`; - let result = await app.models.Ticket.rawSql(query, [ + stmt = new ParameterizedSQL(`UPDATE vn.client SET isCreatedAsServed = 1 WHERE id = ?`, [ params.clientFk, + ]); + stmts.push(stmt); + + stmt = new ParameterizedSQL(`CALL vn.ticketCreateWithUser(?, ?, ?, ?, ?, ?, ?, ?, ?, @newTicketId)`, [ params.clientFk, params.shipped, params.warehouseFk, @@ -203,12 +236,19 @@ describe('ticket ticketCreateWithUser()', () => { params.agencyModeFk, params.routeFk, params.landed, - params.userId, - params.clientFk + params.userId ]); + stmts.push(stmt); - let ticketState = result[4][0]; + let ticketStateCodeIndex = stmts.push(`SELECT code FROM vn.ticketState WHERE ticketFk = @newTicketId`) - 1; - expect(ticketState.code).toEqual('DELIVERED'); + stmts.push('ROLLBACK'); + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await app.models.Ticket.rawStmt(sql); + + let ticketStateCode = result[ticketStateCodeIndex][0].code; + + expect(ticketStateCode).toEqual('DELIVERED'); }); });