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-08 08:15:22 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-07 14:54:29 +00:00
|
|
|
const should = require('should');
|
|
|
|
const helpers = require('./_helpers');
|
2016-08-08 08:15:22 +00:00
|
|
|
|
|
|
|
module.exports = function(dataSourceFactory, connectorCapabilities) {
|
2018-12-07 14:54:29 +00:00
|
|
|
const TTL_PRECISION = connectorCapabilities.ttlPrecision;
|
2016-10-07 14:26:18 +00:00
|
|
|
|
2016-08-08 08:15:22 +00:00
|
|
|
describe('get/set', function() {
|
2018-12-07 14:54:29 +00:00
|
|
|
let CacheItem;
|
2017-01-10 08:18:04 +00:00
|
|
|
beforeEach(setupCacheItem);
|
2016-08-08 08:15:22 +00:00
|
|
|
|
|
|
|
it('works for string values - Callback API', function(done) {
|
|
|
|
CacheItem.set('a-key', 'a-value', function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
CacheItem.get('a-key', function(err, value) {
|
|
|
|
if (err) return done(err);
|
|
|
|
should.equal(value, 'a-value');
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works for string values - Promise API', function() {
|
|
|
|
return CacheItem.set('a-key', 'a-value')
|
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
|
|
|
.then(function(value) { should.equal(value, 'a-value'); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works for Object values', function() {
|
2016-08-19 17:46:59 +00:00
|
|
|
return CacheItem.set('a-key', {a: 1, b: 2})
|
2016-08-08 08:15:22 +00:00
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
2016-08-19 17:46:59 +00:00
|
|
|
.then(function(value) { value.should.eql({a: 1, b: 2}); });
|
2016-08-08 08:15:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('works for Buffer values', function() {
|
|
|
|
return CacheItem.set('a-key', new Buffer([1, 2, 3]))
|
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
|
|
|
.then(function(value) { value.should.eql(new Buffer([1, 2, 3])); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works for Date values', function() {
|
|
|
|
return CacheItem.set('a-key', new Date('2016-08-03T11:53:03.470Z'))
|
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
|
|
|
.then(function(value) {
|
|
|
|
value.should.be.instanceOf(Date);
|
|
|
|
value.toISOString().should.equal('2016-08-03T11:53:03.470Z');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works for Number values - integers', function() {
|
|
|
|
return CacheItem.set('a-key', 12345)
|
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
|
|
|
.then(function(value) { value.should.equal(12345); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works for Number values - floats', function() {
|
|
|
|
return CacheItem.set('a-key', 12.345)
|
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
|
|
|
.then(function(value) { value.should.equal(12.345); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('works for Boolean values', function() {
|
|
|
|
return CacheItem.set('a-key', false)
|
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
|
|
|
.then(function(value) { value.should.equal(false); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('honours options.ttl', function() {
|
2016-10-07 14:26:18 +00:00
|
|
|
return CacheItem.set('a-key', 'a-value', {ttl: TTL_PRECISION})
|
2018-10-05 07:30:02 +00:00
|
|
|
.then(() => helpers.delay(2 * TTL_PRECISION))
|
2018-06-12 07:13:32 +00:00
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
|
|
|
.then(function(value) { should.equal(value, null); });
|
2016-08-08 08:15:22 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('get', function() {
|
|
|
|
it('returns "null" when key does not exist', function() {
|
|
|
|
return CacheItem.get('key-does-not-exist')
|
|
|
|
.then(function(value) { should.equal(value, null); });
|
|
|
|
});
|
2016-10-07 14:26:18 +00:00
|
|
|
});
|
2016-08-08 08:15:22 +00:00
|
|
|
|
2016-10-07 14:26:18 +00:00
|
|
|
describe('set', function() {
|
2016-08-08 08:15:22 +00:00
|
|
|
it('converts numeric options arg to options.ttl', function() {
|
2016-10-07 14:26:18 +00:00
|
|
|
return CacheItem.set('a-key', 'a-value', TTL_PRECISION)
|
2018-10-05 07:30:02 +00:00
|
|
|
.then(() => helpers.delay(2 * TTL_PRECISION))
|
2016-08-08 08:15:22 +00:00
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
|
|
|
.then(function(value) { should.equal(value, null); });
|
|
|
|
});
|
|
|
|
|
|
|
|
it('resets TTL timer', function() {
|
2016-10-07 14:26:18 +00:00
|
|
|
return CacheItem.set('a-key', 'a-value', {ttl: TTL_PRECISION})
|
2016-08-08 08:15:22 +00:00
|
|
|
.then(function() {
|
|
|
|
return CacheItem.set('a-key', 'another-value'); // no TTL
|
|
|
|
})
|
2018-10-05 07:30:02 +00:00
|
|
|
.then(() => helpers.delay(2 * TTL_PRECISION))
|
2016-08-08 08:15:22 +00:00
|
|
|
.then(function() { return CacheItem.get('a-key'); })
|
|
|
|
.then(function(value) { should.equal(value, 'another-value'); });
|
|
|
|
});
|
|
|
|
});
|
2017-01-10 08:18:04 +00:00
|
|
|
|
|
|
|
function setupCacheItem() {
|
|
|
|
return helpers.givenCacheItem(dataSourceFactory)
|
|
|
|
.then(ModelCtor => CacheItem = ModelCtor);
|
2018-12-07 14:43:40 +00:00
|
|
|
}
|
2016-08-08 08:15:22 +00:00
|
|
|
});
|
|
|
|
};
|