refactor(entry): back unit tests refactor to transactions
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Jimenez Ruiz 2021-11-24 14:29:15 +01:00
parent c3da93bfd3
commit c4bc89ee29
9 changed files with 489 additions and 220 deletions

View File

@ -1,4 +1,4 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('entry addBuy()', () => {
@ -25,11 +25,11 @@ describe('entry addBuy()', () => {
packageFk: 3
};
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
try {
const newBuy = await app.models.Entry.addBuy(ctx, options);
const newBuy = await models.Entry.addBuy(ctx, options);
expect(newBuy.itemFk).toEqual(itemId);

View File

@ -1,4 +1,4 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('sale deleteBuys()', () => {
@ -15,13 +15,13 @@ describe('sale deleteBuys()', () => {
active: activeCtx
});
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
ctx.args = {buys: [{id: 1}]};
try {
const result = await app.models.Buy.deleteBuys(ctx, options);
const result = await models.Buy.deleteBuys(ctx, options);
expect(result).toEqual([{count: 1}]);
await tx.rollback();

View File

@ -1,9 +1,8 @@
const app = require('vn-loopback/server/server');
const model = app.models.Buy;
const models = require('vn-loopback/server/server').models;
describe('Buy editLatestsBuys()', () => {
it('should change the value of a given column for the selected buys', async() => {
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
try {
@ -13,15 +12,15 @@ describe('Buy editLatestsBuys()', () => {
}
};
const [original] = await model.latestBuysFilter(ctx, null, options);
const [original] = await models.Buy.latestBuysFilter(ctx, null, options);
const field = 'size';
const newValue = 99;
const lines = [{itemFk: original.itemFk, id: original.id}];
await model.editLatestBuys(field, newValue, lines, options);
await models.Buy.editLatestBuys(field, newValue, lines, options);
const [result] = await model.latestBuysFilter(ctx, null, options);
const [result] = await models.Buy.latestBuysFilter(ctx, null, options);
expect(result[field]).toEqual(newValue);

View File

@ -1,77 +1,137 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('Entry filter()', () => {
it('should return the entry matching "search"', async() => {
let ctx = {
args: {
search: 1
}
};
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
let result = await app.models.Entry.filter(ctx);
try {
const ctx = {
args: {
search: 1
}
};
expect(result.length).toEqual(1);
expect(result[0].id).toEqual(1);
const result = await models.Entry.filter(ctx, options);
expect(result.length).toEqual(1);
expect(result[0].id).toEqual(1);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return the entry matching the currency', async() => {
let ctx = {
args: {
currencyFk: 1
}
};
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
let result = await app.models.Entry.filter(ctx);
try {
const ctx = {
args: {
currencyFk: 1
}
};
expect(result.length).toEqual(8);
const result = await models.Entry.filter(ctx, options);
expect(result.length).toEqual(8);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return the entry matching the supplier', async() => {
let ctx = {
args: {
supplierFk: 2
}
};
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
let result = await app.models.Entry.filter(ctx);
try {
const ctx = {
args: {
supplierFk: 2
}
};
expect(result.length).toEqual(6);
const result = await models.Entry.filter(ctx, options);
expect(result.length).toEqual(6);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return the entry matching the company', async() => {
let ctx = {
args: {
companyFk: 442
}
};
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
let result = await app.models.Entry.filter(ctx);
try {
const ctx = {
args: {
companyFk: 442
}
};
expect(result.length).toEqual(7);
const result = await models.Entry.filter(ctx, options);
expect(result.length).toEqual(7);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return the entries matching isBooked', async() => {
let ctx = {
args: {
isBooked: true,
}
};
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
let result = await app.models.Entry.filter(ctx);
try {
const ctx = {
args: {
isBooked: true,
}
};
expect(result.length).toEqual(0);
const result = await models.Entry.filter(ctx, options);
expect(result.length).toEqual(0);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return the routes matching the reference and travel', async() => {
let ctx = {
args: {
reference: 'movement',
travelFk: '2'
}
};
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
let result = await app.models.Entry.filter(ctx);
try {
const ctx = {
args: {
reference: 'movement',
travelFk: '2'
}
};
expect(result.length).toEqual(2);
const result = await models.Entry.filter(ctx, options);
expect(result.length).toEqual(2);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -1,14 +1,24 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('entry getBuys()', () => {
const entryId = 4;
it('should get the buys and items of an entry', async() => {
const result = await app.models.Entry.getBuys(entryId);
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
const length = result.length;
const anyResult = result[Math.floor(Math.random() * Math.floor(length))];
try {
const result = await models.Entry.getBuys(entryId, options);
expect(result.length).toEqual(4);
expect(anyResult.item).toBeDefined();
const length = result.length;
const anyResult = result[Math.floor(Math.random() * Math.floor(length))];
expect(result.length).toEqual(4);
expect(anyResult.item).toBeDefined();
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -1,31 +1,71 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('travel getEntry()', () => {
const entryId = 1;
it('should check the entry contains the id', async() => {
const entry = await app.models.Entry.getEntry(entryId);
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
expect(entry.id).toEqual(entryId);
try {
const entry = await models.Entry.getEntry(entryId, options);
expect(entry.id).toEqual(entryId);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should check the entry contains the supplier name', async() => {
const entry = await app.models.Entry.getEntry(entryId);
const supplierName = entry.supplier().nickname;
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
expect(supplierName).toEqual('Plants nick');
try {
const entry = await models.Entry.getEntry(entryId, options);
const supplierName = entry.supplier().nickname;
expect(supplierName).toEqual('Plants nick');
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should check the entry contains the receiver warehouse name', async() => {
const entry = await app.models.Entry.getEntry(entryId);
const receiverWarehouseName = entry.travel().warehouseIn().name;
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
expect(receiverWarehouseName).toEqual('Warehouse One');
try {
const entry = await models.Entry.getEntry(entryId, options);
const receiverWarehouseName = entry.travel().warehouseIn().name;
expect(receiverWarehouseName).toEqual('Warehouse One');
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should check the entry contains the company code', async() => {
const entry = await app.models.Entry.getEntry(entryId);
const companyCode = entry.company().code;
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
expect(companyCode).toEqual('VNL');
try {
const entry = await models.Entry.getEntry(entryId, options);
const companyCode = entry.company().code;
expect(companyCode).toEqual('VNL');
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -1,4 +1,4 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('entry import()', () => {
@ -48,10 +48,10 @@ describe('entry import()', () => {
]
}
};
const tx = await app.models.Entry.beginTransaction({});
const tx = await models.Entry.beginTransaction({});
try {
const options = {transaction: tx};
const newEntry = await app.models.Entry.create({
const newEntry = await models.Entry.create({
dated: new Date(),
supplierFk: supplierId,
travelFk: travelId,
@ -60,10 +60,10 @@ describe('entry import()', () => {
ref: 'Entry ref'
}, options);
await app.models.Entry.importBuys(ctx, newEntry.id, options);
await models.Entry.importBuys(ctx, newEntry.id, options);
const updatedEntry = await app.models.Entry.findById(newEntry.id, null, options);
const entryBuys = await app.models.Buy.find({
const updatedEntry = await models.Entry.findById(newEntry.id, null, options);
const entryBuys = await models.Buy.find({
where: {entryFk: newEntry.id}
}, options);

View File

@ -1,4 +1,4 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
const LoopBackContext = require('loopback-context');
describe('entry importBuysPreview()', () => {
@ -10,30 +10,40 @@ describe('entry importBuysPreview()', () => {
});
it('should return the buys with the calculated packageFk', async() => {
const expectedPackageFk = '3';
const buys = [
{
itemFk: 1,
buyingValue: 5.77,
description: 'Bow',
grouping: 1,
size: 1,
volume: 1200
},
{
itemFk: 4,
buyingValue: 2.16,
description: 'Arrow',
grouping: 1,
size: 25,
volume: 1125
}
];
const tx = await models.Entry.beginTransaction({});
const options = {transaction: tx};
const result = await app.models.Entry.importBuysPreview(entryId, buys);
const randomIndex = Math.floor(Math.random() * result.length);
const buy = result[randomIndex];
try {
const expectedPackageFk = '3';
const buys = [
{
itemFk: 1,
buyingValue: 5.77,
description: 'Bow',
grouping: 1,
size: 1,
volume: 1200
},
{
itemFk: 4,
buyingValue: 2.16,
description: 'Arrow',
grouping: 1,
size: 25,
volume: 1125
}
];
expect(buy.packageFk).toEqual(expectedPackageFk);
const result = await models.Entry.importBuysPreview(entryId, buys, options);
const randomIndex = Math.floor(Math.random() * result.length);
const buy = result[randomIndex];
expect(buy.packageFk).toEqual(expectedPackageFk);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -1,195 +1,345 @@
const app = require('vn-loopback/server/server');
const models = require('vn-loopback/server/server').models;
describe('Buy latests buys filter()', () => {
it('should return the entry matching "search"', async() => {
let ctx = {
args: {
search: 'Ranged weapon longbow 2m'
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
let firstBuy = results[0];
try {
const ctx = {
args: {
search: 'Ranged weapon longbow 2m'
}
};
expect(results.length).toEqual(1);
expect(firstBuy.size).toEqual(70);
const results = await models.Buy.latestBuysFilter(ctx);
const firstBuy = results[0];
expect(results.length).toEqual(1);
expect(firstBuy.size).toEqual(70);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return the entry matching "id"', async() => {
let ctx = {
args: {
id: 1
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
id: 1
}
};
expect(results.length).toEqual(1);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toEqual(1);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "tags"', async() => {
let ctx = {
args: {
tags: [
{tagFk: 27, value: '2m'}
]
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
tags: [
{tagFk: 27, value: '2m'}
]
}
};
expect(results.length).toBe(2);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(2);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "categoryFk"', async() => {
let ctx = {
args: {
categoryFk: 1
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
categoryFk: 1
}
};
expect(results.length).toBe(4);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(4);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "typeFk"', async() => {
let ctx = {
args: {
typeFk: 2
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
typeFk: 2
}
};
expect(results.length).toBe(4);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(4);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "active"', async() => {
let ctx = {
args: {
active: true
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
active: true
}
};
expect(results.length).toBe(6);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(6);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "not active"', async() => {
let ctx = {
args: {
active: false
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
active: false
}
};
expect(results.length).toBe(0);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(0);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "visible"', async() => {
let ctx = {
args: {
visible: true
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
visible: true
}
};
expect(results.length).toBe(5);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(5);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "not visible"', async() => {
let ctx = {
args: {
visible: false
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
visible: false
}
};
expect(results.length).toBe(0);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(0);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "floramondo"', async() => {
let ctx = {
args: {
floramondo: true
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
floramondo: true
}
};
expect(results.length).toBe(1);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(1);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "not floramondo"', async() => {
let ctx = {
args: {
floramondo: false
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
floramondo: false
}
};
expect(results.length).toBe(5);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(5);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "salesPersonFk"', async() => {
let ctx = {
args: {
salesPersonFk: 35
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
salesPersonFk: 35
}
};
expect(results.length).toBe(6);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(6);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "description"', async() => {
let ctx = {
args: {
description: 'Increases block'
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
description: 'Increases block'
}
};
expect(results.length).toBe(1);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(1);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "supplierFk"', async() => {
let ctx = {
args: {
supplierFk: 1
}
};
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
let results = await app.models.Buy.latestBuysFilter(ctx);
try {
const ctx = {
args: {
supplierFk: 1
}
};
expect(results.length).toBe(2);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(2);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should return results matching "from" and "to"', async() => {
const from = new Date();
from.setHours(0, 0, 0, 0);
const tx = await models.Buy.beginTransaction({});
const options = {transaction: tx};
const to = new Date();
to.setHours(23, 59, 59, 999);
try {
const from = new Date();
from.setHours(0, 0, 0, 0);
let ctx = {
args: {
from: from,
to: to
}
};
const to = new Date();
to.setHours(23, 59, 59, 999);
let results = await app.models.Buy.latestBuysFilter(ctx);
const ctx = {
args: {
from: from,
to: to
}
};
expect(results.length).toBe(2);
const results = await models.Buy.latestBuysFilter(ctx, options);
expect(results.length).toBe(2);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});