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); }); });