From f3eecf53b7fc525af5bfc97f6e0c14ee1b93db32 Mon Sep 17 00:00:00 2001 From: Carlos Jimenez <=> Date: Wed, 18 Apr 2018 14:37:17 +0200 Subject: [PATCH] #192 refactor on back end unit test helpers to ready loopback methods extensions CR Joan --- .../client/specs/getAverageInvoiced.spec.js | 5 ++- .../methods/client/specs/getMana.spec.js | 5 ++- .../loopback/common/methods/item/clone.js | 2 +- .../common/methods/item/specs/clone.spec.js | 20 ++++++++++ .../common/methods/item/updateTaxes.js | 2 +- .../methods/ticket/specs/get-taxes.spec.js | 5 ++- .../methods/ticket/specs/get-total.spec.js | 5 ++- .../methods/ticket/specs/get-volume.spec.js | 7 ++-- .../loopback/common/test-helpers/loopback.js | 39 +++++++++++++++++++ .../loopback/common/test-helpers/rawSql.js | 14 ------- 10 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 services/loopback/common/methods/item/specs/clone.spec.js create mode 100644 services/loopback/common/test-helpers/loopback.js delete mode 100644 services/loopback/common/test-helpers/rawSql.js diff --git a/services/loopback/common/methods/client/specs/getAverageInvoiced.spec.js b/services/loopback/common/methods/client/specs/getAverageInvoiced.spec.js index 0775423e0..2493c17b0 100644 --- a/services/loopback/common/methods/client/specs/getAverageInvoiced.spec.js +++ b/services/loopback/common/methods/client/specs/getAverageInvoiced.spec.js @@ -1,10 +1,11 @@ const getAverageInvoiced = require('../getAverageInvoiced'); -const {rawSql} = require('../../../test-helpers/rawSql'); const model = { remoteMethod: () => {} }; +let Loopback = require('../../../test-helpers/loopback'); +Loopback.init(model); -rawSql(model); +Loopback.rawSql(model); getAverageInvoiced(model); describe('client getAverageInvoiced()', () => { diff --git a/services/loopback/common/methods/client/specs/getMana.spec.js b/services/loopback/common/methods/client/specs/getMana.spec.js index 942eaaca0..940930eb5 100644 --- a/services/loopback/common/methods/client/specs/getMana.spec.js +++ b/services/loopback/common/methods/client/specs/getMana.spec.js @@ -1,10 +1,11 @@ const getMana = require('../getMana'); -const {rawSql} = require('../../../test-helpers/rawSql'); const model = { remoteMethod: () => {} }; +let Loopback = require('../../../test-helpers/loopback'); +Loopback.init(model); -rawSql(model); +Loopback.rawSql(model); getMana(model); describe('client getMana()', () => { diff --git a/services/loopback/common/methods/item/clone.js b/services/loopback/common/methods/item/clone.js index a3e9e38fc..f0a66d0d9 100644 --- a/services/loopback/common/methods/item/clone.js +++ b/services/loopback/common/methods/item/clone.js @@ -1,4 +1,4 @@ -var UserError = require('../../helpers').UserError; +let UserError = require('../../helpers').UserError; module.exports = Self => { Self.remoteMethod('clone', { diff --git a/services/loopback/common/methods/item/specs/clone.spec.js b/services/loopback/common/methods/item/specs/clone.spec.js new file mode 100644 index 000000000..345b7f7e5 --- /dev/null +++ b/services/loopback/common/methods/item/specs/clone.spec.js @@ -0,0 +1,20 @@ +const clone = require('../clone'); +const model = { + remoteMethod: () => {} +}; +let Loopback = require('../../../test-helpers/loopback'); +Loopback.init(model); + +Loopback.rawSql(model); +clone(model); + +describe('item clone()', () => { + it('should', () => { + let itemFk = 1; + model.clone(itemFk) + .then(response => { + expect(response).toEqual('whatever'); + done(); + }); + }); +}); diff --git a/services/loopback/common/methods/item/updateTaxes.js b/services/loopback/common/methods/item/updateTaxes.js index 70a2511bc..e70af8ca2 100644 --- a/services/loopback/common/methods/item/updateTaxes.js +++ b/services/loopback/common/methods/item/updateTaxes.js @@ -25,7 +25,7 @@ module.exports = Self => { } }); - Self.updateTaxes = async (id, taxes) => { + Self.updateTaxes = async(id, taxes) => { let promises = []; for (let tax of taxes) { if (!tax.taxClassFk) diff --git a/services/loopback/common/methods/ticket/specs/get-taxes.spec.js b/services/loopback/common/methods/ticket/specs/get-taxes.spec.js index f7a4fbb70..179b8907c 100644 --- a/services/loopback/common/methods/ticket/specs/get-taxes.spec.js +++ b/services/loopback/common/methods/ticket/specs/get-taxes.spec.js @@ -1,10 +1,11 @@ const getTaxes = require('../get-taxes'); -const {rawSql} = require('../../../test-helpers/rawSql'); const model = { remoteMethod: () => {} }; +let Loopback = require('../../../test-helpers/loopback'); +Loopback.init(model); -rawSql(model); +Loopback.rawSql(model); getTaxes(model); describe('ticket getTaxes()', () => { diff --git a/services/loopback/common/methods/ticket/specs/get-total.spec.js b/services/loopback/common/methods/ticket/specs/get-total.spec.js index 73adcb87e..a42234194 100644 --- a/services/loopback/common/methods/ticket/specs/get-total.spec.js +++ b/services/loopback/common/methods/ticket/specs/get-total.spec.js @@ -1,10 +1,11 @@ const getTotal = require('../get-total'); -const {rawSql} = require('../../../test-helpers/rawSql'); const model = { remoteMethod: () => {} }; +let Loopback = require('../../../test-helpers/loopback'); +Loopback.init(model); -rawSql(model); +Loopback.rawSql(model); getTotal(model); describe('ticket getTotal()', () => { diff --git a/services/loopback/common/methods/ticket/specs/get-volume.spec.js b/services/loopback/common/methods/ticket/specs/get-volume.spec.js index dcb01ac7f..99e1d4a63 100644 --- a/services/loopback/common/methods/ticket/specs/get-volume.spec.js +++ b/services/loopback/common/methods/ticket/specs/get-volume.spec.js @@ -1,10 +1,11 @@ const getVolume = require('../get-volume'); -const {rawSql} = require('../../../test-helpers/rawSql'); const model = { remoteMethod: () => {} }; +let Loopback = require('../../../test-helpers/loopback'); +Loopback.init(model); -rawSql(model); +Loopback.rawSql(model); getVolume(model); describe('ticket getVolume()', () => { @@ -16,4 +17,4 @@ describe('ticket getVolume()', () => { done(); }); }); -}); \ No newline at end of file +}); diff --git a/services/loopback/common/test-helpers/loopback.js b/services/loopback/common/test-helpers/loopback.js new file mode 100644 index 000000000..c6a11cb6d --- /dev/null +++ b/services/loopback/common/test-helpers/loopback.js @@ -0,0 +1,39 @@ +module.exports = { + importLoopbackModules: function() { + this.rawSqlModule = require('../methods/vnModel/rawSql.js'); + this.findOneModule = require('../methods/vnModel/rawSql.js'); + }, + + /** + * Initializes DataSource once + * @param {Object} Self - Model + */ + init: function(Self) { + if (!this.dataSource) + this.connect(); + + Self.dataSource = this.dataSource; + + this.importLoopbackModules(); + }, + + /** + * Instantiate Loopback DataSource + */ + connect: function() { + let DataSource = require('loopback-datasource-juggler').DataSource; + let dataSourceConfig = { + connector: 'mysql', + host: 'localhost', + user: 'root', + password: 'root', + database: 'salix' + }; + + this.dataSource = new DataSource(dataSourceConfig); + }, + + rawSql: function(Self) { + this.rawSqlModule(Self); + } +}; diff --git a/services/loopback/common/test-helpers/rawSql.js b/services/loopback/common/test-helpers/rawSql.js deleted file mode 100644 index 880520321..000000000 --- a/services/loopback/common/test-helpers/rawSql.js +++ /dev/null @@ -1,14 +0,0 @@ -module.exports.rawSql = Self => { - const DataSource = require('loopback-datasource-juggler').DataSource; - const rawSql = require('../methods/vnModel/rawSql.js'); - const dataSourceConfig = { - connector: 'mysql', - host: 'localhost', - user: 'root', - password: 'root', - database: 'salix' - }; - const dataSource = new DataSource(dataSourceConfig); - Self.dataSource = dataSource; - rawSql(Self); -};