From 616a81b4968112654f4e7406d829a973fb08b47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 12 Apr 2016 14:32:34 +0200 Subject: [PATCH] test: extract uid-generator helper --- test/helpers/uid-generator.js | 19 +++++++++++++++++++ test/persistence-hooks.suite.js | 24 +++++++++--------------- 2 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 test/helpers/uid-generator.js diff --git a/test/helpers/uid-generator.js b/test/helpers/uid-generator.js new file mode 100644 index 00000000..f2bc95b4 --- /dev/null +++ b/test/helpers/uid-generator.js @@ -0,0 +1,19 @@ +// Copyright IBM Corp. 2016. All Rights Reserved. +// Node module: loopback-datasource-juggler +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +var lastId = 0; + +exports.next = function() { + lastId++; + return exports.last(); +}; + +exports.last = function() { + return '' + lastId; +}; + +exports.reset = function() { + lastId = 0; +}; diff --git a/test/persistence-hooks.suite.js b/test/persistence-hooks.suite.js index 1fed18e4..a9caefae 100644 --- a/test/persistence-hooks.suite.js +++ b/test/persistence-hooks.suite.js @@ -10,6 +10,9 @@ var ContextRecorder = contextTestHelpers.ContextRecorder; var deepCloneToObject = contextTestHelpers.deepCloneToObject; var aCtxForModel = contextTestHelpers.aCtxForModel; +var uid = require('./helpers/uid-generator'); +var getLastGeneratedUid = uid.last; + module.exports = function(dataSource, should, connectorCapabilities) { if (!connectorCapabilities) connectorCapabilities = {}; if (connectorCapabilities.replaceOrCreateReportsNewInstance === undefined) { @@ -19,7 +22,7 @@ module.exports = function(dataSource, should, connectorCapabilities) { describe('Persistence hooks', function() { var ctxRecorder, expectedError, observersCalled; var TestModel, existingInstance; - var migrated = false, lastId; + var migrated = false; var triggered; var undefinedValue = undefined; @@ -31,12 +34,12 @@ module.exports = function(dataSource, should, connectorCapabilities) { TestModel = dataSource.createModel('TestModel', { // Set id.generated to false to honor client side values - id: { type: String, id: true, generated: false, default: uid }, + id: { type: String, id: true, generated: false, default: uid.next }, name: { type: String, required: true }, extra: { type: String, required: false }, }); - lastId = 0; + uid.reset(); if (migrated) { TestModel.deleteAll(done); @@ -229,7 +232,7 @@ module.exports = function(dataSource, should, connectorCapabilities) { next(); }); - TestModel.create({ id: uid(), name: 'a-name' }, function(err, instance) { + TestModel.create({ id: uid.next(), name: 'a-name' }, function(err, instance) { if (err) return done(err); instance.should.have.property('extra', 'hook data'); done(); @@ -1258,7 +1261,7 @@ module.exports = function(dataSource, should, connectorCapabilities) { }); var User = dataSource.createModel('UserWithAddress', { - id: { type: String, id: true, default: uid() }, + id: { type: String, id: true, default: uid.next }, name: { type: String, required: true }, address: { type: Address, required: false }, extra: { type: String }, @@ -1510,7 +1513,7 @@ module.exports = function(dataSource, should, connectorCapabilities) { }); var User = dataSource.createModel('UserWithAddress', { - id: { type: String, id: true, default: uid() }, + id: { type: String, id: true, default: uid.next }, name: { type: String, required: true }, address: { type: Address, required: false }, extra: { type: String }, @@ -3013,15 +3016,6 @@ module.exports = function(dataSource, should, connectorCapabilities) { TestModel.findOne({ where: { id: id }}, { notify: false }, cb); } - function uid() { - lastId += 1; - return '' + lastId; - } - - function getLastGeneratedUid() { - return '' + lastId; - } - function monitorHookExecution() { triggered = []; TestModel._notify = TestModel.notifyObserversOf;