Merge pull request #1218 from strongloop/clear-cache-if-delete-all-supported

Detect deleteAll support in KVAO tests
This commit is contained in:
Simon Ho 2017-01-11 19:31:22 -08:00 committed by GitHub
commit 736a4ad185
8 changed files with 83 additions and 59 deletions

View File

@ -2,15 +2,28 @@
var Promise = require('bluebird'); var Promise = require('bluebird');
exports.givenCacheItem = function(dataSourceFactory) { exports.givenCacheItem = givenCacheItem;
var dataSource = dataSourceFactory(); exports.givenKeys = givenKeys;
return dataSource.createModel('CacheItem', { exports.givenModel = givenModel;
function givenCacheItem(dataSourceFactory) {
const modelProperties = {
key: String, key: String,
value: 'any', value: 'Any',
}); };
return givenModel(dataSourceFactory, 'CacheItem', modelProperties);
}; };
exports.givenKeys = function(Model, keys, cb) { function givenModel(dataSourceFactory, modelName,
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) {
var p = Promise.all( var p = Promise.all(
keys.map(function(k) { keys.map(function(k) {
return Model.set(k, 'value-' + k); return Model.set(k, 'value-' + k);

View File

@ -7,30 +7,31 @@ const should = require('should');
module.exports = function(dataSourceFactory, connectorCapabilities) { module.exports = function(dataSourceFactory, connectorCapabilities) {
var supportsDeleteAll = 'deleteAll' in dataSourceFactory().connector; var supportsDeleteAll = 'deleteAll' in dataSourceFactory().connector;
bdd.describeIf(supportsDeleteAll, 'deleteAll', function() { bdd.describeIf(supportsDeleteAll, 'deleteAll', () => {
let CacheItem; let CacheItem;
beforeEach(function unpackContext() { beforeEach(setupCacheItem);
CacheItem = helpers.givenCacheItem(dataSourceFactory);
});
it('removes all key-value pairs for the given model', function() { it('removes all key-value pairs for the given model', () => {
return helpers.givenKeys(CacheItem, ['key1', 'key2']) return helpers.givenKeys(CacheItem, ['key1', 'key2'])
.then(() => CacheItem.deleteAll()) .then(() => CacheItem.deleteAll())
.then(() => CacheItem.keys()) .then(() => CacheItem.keys())
.then((keys) => { .then(keys => should(keys).eql([]));
should(keys).eql([]);
});
}); });
it('does not remove data from other existing models', function() { it('does not remove data from other existing models', () => {
var AnotherModel = dataSourceFactory().createModel('AnotherModel'); let AnotherModel;
return helpers.givenKeys(CacheItem, ['key1', 'key2']) helpers.givenModel(dataSourceFactory, 'AnotherModel')
.then(ModelCtor => AnotherModel = ModelCtor)
.then(() => helpers.givenKeys(CacheItem, ['key1', 'key2']))
.then(() => helpers.givenKeys(AnotherModel, ['key3', 'key4'])) .then(() => helpers.givenKeys(AnotherModel, ['key3', 'key4']))
.then(() => CacheItem.deleteAll()) .then(() => CacheItem.deleteAll())
.then(() => AnotherModel.keys()) .then(() => AnotherModel.keys())
.then((keys) => { .then(keys => should(keys.sort()).eql(['key3', 'key4']));
should(keys).eql(['key3', 'key4']);
});
}); });
function setupCacheItem() {
return helpers.givenCacheItem(dataSourceFactory)
.then(ModelCtor => CacheItem = ModelCtor);
}
}); });
}; };

View File

@ -5,16 +5,13 @@ const helpers = require('./_helpers');
const should = require('should'); const should = require('should');
module.exports = function(dataSourceFactory, connectorCapabilities) { module.exports = function(dataSourceFactory, connectorCapabilities) {
var supportsDelete = 'delete' in dataSourceFactory().connector; const supportsDelete = 'delete' in dataSourceFactory().connector;
bdd.describeIf(supportsDelete, 'delete', function() { bdd.describeIf(supportsDelete, 'delete', () => {
let CacheItem; let CacheItem;
beforeEach(function unpackContext() { beforeEach(setupCacheItem);
CacheItem = helpers.givenCacheItem(dataSourceFactory);
return CacheItem.deleteAll();
});
it('removes the key-value pair for the given key', function() { it('removes the key-value pair for the given key', () => {
return helpers.givenKeys(CacheItem, ['key1', 'key2']) return helpers.givenKeys(CacheItem, ['key1', 'key2'])
.then(() => CacheItem.delete('key1')) .then(() => CacheItem.delete('key1'))
.then(() => CacheItem.keys()) .then(() => CacheItem.keys())
@ -22,5 +19,10 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
keys.should.eql(['key2']); keys.should.eql(['key2']);
}); });
}); });
function setupCacheItem() {
return helpers.givenCacheItem(dataSourceFactory)
.then(ModelCtor => CacheItem = ModelCtor);
}
}); });
}; };

View File

@ -14,9 +14,7 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
bdd.describeIf(canExpire, 'expire', function() { bdd.describeIf(canExpire, 'expire', function() {
var CacheItem; var CacheItem;
beforeEach(function unpackContext() { beforeEach(setupCacheItem);
CacheItem = helpers.givenCacheItem(dataSourceFactory);
});
it('sets key ttl - Callback API', function(done) { it('sets key ttl - Callback API', function(done) {
CacheItem.set('a-key', 'a-value', function(err) { CacheItem.set('a-key', 'a-value', function(err) {
@ -62,5 +60,10 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
err.should.have.property('statusCode', 404); err.should.have.property('statusCode', 404);
}); });
}); });
function setupCacheItem() {
return helpers.givenCacheItem(dataSourceFactory)
.then(ModelCtor => CacheItem = ModelCtor);
};
}); });
}; };

View File

@ -9,9 +9,7 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
describe('get/set', function() { describe('get/set', function() {
var CacheItem; var CacheItem;
beforeEach(function unpackContext() { beforeEach(setupCacheItem);
CacheItem = helpers.givenCacheItem(dataSourceFactory);
});
it('works for string values - Callback API', function(done) { it('works for string values - Callback API', function(done) {
CacheItem.set('a-key', 'a-value', function(err) { CacheItem.set('a-key', 'a-value', function(err) {
@ -101,5 +99,10 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
.then(function(value) { should.equal(value, 'another-value'); }); .then(function(value) { should.equal(value, 'another-value'); });
}); });
}); });
function setupCacheItem() {
return helpers.givenCacheItem(dataSourceFactory)
.then(ModelCtor => CacheItem = ModelCtor);
};
}); });
}; };

