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
|
|
|
|
|
2016-08-15 12:55:26 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const bdd = require('../helpers/bdd-if');
|
|
|
|
const helpers = require('./_helpers');
|
|
|
|
const should = require('should');
|
2016-08-15 12:55:26 +00:00
|
|
|
|
|
|
|
module.exports = function(dataSourceFactory, connectorCapabilities) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const canIterateKeys = connectorCapabilities.canIterateKeys !== false;
|
2016-10-07 14:26:18 +00:00
|
|
|
|
|
|
|
bdd.describeIf(canIterateKeys, 'keys', function() {
|
2017-01-10 08:18:04 +00:00
|
|
|
let CacheItem;
|
|
|
|
beforeEach(function setupCacheItem() {
|
|
|
|
return helpers.givenCacheItem(dataSourceFactory)
|
|
|
|
.then(ModelCtor => CacheItem = ModelCtor)
|
|
|
|
.then(() => {
|
|
|
|
CacheItem.sortedKeys = function(filter, options) {
|
|
|
|
return this.keys(filter, options).then(keys => keys.sort());
|
|
|
|
};
|
2016-08-17 12:24:20 +00:00
|
|
|
});
|
2016-08-15 12:55:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('returns all keys - Callback API', function(done) {
|
|
|
|
helpers.givenKeys(CacheItem, ['key1', 'key2'], function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
CacheItem.keys(function(err, keys) {
|
|
|
|
if (err) return done(err);
|
|
|
|
keys.sort();
|
|
|
|
should(keys).eql(['key1', 'key2']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns all keys - Promise API', function() {
|
|
|
|
return helpers.givenKeys(CacheItem, ['key1', 'key2'])
|
|
|
|
.then(function() {
|
|
|
|
return CacheItem.keys();
|
|
|
|
})
|
|
|
|
.then(function(keys) {
|
|
|
|
keys.sort();
|
|
|
|
should(keys).eql(['key1', 'key2']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns keys of the given model only', function() {
|
2017-01-10 08:18:04 +00:00
|
|
|
let AnotherModel;
|
2017-01-12 03:35:37 +00:00
|
|
|
return helpers.givenModel(dataSourceFactory, 'AnotherModel')
|
2017-01-10 08:18:04 +00:00
|
|
|
.then(ModelCtor => AnotherModel = ModelCtor)
|
|
|
|
.then(() => helpers.givenKeys(CacheItem, ['key1', 'key2']))
|
|
|
|
.then(() => helpers.givenKeys(AnotherModel, ['otherKey1', 'otherKey2']))
|
|
|
|
.then(() => CacheItem.sortedKeys())
|
|
|
|
.then(keys => should(keys).eql(['key1', 'key2']));
|
2016-08-15 12:55:26 +00:00
|
|
|
});
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const largeKeySets = connectorCapabilities.canIterateLargeKeySets !== false;
|
2016-10-07 14:26:18 +00:00
|
|
|
bdd.itIf(largeKeySets, 'handles large key set', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
const expectedKeys = [];
|
|
|
|
for (let ix = 0; ix < 1000; ix++)
|
2016-08-15 12:55:26 +00:00
|
|
|
expectedKeys.push('key-' + ix);
|
2016-08-17 12:24:20 +00:00
|
|
|
expectedKeys.sort();
|
2016-08-15 12:55:26 +00:00
|
|
|
|
|
|
|
return helpers.givenKeys(CacheItem, expectedKeys)
|
|
|
|
.then(function() {
|
2016-08-17 12:24:20 +00:00
|
|
|
return CacheItem.sortedKeys();
|
2016-08-15 12:55:26 +00:00
|
|
|
})
|
|
|
|
.then(function(keys) {
|
|
|
|
should(keys).eql(expectedKeys);
|
|
|
|
});
|
|
|
|
});
|
2016-08-17 12:24:20 +00:00
|
|
|
|
|
|
|
context('with "filter.match"', function() {
|
|
|
|
beforeEach(function createTestData() {
|
|
|
|
return helpers.givenKeys(CacheItem, [
|
|
|
|
'hallo',
|
|
|
|
'hello',
|
|
|
|
'hxllo',
|
|
|
|
'hllo',
|
|
|
|
'heeello',
|
|
|
|
'foo',
|
|
|
|
'bar',
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports "?" operator', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return CacheItem.sortedKeys({match: 'h?llo'}).then(function(keys) {
|
2016-08-17 12:24:20 +00:00
|
|
|
should(keys).eql(['hallo', 'hello', 'hxllo']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports "*" operator', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return CacheItem.sortedKeys({match: 'h*llo'}).then(function(keys) {
|
2016-08-17 12:24:20 +00:00
|
|
|
should(keys).eql(['hallo', 'heeello', 'hello', 'hllo', 'hxllo']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('handles no matches found', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return CacheItem.sortedKeys({match: 'not-found'})
|
2016-08-17 12:24:20 +00:00
|
|
|
.then(function(keys) {
|
|
|
|
should(keys).eql([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-08-15 12:55:26 +00:00
|
|
|
});
|
|
|
|
};
|