diff --git a/db/changes/225001/00-supplier_beforeUpdate.sql b/db/changes/225001/00-supplier_beforeUpdate.sql index 08af8666b..e87415659 100644 --- a/db/changes/225001/00-supplier_beforeUpdate.sql +++ b/db/changes/225001/00-supplier_beforeUpdate.sql @@ -44,3 +44,6 @@ BEGIN END$$ DELIMITER ; + +INSERT INTO `util`.`notification` (`id`, `name`,`description`) + VALUES (3, 'supplier-pay-method-update', 'A supplier pay method has been updated'); diff --git a/modules/supplier/back/models/specs/supplier.spec.js b/modules/supplier/back/models/specs/supplier.spec.js index 3140981c3..effb70e35 100644 --- a/modules/supplier/back/models/specs/supplier.spec.js +++ b/modules/supplier/back/models/specs/supplier.spec.js @@ -2,13 +2,7 @@ const models = require('vn-loopback/server/server').models; const LoopBackContext = require('loopback-context'); describe('loopback model Supplier', () => { - let supplierOne; - let supplierTwo; - beforeAll(async() => { - supplierOne = await models.Supplier.findById(1); - supplierTwo = await models.Supplier.findById(442); - const activeCtx = { accessToken: {userId: 9}, http: { @@ -23,71 +17,112 @@ describe('loopback model Supplier', () => { }); }); - afterAll(async() => { - await supplierOne.updateAttribute('payMethodFk', supplierOne.payMethodFk); - await supplierTwo.updateAttribute('payMethodFk', supplierTwo.payMethodFk); - }); - describe('payMethodFk', () => { it('should throw an error when attempting to set an invalid payMethod id in the supplier', async() => { - let error; - const expectedError = 'You can not select this payment method without a registered bankery account'; - const supplier = await models.Supplier.findById(1); + const tx = await models.Supplier.beginTransaction({}); + const options = {transaction: tx}; - await supplier.updateAttribute('payMethodFk', 8) - .catch(e => { - error = e; + try { + let error; + const expectedError = 'You can not select this payment method without a registered bankery account'; + const supplier = await models.Supplier.findOne({where: {id: 1}}, options); - expect(error.message).toContain(expectedError); - }); + await supplier.updateAttribute('payMethodFk', 8, options) + .catch(e => { + error = e; - expect(error).toBeDefined(); + expect(error.message).toContain(expectedError); + }); + + expect(error).toBeDefined(); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should not throw if the payMethod id is valid', async() => { - let error; - const supplier = await models.Supplier.findById(442); - await supplier.updateAttribute('payMethodFk', 4) - .catch(e => { - error = e; - }); + const tx = await models.Supplier.beginTransaction({}); + const options = {transaction: tx}; - expect(error).not.toBeDefined(); + try { + let error; + const supplier = await models.Supplier.findOne({where: {id: 442}}, options); + await supplier.updateAttribute('payMethodFk', 4, options) + .catch(e => { + error = e; + }); + + expect(error).not.toBeDefined(); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should have checked isPayMethodChecked for payMethod hasVerfified is false', async() => { - const supplier = await models.Supplier.findById(442); - await supplier.updateAttribute('isPayMethodChecked', true); - await supplier.updateAttribute('payMethodFk', 5); + const tx = await models.Supplier.beginTransaction({}); + const options = {transaction: tx}; - const result = await models.Supplier.findById(442); + try { + const supplier = await models.Supplier.findOne({where: {id: 442}}, options); + await supplier.updateAttribute('isPayMethodChecked', true, options); + await supplier.updateAttribute('payMethodFk', 5, options); - expect(result.isPayMethodChecked).toEqual(true); + const result = await models.Supplier.findOne({where: {id: 442}}, options); + + expect(result.isPayMethodChecked).toEqual(true); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should have unchecked isPayMethodChecked for payMethod hasVerfified is true', async() => { - const supplier = await models.Supplier.findById(442); - await supplier.updateAttribute('isPayMethodChecked', true); - await supplier.updateAttribute('payMethodFk', 2); + const tx = await models.Supplier.beginTransaction({}); + const options = {transaction: tx}; - const result = await models.Supplier.findById(442); + try { + const supplier = await models.Supplier.findOne({where: {id: 442}}, options); + await supplier.updateAttribute('isPayMethodChecked', true, options); + await supplier.updateAttribute('payMethodFk', 2, options); - expect(result.isPayMethodChecked).toEqual(false); + const result = await models.Supplier.findOne({where: {id: 442}}, options); + + expect(result.isPayMethodChecked).toEqual(false); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); it('should have unchecked isPayMethodChecked for payDay and peyDemFk', async() => { - const supplier = await models.Supplier.findById(442); + const tx = await models.Supplier.beginTransaction({}); + const options = {transaction: tx}; - await supplier.updateAttribute('isPayMethodChecked', true); - await supplier.updateAttribute('payDay', 5); - const firstResult = await models.Supplier.findById(442); + try { + const supplier = await models.Supplier.findOne({where: {id: 442}}, options); + await supplier.updateAttribute('payMethodFk', 2, options); - await supplier.updateAttribute('isPayMethodChecked', true); - await supplier.updateAttribute('payDemFk', 1); - const secondResult = await models.Supplier.findById(442); + await supplier.updateAttribute('isPayMethodChecked', true, options); + await supplier.updateAttribute('payDay', 5, options); + const firstResult = await models.Supplier.findOne({where: {id: 442}}, options); - expect(firstResult.isPayMethodChecked).toEqual(false); - expect(secondResult.isPayMethodChecked).toEqual(false); + await supplier.updateAttribute('isPayMethodChecked', true, options); + await supplier.updateAttribute('payDemFk', 1, options); + const secondResult = await models.Supplier.findOne({where: {id: 442}}, options); + + expect(firstResult.isPayMethodChecked).toEqual(false); + expect(secondResult.isPayMethodChecked).toEqual(false); + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); });