test: extract uid-generator helper
This commit is contained in:
parent
3435b43a5c
commit
616a81b496
|
@ -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;
|
||||
};
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue