VnModel.crud code impruved and bugs/tests fixed

This commit is contained in:
Juan 2018-10-22 09:29:58 +02:00
parent c276e50beb
commit 79bb359051
3 changed files with 94 additions and 109 deletions

View File

@ -0,0 +1,47 @@
const app = require('../../../../../item/server/server');
fdescribe('Model crud()', () => {
let insertId;
let ItemBarcode = app.models.ItemBarcode;
it('should inherit crud method from VnModel', () => {
expect(ItemBarcode.crud).toBeDefined();
});
it('should create a new instance', async() => {
let data = {code: '500', itemFk: '1'};
crudObject = {
create: [data]
};
await ItemBarcode.crud(crudObject);
let instance = await ItemBarcode.findOne({where: data});
insertId = instance.id;
expect(instance).not.toEqual(null);
expect(instance.code).toEqual('500');
});
it('should update the instance', async() => {
crudObject = {
update: [{
where: {id: insertId},
data: {code: '501', itemFk: 1}
}]
};
await ItemBarcode.crud(crudObject);
let instance = await ItemBarcode.findById(insertId);
expect(instance.code).toEqual('501');
});
it('should delete the created instance', async() => {
crudObject = {
delete: [insertId]
};
await ItemBarcode.crud(crudObject);
let instance = await ItemBarcode.findById(insertId);
expect(instance).toEqual(null);
});
});

View File

@ -1,60 +0,0 @@
const app = require('../../../../../item/server/server');
describe('Model installCrudModel()', () => {
it('should extend installCrudModel properties to any model passed', () => {
let exampleModel = app.models.ItemBarcode;
expect(exampleModel.installCrudModel).toBeDefined();
});
describe('installCrudModel()', () => {
it('should create a new remothed method', () => {
let exampleModel = app.models.ItemBarcode;
exampleModel.installCrudModel('crudItemBarcodes');
expect(exampleModel.crudItemBarcodes).toBeDefined();
});
});
describe('ItemBarcode crudMethod()', () => {
let createdId;
it('should create a new barcode', async() => {
crudObject = {
create: [{code: '500', itemFk: '1'}],
update: [],
delete: []
};
await app.models.ItemBarcode.crudItemBarcodes(crudObject);
let result = await app.models.ItemBarcode.find({where: {itemFk: 1}});
createdId = result[3].id;
expect(result[3].code).toEqual('500');
expect(result.length).toEqual(4);
});
it('should update a barcode', async() => {
crudObject = {
create: [],
update: [{id: createdId, code: '501', itemFk: 1}],
delete: []
};
await app.models.ItemBarcode.crudItemBarcodes(crudObject);
let result = await app.models.ItemBarcode.find({where: {itemFk: 1}});
expect(result[3].code).toEqual('501');
expect(result.length).toEqual(4);
});
it('should delete a barcode', async() => {
crudObject = {
create: [],
update: [],
delete: [createdId]
};
await app.models.ItemBarcode.crudItemBarcodes(crudObject);
let result = await app.models.ItemBarcode.find({where: {itemFk: 1}});
expect(result.length).toEqual(3);
});
});
});

View File

@ -29,7 +29,24 @@ module.exports = function(Self) {
// this.disableRemoteMethod(method, disableMethods[method]);
}
*/
this.installCrudModel('crud');
this.remoteMethod('crud', {
description: 'Create, update or/and delete instances from model with a single request',
accessType: 'WRITE',
accepts: [
{
arg: 'actions',
type: 'Object',
require: true,
description: 'Instances to update, example: {create: [instances], update: [instances], delete: [ids]}',
http: {source: 'body'}
}
],
http: {
path: `/crud`,
verb: 'POST'
}
});
};
Self.defineScope = function(serverFilter) {
@ -123,25 +140,7 @@ module.exports = function(Self) {
};
};
Self.installCrudModel = function(methodName) {
this.remoteMethod(methodName, {
description: 'Create, update or/and delete instances from model in a single request',
accessType: 'WRITE',
accepts: [
{
arg: 'actions',
type: 'Object',
require: true,
description: 'Instances to update, example: {create: [instances], update: [instances], delete: [ids]}',
http: {source: 'body'}
}
],
http: {
path: `/${methodName}`,
verb: 'POST'
}
});
this[methodName] = async actions => {
Self.crud = async function(actions) {
let transaction = await this.beginTransaction({});
let options = {transaction: transaction};
@ -173,7 +172,6 @@ module.exports = function(Self) {
throw error;
}
};
};
/**
* Executes an SQL query