loopback-datasource-juggler/test/kvao/keys.suite.js

110 lines
3.2 KiB
JavaScript
Raw Normal View History

'use strict';
var bdd = require('../helpers/bdd-if');
var helpers = require('./_helpers');
var Promise = require('bluebird');
var should = require('should');
module.exports = function(dataSourceFactory, connectorCapabilities) {
var canIterateKeys = connectorCapabilities.canIterateKeys !== false;
bdd.describeIf(canIterateKeys, 'keys', function() {
var CacheItem;
beforeEach(function unpackContext() {
CacheItem = helpers.givenCacheItem(dataSourceFactory);
2016-08-17 12:24:20 +00:00
CacheItem.sortedKeys = function(filter, options) {
return this.keys(filter, options).then(function(keys) {
keys.sort();
return keys;
});
};
});
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() {
var AnotherModel = CacheItem.dataSource.createModel('AnotherModel');
return helpers.givenKeys(CacheItem, ['key1', 'key2'])
.then(function() {
return helpers.givenKeys(AnotherModel, ['otherKey1', 'otherKey2']);
})
.then(function() {
2016-08-17 12:24:20 +00:00
return CacheItem.sortedKeys();
})
.then(function(keys) {
should(keys).eql(['key1', 'key2']);
});
});
var largeKeySets = connectorCapabilities.canIterateLargeKeySets !== false;
bdd.itIf(largeKeySets, 'handles large key set', function() {
var expectedKeys = [];
for (var ix = 0; ix < 1000; ix++)
expectedKeys.push('key-' + ix);
2016-08-17 12:24:20 +00:00
expectedKeys.sort();
return helpers.givenKeys(CacheItem, expectedKeys)
.then(function() {
2016-08-17 12:24:20 +00:00
return CacheItem.sortedKeys();
})
.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-10-19 21:04:05 +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-10-19 21:04:05 +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-10-19 21:04:05 +00:00
return CacheItem.sortedKeys({match: 'not-found'})
2016-08-17 12:24:20 +00:00
.then(function(keys) {
should(keys).eql([]);
});
});
});
});
};