Skip to content

Commit 9afaeca

Browse files
authored
Merge pull request #1077 from strongloop/refactor/ttl-test-suite
Refactor TTL test suite
2 parents b95d5ca + cf432fc commit 9afaeca

File tree

1 file changed

+37
-42
lines changed

1 file changed

+37
-42
lines changed

test/kvao/ttl.suite.js

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,54 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
1111
CacheItem = helpers.givenCacheItem(dataSourceFactory);
1212
});
1313

14-
it('returns an error when key does not exist', function() {
15-
return CacheItem.ttl('key-does-not-exist').then(
16-
function() { throw new Error('ttl() should have failed'); },
17-
function(err) {
18-
err.message.should.match(/key-does-not-exist/);
19-
err.should.have.property('statusCode', 404);
14+
it('gets TTL when key with unexpired TTL exists - Promise API',
15+
function() {
16+
return Promise.resolve(
17+
CacheItem.set('a-key', 'a-value', { ttl: 1000 }))
18+
.delay(1)
19+
.then(function() { return CacheItem.ttl('a-key'); })
20+
.then(function(ttl) { ttl.should.be.within(1, 1000); });
21+
});
22+
23+
it('gets TTL when key with unexpired TTL exists - Callback API',
24+
function() {
25+
CacheItem.set('a-key', 'a-value', { ttl: 1000 }, function(err) {
26+
if (err) return done(err);
27+
CacheItem.ttl('a-key', function(err, ttl) {
28+
if (err) return done(err);
29+
ttl.should.be.within(1, 1000);
30+
done();
2031
});
32+
});
2133
});
2234

23-
it('returns `undefined` when key does not expire', function() {
35+
it('succeeds when key without TTL exists', function() {
2436
return CacheItem.set('a-key', 'a-value')
2537
.then(function() { return CacheItem.ttl('a-key'); })
2638
.then(function(ttl) { should.not.exist(ttl); });
2739
});
2840

29-
context('existing key with expire before expiration time', function() {
30-
it('returns ttl - Callback API', function(done) {
31-
CacheItem.set('a-key', 'a-value', 10, function(err) {
32-
if (err) return done(err);
33-
CacheItem.ttl('a-key', function(err, ttl) {
34-
if (err) return done(err);
35-
ttl.should.be.within(0, 10);
36-
done();
41+
it('fails when getting TTL for a key with expired TTL', function() {
42+
return Promise.resolve(
43+
CacheItem.set('expired-key', 'a-value', { ttl: 10 })).delay(20)
44+
.then(function() {
45+
return CacheItem.ttl('expired-key');
46+
})
47+
.then(
48+
function() { throw new Error('ttl() should have failed'); },
49+
function(err) {
50+
err.message.should.match(/expired-key/);
51+
err.should.have.property('statusCode', 404);
3752
});
38-
});
39-
});
40-
41-
it('returns ttl - Promise API', function() {
42-
return Promise.resolve(
43-
CacheItem.set('a-key', 'a-value', 10)
44-
)
45-
.delay(1)
46-
.then(function() { return CacheItem.ttl('a-key'); })
47-
.then(function(ttl) { ttl.should.be.within(0, 10); });
48-
});
4953
});
5054

51-
context('existing key with expire after expiration time', function(done) {
52-
it('returns an error', function() {
53-
return Promise.resolve(
54-
CacheItem.set('key-does-not-exist', 'a-value', 10)
55-
)
56-
.delay(20)
57-
.then(function() {
58-
return CacheItem.ttl('key-does-not-exist');
59-
})
60-
.then(
61-
function() { throw new Error('ttl() should have failed'); },
62-
function(err) {
63-
err.message.should.match(/key-does-not-exist/);
64-
err.should.have.property('statusCode', 404);
65-
});
66-
});
55+
it('fails when key does not exist', function() {
56+
return CacheItem.ttl('key-does-not-exist').then(
57+
function() { throw new Error('ttl() should have failed'); },
58+
function(err) {
59+
err.message.should.match(/key-does-not-exist/);
60+
err.should.have.property('statusCode', 404);
61+
});
6762
});
6863
});
6964
};

0 commit comments

Comments
 (0)