refactor: refs #6889 sale tests e2e
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Jorge Penadés 2024-05-31 13:58:26 +02:00
parent ee09617b27
commit 836cfcbd07
1 changed files with 87 additions and 222 deletions

View File

@ -1,42 +1,36 @@
/* eslint max-len: ["error", { "code": 150 }]*/ /* eslint max-len: ["error", { "code": 150 }]*/
const {models} = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context'); const LoopBackContext = require('loopback-context');
describe('sale model ', () => { fdescribe('sale model ', () => {
const ctx = { const developerId = 9;
req: { const buyerId = 35;
accessToken: {userId: 9}, const employeeId = 1;
headers: {origin: 'localhost:5000'},
__: () => {} const ctx = getCtx(developerId);
} let tx;
}; let options;
function getActiveCtx(userId) {
return { function getCtx(userId, active = false) {
active: { const accessToken = {userId};
accessToken: {userId}, const headers = {origin: 'localhost:5000'};
http: { if (!active) return {req: {accessToken, headers, __: () => {}}};
req: { return {active: {accessToken, http: {req: {headers}}}};
headers: {origin: 'http://localhost'}
}
}
}
};
} }
beforeEach(async() => {
tx = await models.Sale.beginTransaction({});
options = {transaction: tx};
});
afterEach(async() => await tx.rollback());
describe('quantity field ', () => { describe('quantity field ', () => {
it('should add quantity if the quantity is greater than it should be and is role advanced', async() => { it('should add quantity if the quantity is greater than it should be and is role advanced', async() => {
const saleId = 17; const saleId = 17;
const buyerId = 35; const ctx = getCtx(buyerId);
const ctx = {
req: { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getCtx(buyerId, true));
accessToken: {userId: buyerId},
headers: {origin: 'localhost:5000'},
__: () => {}
}
};
const tx = await models.Sale.beginTransaction({});
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(buyerId));
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => { spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
if (sqlStatement.includes('catalog_calcFromItem')) { if (sqlStatement.includes('catalog_calcFromItem')) {
sqlStatement = `CREATE OR REPLACE TEMPORARY TABLE tmp.ticketCalculateItem ENGINE = MEMORY sqlStatement = `CREATE OR REPLACE TEMPORARY TABLE tmp.ticketCalculateItem ENGINE = MEMORY
@ -46,9 +40,6 @@ describe('sale model ', () => {
return models.Ticket.rawSql(sqlStatement, params, options); return models.Ticket.rawSql(sqlStatement, params, options);
}); });
try {
const options = {transaction: tx};
const isRoleAdvanced = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*'); const isRoleAdvanced = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*');
expect(isRoleAdvanced).toEqual(true); expect(isRoleAdvanced).toEqual(true);
@ -63,24 +54,14 @@ describe('sale model ', () => {
const modifiedLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options); const modifiedLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options);
expect(modifiedLine.quantity).toEqual(newQuantity); expect(modifiedLine.quantity).toEqual(newQuantity);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
it('should update the quantity of a given sale current line', async() => { it('should update the quantity of a given sale current line', async() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(9)); spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getCtx(developerId, true));
const tx = await models.Sale.beginTransaction({});
const saleId = 25; const saleId = 25;
const newQuantity = 4; const newQuantity = 4;
try {
const options = {transaction: tx};
const originalLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options); const originalLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options);
expect(originalLine.quantity).toEqual(20); expect(originalLine.quantity).toEqual(20);
@ -90,87 +71,43 @@ describe('sale model ', () => {
const modifiedLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options); const modifiedLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options);
expect(modifiedLine.quantity).toEqual(newQuantity); expect(modifiedLine.quantity).toEqual(newQuantity);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
it('should throw an error if the quantity is negative and it is not a refund ticket', async() => { it('should throw an error if the quantity is negative and it is not a refund ticket', async() => {
const ctx = { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getCtx(employeeId, true));
req: {
accessToken: {userId: 1},
headers: {origin: 'localhost:5000'},
__: () => {}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
const saleId = 17; const saleId = 17;
const newQuantity = -10; const newQuantity = -10;
const tx = await models.Sale.beginTransaction({});
let error;
try { try {
const options = {transaction: tx};
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options); await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); expect(e).toEqual(new Error('You can only add negative amounts in refund tickets'));
error = e;
} }
expect(error).toEqual(new Error('You can only add negative amounts in refund tickets'));
}); });
it('should update a negative quantity when is a ticket refund', async() => { it('should update a negative quantity when is a ticket refund', async() => {
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(9)); spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getCtx(developerId, true));
const tx = await models.Sale.beginTransaction({});
const saleId = 32; const saleId = 32;
const newQuantity = -10; const newQuantity = -10;
try {
const options = {transaction: tx};
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options); await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
const modifiedLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options); const modifiedLine = await models.Sale.findOne({where: {id: saleId}, fields: ['quantity']}, options);
expect(modifiedLine.quantity).toEqual(newQuantity); expect(modifiedLine.quantity).toEqual(newQuantity);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
it('should throw an error if the quantity is less than the minimum quantity of the item', async() => { it('should throw an error if the quantity is less than the minimum quantity of the item', async() => {
const ctx = { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getCtx(employeeId, true));
req: {
accessToken: {userId: 1},
headers: {origin: 'localhost:5000'},
__: () => {}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
const tx = await models.Sale.beginTransaction({});
const itemId = 2; const itemId = 2;
const saleId = 17; const saleId = 17;
const minQuantity = 30; const minQuantity = 30;
const newQuantity = minQuantity - 1; const newQuantity = minQuantity - 1;
let error;
try { try {
const options = {transaction: tx};
const item = await models.Item.findById(itemId, null, options); const item = await models.Item.findById(itemId, null, options);
await item.updateAttribute('minQuantity', minQuantity, options); await item.updateAttribute('minQuantity', minQuantity, options);
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => { spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
@ -183,35 +120,19 @@ describe('sale model ', () => {
}); });
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options); await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); expect(e).toEqual(new Error('The amount cannot be less than the minimum'));
error = e;
} }
expect(error).toEqual(new Error('The amount cannot be less than the minimum'));
}); });
it('should change quantity if has minimum quantity and new quantity is equal than item available', async() => { it('should change quantity if has minimum quantity and new quantity is equal than item available', async() => {
const ctx = { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getCtx(employeeId, true));
req: {
accessToken: {userId: 1},
headers: {origin: 'localhost:5000'},
__: () => {}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
const tx = await models.Sale.beginTransaction({});
const itemId = 2; const itemId = 2;
const saleId = 17; const saleId = 17;
const minQuantity = 30; const minQuantity = 30;
const newQuantity = minQuantity - 1; const newQuantity = minQuantity - 1;
try {
const options = {transaction: tx};
const item = await models.Item.findById(itemId, null, options); const item = await models.Item.findById(itemId, null, options);
await item.updateAttribute('minQuantity', minQuantity, options); await item.updateAttribute('minQuantity', minQuantity, options);
@ -225,34 +146,17 @@ describe('sale model ', () => {
}); });
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options); await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
describe('newPrice', () => { describe('newPrice', () => {
it('should increase quantity if you have enough available and the new price is the same as the previous one', async() => { it('should increase quantity if you have enough available and the new price is the same as the previous one', async() => {
const ctx = { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getCtx(employeeId, true));
req: {
accessToken: {userId: 1},
headers: {origin: 'localhost:5000'},
__: () => {}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
const tx = await models.Sale.beginTransaction({});
const itemId = 2; const itemId = 2;
const saleId = 17; const saleId = 17;
const minQuantity = 30; const minQuantity = 30;
const newQuantity = 31; const newQuantity = 31;
try {
const options = {transaction: tx};
const item = await models.Item.findById(itemId, null, options); const item = await models.Item.findById(itemId, null, options);
await item.updateAttribute('minQuantity', minQuantity, options); await item.updateAttribute('minQuantity', minQuantity, options);
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => { spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
@ -266,33 +170,16 @@ describe('sale model ', () => {
}); });
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options); await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
it('should increase quantity when the new price is lower than the previous one', async() => { it('should increase quantity when the new price is lower than the previous one', async() => {
const ctx = { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getCtx(employeeId, true));
req: {
accessToken: {userId: 1},
headers: {origin: 'localhost:5000'},
__: () => {}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
const tx = await models.Sale.beginTransaction({});
const itemId = 2; const itemId = 2;
const saleId = 17; const saleId = 17;
const minQuantity = 30; const minQuantity = 30;
const newQuantity = 31; const newQuantity = 31;
try {
const options = {transaction: tx};
const item = await models.Item.findById(itemId, null, options); const item = await models.Item.findById(itemId, null, options);
await item.updateAttribute('minQuantity', minQuantity, options); await item.updateAttribute('minQuantity', minQuantity, options);
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => { spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
@ -306,34 +193,17 @@ describe('sale model ', () => {
}); });
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options); await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
}); });
it('should throw error when increase quantity and the new price is higher than the previous one', async() => { it('should throw error when increase quantity and the new price is higher than the previous one', async() => {
const ctx = { spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getCtx(employeeId, true));
req: {
accessToken: {userId: 1},
headers: {origin: 'localhost:5000'},
__: () => {}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue(getActiveCtx(1));
const tx = await models.Sale.beginTransaction({});
const itemId = 2; const itemId = 2;
const saleId = 17; const saleId = 17;
const minQuantity = 30; const minQuantity = 30;
const newQuantity = 31; const newQuantity = 31;
let error;
try { try {
const options = {transaction: tx};
const item = await models.Item.findById(itemId, null, options); const item = await models.Item.findById(itemId, null, options);
await item.updateAttribute('minQuantity', minQuantity, options); await item.updateAttribute('minQuantity', minQuantity, options);
spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => { spyOn(models.Sale, 'rawSql').and.callFake((sqlStatement, params, options) => {
@ -347,14 +217,9 @@ describe('sale model ', () => {
}); });
await models.Sale.updateQuantity(ctx, saleId, newQuantity, options); await models.Sale.updateQuantity(ctx, saleId, newQuantity, options);
await tx.rollback();
} catch (e) { } catch (e) {
await tx.rollback(); expect(e).toEqual(new Error('The price of the item changed'));
error = e;
} }
expect(error).toEqual(new Error('The price of the item changed'));
}); });
}); });
}); });