View File

@ -12,9 +12,7 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
bdd.describeIf(canIterateKeys, 'iterateKeys', function() { bdd.describeIf(canIterateKeys, 'iterateKeys', function() {
var CacheItem; var CacheItem;
beforeEach(function unpackContext() { beforeEach(setupCacheItem);
CacheItem = helpers.givenCacheItem(dataSourceFactory);
});
it('returns AsyncIterator covering all keys', function() { it('returns AsyncIterator covering all keys', function() {
return helpers.givenKeys(CacheItem, ['key1', 'key2']) return helpers.givenKeys(CacheItem, ['key1', 'key2'])
@ -47,5 +45,10 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
should(key).equal(undefined); should(key).equal(undefined);
}); });
}); });
function setupCacheItem() {
return helpers.givenCacheItem(dataSourceFactory)
.then(ModelCtor => CacheItem = ModelCtor);
};
}); });
}; };

View File

@ -9,15 +9,15 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
var canIterateKeys = connectorCapabilities.canIterateKeys !== false; var canIterateKeys = connectorCapabilities.canIterateKeys !== false;
bdd.describeIf(canIterateKeys, 'keys', function() { bdd.describeIf(canIterateKeys, 'keys', function() {
var CacheItem; let CacheItem;
beforeEach(function unpackContext() { beforeEach(function setupCacheItem() {
CacheItem = helpers.givenCacheItem(dataSourceFactory); return helpers.givenCacheItem(dataSourceFactory)
CacheItem.sortedKeys = function(filter, options) { .then(ModelCtor => CacheItem = ModelCtor)
return this.keys(filter, options).then(function(keys) { .then(() => {
keys.sort(); CacheItem.sortedKeys = function(filter, options) {
return keys; return this.keys(filter, options).then(keys => keys.sort());
};
}); });
};
}); });
it('returns all keys - Callback API', function(done) { it('returns all keys - Callback API', function(done) {
@ -44,17 +44,13 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
}); });
it('returns keys of the given model only', function() { it('returns keys of the given model only', function() {
var AnotherModel = CacheItem.dataSource.createModel('AnotherModel'); let AnotherModel;
return helpers.givenKeys(CacheItem, ['key1', 'key2']) helpers.givenModel(dataSourceFactory, 'AnotherModel')
.then(function() { .then(ModelCtor => AnotherModel = ModelCtor)
return helpers.givenKeys(AnotherModel, ['otherKey1', 'otherKey2']); .then(() => helpers.givenKeys(CacheItem, ['key1', 'key2']))
}) .then(() => helpers.givenKeys(AnotherModel, ['otherKey1', 'otherKey2']))
.then(function() { .then(() => CacheItem.sortedKeys())
return CacheItem.sortedKeys(); .then(keys => should(keys).eql(['key1', 'key2']));
})
.then(function(keys) {
should(keys).eql(['key1', 'key2']);
});
}); });
var largeKeySets = connectorCapabilities.canIterateLargeKeySets !== false; var largeKeySets = connectorCapabilities.canIterateLargeKeySets !== false;

View File

@ -20,10 +20,8 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
var canQueryTtl = connectorCapabilities.canQueryTtl !== false; var canQueryTtl = connectorCapabilities.canQueryTtl !== false;
bdd.describeIf(canQueryTtl, 'ttl', function() { bdd.describeIf(canQueryTtl, 'ttl', function() {
var CacheItem; let CacheItem;
beforeEach(function unpackContext() { beforeEach(setupCacheItem);
CacheItem = helpers.givenCacheItem(dataSourceFactory);
});
it('gets TTL when key with unexpired TTL exists - Promise API', it('gets TTL when key with unexpired TTL exists - Promise API',
function() { function() {
@ -75,5 +73,10 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
err.should.have.property('statusCode', 404); err.should.have.property('statusCode', 404);
}); });
}); });
function setupCacheItem() {
return helpers.givenCacheItem(dataSourceFactory)
.then(ModelCtor => CacheItem = ModelCtor);
}
}); });
}; };