Blame view

node_modules/loopback-datasource-juggler/test/kvao/get-set.suite.js 3.76 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  'use strict';
  
  var should = require('should');
  var helpers = require('./_helpers');
  var Promise = require('bluebird');
  
  module.exports = function(dataSourceFactory, connectorCapabilities) {
    var TTL_PRECISION = connectorCapabilities.ttlPrecision;
  
    describe('get/set', function() {
      var CacheItem;
      beforeEach(function unpackContext() {
        CacheItem = helpers.givenCacheItem(dataSourceFactory);
      });
  
      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() {
        return CacheItem.set('a-key', {a: 1, b: 2})
          .then(function() { return CacheItem.get('a-key'); })
          .then(function(value) { value.should.eql({a: 1, b: 2}); });
      });
  
      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() {
        return Promise.resolve(CacheItem.set('a-key', 'a-value', {ttl: TTL_PRECISION}))
        .delay(2 * TTL_PRECISION)
        .then(function() { return CacheItem.get('a-key'); })
        .then(function(value) { should.equal(value, null); });
      });
  
      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); });
        });
      });
  
      describe('set', function() {
        it('converts numeric options arg to options.ttl', function() {
          return Promise.resolve(CacheItem.set('a-key', 'a-value', TTL_PRECISION))
            .delay(2 * TTL_PRECISION)
            .then(function() { return CacheItem.get('a-key'); })
            .then(function(value) { should.equal(value, null); });
        });
  
        it('resets TTL timer', function() {
          return Promise.resolve(CacheItem.set('a-key', 'a-value', {ttl: TTL_PRECISION}))
            .then(function() {
              return CacheItem.set('a-key', 'another-value'); // no TTL
            })
            .delay(2 * TTL_PRECISION)
            .then(function() { return CacheItem.get('a-key'); })
            .then(function(value) { should.equal(value, 'another-value'); });
        });
      });
    });
  };