loopback-datasource-juggler/test/kvao/_helpers.js

47 lines
1.1 KiB
JavaScript
Raw Normal View History

2019-05-08 15:45:37 +00:00
// Copyright IBM Corp. 2016,2018. 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
'use strict';
exports.givenCacheItem = givenCacheItem;
exports.givenKeys = givenKeys;
exports.givenModel = givenModel;
exports.delay = delay;
function givenCacheItem(dataSourceFactory) {
const modelProperties = {
key: String,
value: 'Any',
};
return givenModel(dataSourceFactory, 'CacheItem', modelProperties);
}
function givenModel(dataSourceFactory, modelName,
2018-06-12 07:13:32 +00:00
modelProperties, options) {
const dataSource = dataSourceFactory();
const Model = dataSource.createModel(modelName, modelProperties);
const p = 'deleteAll' in dataSource.connector ?
Model.deleteAll() : Promise.resolve();
return p.then(() => Model);
}
function givenKeys(Model, keys, cb) {
2018-12-07 14:54:29 +00:00
let p = Promise.all(
keys.map(function(k) {
return Model.set(k, 'value-' + k);
})
);
if (cb) {
p = p.then(function(r) { cb(null, r); }, cb);
}
return p;
}
function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}