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

55 lines
1.7 KiB
JavaScript
Raw Normal View History

'use strict';
2018-12-07 16:13:48 +00:00
const asyncIterators = require('async-iterators');
const bdd = require('../helpers/bdd-if');
const helpers = require('./_helpers');
const Promise = require('bluebird');
const should = require('should');
const toArray = Promise.promisify(asyncIterators.toArray);
module.exports = function(dataSourceFactory, connectorCapabilities) {
2018-12-07 16:13:48 +00:00
const canIterateKeys = connectorCapabilities.canIterateKeys !== false;
bdd.describeIf(canIterateKeys, 'iterateKeys', function() {
2018-12-07 16:13:48 +00:00
let CacheItem;
beforeEach(setupCacheItem);
it('returns AsyncIterator covering all keys', function() {
return helpers.givenKeys(CacheItem, ['key1', 'key2'])
.then(function() {
2018-12-07 16:13:48 +00:00
const it = CacheItem.iterateKeys();
should(it).have.property('next');
return toArray(it);
})
.then(function(keys) {
keys.sort();
should(keys).eql(['key1', 'key2']);
});
});
it('returns AsyncIterator supporting Promises', function() {
2018-12-07 16:13:48 +00:00
let iterator;
return helpers.givenKeys(CacheItem, ['key'])
.then(function() {
iterator = CacheItem.iterateKeys();
return iterator.next();
})
.then(function(key) {
should(key).equal('key');
return iterator.next();
})
.then(function(key) {
// Note: AsyncIterator contract requires `undefined` to signal
// the end of the sequence. Other false-y values like `null`
// don't work.
should(key).equal(undefined);
});
});
function setupCacheItem() {
return helpers.givenCacheItem(dataSourceFactory)
.then(ModelCtor => CacheItem = ModelCtor);
2018-12-07 16:13:48 +00:00
}
});
};