@@ -11,55 +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 ( / k e y - d o e s - n o t - e x i s t / ) ;
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 ( / e x p i r e d - k e y / ) ;
51+ err . should . have . property ( 'statusCode' , 404 ) ;
3752 } ) ;
38- } ) ;
39- } ) ;
40-
41- it ( 'returns ttl - Promise API' , function ( ) {
42- return CacheItem . set ( 'a-key' , 'a-value' , 10 )
43- . delay ( 1 )
44- . then ( function ( ) { return CacheItem . ttl ( 'a-key' ) ; } )
45- . then ( function ( ttl ) { ttl . should . be . within ( 0 , 10 ) ; } ) ;
46- } ) ;
4753 } ) ;
4854
49- context ( 'existing key with expire after expiration time' , function ( done ) {
50- it ( 'returns an error' , function ( ) {
51- return CacheItem . set ( 'key-does-not-exist' , 'a-value' , 10 )
52- . delay ( 20 )
53- . then ( function ( ) {
54- return CacheItem . ttl ( 'key-does-not-exist' ) ;
55- } )
56- . then (
57- function ( ) { throw new Error ( 'ttl() should have failed' ) ; } ,
58- function ( err ) {
59- err . message . should . match ( / k e y - d o e s - n o t - e x i s t / ) ;
60- err . should . have . property ( 'statusCode' , 404 ) ;
61- } ) ;
62- } ) ;
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 ( / k e y - d o e s - n o t - e x i s t / ) ;
60+ err . should . have . property ( 'statusCode' , 404 ) ;
61+ } ) ;
6362 } ) ;
6463 } ) ;
6564} ;
0 commit comments